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