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