]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules.cpp
Updated header comments
[user/henk/code/inspircd.git] / src / modules.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 #include "inspircd.h"
18 #include "inspircd_io.h"
19 #include "inspircd_util.h"
20 #include "inspircd_config.h"
21 #include <unistd.h>
22 #include <fcntl.h>
23 #include <sys/errno.h>
24 #include <sys/ioctl.h>
25 #include <sys/utsname.h>
26 #include <cstdio>
27 #include <time.h>
28 #include <string>
29 #ifdef GCC3
30 #include <ext/hash_map>
31 #else
32 #include <hash_map>
33 #endif
34 #include <map>
35 #include <sstream>
36 #include <vector>
37 #include <errno.h>
38 #include <deque>
39 #include <errno.h>
40 #include <unistd.h>
41 #include <sched.h>
42 #include "connection.h"
43 #include "users.h"
44 #include "servers.h"
45 #include "ctables.h"
46 #include "globals.h"
47 #include "modules.h"
48 #include "dynamic.h"
49 #include "wildcard.h"
50 #include "message.h"
51 #include "mode.h"
52 #include "xline.h"
53 #include "commands.h"
54
55 #ifdef GCC3
56 #define nspace __gnu_cxx
57 #else
58 #define nspace std
59 #endif
60
61 using namespace std;
62
63 extern int MODCOUNT;
64 extern vector<Module*> modules;
65 extern vector<ircd_module*> factory;
66
67 extern int LogLevel;
68 extern char ServerName[MAXBUF];
69 extern char Network[MAXBUF];
70 extern char ServerDesc[MAXBUF];
71 extern char AdminName[MAXBUF];
72 extern char AdminEmail[MAXBUF];
73 extern char AdminNick[MAXBUF];
74 extern char diepass[MAXBUF];
75 extern char restartpass[MAXBUF];
76 extern char motd[MAXBUF];
77 extern char rules[MAXBUF];
78 extern char list[MAXBUF];
79 extern char PrefixQuit[MAXBUF];
80 extern char DieValue[MAXBUF];
81
82 extern int debugging;
83 extern int WHOWAS_STALE;
84 extern int WHOWAS_MAX;
85 extern int DieDelay;
86 extern time_t startup_time;
87 extern int NetBufferSize;
88 extern int MaxWhoResults;
89 extern time_t nb_start;
90
91 extern std::vector<int> fd_reap;
92 extern std::vector<std::string> module_names;
93
94 extern int boundPortCount;
95 extern int portCount;
96 extern int UDPportCount;
97 extern int ports[MAXSOCKS];
98 extern int defaultRoute;
99
100 extern std::vector<long> auth_cookies;
101 extern std::stringstream config_f;
102
103 extern serverrec* me[32];
104
105 extern FILE *log_file;
106
107
108 namespace nspace
109 {
110         template<> struct nspace::hash<in_addr>
111         {
112                 size_t operator()(const struct in_addr &a) const
113                 {
114                         size_t q;
115                         memcpy(&q,&a,sizeof(size_t));
116                         return q;
117                 }
118         };
119
120         template<> struct nspace::hash<string>
121         {
122                 size_t operator()(const string &s) const
123                 {
124                         char a[MAXBUF];
125                         static struct hash<const char *> strhash;
126                         strcpy(a,s.c_str());
127                         strlower(a);
128                         return strhash(a);
129                 }
130         };
131 }       
132
133
134 struct StrHashComp
135 {
136
137         bool operator()(const string& s1, const string& s2) const
138         {
139                 char a[MAXBUF],b[MAXBUF];
140                 strcpy(a,s1.c_str());
141                 strcpy(b,s2.c_str());
142                 return (strcasecmp(a,b) == 0);
143         }
144
145 };
146
147 struct InAddr_HashComp
148 {
149
150         bool operator()(const in_addr &s1, const in_addr &s2) const
151         {
152                 size_t q;
153                 size_t p;
154                 
155                 memcpy(&q,&s1,sizeof(size_t));
156                 memcpy(&p,&s2,sizeof(size_t));
157                 
158                 return (q == p);
159         }
160
161 };
162
163
164 typedef nspace::hash_map<std::string, userrec*, nspace::hash<string>, StrHashComp> user_hash;
165 typedef nspace::hash_map<std::string, chanrec*, nspace::hash<string>, StrHashComp> chan_hash;
166 typedef nspace::hash_map<in_addr,string*, nspace::hash<in_addr>, InAddr_HashComp> address_cache;
167 typedef std::deque<command_t> command_table;
168
169
170 extern user_hash clientlist;
171 extern chan_hash chanlist;
172 extern user_hash whowas;
173 extern command_table cmdlist;
174 extern file_cache MOTD;
175 extern file_cache RULES;
176 extern address_cache IP;
177
178
179 // class type for holding an extended mode character - internal to core
180
181 class ExtMode : public classbase
182 {
183 public:
184         char modechar;
185         int type;
186         int params_when_on;
187         int params_when_off;
188         bool needsoper;
189         bool list;
190         ExtMode(char mc, int ty, bool oper, int p_on, int p_off) : modechar(mc), type(ty), needsoper(oper), params_when_on(p_on), params_when_off(p_off) { };
191 };                                     
192
193 typedef std::vector<ExtMode> ExtModeList;
194 typedef ExtModeList::iterator ExtModeListIter;
195
196
197 ExtModeList EMode;
198
199 // returns true if an extended mode character is in use
200 bool ModeDefined(char modechar, int type)
201 {
202         log(DEBUG,"Size of extmodes vector is %d",EMode.size());
203         for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
204         {
205                 if ((i->modechar == modechar) && (i->type == type))
206                 {
207                         return true;
208                 }
209         }
210         return false;
211 }
212
213 bool ModeIsListMode(char modechar, int type)
214 {
215         log(DEBUG,"Size of extmodes vector is %d",EMode.size());
216         for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
217         {
218                 if ((i->modechar == modechar) && (i->type == type) && (i->list == true))
219                 {
220                         return true;
221                 }
222         }
223         return false;
224 }
225
226 bool ModeDefinedOper(char modechar, int type)
227 {
228         log(DEBUG,"Size of extmodes vector is %d",EMode.size());
229         for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
230         {
231                 if ((i->modechar == modechar) && (i->type == type) && (i->needsoper == true))
232                 {
233                         return true;
234                 }
235         }
236         return false;
237 }
238
239 // returns number of parameters for a custom mode when it is switched on
240 int ModeDefinedOn(char modechar, int type)
241 {
242         for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
243         {
244                 if ((i->modechar == modechar) && (i->type == type))
245                 {
246                         return i->params_when_on;
247                 }
248         }
249         return 0;
250 }
251
252 // returns number of parameters for a custom mode when it is switched on
253 int ModeDefinedOff(char modechar, int type)
254 {
255         for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
256         {
257                 if ((i->modechar == modechar) && (i->type == type))
258                 {
259                         return i->params_when_off;
260                 }
261         }
262         return 0;
263 }
264
265 // returns true if an extended mode character is in use
266 bool DoAddExtendedMode(char modechar, int type, bool requires_oper, int params_on, int params_off)
267 {
268         if (ModeDefined(modechar,type)) {
269                 return false;
270         }
271         EMode.push_back(ExtMode(modechar,type,requires_oper,params_on,params_off));
272         return true;
273 }
274
275 // turns a mode into a listmode
276 void ModeMakeList(char modechar)
277 {
278         for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
279         {
280                 if ((i->modechar == modechar) && (i->type == MT_CHANNEL))
281                 {
282                         i->list = true;
283                         return;
284                 }
285         }
286         return;
287 }
288
289 // version is a simple class for holding a modules version number
290
291 Version::Version(int major, int minor, int revision, int build) : Major(major), Minor(minor), Revision(revision), Build(build) { };
292
293 // admin is a simple class for holding a server's administrative info
294
295 Admin::Admin(std::string name, std::string email, std::string nick) : Name(name), Email(email), Nick(nick) { };
296
297 Module::Module() { }
298 Module::~Module() { }
299 void Module::OnUserConnect(userrec* user) { }
300 void Module::OnUserQuit(userrec* user) { }
301 void Module::OnUserJoin(userrec* user, chanrec* channel) { }
302 void Module::OnUserPart(userrec* user, chanrec* channel) { }
303 void Module::OnPacketTransmit(char *p) { }
304 void Module::OnPacketReceive(char *p) { }
305 void Module::OnRehash() { }
306 void Module::OnServerRaw(std::string &raw, bool inbound, userrec* user) { }
307 int Module::OnUserPreJoin(userrec* user, chanrec* chan, const char* cname) { return 0; }
308 int Module::OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list &params) { return false; }
309 Version Module::GetVersion() { return Version(1,0,0,0); }
310 void Module::OnOper(userrec* user) { };
311 void Module::OnInfo(userrec* user) { };
312 void Module::OnWhois(userrec* source, userrec* dest) { };
313 int Module::OnUserPreMessage(userrec* user,void* dest,int target_type, std::string text) { return 0; };
314 int Module::OnUserPreNotice(userrec* user,void* dest,int target_type, std::string text) { return 0; };
315 int Module::OnUserPreNick(userrec* user, std::string newnick) { return 0; };
316 int Module::OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type) { return ACR_DEFAULT; };
317 string_list Module::OnUserSync(userrec* user) { string_list empty; return empty; }
318 string_list Module::OnChannelSync(chanrec* chan) { string_list empty; return empty; }
319
320
321 // server is a wrapper class that provides methods to all of the C-style
322 // exports in the core
323 //
324
325 Server::Server()
326 {
327 }
328
329 Server::~Server()
330 {
331 }
332
333 void Server::SendOpers(std::string s)
334 {
335         WriteOpers("%s",s.c_str());
336 }
337
338 bool Server::MatchText(std::string sliteral, std::string spattern)
339 {
340         char literal[MAXBUF],pattern[MAXBUF];
341         strncpy(literal,sliteral.c_str(),MAXBUF);
342         strncpy(pattern,spattern.c_str(),MAXBUF);
343         return match(literal,pattern);
344 }
345
346 void Server::SendToModeMask(std::string modes, int flags, std::string text)
347 {
348         WriteMode(modes.c_str(),flags,"%s",text.c_str());
349 }
350
351 chanrec* Server::JoinUserToChannel(userrec* user, std::string cname, std::string key)
352 {
353         return add_channel(user,cname.c_str(),key.c_str(),true);
354 }
355
356 chanrec* Server::PartUserFromChannel(userrec* user, std::string cname, std::string reason)
357 {
358         return del_channel(user,cname.c_str(),reason.c_str(),false);
359 }
360
361 chanuserlist Server::GetUsers(chanrec* chan)
362 {
363         chanuserlist userl;
364         userl.clear();
365         for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
366         {
367                 if (i->second)
368                 {
369                         if (has_channel(i->second,chan))
370                         {
371                                 if (isnick(i->second->nick))
372                                 {
373                                         userl.push_back(i->second);
374                                 }
375                         }
376                 }
377         }
378         return userl;
379 }
380 void Server::ChangeUserNick(userrec* user, std::string nickname)
381 {
382         force_nickchange(user,nickname.c_str());
383 }
384
385 void Server::QuitUser(userrec* user, std::string reason)
386 {
387         send_network_quit(user->nick,reason.c_str());
388         kill_link(user,reason.c_str());
389 }
390
391 bool Server::IsUlined(std::string server)
392 {
393         return is_uline(server.c_str());
394 }
395
396 void Server::CallCommandHandler(std::string commandname, char** parameters, int pcnt, userrec* user)
397 {
398         call_handler(commandname.c_str(),parameters,pcnt,user);
399 }
400
401 void Server::Log(int level, std::string s)
402 {
403         log(level,"%s",s.c_str());
404 }
405
406 void Server::AddCommand(char* cmd, handlerfunc f, char flags, int minparams)
407 {
408         createcommand(cmd,f,flags,minparams);
409 }
410
411 void Server::SendMode(char **parameters, int pcnt, userrec *user)
412 {
413         server_mode(parameters,pcnt,user);
414 }
415
416 void Server::Send(int Socket, std::string s)
417 {
418         Write(Socket,"%s",s.c_str());
419 }
420
421 void Server::SendServ(int Socket, std::string s)
422 {
423         WriteServ(Socket,"%s",s.c_str());
424 }
425
426 void Server::SendFrom(int Socket, userrec* User, std::string s)
427 {
428         WriteFrom(Socket,User,"%s",s.c_str());
429 }
430
431 void Server::SendTo(userrec* Source, userrec* Dest, std::string s)
432 {
433         if (!Source)
434         {
435                 // if source is NULL, then the message originates from the local server
436                 Write(Dest->fd,":%s %s",this->GetServerName().c_str(),s.c_str());
437         }
438         else
439         {
440                 // otherwise it comes from the user specified
441                 WriteTo(Source,Dest,"%s",s.c_str());
442         }
443 }
444
445 void Server::SendChannel(userrec* User, chanrec* Channel, std::string s,bool IncludeSender)
446 {
447         if (IncludeSender)
448         {
449                 WriteChannel(Channel,User,"%s",s.c_str());
450         }
451         else
452         {
453                 ChanExceptSender(Channel,User,"%s",s.c_str());
454         }
455 }
456
457 bool Server::CommonChannels(userrec* u1, userrec* u2)
458 {
459         return (common_channels(u1,u2) != 0);
460 }
461
462 void Server::SendCommon(userrec* User, std::string text,bool IncludeSender)
463 {
464         if (IncludeSender)
465         {
466                 WriteCommon(User,"%s",text.c_str());
467         }
468         else
469         {
470                 WriteCommonExcept(User,"%s",text.c_str());
471         }
472 }
473
474 void Server::SendWallops(userrec* User, std::string text)
475 {
476         WriteWallOps(User,false,"%s",text.c_str());
477 }
478
479 void Server::ChangeHost(userrec* user, std::string host)
480 {
481         ChangeDisplayedHost(user,host.c_str());
482 }
483
484 void Server::ChangeGECOS(userrec* user, std::string gecos)
485 {
486         ChangeName(user,gecos.c_str());
487 }
488
489 bool Server::IsNick(std::string nick)
490 {
491         return (isnick(nick.c_str()) != 0);
492 }
493
494 userrec* Server::FindNick(std::string nick)
495 {
496         return Find(nick);
497 }
498
499 chanrec* Server::FindChannel(std::string channel)
500 {
501         return FindChan(channel.c_str());
502 }
503
504 std::string Server::ChanMode(userrec* User, chanrec* Chan)
505 {
506         return cmode(User,Chan);
507 }
508
509 bool Server::IsOnChannel(userrec* User, chanrec* Chan)
510 {
511         return has_channel(User,Chan);
512 }
513
514 std::string Server::GetServerName()
515 {
516         return getservername();
517 }
518
519 std::string Server::GetNetworkName()
520 {
521         return getnetworkname();
522 }
523
524 Admin Server::GetAdmin()
525 {
526         return Admin(getadminname(),getadminemail(),getadminnick());
527 }
528
529
530
531 bool Server::AddExtendedMode(char modechar, int type, bool requires_oper, int params_when_on, int params_when_off)
532 {
533         if (type == MT_SERVER)
534         {
535                 log(DEBUG,"*** API ERROR *** Modes of type MT_SERVER are reserved for future expansion");
536                 return false;
537         }
538         if (((params_when_on>0) || (params_when_off>0)) && (type == MT_CLIENT))
539         {
540                 log(DEBUG,"*** API ERROR *** Parameters on MT_CLIENT modes are not supported");
541                 return false;
542         }
543         if ((params_when_on>1) || (params_when_off>1))
544         {
545                 log(DEBUG,"*** API ERROR *** More than one parameter for an MT_CHANNEL mode is not yet supported");
546                 return false;
547         }
548         return DoAddExtendedMode(modechar,type,requires_oper,params_when_on,params_when_off);
549 }
550
551 bool Server::AddExtendedListMode(char modechar)
552 {
553         bool res = DoAddExtendedMode(modechar,MT_CHANNEL,false,1,1);
554         if (res)
555                 ModeMakeList(modechar);
556         return res;
557 }
558
559 int Server::CountUsers(chanrec* c)
560 {
561         return usercount(c);
562 }
563
564
565 ConfigReader::ConfigReader()
566 {
567         this->cache = new std::stringstream(std::stringstream::in | std::stringstream::out);
568         this->readerror = LoadConf(CONFIG_FILE,this->cache);
569         if (!this->readerror)
570                 this->error = CONF_FILE_NOT_FOUND;
571 }
572
573
574 ConfigReader::~ConfigReader()
575 {
576         if (this->cache)
577                 delete this->cache;
578 }
579
580
581 ConfigReader::ConfigReader(std::string filename)
582 {
583         this->cache = new std::stringstream(std::stringstream::in | std::stringstream::out);
584         this->readerror = LoadConf(filename.c_str(),this->cache);
585         if (!this->readerror)
586                 this->error = CONF_FILE_NOT_FOUND;
587 };
588
589 std::string ConfigReader::ReadValue(std::string tag, std::string name, int index)
590 {
591         char val[MAXBUF];
592         char t[MAXBUF];
593         char n[MAXBUF];
594         strncpy(t,tag.c_str(),MAXBUF);
595         strncpy(n,name.c_str(),MAXBUF);
596         int res = ReadConf(cache,t,n,index,val);
597         if (!res)
598         {
599                 this->error = CONF_VALUE_NOT_FOUND;
600                 return "";
601         }
602         return std::string(val);
603 }
604
605 bool ConfigReader::ReadFlag(std::string tag, std::string name, int index)
606 {
607         char val[MAXBUF];
608         char t[MAXBUF];
609         char n[MAXBUF];
610         strncpy(t,tag.c_str(),MAXBUF);
611         strncpy(n,name.c_str(),MAXBUF);
612         int res = ReadConf(cache,t,n,index,val);
613         if (!res)
614         {
615                 this->error = CONF_VALUE_NOT_FOUND;
616                 return false;
617         }
618         std::string s = val;
619         return ((s == "yes") || (s == "YES") || (s == "true") || (s == "TRUE") || (s == "1"));
620 }
621
622 long ConfigReader::ReadInteger(std::string tag, std::string name, int index, bool needs_unsigned)
623 {
624         char val[MAXBUF];
625         char t[MAXBUF];
626         char n[MAXBUF];
627         strncpy(t,tag.c_str(),MAXBUF);
628         strncpy(n,name.c_str(),MAXBUF);
629         int res = ReadConf(cache,t,n,index,val);
630         if (!res)
631         {
632                 this->error = CONF_VALUE_NOT_FOUND;
633                 return 0;
634         }
635         for (int i = 0; i < strlen(val); i++)
636         {
637                 if (!isdigit(val[i]))
638                 {
639                         this->error = CONF_NOT_A_NUMBER;
640                         return 0;
641                 }
642         }
643         if ((needs_unsigned) && (atoi(val)<0))
644         {
645                 this->error = CONF_NOT_UNSIGNED;
646                 return 0;
647         }
648         return atoi(val);
649 }
650
651 long ConfigReader::GetError()
652 {
653         long olderr = this->error;
654         this->error = 0;
655         return olderr;
656 }
657
658
659 int ConfigReader::Enumerate(std::string tag)
660 {
661         return EnumConf(cache,tag.c_str());
662 }
663
664 int ConfigReader::EnumerateValues(std::string tag, int index)
665 {
666         return EnumValues(cache, tag.c_str(), index);
667 }
668
669 bool ConfigReader::Verify()
670 {
671         return this->readerror;
672 }
673
674
675 FileReader::FileReader(std::string filename)
676 {
677         file_cache c;
678         readfile(c,filename.c_str());
679         this->fc = c;
680 }
681
682 FileReader::FileReader()
683 {
684 }
685
686 void FileReader::LoadFile(std::string filename)
687 {
688         file_cache c;
689         readfile(c,filename.c_str());
690         this->fc = c;
691 }
692
693
694 FileReader::~FileReader()
695 {
696 }
697
698 bool FileReader::Exists()
699 {
700         if (fc.size() == 0)
701         {
702                 return(false);
703         }
704         else
705         {
706                 return(true);
707         }
708 }
709
710 std::string FileReader::GetLine(int x)
711 {
712         if ((x<0) || (x>fc.size()))
713                 return "";
714         return fc[x];
715 }
716
717 int FileReader::FileSize()
718 {
719         return fc.size();
720 }
721
722
723 std::vector<Module*> modules(255);
724 std::vector<ircd_module*> factory(255);
725
726 int MODCOUNT  = -1;
727
728