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