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