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