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