]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules.cpp
Incorporating InspTimer into safelist as a test
[user/henk/code/inspircd.git] / src / modules.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 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 using namespace std;
18
19 #include "inspircd_config.h"
20 #include "inspircd.h"
21 #include "inspircd_io.h"
22 #include <unistd.h>
23 #include <sys/errno.h>
24 #include <time.h>
25 #include <string>
26 #ifdef GCC3
27 #include <ext/hash_map>
28 #else
29 #include <hash_map>
30 #endif
31 #include <map>
32 #include <sstream>
33 #include <vector>
34 #include <deque>
35 #include "users.h"
36 #include "ctables.h"
37 #include "globals.h"
38 #include "modules.h"
39 #include "dynamic.h"
40 #include "wildcard.h"
41 #include "message.h"
42 #include "mode.h"
43 #include "xline.h"
44 #include "commands.h"
45 #include "inspstring.h"
46 #include "helperfuncs.h"
47 #include "hashcomp.h"
48 #include "socket.h"
49 #include "socketengine.h"
50 #include "typedefs.h"
51 #include "modules.h"
52 #include "command_parse.h"
53 #include "timer.h"
54
55 extern ServerConfig *Config;
56 extern InspIRCd* ServerInstance;
57 extern int MODCOUNT;
58 extern std::vector<Module*> modules;
59 extern std::vector<ircd_module*> factory;
60 extern std::vector<InspSocket*> module_sockets;
61 extern std::vector<userrec*> local_users;
62 extern time_t TIME;
63 class Server;
64 extern userrec* fd_ref_table[MAX_DESCRIPTORS];
65
66 extern user_hash clientlist;
67 extern chan_hash chanlist;
68 extern command_table cmdlist;
69 ExtModeList EMode;
70
71 // returns true if an extended mode character is in use
72 bool ModeDefined(char modechar, int type)
73 {
74         for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
75         {
76                 if ((i->modechar == modechar) && (i->type == type))
77                 {
78                         return true;
79                 }
80         }
81         return false;
82 }
83
84 bool ModeIsListMode(char modechar, int type)
85 {
86         for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
87         {
88                 if ((i->modechar == modechar) && (i->type == type) && (i->list == true))
89                 {
90                         return true;
91                 }
92         }
93         return false;
94 }
95
96 bool ModeDefinedOper(char modechar, int type)
97 {
98         for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
99         {
100                 if ((i->modechar == modechar) && (i->type == type) && (i->needsoper == true))
101                 {
102                         return true;
103                 }
104         }
105         return false;
106 }
107
108 // returns number of parameters for a custom mode when it is switched on
109 int ModeDefinedOn(char modechar, int type)
110 {
111         for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
112         {
113                 if ((i->modechar == modechar) && (i->type == type))
114                 {
115                         return i->params_when_on;
116                 }
117         }
118         return 0;
119 }
120
121 // returns number of parameters for a custom mode when it is switched on
122 int ModeDefinedOff(char modechar, int type)
123 {
124         for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
125         {
126                 if ((i->modechar == modechar) && (i->type == type))
127                 {
128                         return i->params_when_off;
129                 }
130         }
131         return 0;
132 }
133
134 // returns true if an extended mode character is in use
135 bool DoAddExtendedMode(char modechar, int type, bool requires_oper, int params_on, int params_off)
136 {
137         if (ModeDefined(modechar,type)) {
138                 return false;
139         }
140         EMode.push_back(ExtMode(modechar,type,requires_oper,params_on,params_off));
141         return true;
142 }
143
144 // turns a mode into a listmode
145 void ModeMakeList(char modechar)
146 {
147         for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
148         {
149                 if ((i->modechar == modechar) && (i->type == MT_CHANNEL))
150                 {
151                         i->list = true;
152                         return;
153                 }
154         }
155         return;
156 }
157
158 // version is a simple class for holding a modules version number
159
160 Version::Version(int major, int minor, int revision, int build, int flags) : Major(major), Minor(minor), Revision(revision), Build(build), Flags(flags) { };
161
162 // admin is a simple class for holding a server's administrative info
163
164 Admin::Admin(std::string name, std::string email, std::string nick) : Name(name), Email(email), Nick(nick) { };
165
166 Request::Request(char* anydata, Module* src, Module* dst) : data(anydata), source(src), dest(dst) { };
167
168 char* Request::GetData()
169 {
170         return this->data;
171 }
172
173 Module* Request::GetSource()
174 {
175         return this->source;
176 }
177
178 Module* Request::GetDest()
179 {
180         return this->dest;
181 }
182
183 char* Request::Send()
184 {
185         if (this->dest)
186         {
187                 return dest->OnRequest(this);
188         }
189         else
190         {
191                 return NULL;
192         }
193 }
194
195 Event::Event(char* anydata, Module* src, std::string eventid) : data(anydata), source(src), id(eventid) { };
196
197 char* Event::GetData()
198 {
199         return this->data;
200 }
201
202 Module* Event::GetSource()
203 {
204         return this->source;
205 }
206
207 char* Event::Send()
208 {
209         FOREACH_MOD(I_OnEvent,OnEvent(this));
210         return NULL;
211 }
212
213 std::string Event::GetEventID()
214 {
215         return this->id;
216 }
217
218
219 // These declarations define the behavours of the base class Module (which does nothing at all)
220
221                 Module::Module(Server* Me) { }
222                 Module::~Module() { }
223 void            Module::OnUserConnect(userrec* user) { }
224 void            Module::OnUserQuit(userrec* user, std::string message) { }
225 void            Module::OnUserDisconnect(userrec* user) { }
226 void            Module::OnUserJoin(userrec* user, chanrec* channel) { }
227 void            Module::OnUserPart(userrec* user, chanrec* channel, std::string partmessage) { }
228 void            Module::OnRehash(std::string parameter) { }
229 void            Module::OnServerRaw(std::string &raw, bool inbound, userrec* user) { }
230 int             Module::OnUserPreJoin(userrec* user, chanrec* chan, const char* cname) { return 0; }
231 int             Module::OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list &params) { return false; }
232 void            Module::OnMode(userrec* user, void* dest, int target_type, std::string text) { };
233 Version         Module::GetVersion() { return Version(1,0,0,0,VF_VENDOR); }
234 void            Module::OnOper(userrec* user, std::string opertype) { };
235 void            Module::OnPostOper(userrec* user, std::string opertype) { };
236 void            Module::OnInfo(userrec* user) { };
237 void            Module::OnWhois(userrec* source, userrec* dest) { };
238 int             Module::OnUserPreInvite(userrec* source,userrec* dest,chanrec* channel) { return 0; };
239 int             Module::OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text,char status) { return 0; };
240 int             Module::OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text,char status) { return 0; };
241 int             Module::OnUserPreNick(userrec* user, std::string newnick) { return 0; };
242 void            Module::OnUserPostNick(userrec* user, std::string oldnick) { };
243 int             Module::OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type) { return ACR_DEFAULT; };
244 void            Module::On005Numeric(std::string &output) { };
245 int             Module::OnKill(userrec* source, userrec* dest, std::string reason) { return 0; };
246 void            Module::OnLoadModule(Module* mod,std::string name) { };
247 void            Module::OnUnloadModule(Module* mod,std::string name) { };
248 void            Module::OnBackgroundTimer(time_t curtime) { };
249 void            Module::OnSendList(userrec* user, chanrec* channel, char mode) { };
250 int             Module::OnPreCommand(std::string command, char **parameters, int pcnt, userrec *user, bool validated) { return 0; };
251 bool            Module::OnCheckReady(userrec* user) { return true; };
252 void            Module::OnUserRegister(userrec* user) { };
253 int             Module::OnUserPreKick(userrec* source, userrec* user, chanrec* chan, std::string reason) { return 0; };
254 void            Module::OnUserKick(userrec* source, userrec* user, chanrec* chan, std::string reason) { };
255 int             Module::OnRawMode(userrec* user, chanrec* chan, char mode, std::string param, bool adding, int pcnt) { return 0; };
256 int             Module::OnCheckInvite(userrec* user, chanrec* chan) { return 0; };
257 int             Module::OnCheckKey(userrec* user, chanrec* chan, std::string keygiven) { return 0; };
258 int             Module::OnCheckLimit(userrec* user, chanrec* chan) { return 0; };
259 int             Module::OnCheckBan(userrec* user, chanrec* chan) { return 0; };
260 int             Module::OnStats(char symbol, userrec* user) { return 0; };
261 int             Module::OnChangeLocalUserHost(userrec* user, std::string newhost) { return 0; };
262 int             Module::OnChangeLocalUserGECOS(userrec* user, std::string newhost) { return 0; };
263 int             Module::OnLocalTopicChange(userrec* user, chanrec* chan, std::string topic) { return 0; };
264 void            Module::OnEvent(Event* event) { return; };
265 char*           Module::OnRequest(Request* request) { return NULL; };
266 int             Module::OnOperCompare(std::string password, std::string input) { return 0; };
267 void            Module::OnGlobalOper(userrec* user) { };
268 void            Module::OnGlobalConnect(userrec* user) { };
269 int             Module::OnAddBan(userrec* source, chanrec* channel,std::string banmask) { return 0; };
270 int             Module::OnDelBan(userrec* source, chanrec* channel,std::string banmask) { return 0; };
271 void            Module::OnRawSocketAccept(int fd, std::string ip, int localport) { };
272 int             Module::OnRawSocketWrite(int fd, char* buffer, int count) { return 0; };
273 void            Module::OnRawSocketClose(int fd) { };
274 int             Module::OnRawSocketRead(int fd, char* buffer, unsigned int count, int &readresult) { return 0; };
275 void            Module::OnUserMessage(userrec* user, void* dest, int target_type, std::string text, char status) { };
276 void            Module::OnUserNotice(userrec* user, void* dest, int target_type, std::string text, char status) { };
277 void            Module::OnRemoteKill(userrec* source, userrec* dest, std::string reason) { };
278 void            Module::OnUserInvite(userrec* source,userrec* dest,chanrec* channel) { };
279 void            Module::OnPostLocalTopicChange(userrec* user, chanrec* chan, std::string topic) { };
280 void            Module::OnGetServerDescription(std::string servername,std::string &description) { };
281 void            Module::OnSyncUser(userrec* user, Module* proto, void* opaque) { };
282 void            Module::OnSyncChannel(chanrec* chan, Module* proto, void* opaque) { };
283 void            Module::ProtoSendMode(void* opaque, int target_type, void* target, std::string modeline) { };
284 void            Module::OnSyncChannelMetaData(chanrec* chan, Module* proto,void* opaque, std::string extname) { };
285 void            Module::OnSyncUserMetaData(userrec* user, Module* proto,void* opaque, std::string extname) { };
286 void            Module::OnSyncOtherMetaData(Module* proto, void* opaque) { };
287 void            Module::OnDecodeMetaData(int target_type, void* target, std::string extname, std::string extdata) { };
288 void            Module::ProtoSendMetaData(void* opaque, int target_type, void* target, std::string extname, std::string extdata) { };
289 void            Module::OnWallops(userrec* user, std::string text) { };
290 void            Module::OnChangeHost(userrec* user, std::string newhost) { };
291 void            Module::OnChangeName(userrec* user, std::string gecos) { };
292 void            Module::OnAddGLine(long duration, userrec* source, std::string reason, std::string hostmask) { };
293 void            Module::OnAddZLine(long duration, userrec* source, std::string reason, std::string ipmask) { };
294 void            Module::OnAddKLine(long duration, userrec* source, std::string reason, std::string hostmask) { };
295 void            Module::OnAddQLine(long duration, userrec* source, std::string reason, std::string nickmask) { };
296 void            Module::OnAddELine(long duration, userrec* source, std::string reason, std::string hostmask) { };
297 void            Module::OnDelGLine(userrec* source, std::string hostmask) { };
298 void            Module::OnDelZLine(userrec* source, std::string ipmask) { };
299 void            Module::OnDelKLine(userrec* source, std::string hostmask) { };
300 void            Module::OnDelQLine(userrec* source, std::string nickmask) { };
301 void            Module::OnDelELine(userrec* source, std::string hostmask) { };
302 void            Module::OnCleanup(int target_type, void* item) { };
303 void            Module::Implements(char* Implements) { for (int j = 0; j < 255; j++) Implements[j] = 0; };
304 void            Module::OnChannelDelete(chanrec* chan) { };
305 Priority        Module::Prioritize() { return PRIORITY_DONTCARE; }
306 void            Module::OnSetAway(userrec* user) { };
307 void            Module::OnCancelAway(userrec* user) { };
308
309 /* server is a wrapper class that provides methods to all of the C-style
310  * exports in the core
311  */
312
313 Server::Server()
314 {
315 }
316
317 Server::~Server()
318 {
319 }
320
321 void Server::AddSocket(InspSocket* sock)
322 {
323         module_sockets.push_back(sock);
324 }
325
326 void Server::RemoveSocket(InspSocket* sock)
327 {
328         for (std::vector<InspSocket*>::iterator a = module_sockets.begin(); a < module_sockets.end(); a++)
329         {
330                 InspSocket* s = (InspSocket*)*a;
331                 if (s == sock)
332                 {
333                         log(DEBUG,"Forcibly removed socket");
334                         ServerInstance->SE->DelFd(s->GetFd());
335                         s->Close();
336                         module_sockets.erase(a);
337                         delete s;
338                         return;
339                 }
340         }
341 }
342
343 long Server::PriorityAfter(std::string modulename)
344 {
345         for (unsigned int j = 0; j < Config->module_names.size(); j++)
346         {
347                 if (Config->module_names[j] == modulename)
348                 {
349                         return ((j << 8) | PRIORITY_AFTER);
350                 }
351         }
352         return PRIORITY_DONTCARE;
353 }
354
355 long Server::PriorityBefore(std::string modulename)
356 {
357         for (unsigned int j = 0; j < Config->module_names.size(); j++)
358         {
359                 if (Config->module_names[j] == modulename)
360                 {
361                         return ((j << 8) | PRIORITY_BEFORE);
362                 }
363         }
364         return PRIORITY_DONTCARE;
365 }
366
367 void Server::RehashServer()
368 {
369         WriteOpers("*** Rehashing config file");
370         Config->Read(false,NULL);
371 }
372
373 ServerConfig* Server::GetConfig()
374 {
375         return Config;
376 }
377
378 std::string Server::GetVersion()
379 {
380         return ServerInstance->GetVersionString();
381 }
382
383 void Server::DelSocket(InspSocket* sock)
384 {
385         for (std::vector<InspSocket*>::iterator a = module_sockets.begin(); a < module_sockets.end(); a++)
386         {
387                 if (*a == sock)
388                 {
389                         module_sockets.erase(a);
390                         return;
391                 }
392         }
393 }
394
395 long Server::GetChannelCount()
396 {
397         return (long)chanlist.size();
398 }
399
400 /* This is ugly, yes, but hash_map's arent designed to be
401  * addressed in this manner, and this is a bit of a kludge.
402  * Luckily its a specialist function and rarely used by
403  * many modules (in fact, it was specially created to make
404  * m_safelist possible, initially).
405  */
406
407 chanrec* Server::GetChannelIndex(long index)
408 {
409         int target = 0;
410         for (chan_hash::iterator n = chanlist.begin(); n != chanlist.end(); n++, target++)
411         {
412                 if (index == target)
413                         return n->second;
414         }
415         return NULL;
416 }
417
418 void Server::AddTimer(InspTimer* T)
419 {
420         AddTimer(T);
421 }
422
423 void Server::SendOpers(std::string s)
424 {
425         WriteOpers("%s",s.c_str());
426 }
427
428 bool Server::MatchText(std::string sliteral, std::string spattern)
429 {
430         char literal[MAXBUF],pattern[MAXBUF];
431         strlcpy(literal,sliteral.c_str(),MAXBUF);
432         strlcpy(pattern,spattern.c_str(),MAXBUF);
433         return match(literal,pattern);
434 }
435
436 void Server::SendToModeMask(std::string modes, int flags, std::string text)
437 {
438         WriteMode(modes.c_str(),flags,"%s",text.c_str());
439 }
440
441 chanrec* Server::JoinUserToChannel(userrec* user, std::string cname, std::string key)
442 {
443         return add_channel(user,cname.c_str(),key.c_str(),false);
444 }
445
446 chanrec* Server::PartUserFromChannel(userrec* user, std::string cname, std::string reason)
447 {
448         return del_channel(user,cname.c_str(),reason.c_str(),false);
449 }
450
451 chanuserlist Server::GetUsers(chanrec* chan)
452 {
453         chanuserlist userl;
454         userl.clear();
455         std::map<char*,char*> *list = chan->GetUsers();
456         for (std::map<char*,char*>::iterator i = list->begin(); i != list->end(); i++)
457         {
458                 char* o = i->second;
459                 userl.push_back((userrec*)o);
460         }
461         return userl;
462 }
463 void Server::ChangeUserNick(userrec* user, std::string nickname)
464 {
465         force_nickchange(user,nickname.c_str());
466 }
467
468 void Server::KickUser(userrec* source, userrec* target, chanrec* chan, std::string reason)
469 {
470         if (source)
471         {
472                 kick_channel(source,target,chan,(char*)reason.c_str());
473         }
474         else
475         {
476                 server_kick_channel(target,chan,(char*)reason.c_str(),true);
477         }
478 }
479
480 void Server::QuitUser(userrec* user, std::string reason)
481 {
482         kill_link(user,reason.c_str());
483 }
484
485 bool Server::IsUlined(std::string server)
486 {
487         return is_uline(server.c_str());
488 }
489
490 void Server::CallCommandHandler(std::string commandname, char** parameters, int pcnt, userrec* user)
491 {
492         ServerInstance->Parser->CallHandler(commandname,parameters,pcnt,user);
493 }
494
495 bool Server::IsValidModuleCommand(std::string commandname, int pcnt, userrec* user)
496 {
497         return ServerInstance->Parser->IsValidCommand(commandname, pcnt, user);
498 }
499
500 void Server::Log(int level, std::string s)
501 {
502         log(level,"%s",s.c_str());
503 }
504
505 void Server::AddCommand(command_t *f)
506 {
507         if (!ServerInstance->Parser->CreateCommand(f))
508         {
509                 ModuleException err("Command "+std::string(f->command)+" already exists.");
510                 throw (err);
511         }
512 }
513
514 void Server::SendMode(char **parameters, int pcnt, userrec *user)
515 {
516         ServerInstance->ModeGrok->ServerMode(parameters,pcnt,user);
517 }
518
519 void Server::Send(int Socket, std::string s)
520 {
521         Write_NoFormat(Socket,s.c_str());
522 }
523
524 void Server::SendServ(int Socket, std::string s)
525 {
526         WriteServ_NoFormat(Socket,s.c_str());
527 }
528
529 void Server::SendFrom(int Socket, userrec* User, std::string s)
530 {
531         WriteFrom_NoFormat(Socket,User,s.c_str());
532 }
533
534 void Server::SendTo(userrec* Source, userrec* Dest, std::string s)
535 {
536         if (!Source)
537         {
538                 // if source is NULL, then the message originates from the local server
539                 Write(Dest->fd,":%s %s",this->GetServerName().c_str(),s.c_str());
540         }
541         else
542         {
543                 // otherwise it comes from the user specified
544                 WriteTo_NoFormat(Source,Dest,s.c_str());
545         }
546 }
547
548 void Server::SendChannelServerNotice(std::string ServName, chanrec* Channel, std::string text)
549 {
550         WriteChannelWithServ_NoFormat((char*)ServName.c_str(), Channel, text.c_str());
551 }
552
553 void Server::SendChannel(userrec* User, chanrec* Channel, std::string s,bool IncludeSender)
554 {
555         if (IncludeSender)
556         {
557                 WriteChannel_NoFormat(Channel,User,s.c_str());
558         }
559         else
560         {
561                 ChanExceptSender_NoFormat(Channel,User,0,s.c_str());
562         }
563 }
564
565 bool Server::CommonChannels(userrec* u1, userrec* u2)
566 {
567         return (common_channels(u1,u2) != 0);
568 }
569
570 void Server::SendCommon(userrec* User, std::string text,bool IncludeSender)
571 {
572         if (IncludeSender)
573         {
574                 WriteCommon_NoFormat(User,text.c_str());
575         }
576         else
577         {
578                 WriteCommonExcept_NoFormat(User,text.c_str());
579         }
580 }
581
582 void Server::SendWallops(userrec* User, std::string text)
583 {
584         WriteWallOps(User,false,"%s",text.c_str());
585 }
586
587 void Server::ChangeHost(userrec* user, std::string host)
588 {
589         ChangeDisplayedHost(user,host.c_str());
590 }
591
592 void Server::ChangeGECOS(userrec* user, std::string gecos)
593 {
594         ChangeName(user,gecos.c_str());
595 }
596
597 bool Server::IsNick(std::string nick)
598 {
599         return (isnick(nick.c_str()) != 0);
600 }
601
602 userrec* Server::FindNick(std::string nick)
603 {
604         return Find(nick);
605 }
606
607 userrec* Server::FindDescriptor(int socket)
608 {
609         return (socket < 65536 ? fd_ref_table[socket] : NULL);
610 }
611
612 chanrec* Server::FindChannel(std::string channel)
613 {
614         return FindChan(channel.c_str());
615 }
616
617 std::string Server::ChanMode(userrec* User, chanrec* Chan)
618 {
619         return cmode(User,Chan);
620 }
621
622 bool Server::IsOnChannel(userrec* User, chanrec* Chan)
623 {
624         return has_channel(User,Chan);
625 }
626
627 std::string Server::GetServerName()
628 {
629         return Config->ServerName;
630 }
631
632 std::string Server::GetNetworkName()
633 {
634         return Config->Network;
635 }
636
637 std::string Server::GetServerDescription()
638 {
639         return Config->ServerDesc;
640 }
641
642 Admin Server::GetAdmin()
643 {
644         return Admin(Config->AdminName,Config->AdminEmail,Config->AdminNick);
645 }
646
647
648
649 bool Server::AddExtendedMode(char modechar, int type, bool requires_oper, int params_when_on, int params_when_off)
650 {
651         if (((modechar >= 'A') && (modechar <= 'Z')) || ((modechar >= 'a') && (modechar <= 'z')))
652         {
653                 if (type == MT_SERVER)
654                 {
655                         ModuleException e("Modes of type MT_SERVER are reserved for future expansion");
656                         throw(e);
657                         return false;
658                 }
659                 if (((params_when_on>0) || (params_when_off>0)) && (type == MT_CLIENT))
660                 {
661                         ModuleException e("Parameters on MT_CLIENT modes are not supported");
662                         throw(e);
663                         return false;
664                 }
665                 if ((params_when_on>1) || (params_when_off>1))
666                 {
667                         ModuleException e("More than one parameter for an MT_CHANNEL mode is not yet supported");
668                         throw(e);
669                         return false;
670                 }
671                 return DoAddExtendedMode(modechar,type,requires_oper,params_when_on,params_when_off);
672         }
673         else
674         {
675                 ModuleException e("Muppet modechar detected.");
676                 throw(e);
677         }
678         return false;
679 }
680
681 bool Server::AddExtendedListMode(char modechar)
682 {
683         bool res = DoAddExtendedMode(modechar,MT_CHANNEL,false,1,1);
684         if (res)
685                 ModeMakeList(modechar);
686         return res;
687 }
688
689 int Server::CountUsers(chanrec* c)
690 {
691         return usercount(c);
692 }
693
694
695 bool Server::UserToPseudo(userrec* user,std::string message)
696 {
697         unsigned int old_fd = user->fd;
698         Write(old_fd,"ERROR :Closing link (%s@%s) [%s]",user->ident,user->host,message.c_str());
699         user->FlushWriteBuf();
700         user->ClearBuffer();
701         user->fd = FD_MAGIC_NUMBER;
702
703         if (find(local_users.begin(),local_users.end(),user) != local_users.end())
704         {
705                 local_users.erase(find(local_users.begin(),local_users.end(),user));
706                 log(DEBUG,"Delete local user");
707         }
708
709         ServerInstance->SE->DelFd(old_fd);
710         shutdown(old_fd,2);
711         close(old_fd);
712         return true;
713 }
714
715 bool Server::PseudoToUser(userrec* alive,userrec* zombie,std::string message)
716 {
717         log(DEBUG,"PseudoToUser");
718         zombie->fd = alive->fd;
719         FOREACH_MOD(I_OnUserQuit,OnUserQuit(alive,message));
720         alive->fd = FD_MAGIC_NUMBER;
721         alive->FlushWriteBuf();
722         alive->ClearBuffer();
723         // save these for later
724         std::string oldnick = alive->nick;
725         std::string oldhost = alive->host;
726         std::string oldident = alive->ident;
727         kill_link(alive,message.c_str());
728         if (find(local_users.begin(),local_users.end(),alive) != local_users.end())
729         {
730                 local_users.erase(find(local_users.begin(),local_users.end(),alive));
731                 log(DEBUG,"Delete local user");
732         }
733         // Fix by brain - cant write the user until their fd table entry is updated
734         fd_ref_table[zombie->fd] = zombie;
735         Write(zombie->fd,":%s!%s@%s NICK %s",oldnick.c_str(),oldident.c_str(),oldhost.c_str(),zombie->nick);
736         for (unsigned int i = 0; i < zombie->chans.size(); i++)
737         {
738                 if (zombie->chans[i].channel != NULL)
739                 {
740                         if (zombie->chans[i].channel->name)
741                         {
742                                 chanrec* Ptr = zombie->chans[i].channel;
743                                 WriteFrom(zombie->fd,zombie,"JOIN %s",Ptr->name);
744                                 if (Ptr->topicset)
745                                 {
746                                         WriteServ(zombie->fd,"332 %s %s :%s", zombie->nick, Ptr->name, Ptr->topic);
747                                         WriteServ(zombie->fd,"333 %s %s %s %d", zombie->nick, Ptr->name, Ptr->setby, Ptr->topicset);
748                                 }
749                                 userlist(zombie,Ptr);
750                                 WriteServ(zombie->fd,"366 %s %s :End of /NAMES list.", zombie->nick, Ptr->name);
751
752                         }
753                 }
754         }
755         if ((find(local_users.begin(),local_users.end(),zombie) == local_users.end()) && (zombie->fd != FD_MAGIC_NUMBER))
756                 local_users.push_back(zombie);
757
758         return true;
759 }
760
761 void Server::AddGLine(long duration, std::string source, std::string reason, std::string hostmask)
762 {
763         add_gline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
764 }
765
766 void Server::AddQLine(long duration, std::string source, std::string reason, std::string nickname)
767 {
768         add_qline(duration, source.c_str(), reason.c_str(), nickname.c_str());
769 }
770
771 void Server::AddZLine(long duration, std::string source, std::string reason, std::string ipaddr)
772 {
773         add_zline(duration, source.c_str(), reason.c_str(), ipaddr.c_str());
774 }
775
776 void Server::AddKLine(long duration, std::string source, std::string reason, std::string hostmask)
777 {
778         add_kline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
779 }
780
781 void Server::AddELine(long duration, std::string source, std::string reason, std::string hostmask)
782 {
783         add_eline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
784 }
785
786 bool Server::DelGLine(std::string hostmask)
787 {
788         return del_gline(hostmask.c_str());
789 }
790
791 bool Server::DelQLine(std::string nickname)
792 {
793         return del_qline(nickname.c_str());
794 }
795
796 bool Server::DelZLine(std::string ipaddr)
797 {
798         return del_zline(ipaddr.c_str());
799 }
800
801 bool Server::DelKLine(std::string hostmask)
802 {
803         return del_kline(hostmask.c_str());
804 }
805
806 bool Server::DelELine(std::string hostmask)
807 {
808         return del_eline(hostmask.c_str());
809 }
810
811 long Server::CalcDuration(std::string delta)
812 {
813         return duration(delta.c_str());
814 }
815
816 bool Server::IsValidMask(std::string mask)
817 {
818         char* dest = (char*)mask.c_str();
819         if (strchr(dest,'!')==0)
820                 return false;
821         if (strchr(dest,'@')==0)
822                 return false;
823         for (char* i = dest; *i; i++)
824                 if (*i < 32)
825                         return false;
826         for (char* i = dest; *i; i++)
827                 if (*i > 126)
828                         return false;
829         unsigned int c = 0;
830         for (char* i = dest; *i; i++)
831                 if (*i == '!')
832                         c++;
833         if (c>1)
834                 return false;
835         c = 0;
836         for (char* i = dest; *i; i++)
837                 if (*i == '@')
838                         c++;
839         if (c>1)
840                 return false;
841
842         return true;
843 }
844
845 Module* Server::FindModule(std::string name)
846 {
847         for (int i = 0; i <= MODCOUNT; i++)
848         {
849                 if (Config->module_names[i] == name)
850                 {
851                         return modules[i];
852                 }
853         }
854         return NULL;
855 }
856
857 ConfigReader::ConfigReader()
858 {
859         Config->ClearStack();
860         this->cache = new std::stringstream(std::stringstream::in | std::stringstream::out);
861         this->errorlog = new std::stringstream(std::stringstream::in | std::stringstream::out);
862         this->readerror = Config->LoadConf(CONFIG_FILE,this->cache,this->errorlog);
863         if (!this->readerror)
864                 this->error = CONF_FILE_NOT_FOUND;
865 }
866
867
868 ConfigReader::~ConfigReader()
869 {
870         if (this->cache)
871                 delete this->cache;
872         if (this->errorlog)
873                 delete this->errorlog;
874 }
875
876
877 ConfigReader::ConfigReader(std::string filename)
878 {
879         Config->ClearStack();
880         this->cache = new std::stringstream(std::stringstream::in | std::stringstream::out);
881         this->errorlog = new std::stringstream(std::stringstream::in | std::stringstream::out);
882         this->readerror = Config->LoadConf(filename.c_str(),this->cache,this->errorlog);
883         if (!this->readerror)
884                 this->error = CONF_FILE_NOT_FOUND;
885 };
886
887 std::string ConfigReader::ReadValue(std::string tag, std::string name, int index)
888 {
889         char val[MAXBUF];
890         char t[MAXBUF];
891         char n[MAXBUF];
892         strlcpy(t,tag.c_str(),MAXBUF);
893         strlcpy(n,name.c_str(),MAXBUF);
894         int res = Config->ReadConf(cache,t,n,index,val);
895         if (!res)
896         {
897                 this->error = CONF_VALUE_NOT_FOUND;
898                 return "";
899         }
900         return val;
901 }
902
903 bool ConfigReader::ReadFlag(std::string tag, std::string name, int index)
904 {
905         char val[MAXBUF];
906         char t[MAXBUF];
907         char n[MAXBUF];
908         strlcpy(t,tag.c_str(),MAXBUF);
909         strlcpy(n,name.c_str(),MAXBUF);
910         int res = Config->ReadConf(cache,t,n,index,val);
911         if (!res)
912         {
913                 this->error = CONF_VALUE_NOT_FOUND;
914                 return false;
915         }
916         std::string s = val;
917         return ((s == "yes") || (s == "YES") || (s == "true") || (s == "TRUE") || (s == "1"));
918 }
919
920 long ConfigReader::ReadInteger(std::string tag, std::string name, int index, bool needs_unsigned)
921 {
922         char val[MAXBUF];
923         char t[MAXBUF];
924         char n[MAXBUF];
925         strlcpy(t,tag.c_str(),MAXBUF);
926         strlcpy(n,name.c_str(),MAXBUF);
927         int res = Config->ReadConf(cache,t,n,index,val);
928         if (!res)
929         {
930                 this->error = CONF_VALUE_NOT_FOUND;
931                 return 0;
932         }
933         for (char* i = val; *i; i++)
934         {
935                 if (!isdigit(*i))
936                 {
937                         this->error = CONF_NOT_A_NUMBER;
938                         return 0;
939                 }
940         }
941         if ((needs_unsigned) && (atoi(val)<0))
942         {
943                 this->error = CONF_NOT_UNSIGNED;
944                 return 0;
945         }
946         return atoi(val);
947 }
948
949 long ConfigReader::GetError()
950 {
951         long olderr = this->error;
952         this->error = 0;
953         return olderr;
954 }
955
956 void ConfigReader::DumpErrors(bool bail, userrec* user)
957 {
958         if (bail)
959         {
960                 printf("There were errors in your configuration:\n%s",errorlog->str().c_str());
961                 exit(0);
962         }
963         else
964         {
965                 char dataline[1024];
966                 if (user)
967                 {
968                         WriteServ(user->fd,"NOTICE %s :There were errors in the configuration file:",user->nick);
969                         while (!errorlog->eof())
970                         {
971                                 errorlog->getline(dataline,1024);
972                                 WriteServ(user->fd,"NOTICE %s :%s",user->nick,dataline);
973                         }
974                 }
975                 else
976                 {
977                         WriteOpers("There were errors in the configuration file:",user->nick);
978                         while (!errorlog->eof())
979                         {
980                                 errorlog->getline(dataline,1024);
981                                 WriteOpers(dataline);
982                         }
983                 }
984                 return;
985         }
986 }
987
988
989 int ConfigReader::Enumerate(std::string tag)
990 {
991         return Config->EnumConf(cache,tag.c_str());
992 }
993
994 int ConfigReader::EnumerateValues(std::string tag, int index)
995 {
996         return Config->EnumValues(cache, tag.c_str(), index);
997 }
998
999 bool ConfigReader::Verify()
1000 {
1001         return this->readerror;
1002 }
1003
1004
1005 FileReader::FileReader(std::string filename)
1006 {
1007         file_cache c;
1008         readfile(c,filename.c_str());
1009         this->fc = c;
1010 }
1011
1012 FileReader::FileReader()
1013 {
1014 }
1015
1016 void FileReader::LoadFile(std::string filename)
1017 {
1018         file_cache c;
1019         readfile(c,filename.c_str());
1020         this->fc = c;
1021 }
1022
1023
1024 FileReader::~FileReader()
1025 {
1026 }
1027
1028 bool FileReader::Exists()
1029 {
1030         if (fc.size() == 0)
1031         {
1032                 return(false);
1033         }
1034         else
1035         {
1036                 return(true);
1037         }
1038 }
1039
1040 std::string FileReader::GetLine(int x)
1041 {
1042         if ((x<0) || ((unsigned)x>fc.size()))
1043                 return "";
1044         return fc[x];
1045 }
1046
1047 int FileReader::FileSize()
1048 {
1049         return fc.size();
1050 }
1051
1052
1053 std::vector<Module*> modules(255);
1054 std::vector<ircd_module*> factory(255);
1055
1056 int MODCOUNT  = -1;
1057
1058