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