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