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