]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules.cpp
Project structure changes
[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::OnUserPreInvite(userrec* source,userrec* dest,chanrec* channel) { return 0; };
314 int Module::OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text) { return 0; };
315 int Module::OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text) { return 0; };
316 int Module::OnUserPreNick(userrec* user, std::string newnick) { return 0; };
317 int Module::OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type) { return ACR_DEFAULT; };
318 string_list Module::OnUserSync(userrec* user) { string_list empty; return empty; }
319 string_list Module::OnChannelSync(chanrec* chan) { string_list empty; return empty; }
320
321
322 // server is a wrapper class that provides methods to all of the C-style
323 // exports in the core
324 //
325
326 Server::Server()
327 {
328 }
329
330 Server::~Server()
331 {
332 }
333
334 void Server::SendOpers(std::string s)
335 {
336         WriteOpers("%s",s.c_str());
337 }
338
339 bool Server::MatchText(std::string sliteral, std::string spattern)
340 {
341         char literal[MAXBUF],pattern[MAXBUF];
342         strncpy(literal,sliteral.c_str(),MAXBUF);
343         strncpy(pattern,spattern.c_str(),MAXBUF);
344         return match(literal,pattern);
345 }
346
347 void Server::SendToModeMask(std::string modes, int flags, std::string text)
348 {
349         WriteMode(modes.c_str(),flags,"%s",text.c_str());
350 }
351
352 chanrec* Server::JoinUserToChannel(userrec* user, std::string cname, std::string key)
353 {
354         return add_channel(user,cname.c_str(),key.c_str(),true);
355 }
356
357 chanrec* Server::PartUserFromChannel(userrec* user, std::string cname, std::string reason)
358 {
359         return del_channel(user,cname.c_str(),reason.c_str(),false);
360 }
361
362 chanuserlist Server::GetUsers(chanrec* chan)
363 {
364         chanuserlist userl;
365         userl.clear();
366         for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
367         {
368                 if (i->second)
369                 {
370                         if (has_channel(i->second,chan))
371                         {
372                                 if (isnick(i->second->nick))
373                                 {
374                                         userl.push_back(i->second);
375                                 }
376                         }
377                 }
378         }
379         return userl;
380 }
381 void Server::ChangeUserNick(userrec* user, std::string nickname)
382 {
383         force_nickchange(user,nickname.c_str());
384 }
385
386 void Server::QuitUser(userrec* user, std::string reason)
387 {
388         send_network_quit(user->nick,reason.c_str());
389         kill_link(user,reason.c_str());
390 }
391
392 bool Server::IsUlined(std::string server)
393 {
394         return is_uline(server.c_str());
395 }
396
397 void Server::CallCommandHandler(std::string commandname, char** parameters, int pcnt, userrec* user)
398 {
399         call_handler(commandname.c_str(),parameters,pcnt,user);
400 }
401
402 void Server::Log(int level, std::string s)
403 {
404         log(level,"%s",s.c_str());
405 }
406
407 void Server::AddCommand(char* cmd, handlerfunc f, char flags, int minparams)
408 {
409         createcommand(cmd,f,flags,minparams);
410 }
411
412 void Server::SendMode(char **parameters, int pcnt, userrec *user)
413 {
414         server_mode(parameters,pcnt,user);
415 }
416
417 void Server::Send(int Socket, std::string s)
418 {
419         Write(Socket,"%s",s.c_str());
420 }
421
422 void Server::SendServ(int Socket, std::string s)
423 {
424         WriteServ(Socket,"%s",s.c_str());
425 }
426
427 void Server::SendFrom(int Socket, userrec* User, std::string s)
428 {
429         WriteFrom(Socket,User,"%s",s.c_str());
430 }
431
432 void Server::SendTo(userrec* Source, userrec* Dest, std::string s)
433 {
434         if (!Source)
435         {
436                 // if source is NULL, then the message originates from the local server
437                 Write(Dest->fd,":%s %s",this->GetServerName().c_str(),s.c_str());
438         }
439         else
440         {
441                 // otherwise it comes from the user specified
442                 WriteTo(Source,Dest,"%s",s.c_str());
443         }
444 }
445
446 void Server::SendChannel(userrec* User, chanrec* Channel, std::string s,bool IncludeSender)
447 {
448         if (IncludeSender)
449         {
450                 WriteChannel(Channel,User,"%s",s.c_str());
451         }
452         else
453         {
454                 ChanExceptSender(Channel,User,"%s",s.c_str());
455         }
456 }
457
458 bool Server::CommonChannels(userrec* u1, userrec* u2)
459 {
460         return (common_channels(u1,u2) != 0);
461 }
462
463 void Server::SendCommon(userrec* User, std::string text,bool IncludeSender)
464 {
465         if (IncludeSender)
466         {
467                 WriteCommon(User,"%s",text.c_str());
468         }
469         else
470         {
471                 WriteCommonExcept(User,"%s",text.c_str());
472         }
473 }
474
475 void Server::SendWallops(userrec* User, std::string text)
476 {
477         WriteWallOps(User,false,"%s",text.c_str());
478 }
479
480 void Server::ChangeHost(userrec* user, std::string host)
481 {
482         ChangeDisplayedHost(user,host.c_str());
483 }
484
485 void Server::ChangeGECOS(userrec* user, std::string gecos)
486 {
487         ChangeName(user,gecos.c_str());
488 }
489
490 bool Server::IsNick(std::string nick)
491 {
492         return (isnick(nick.c_str()) != 0);
493 }
494
495 userrec* Server::FindNick(std::string nick)
496 {
497         return Find(nick);
498 }
499
500 chanrec* Server::FindChannel(std::string channel)
501 {
502         return FindChan(channel.c_str());
503 }
504
505 std::string Server::ChanMode(userrec* User, chanrec* Chan)
506 {
507         return cmode(User,Chan);
508 }
509
510 bool Server::IsOnChannel(userrec* User, chanrec* Chan)
511 {
512         return has_channel(User,Chan);
513 }
514
515 std::string Server::GetServerName()
516 {
517         return getservername();
518 }
519
520 std::string Server::GetNetworkName()
521 {
522         return getnetworkname();
523 }
524
525 Admin Server::GetAdmin()
526 {
527         return Admin(getadminname(),getadminemail(),getadminnick());
528 }
529
530
531
532 bool Server::AddExtendedMode(char modechar, int type, bool requires_oper, int params_when_on, int params_when_off)
533 {
534         if (type == MT_SERVER)
535         {
536                 log(DEBUG,"*** API ERROR *** Modes of type MT_SERVER are reserved for future expansion");
537                 return false;
538         }
539         if (((params_when_on>0) || (params_when_off>0)) && (type == MT_CLIENT))
540         {
541                 log(DEBUG,"*** API ERROR *** Parameters on MT_CLIENT modes are not supported");
542                 return false;
543         }
544         if ((params_when_on>1) || (params_when_off>1))
545         {
546                 log(DEBUG,"*** API ERROR *** More than one parameter for an MT_CHANNEL mode is not yet supported");
547                 return false;
548         }
549         return DoAddExtendedMode(modechar,type,requires_oper,params_when_on,params_when_off);
550 }
551
552 bool Server::AddExtendedListMode(char modechar)
553 {
554         bool res = DoAddExtendedMode(modechar,MT_CHANNEL,false,1,1);
555         if (res)
556                 ModeMakeList(modechar);
557         return res;
558 }
559
560 int Server::CountUsers(chanrec* c)
561 {
562         return usercount(c);
563 }
564
565
566 ConfigReader::ConfigReader()
567 {
568         this->cache = new std::stringstream(std::stringstream::in | std::stringstream::out);
569         this->readerror = LoadConf(CONFIG_FILE,this->cache);
570         if (!this->readerror)
571                 this->error = CONF_FILE_NOT_FOUND;
572 }
573
574
575 ConfigReader::~ConfigReader()
576 {
577         if (this->cache)
578                 delete this->cache;
579 }
580
581
582 ConfigReader::ConfigReader(std::string filename)
583 {
584         this->cache = new std::stringstream(std::stringstream::in | std::stringstream::out);
585         this->readerror = LoadConf(filename.c_str(),this->cache);
586         if (!this->readerror)
587                 this->error = CONF_FILE_NOT_FOUND;
588 };
589
590 std::string ConfigReader::ReadValue(std::string tag, std::string name, int index)
591 {
592         char val[MAXBUF];
593         char t[MAXBUF];
594         char n[MAXBUF];
595         strncpy(t,tag.c_str(),MAXBUF);
596         strncpy(n,name.c_str(),MAXBUF);
597         int res = ReadConf(cache,t,n,index,val);
598         if (!res)
599         {
600                 this->error = CONF_VALUE_NOT_FOUND;
601                 return "";
602         }
603         return std::string(val);
604 }
605
606 bool ConfigReader::ReadFlag(std::string tag, std::string name, int index)
607 {
608         char val[MAXBUF];
609         char t[MAXBUF];
610         char n[MAXBUF];
611         strncpy(t,tag.c_str(),MAXBUF);
612         strncpy(n,name.c_str(),MAXBUF);
613         int res = ReadConf(cache,t,n,index,val);
614         if (!res)
615         {
616                 this->error = CONF_VALUE_NOT_FOUND;
617                 return false;
618         }
619         std::string s = val;
620         return ((s == "yes") || (s == "YES") || (s == "true") || (s == "TRUE") || (s == "1"));
621 }
622
623 long ConfigReader::ReadInteger(std::string tag, std::string name, int index, bool needs_unsigned)
624 {
625         char val[MAXBUF];
626         char t[MAXBUF];
627         char n[MAXBUF];
628         strncpy(t,tag.c_str(),MAXBUF);
629         strncpy(n,name.c_str(),MAXBUF);
630         int res = ReadConf(cache,t,n,index,val);
631         if (!res)
632         {
633                 this->error = CONF_VALUE_NOT_FOUND;
634                 return 0;
635         }
636         for (int i = 0; i < strlen(val); i++)
637         {
638                 if (!isdigit(val[i]))
639                 {
640                         this->error = CONF_NOT_A_NUMBER;
641                         return 0;
642                 }
643         }
644         if ((needs_unsigned) && (atoi(val)<0))
645         {
646                 this->error = CONF_NOT_UNSIGNED;
647                 return 0;
648         }
649         return atoi(val);
650 }
651
652 long ConfigReader::GetError()
653 {
654         long olderr = this->error;
655         this->error = 0;
656         return olderr;
657 }
658
659
660 int ConfigReader::Enumerate(std::string tag)
661 {
662         return EnumConf(cache,tag.c_str());
663 }
664
665 int ConfigReader::EnumerateValues(std::string tag, int index)
666 {
667         return EnumValues(cache, tag.c_str(), index);
668 }
669
670 bool ConfigReader::Verify()
671 {
672         return this->readerror;
673 }
674
675
676 FileReader::FileReader(std::string filename)
677 {
678         file_cache c;
679         readfile(c,filename.c_str());
680         this->fc = c;
681 }
682
683 FileReader::FileReader()
684 {
685 }
686
687 void FileReader::LoadFile(std::string filename)
688 {
689         file_cache c;
690         readfile(c,filename.c_str());
691         this->fc = c;
692 }
693
694
695 FileReader::~FileReader()
696 {
697 }
698
699 bool FileReader::Exists()
700 {
701         if (fc.size() == 0)
702         {
703                 return(false);
704         }
705         else
706         {
707                 return(true);
708         }
709 }
710
711 std::string FileReader::GetLine(int x)
712 {
713         if ((x<0) || (x>fc.size()))
714                 return "";
715         return fc[x];
716 }
717
718 int FileReader::FileSize()
719 {
720         return fc.size();
721 }
722
723
724 std::vector<Module*> modules(255);
725 std::vector<ircd_module*> factory(255);
726
727 int MODCOUNT  = -1;
728
729