]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/helperfuncs.cpp
Removed unneccessary backticks from a copy/paste
[user/henk/code/inspircd.git] / src / helperfuncs.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 using namespace std;
18
19 #include "inspircd_config.h"
20 #include "inspircd.h"
21 #include "inspircd_io.h"
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <sys/errno.h>
25 #include <time.h>
26 #include <string>
27 #ifdef GCC3
28 #include <ext/hash_map>
29 #else
30 #include <hash_map>
31 #endif
32 #include <sstream>
33 #include <vector>
34 #include <deque>
35 #include <stdarg.h>
36 #include "connection.h"
37 #include "users.h"
38 #include "ctables.h"
39 #include "globals.h"
40 #include "modules.h"
41 #include "dynamic.h"
42 #include "wildcard.h"
43 #include "message.h"
44 #include "mode.h"
45 #include "xline.h"
46 #include "commands.h"
47 #include "inspstring.h"
48 #include "helperfuncs.h"
49 #include "hashcomp.h"
50 #include "typedefs.h"
51
52 extern int MODCOUNT;
53 extern std::vector<Module*> modules;
54 extern ServerConfig *Config;
55 extern InspIRCd* ServerInstance;
56 extern time_t TIME;
57 extern char lowermap[255];
58 static char list[MAXBUF];
59 extern userrec* fd_ref_table[MAX_DESCRIPTORS];
60 static char already_sent[MAX_DESCRIPTORS];
61 extern std::vector<userrec*> all_opers;
62 extern user_hash clientlist;
63 extern chan_hash chanlist;
64
65 extern std::vector<userrec*> local_users;
66
67 static char TIMESTR[26];
68 static time_t LAST = 0;
69
70 /** log()
71  *  Write a line of text `text' to the logfile (and stdout, if in nofork) if the level `level'
72  *  is greater than the configured loglevel.
73  */
74 void log(int level, char *text, ...)
75 {
76         va_list argsPtr;
77
78         if (level < Config->LogLevel)
79                 return;
80
81         char textbuffer[MAXBUF];
82         if (TIME != LAST)
83         {
84                 struct tm * timeinfo;
85                 timeinfo = localtime(&TIME);
86                 strlcpy(TIMESTR,asctime(timeinfo),26);
87                 TIMESTR[24] = ':';
88                 LAST = TIME;
89         }
90
91         if (Config->log_file)
92         {
93                 va_start (argsPtr, text);
94                 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
95                 va_end(argsPtr);
96
97                 if (Config->log_file)
98                         fprintf(Config->log_file,"%s %s\n",TIMESTR,textbuffer);
99                 if (Config->nofork)
100                 {
101                         // nofork enabled? display it on terminal too
102                         printf("%s %s\n",TIMESTR,textbuffer);
103                 }
104         }
105 }
106
107 /** readfile()
108  *  Read the contents of a file located by `fname' into a file_cache pointed at by `F'.
109  *
110  *  XXX - we may want to consider returning a file_cache or pointer to one, less confusing.
111  */
112 void readfile(file_cache &F, const char* fname)
113 {
114         FILE* file;
115         char linebuf[MAXBUF];
116
117         log(DEBUG,"readfile: loading %s",fname);
118         F.clear();
119         file =  fopen(fname,"r");
120         if (file)
121         {
122                 while (!feof(file))
123                 {
124                         fgets(linebuf,sizeof(linebuf),file);
125                         linebuf[strlen(linebuf)-1]='\0';
126                         if (!*linebuf)
127                         {
128                                 strcpy(linebuf,"  ");
129                         }
130                         if (!feof(file))
131                         {
132                                 F.push_back(linebuf);
133                         }
134                 }
135                 fclose(file);
136         }
137         else
138         {
139                 log(DEBUG,"readfile: failed to load file: %s",fname);
140         }
141         log(DEBUG,"readfile: loaded %s, %lu lines",fname,(unsigned long)F.size());
142 }
143
144 /** Write_NoFormat()
145  *  Writes a given string in `text' to the socket on fd `sock' - only if the socket
146  *  is a valid entry in the local FD table.
147  */
148 void Write_NoFormat(int sock, const char *text)
149 {
150         if ((sock < 0) || (!text) || (sock > MAX_DESCRIPTORS))
151                 return;
152
153         char tb[MAXBUF];
154         int bytes = snprintf(tb,MAXBUF,"%s\r\n",text);
155         chop(tb);
156         if (fd_ref_table[sock])
157         {
158                 if (Config->GetIOHook(fd_ref_table[sock]->port))
159                 {
160                         try
161                         {
162                                 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
163                         }
164                         catch (ModuleException& modexcept)
165                         {
166                                 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
167                         }
168                 }
169                 else
170                 {
171                         fd_ref_table[sock]->AddWriteBuf(tb);
172                 }
173                 ServerInstance->stats->statsSent += bytes;
174         }
175         else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
176 }
177
178 /** Write()
179  *  Same as Write_NoFormat(), but formatted printf() style first.
180  */
181 void Write(int sock, char *text, ...)
182 {
183         if ((sock < 0) || (sock > MAX_DESCRIPTORS))
184                 return;
185         if (!text)
186         {
187                 log(DEFAULT,"*** BUG *** Write was given an invalid parameter");
188                 return;
189         }
190         va_list argsPtr;
191         char textbuffer[MAXBUF],tb[MAXBUF];
192         va_start (argsPtr, text);
193         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
194         va_end(argsPtr);
195         int bytes = snprintf(tb,MAXBUF,"%s\r\n",textbuffer);
196         chop(tb);
197         if (fd_ref_table[sock])
198         {
199                 if (Config->GetIOHook(fd_ref_table[sock]->port))
200                 {
201                         try
202                         {
203                                 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
204                         }
205                         catch (ModuleException& modexcept)
206                         {
207                                 log(DEBUG,"Module exception cought: %s",modexcept.GetReason());
208                         }                                                               
209                 }
210                 else
211                 {
212                         fd_ref_table[sock]->AddWriteBuf(tb);
213                 }
214                 ServerInstance->stats->statsSent += bytes;
215         }
216         else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
217 }
218
219 /** WriteServ_NoFormat()
220  *  Same as Write_NoFormat(), except prefixes `text' with `:server.name '.
221  */
222 void WriteServ_NoFormat(int sock, const char* text)
223 {
224         if ((sock < 0) || (!text) || (sock > MAX_DESCRIPTORS))
225                 return;
226         char tb[MAXBUF];
227         int bytes = snprintf(tb,MAXBUF,":%s %s\r\n",Config->ServerName,text);
228         chop(tb);
229         if (fd_ref_table[sock])
230         {
231                 if (Config->GetIOHook(fd_ref_table[sock]->port))
232                 {
233                         try
234                         {
235                                 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
236                         }
237                         catch (ModuleException& modexcept)
238                         {
239                                 log(DEBUG,"Module exception cought: %s",modexcept.GetReason());
240                         }
241                 }
242                 else
243                 {
244                         fd_ref_table[sock]->AddWriteBuf(tb);
245                 }
246                 ServerInstance->stats->statsSent += bytes;
247         }
248         else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
249 }
250
251 /** WriteServ()
252  *  Same as Write(), except `text' is prefixed with `:server.name '.
253  */
254 void WriteServ(int sock, char* text, ...)
255 {
256         if ((sock < 0) || (sock > MAX_DESCRIPTORS))
257                 return;
258         if (!text)
259         {
260                 log(DEFAULT,"*** BUG *** WriteServ was given an invalid parameter");
261                 return;
262         }
263         va_list argsPtr;
264         va_start (argsPtr, text);
265         char textbuffer[MAXBUF],tb[MAXBUF];
266         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
267         va_end(argsPtr);
268         int bytes = snprintf(tb,MAXBUF,":%s %s\r\n",Config->ServerName,textbuffer);
269         chop(tb);
270         if (fd_ref_table[sock])
271         {
272                 if (Config->GetIOHook(fd_ref_table[sock]->port))
273                 {
274                         try
275                         {
276                                 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
277                         }
278                         catch (ModuleException& modexcept)
279                         {
280                                 log(DEBUG,"Module exception cought: %s",modexcept.GetReason());
281                         }
282                 }
283                 else
284                 {
285                         fd_ref_table[sock]->AddWriteBuf(tb);
286                 }
287                 ServerInstance->stats->statsSent += bytes;
288         }
289         else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
290 }
291
292 /** WriteFrom_NoFormat()
293  * Write `text' to a socket with fd `sock' prefixed with `:n!u@h' - taken from
294  * the nick, user, and host of `user'.
295  */
296 void WriteFrom_NoFormat(int sock, userrec *user, const char* text)
297 {
298         if ((sock < 0) || (!text) || (!user) || (sock > MAX_DESCRIPTORS))
299                 return;
300         char tb[MAXBUF];
301         int bytes = snprintf(tb,MAXBUF,":%s %s\r\n",user->GetFullHost(),text);
302         chop(tb);
303         if (fd_ref_table[sock])
304         {
305                 if (Config->GetIOHook(fd_ref_table[sock]->port))
306                 {
307                         try
308                         {
309                                 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
310                         }
311                         catch (ModuleException& modexcept)
312                         {
313                                 log(DEBUG,"Module exception cought: %s",modexcept.GetReason());
314                         }
315                 }
316                 else
317                 {
318                         fd_ref_table[sock]->AddWriteBuf(tb);
319                 }
320                 ServerInstance->stats->statsSent += bytes;
321         }
322         else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
323 }
324
325 /* write text from an originating user to originating user */
326
327 void WriteFrom(int sock, userrec *user,char* text, ...)
328 {
329         if ((sock < 0) || (sock > MAX_DESCRIPTORS))
330                 return;
331         if ((!text) || (!user))
332         {
333                 log(DEFAULT,"*** BUG *** WriteFrom was given an invalid parameter");
334                 return;
335         }
336         va_list argsPtr;
337         va_start (argsPtr, text);
338         char textbuffer[MAXBUF],tb[MAXBUF];
339         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
340         va_end(argsPtr);
341         int bytes = snprintf(tb,MAXBUF,":%s %s\r\n",user->GetFullHost(),textbuffer);
342         chop(tb);
343         if (fd_ref_table[sock])
344         {
345                 if (Config->GetIOHook(fd_ref_table[sock]->port))
346                 {
347                         try
348                         {
349                                 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
350                         }
351                         catch (ModuleException& modexcept)
352                         {
353                                 log(DEBUG,"Module exception cought: %s",modexcept.GetReason());
354                         }
355                 }
356                 else
357                 {
358                         fd_ref_table[sock]->AddWriteBuf(tb);
359                 }
360                 ServerInstance->stats->statsSent += bytes;
361         }
362         else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
363 }
364
365 /* write text to an destination user from a source user (e.g. user privmsg) */
366
367 void WriteTo(userrec *source, userrec *dest,char *data, ...)
368 {
369         if ((!dest) || (!data))
370         {
371                 log(DEFAULT,"*** BUG *** WriteTo was given an invalid parameter");
372                 return;
373         }
374         if (!IS_LOCAL(dest))
375                 return;
376         char textbuffer[MAXBUF];
377         va_list argsPtr;
378         va_start (argsPtr, data);
379         vsnprintf(textbuffer, MAXBUF, data, argsPtr);
380         va_end(argsPtr);
381         chop(textbuffer);
382
383         // if no source given send it from the server.
384         if (!source)
385         {
386                 WriteServ_NoFormat(dest->fd,textbuffer);
387         }
388         else
389         {
390                 WriteFrom_NoFormat(dest->fd,source,textbuffer);
391         }
392 }
393
394 void WriteTo_NoFormat(userrec *source, userrec *dest, const char *data)
395 {
396         if ((!dest) || (!data))
397                 return;
398         if (!source)
399         {
400                 WriteServ_NoFormat(dest->fd,data);
401         }
402         else
403         {
404                 WriteFrom_NoFormat(dest->fd,source,data);
405         }
406 }
407
408 /* write formatted text from a source user to all users on a channel
409  * including the sender (NOT for privmsg, notice etc!) */
410
411 void WriteChannel(chanrec* Ptr, userrec* user, char* text, ...)
412 {
413         if ((!Ptr) || (!user) || (!text))
414         {
415                 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
416                 return;
417         }
418         char textbuffer[MAXBUF];
419         va_list argsPtr;
420         va_start (argsPtr, text);
421         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
422         va_end(argsPtr);
423
424         std::map<char*,char*> *ulist= Ptr->GetUsers();
425         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
426         {
427                 char* o = i->second;
428                 userrec* otheruser = (userrec*)o;
429                 if (otheruser->fd != FD_MAGIC_NUMBER)
430                         WriteTo_NoFormat(user,otheruser,textbuffer);
431         }
432 }
433
434 void WriteChannel_NoFormat(chanrec* Ptr, userrec* user, const char* text)
435 {
436         if ((!Ptr) || (!user) || (!text))
437         {
438                 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
439                 return;
440         }
441         std::map<char*,char*> *ulist= Ptr->GetUsers();
442         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
443         {
444                 char* o = i->second;
445                 userrec* otheruser = (userrec*)o;
446                 if (otheruser->fd != FD_MAGIC_NUMBER)
447                         WriteTo_NoFormat(user,otheruser,text);
448         }
449 }
450
451
452 /* write formatted text from a source user to all users on a channel
453  * including the sender (NOT for privmsg, notice etc!) doesnt send to
454  * users on remote servers */
455
456 void WriteChannelLocal(chanrec* Ptr, userrec* user, char* text, ...)
457 {
458         if ((!Ptr) || (!text))
459         {
460                 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
461                 return;
462         }
463         char textbuffer[MAXBUF];
464         va_list argsPtr;
465         va_start (argsPtr, text);
466         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
467         va_end(argsPtr);
468
469         std::map<char*,char*> *ulist= Ptr->GetUsers();
470         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
471         {
472                 char* o = i->second;
473                 userrec* otheruser = (userrec*)o;
474                 if ((otheruser->fd != FD_MAGIC_NUMBER) && (otheruser != user))
475                 {
476                         if (!user)
477                         {
478                                 WriteServ_NoFormat(otheruser->fd,textbuffer);
479                         }
480                         else
481                         {
482                                 WriteTo_NoFormat(user,otheruser,textbuffer);
483                         }
484                 }
485         }
486 }
487
488 void WriteChannelLocal_NoFormat(chanrec* Ptr, userrec* user, const char* text)
489 {
490         if ((!Ptr) || (!text))
491         {
492                 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
493                 return;
494         }
495         std::map<char*,char*> *ulist= Ptr->GetUsers();
496         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
497         {
498                 char* o = i->second;
499                 userrec* otheruser = (userrec*)o;
500                 if ((otheruser->fd != FD_MAGIC_NUMBER) && (otheruser != user))
501                 {
502                         if (!user)
503                         {
504                                 WriteServ_NoFormat(otheruser->fd,text);
505                         }
506                         else
507                         {
508                                 WriteTo_NoFormat(user,otheruser,text);
509                         }
510                 }
511         }
512 }
513
514
515
516 void WriteChannelWithServ(char* ServName, chanrec* Ptr, char* text, ...)
517 {
518         if ((!Ptr) || (!text))
519         {
520                 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
521                 return;
522         }
523         char textbuffer[MAXBUF];
524         va_list argsPtr;
525         va_start (argsPtr, text);
526         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
527         va_end(argsPtr);
528
529
530         std::map<char*,char*> *ulist= Ptr->GetUsers();
531         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
532         {
533                 char* o = i->second;
534                 userrec* otheruser = (userrec*)o;
535                 if (IS_LOCAL(otheruser))
536                         WriteServ_NoFormat(otheruser->fd,textbuffer);
537         }
538 }
539
540 void WriteChannelWithServ_NoFormat(char* ServName, chanrec* Ptr, const char* text)
541 {
542         if ((!Ptr) || (!text))
543         {
544                 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
545                 return;
546         }
547         std::map<char*,char*> *ulist= Ptr->GetUsers();
548         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
549         {
550                 char* o = i->second;
551                 userrec* otheruser = (userrec*)o;
552                 if (IS_LOCAL(otheruser))
553                         WriteServ_NoFormat(otheruser->fd,text);
554         }
555 }
556
557
558
559 /* write formatted text from a source user to all users on a channel except
560  * for the sender (for privmsg etc) */
561
562 void ChanExceptSender(chanrec* Ptr, userrec* user, char status, char* text, ...)
563 {
564         if ((!Ptr) || (!user) || (!text))
565         {
566                 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
567                 return;
568         }
569         char textbuffer[MAXBUF];
570         va_list argsPtr;
571         va_start (argsPtr, text);
572         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
573         va_end(argsPtr);
574
575         std::map<char*,char*> *ulist;
576         switch (status)
577         {
578                 case '@':
579                         ulist = Ptr->GetOppedUsers();
580                 break;
581                 case '%':
582                         ulist = Ptr->GetHalfoppedUsers();
583                 break;
584                 case '+':
585                         ulist = Ptr->GetVoicedUsers();
586                 break;
587                 default:
588                         ulist = Ptr->GetUsers();
589                 break;
590         }
591         log(DEBUG,"%d users to write to",ulist->size());
592         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
593         {
594                 char* o = i->second;
595                 userrec* otheruser = (userrec*)o;
596                 if ((IS_LOCAL(otheruser)) && (user != otheruser))
597                         WriteFrom_NoFormat(otheruser->fd,user,textbuffer);
598         }
599 }
600
601 void ChanExceptSender_NoFormat(chanrec* Ptr, userrec* user, char status, const char* text)
602 {
603         if ((!Ptr) || (!user) || (!text))
604         {
605                 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
606                 return;
607         }
608         std::map<char*,char*> *ulist;
609         switch (status)
610         {
611                 case '@':
612                         ulist = Ptr->GetOppedUsers();
613                 break;  
614                 case '%':
615                         ulist = Ptr->GetHalfoppedUsers();
616                 break;
617                 case '+':
618                         ulist = Ptr->GetVoicedUsers();
619                 break;
620                 default:
621                         ulist = Ptr->GetUsers();
622                 break;
623         }
624         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
625         {
626                 char* o = i->second;
627                 userrec* otheruser = (userrec*)o;
628                 if ((IS_LOCAL(otheruser)) && (user != otheruser))
629                         WriteFrom_NoFormat(otheruser->fd,user,text);
630         }
631 }
632
633 std::string GetServerDescription(char* servername)
634 {
635         std::string description = "";
636         FOREACH_MOD(I_OnGetServerDescription,OnGetServerDescription(servername,description));
637         if (description != "")
638         {
639                 return description;
640         }
641         else
642         {
643                 return Config->ServerDesc; // not a remote server that can be found, it must be me.
644         }
645 }
646
647 /* write a formatted string to all users who share at least one common
648  * channel, including the source user e.g. for use in NICK */
649
650 void WriteCommon(userrec *u, char* text, ...)
651 {
652         if (!u)
653         {
654                 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
655                 return;
656         }
657
658         if (u->registered != 7) {
659                 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
660                 return;
661         }
662
663         char textbuffer[MAXBUF];
664         va_list argsPtr;
665         va_start (argsPtr, text);
666         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
667         va_end(argsPtr);
668
669         // FIX: Stops a message going to the same person more than once
670         memset(&already_sent,0,MAX_DESCRIPTORS);
671
672         bool sent_to_at_least_one = false;
673
674         unsigned int y = u->chans.size();
675         for (unsigned int i = 0; i < y; i++)
676         {
677                 if (u->chans[i].channel)
678                 {
679                         std::map<char*,char*> *ulist= u->chans[i].channel->GetUsers();
680                         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
681                         {
682                                 char* o = i->second;
683                                 userrec* otheruser = (userrec*)o;
684                                 if ((otheruser->fd > -1) && (!already_sent[otheruser->fd]))
685                                 {
686                                         already_sent[otheruser->fd] = 1;
687                                         WriteFrom_NoFormat(otheruser->fd,u,textbuffer);
688                                         sent_to_at_least_one = true;
689                                 }
690                         }
691                 }
692         }
693         // if the user was not in any channels, no users will receive the text. Make sure the user
694         // receives their OWN message for WriteCommon
695         if (!sent_to_at_least_one)
696         {
697                 WriteFrom_NoFormat(u->fd,u,textbuffer);
698         }
699 }
700
701 void WriteCommon_NoFormat(userrec *u, const char* text)
702 {
703         if (!u)
704         {
705                 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
706                 return;
707         }
708                 
709         if (u->registered != 7) {
710                 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
711                 return;
712         }
713         // FIX: Stops a message going to the same person more than once
714         memset(&already_sent,0,MAX_DESCRIPTORS);
715                 
716         bool sent_to_at_least_one = false;
717                         
718         unsigned int y = u->chans.size();
719         for (unsigned int i = 0; i < y; i++)
720         {
721                 if (u->chans[i].channel)
722                 {
723                         std::map<char*,char*> *ulist= u->chans[i].channel->GetUsers();
724                         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
725                         {
726                                 char* o = i->second;
727                                 userrec* otheruser = (userrec*)o;
728                                 if ((otheruser->fd > -1) && (!already_sent[otheruser->fd]))
729                                 {
730                                         already_sent[otheruser->fd] = 1;
731                                         WriteFrom_NoFormat(otheruser->fd,u,text);
732                                         sent_to_at_least_one = true;
733                                 }
734                         }
735                 }
736         }
737         // if the user was not in any channels, no users will receive the text. Make sure the user
738         // receives their OWN message for WriteCommon
739         if (!sent_to_at_least_one)
740         {
741                 WriteFrom_NoFormat(u->fd,u,text);
742         }
743 }
744
745
746 /* write a formatted string to all users who share at least one common
747  * channel, NOT including the source user e.g. for use in QUIT */
748
749 void WriteCommonExcept(userrec *u, char* text, ...)
750 {
751         if (!u)
752         {
753                 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
754                 return;
755         }
756
757         if (u->registered != 7) {
758                 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
759                 return;
760         }
761
762         char textbuffer[MAXBUF];
763         char oper_quit[MAXBUF];
764         bool quit_munge = false;
765
766         va_list argsPtr;
767         va_start (argsPtr, text);
768         int total = vsnprintf(textbuffer, MAXBUF, text, argsPtr);
769         va_end(argsPtr);
770
771         if ((Config->HideSplits) && (total > 6))
772         {
773                 /* Yeah yeah, this is ugly. But its fast, live with it. */
774                 char* check = textbuffer;
775                 if ((*check++ == 'Q') && (*check++ == 'U') && (*check++ == 'I') && (*check++ == 'T') && (*check++ == ' ') && (*check++ == ':'))
776                 {
777                         std::stringstream split(check);
778                         std::string server_one;
779                         std::string server_two;
780                         split >> server_one;
781                         split >> server_two;
782                         if ((FindServerName(server_one)) && (FindServerName(server_two)))
783                         {
784                                 strlcpy(oper_quit,textbuffer,MAXQUIT);
785                                 strlcpy(check,"*.net *.split",MAXQUIT);
786                                 quit_munge = true;
787                         }
788                 }
789         }
790         if ((Config->HideBans) && (total > 13) && (!quit_munge))
791         {
792                 char* check = textbuffer;
793                 if ((*check++ == 'Q') && (*check++ == 'U') && (*check++ == 'I') && (*check++ == 'T') && (*check++ == ' ') && (*check++ == ':'))
794                 {
795                         check++;
796                         if ((*check++ == '-') && (*check++ == 'L') && (*check++ == 'i') && (*check++ == 'n') && (*check++ == 'e') && (*check++ == 'd') && (*check++ == ':'))
797                         {
798                                 strlcpy(oper_quit,textbuffer,MAXQUIT);
799                                 *(--check) = 0;         // We don't need to strlcpy, we just chop it from the :
800                                 quit_munge = true;
801                         }
802                 }
803         }
804
805         memset(&already_sent,0,MAX_DESCRIPTORS);
806
807         unsigned int y = u->chans.size();
808         for (unsigned int i = 0; i < y; i++)
809         {
810                 if (u->chans[i].channel)
811                 {
812                         std::map<char*,char*> *ulist= u->chans[i].channel->GetUsers();
813                         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
814                         {
815                                 char* o = i->second;
816                                 userrec* otheruser = (userrec*)o;
817                                 if (u != otheruser)
818                                 {
819                                         if ((otheruser->fd > -1) && (!already_sent[otheruser->fd]))
820                                         {
821                                                 already_sent[otheruser->fd] = 1;
822                                                 if (quit_munge)
823                                                 {
824                                                         WriteFrom_NoFormat(otheruser->fd,u,*otheruser->oper ? oper_quit : textbuffer);
825                                                 }
826                                                 else WriteFrom_NoFormat(otheruser->fd,u,textbuffer);
827                                         }
828                                 }
829                         }
830                 }
831         }
832 }
833
834 void WriteCommonExcept_NoFormat(userrec *u, const char* text)
835 {
836         if (!u)
837         {
838                 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
839                 return;
840         }
841          
842         if (u->registered != 7) {
843                 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
844                 return;
845         }
846
847         memset(&already_sent,0,MAX_DESCRIPTORS);
848
849         unsigned int y = u->chans.size();
850         for (unsigned int i = 0; i < y; i++)
851         {
852                 if (u->chans[i].channel)
853                 {
854                         std::map<char*,char*> *ulist= u->chans[i].channel->GetUsers();
855                         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
856                         {
857                                 char* o = i->second;
858                                 userrec* otheruser = (userrec*)o;
859                                 if (u != otheruser)
860                                 {
861                                         if ((otheruser->fd > -1) && (!already_sent[otheruser->fd]))
862                                         {
863                                                 already_sent[otheruser->fd] = 1;
864                                                 WriteFrom_NoFormat(otheruser->fd,u,text);
865                                         }
866                                 }
867                         }
868                 }
869         }
870 }
871
872
873
874 void WriteOpers(char* text, ...)
875 {
876         if (!text)
877         {
878                 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
879                 return;
880         }
881
882         char textbuffer[MAXBUF];
883         va_list argsPtr;
884         va_start (argsPtr, text);
885         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
886         va_end(argsPtr);
887
888         for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
889         {
890                 userrec* a = *i;
891                 if (IS_LOCAL(a))
892                 {
893                         if (strchr(a->modes,'s'))
894                         {
895                                 // send server notices to all with +s
896                                 WriteServ(a->fd,"NOTICE %s :%s",a->nick,textbuffer);
897                         }
898                 }
899         }
900 }
901
902 void ServerNoticeAll(char* text, ...)
903 {
904         if (!text)
905                 return;
906
907         char textbuffer[MAXBUF];
908         va_list argsPtr;
909         va_start (argsPtr, text);
910         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
911         va_end(argsPtr);
912
913         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
914         {
915                 userrec* t = (userrec*)(*i);
916                 WriteServ(t->fd,"NOTICE $%s :%s",Config->ServerName,textbuffer);
917         }
918 }
919
920 void ServerPrivmsgAll(char* text, ...)
921 {
922         if (!text)
923                 return;
924
925         char textbuffer[MAXBUF];
926         va_list argsPtr;
927         va_start (argsPtr, text);
928         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
929         va_end(argsPtr);
930
931         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
932         {
933                 userrec* t = (userrec*)(*i);
934                 WriteServ(t->fd,"PRIVMSG $%s :%s",Config->ServerName,textbuffer);
935         }
936 }
937
938 void WriteMode(const char* modes, int flags, const char* text, ...)
939 {
940         if ((!text) || (!modes) || (!flags))
941         {
942                 log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
943                 return;
944         }
945
946         char textbuffer[MAXBUF];
947         va_list argsPtr;
948         va_start (argsPtr, text);
949         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
950         va_end(argsPtr);
951         int modelen = strlen(modes);
952
953         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
954         {
955                 userrec* t = (userrec*)(*i);
956                 bool send_to_user = false;
957                 if (flags == WM_AND)
958                 {
959                         send_to_user = true;
960                         for (int n = 0; n < modelen; n++)
961                         {
962                                 if (!hasumode(t,modes[n]))
963                                 {
964                                         send_to_user = false;
965                                         break;
966                                 }
967                         }
968                 }
969                 else if (flags == WM_OR)
970                 {
971                         send_to_user = false;
972                         for (int n = 0; n < modelen; n++)
973                         {
974                                 if (hasumode(t,modes[n]))
975                                 {
976                                         send_to_user = true;
977                                         break;
978                                 }
979                         }
980                 }
981                 if (send_to_user)
982                 {
983                         WriteServ(t->fd,"NOTICE %s :%s",t->nick,textbuffer);
984                 }
985         }
986 }
987
988 void NoticeAll(userrec *source, bool local_only, char* text, ...)
989 {
990         if ((!text) || (!source))
991         {
992                 log(DEFAULT,"*** BUG *** NoticeAll was given an invalid parameter");
993                 return;
994         }
995
996         char textbuffer[MAXBUF];
997         va_list argsPtr;
998         va_start (argsPtr, text);
999         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1000         va_end(argsPtr);
1001
1002         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1003         {
1004                 userrec* t = (userrec*)(*i);
1005                 WriteFrom(t->fd,source,"NOTICE $* :%s",textbuffer);
1006         }
1007
1008 }
1009
1010
1011 void WriteWallOps(userrec *source, bool local_only, char* text, ...)
1012 {
1013         if ((!text) || (!source))
1014         {
1015                 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
1016                 return;
1017         }
1018
1019         char textbuffer[MAXBUF];
1020         va_list argsPtr;
1021         va_start (argsPtr, text);
1022         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1023         va_end(argsPtr);
1024
1025         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1026         {
1027                 userrec* t = (userrec*)(*i);
1028                 if ((IS_LOCAL(t)) && (strchr(t->modes,'w')))
1029                 {
1030                         WriteTo(source,t,"WALLOPS :%s",textbuffer);
1031                 }
1032         }
1033 }
1034
1035 /* convert a string to lowercase. Note following special circumstances
1036  * taken from RFC 1459. Many "official" server branches still hold to this
1037  * rule so i will too;
1038  *
1039  *  Because of IRC's scandanavian origin, the characters {}| are
1040  *  considered to be the lower case equivalents of the characters []\,
1041  *  respectively. This is a critical issue when determining the
1042  *  equivalence of two nicknames.
1043  */
1044
1045 void strlower(char *n)
1046 {
1047         if (n)
1048         {
1049                 for (char* t = n; *t; t++)
1050                         *t = lowermap[(unsigned)*t];
1051         }
1052 }
1053
1054 /* Find a user record by nickname and return a pointer to it */
1055
1056 userrec* Find(std::string nick)
1057 {
1058         user_hash::iterator iter = clientlist.find(nick);
1059
1060         if (iter == clientlist.end())
1061                 /* Couldn't find it */
1062                 return NULL;
1063
1064         return iter->second;
1065 }
1066
1067 userrec* Find(const char* nick)
1068 {
1069         if (!nick)
1070                 return NULL;
1071         user_hash::iterator iter = clientlist.find(nick);
1072         
1073         if (iter == clientlist.end())
1074                 return NULL;
1075
1076         return iter->second;
1077 }
1078
1079 /* find a channel record by channel name and return a pointer to it */
1080
1081 chanrec* FindChan(const char* chan)
1082 {
1083         if (!chan)
1084         {
1085                 log(DEFAULT,"*** BUG *** Findchan was given an invalid parameter");
1086                 return NULL;
1087         }
1088
1089         chan_hash::iterator iter = chanlist.find(chan);
1090
1091         if (iter == chanlist.end())
1092                 /* Couldn't find it */
1093                 return NULL;
1094
1095         return iter->second;
1096 }
1097
1098
1099 long GetMaxBans(char* name)
1100 {
1101         char CM[MAXBUF];
1102         for (int count = 0; count < Config->ConfValueEnum("banlist",&Config->config_f); count++)
1103         {
1104                 Config->ConfValue("banlist","chan",count,CM,&Config->config_f);
1105                 if (match(name,CM))
1106                 {
1107                         Config->ConfValue("banlist","limit",count,CM,&Config->config_f);
1108                         return atoi(CM);
1109                 }
1110         }
1111         return 64;
1112 }
1113
1114 void purge_empty_chans(userrec* u)
1115 {
1116
1117         int purge = 0;
1118
1119         // firstly decrement the count on each channel
1120         for (unsigned int f = 0; f < u->chans.size(); f++)
1121         {
1122                 if (u->chans[f].channel)
1123                 {
1124                         u->chans[f].channel->DelUser((char*)u);
1125                 }
1126         }
1127
1128         for (unsigned int i = 0; i < u->chans.size(); i++)
1129         {
1130                 if (u->chans[i].channel)
1131                 {
1132                         if (!usercount(u->chans[i].channel))
1133                         {
1134                                 chan_hash::iterator i2 = chanlist.find(u->chans[i].channel->name);
1135                                 /* kill the record */
1136                                 if (i2 != chanlist.end())
1137                                 {
1138                                         log(DEBUG,"del_channel: destroyed: %s",i2->second->name);
1139                                         if (i2->second)
1140                                         {
1141                                                 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(i2->second));
1142                                                 delete i2->second;
1143                                         }
1144                                         chanlist.erase(i2);
1145                                         purge++;
1146                                         u->chans[i].channel = NULL;
1147                                 }
1148                         }
1149                         else
1150                         {
1151                                 log(DEBUG,"skipped purge for %s",u->chans[i].channel->name);
1152                         }
1153                 }
1154         }
1155         log(DEBUG,"completed channel purge, killed %lu",(unsigned long)purge);
1156
1157         DeleteOper(u);
1158 }
1159
1160
1161 char* chanmodes(chanrec *chan, bool showkey)
1162 {
1163         static char scratch[MAXBUF];
1164         static char sparam[MAXBUF];
1165         char* offset = scratch;
1166
1167         if (!chan)
1168         {
1169                 log(DEFAULT,"*** BUG *** chanmodes was given an invalid parameter");
1170                 *scratch = '\0';
1171                 return scratch;
1172         }
1173
1174         *scratch = '\0';
1175         *sparam = '\0';
1176         if (chan->binarymodes & CM_NOEXTERNAL)
1177                 *offset++ = 'n';
1178         if (chan->binarymodes & CM_TOPICLOCK)
1179                 *offset++ = 't';
1180         if (*chan->key)
1181                 *offset++ = 'k';
1182         if (chan->limit)
1183                 *offset++ = 'l';
1184         if (chan->binarymodes & CM_INVITEONLY)
1185                 *offset++ = 'i';
1186         if (chan->binarymodes & CM_MODERATED)
1187                 *offset++ = 'm';
1188         if (chan->binarymodes & CM_SECRET)
1189                 *offset++ = 's';
1190         if (chan->binarymodes & CM_PRIVATE)
1191                 *offset++ = 'p';
1192         if (*chan->key)
1193         {
1194                 snprintf(sparam,MAXBUF," %s",showkey ? chan->key : "<key>");
1195         }
1196         if (chan->limit)
1197         {
1198                 char foo[24];
1199                 sprintf(foo," %lu",(unsigned long)chan->limit);
1200                 strlcat(sparam,foo,MAXBUF);
1201         }
1202         if (*chan->custom_modes)
1203         {
1204                 for (char* t = chan->custom_modes; *t; t++)
1205                         *offset++ = *t;
1206                 for (int z = 0; chan->custom_modes[z]; z++)
1207                 {
1208                         std::string extparam = chan->GetModeParameter(chan->custom_modes[z]);
1209                         if (extparam != "")
1210                         {
1211                                 charlcat(sparam,' ',MAXBUF);
1212                                 strlcat(sparam,extparam.c_str(),MAXBUF);
1213                         }
1214                 }
1215         }
1216         /* Null terminate scratch */
1217         *offset = '\0';
1218         strlcat(scratch,sparam,MAXMODES);
1219         return scratch;
1220 }
1221
1222
1223 /* compile a userlist of a channel into a string, each nick seperated by
1224  * spaces and op, voice etc status shown as @ and + */
1225
1226 void userlist(userrec *user,chanrec *c)
1227 {
1228         if ((!c) || (!user))
1229         {
1230                 log(DEFAULT,"*** BUG *** userlist was given an invalid parameter");
1231                 return;
1232         }
1233
1234         snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1235
1236         std::map<char*,char*> *ulist= c->GetUsers();
1237         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
1238         {
1239                 char* o = i->second;
1240                 userrec* otheruser = (userrec*)o;
1241                 if ((!has_channel(user,c)) && (strchr(otheruser->modes,'i')))
1242                 {
1243                         /* user is +i, and source not on the channel, does not show
1244                          * nick in NAMES list */
1245                         continue;
1246                 }
1247                 strlcat(list,cmode(otheruser,c),MAXBUF);
1248                 strlcat(list,otheruser->nick,MAXBUF);
1249                 charlcat(list,' ',MAXBUF);
1250                 if (strlen(list)>(480-NICKMAX))
1251                 {
1252                         /* list overflowed into
1253                          * multiple numerics */
1254                         WriteServ_NoFormat(user->fd,list);
1255                         snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1256                 }
1257         }
1258         /* if whats left in the list isnt empty, send it */
1259         if (list[strlen(list)-1] != ':')
1260         {
1261                 WriteServ_NoFormat(user->fd,list);
1262         }
1263 }
1264
1265 /* return a count of the users on a specific channel accounting for
1266  * invisible users who won't increase the count. e.g. for /LIST */
1267
1268 int usercount_i(chanrec *c)
1269 {
1270         int count = 0;
1271
1272         if (!c)
1273         {
1274                 log(DEFAULT,"*** BUG *** usercount_i was given an invalid parameter");
1275                 return 0;
1276         }
1277
1278         for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1279         {
1280                         if (has_channel(i->second,c))
1281                         {
1282                                 if (i->second->registered == 7)
1283                                 {
1284                                         if ((!has_channel(i->second,c)) && (strchr(i->second->modes,'i')))
1285                                         {
1286                                                 /* user is +i, and source not on the channel, does not show
1287                                                  * nick in NAMES list */
1288                                                 continue;
1289                                         }
1290                                         count++;
1291                                 }
1292                         }
1293         }
1294         log(DEBUG,"usercount_i: %s %lu",c->name,(unsigned long)count);
1295         return count;
1296 }
1297
1298
1299 int usercount(chanrec *c)
1300 {
1301         return (c ? c->GetUserCounter() : 0);
1302 }
1303
1304
1305 // looks up a users password for their connection class (<ALLOW>/<DENY> tags)
1306
1307 ConnectClass GetClass(userrec *user)
1308 {
1309         for (ClassVector::iterator i = Config->Classes.begin(); i != Config->Classes.end(); i++)
1310         {
1311                 if (match(user->host,i->host.c_str()))
1312                 {
1313                         return *i;
1314                 }
1315         }
1316         return *(Config->Classes.begin());
1317 }
1318
1319 /* sends out an error notice to all connected clients (not to be used
1320  * lightly!) */
1321
1322 void send_error(char *s)
1323 {
1324         log(DEBUG,"send_error: %s",s);
1325         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1326         {
1327                 userrec* t = (userrec*)(*i);
1328                 if (t->registered == 7)
1329                 {
1330                         WriteServ(t->fd,"NOTICE %s :%s",t->nick,s);
1331                 }
1332                 else
1333                 {
1334                         // fix - unregistered connections receive ERROR, not NOTICE
1335                         Write(t->fd,"ERROR :%s",s);
1336                 }
1337         }
1338 }
1339
1340 void Error(int status)
1341 {
1342         signal (SIGALRM, SIG_IGN);
1343         signal (SIGPIPE, SIG_IGN);
1344         signal (SIGTERM, SIG_IGN);
1345         signal (SIGABRT, SIG_IGN);
1346         signal (SIGSEGV, SIG_IGN);
1347         signal (SIGURG, SIG_IGN);
1348         signal (SIGKILL, SIG_IGN);
1349         log(DEFAULT,"*** fell down a pothole in the road to perfection ***");
1350         send_error("Error! Segmentation fault! save meeeeeeeeeeeeee *splat!*");
1351         Exit(status);
1352 }
1353
1354 // this function counts all users connected, wether they are registered or NOT.
1355 int usercnt(void)
1356 {
1357         return clientlist.size();
1358 }
1359
1360 // this counts only registered users, so that the percentages in /MAP don't mess up when users are sitting in an unregistered state
1361 int registered_usercount(void)
1362 {
1363         int c = 0;
1364         for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1365         {
1366                 if (i->second->registered == 7) c++;
1367         }
1368         return c;
1369 }
1370
1371 int usercount_invisible(void)
1372 {
1373         int c = 0;
1374         for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1375         {
1376                 if ((i->second->registered == 7) && (strchr(i->second->modes,'i'))) c++;
1377         }
1378         return c;
1379 }
1380
1381 int usercount_opers(void)
1382 {
1383         int c = 0;
1384         for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1385                 if (*i->second->oper) c++;
1386         return c;
1387 }
1388
1389 int usercount_unknown(void)
1390 {
1391         int c = 0;
1392         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1393         {
1394                 userrec* t = (userrec*)(*i);
1395                 if (t->registered != 7)
1396                         c++;
1397         }
1398         return c;
1399 }
1400
1401 long chancount(void)
1402 {
1403         return chanlist.size();
1404 }
1405
1406 long local_count()
1407 {
1408         int c = 0;
1409         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1410         {
1411                 userrec* t = (userrec*)(*i);
1412                 if (t->registered == 7) c++;
1413         }
1414         return c;
1415 }
1416
1417 void ShowMOTD(userrec *user)
1418 {
1419         static char mbuf[MAXBUF];
1420         static char crud[MAXBUF];
1421         std::string WholeMOTD = "";
1422         if (!Config->MOTD.size())
1423         {
1424                 WriteServ(user->fd,"422 %s :Message of the day file is missing.",user->nick);
1425                 return;
1426         }
1427         snprintf(crud,MAXBUF,":%s 372 %s :- ", Config->ServerName, user->nick);
1428         snprintf(mbuf,MAXBUF,":%s 375 %s :- %s message of the day\r\n", Config->ServerName, user->nick, Config->ServerName);
1429         WholeMOTD = WholeMOTD + mbuf;
1430         for (unsigned int i = 0; i < Config->MOTD.size(); i++)
1431                 WholeMOTD = WholeMOTD + std::string(crud) + Config->MOTD[i].c_str() + std::string("\r\n");
1432         snprintf(mbuf,MAXBUF,":%s 376 %s :End of message of the day.\r\n", Config->ServerName, user->nick);
1433         WholeMOTD = WholeMOTD + mbuf;
1434         // only one write operation
1435         if (Config->GetIOHook(user->port))
1436         {
1437                 try
1438                 {
1439                         Config->GetIOHook(user->port)->OnRawSocketWrite(user->fd,(char*)WholeMOTD.c_str(),WholeMOTD.length());
1440                 }
1441                 catch (ModuleException& modexcept)
1442                 {
1443                         log(DEBUG,"Module exception cought: %s",modexcept.GetReason());
1444                 }
1445
1446         }
1447         else
1448         {
1449                 user->AddWriteBuf(WholeMOTD);
1450         }
1451         ServerInstance->stats->statsSent += WholeMOTD.length();
1452 }
1453
1454 void ShowRULES(userrec *user)
1455 {
1456         if (!Config->RULES.size())
1457         {
1458                 WriteServ(user->fd,"NOTICE %s :Rules file is missing.",user->nick);
1459                 return;
1460         }
1461         WriteServ(user->fd,"NOTICE %s :%s rules",user->nick,Config->ServerName);
1462         for (unsigned int i = 0; i < Config->RULES.size(); i++)
1463                 WriteServ(user->fd,"NOTICE %s :%s",user->nick,Config->RULES[i].c_str());
1464         WriteServ(user->fd,"NOTICE %s :End of %s rules.",user->nick,Config->ServerName);
1465 }
1466
1467 // this returns 1 when all modules are satisfied that the user should be allowed onto the irc server
1468 // (until this returns true, a user will block in the waiting state, waiting to connect up to the
1469 // registration timeout maximum seconds)
1470 bool AllModulesReportReady(userrec* user)
1471 {
1472         if (!Config->global_implementation[I_OnCheckReady])
1473                 return true;
1474         for (int i = 0; i <= MODCOUNT; i++)
1475         {
1476                 if (Config->implement_lists[i][I_OnCheckReady])
1477                 {
1478                         int res = modules[i]->OnCheckReady(user);
1479                         if (!res)
1480                                 return false;
1481                 }
1482         }
1483         return true;
1484 }
1485
1486 bool DirValid(char* dirandfile)
1487 {
1488         char work[MAXBUF];
1489         char buffer[MAXBUF], otherdir[MAXBUF];
1490         strlcpy(work,dirandfile,MAXBUF);
1491         int p = strlen(work);
1492         // we just want the dir
1493         while (*work)
1494         {
1495                 if (work[p] == '/')
1496                 {
1497                         work[p] = '\0';
1498                         break;
1499                 }
1500                 work[p--] = '\0';
1501         }
1502         // Get the current working directory
1503         if( getcwd( buffer, MAXBUF ) == NULL )
1504                 return false;
1505         chdir(work);
1506         if( getcwd( otherdir, MAXBUF ) == NULL )
1507                 return false;
1508         chdir(buffer);
1509         if (strlen(otherdir) >= strlen(work))
1510         {
1511                 otherdir[strlen(work)] = '\0';
1512                 if (!strcmp(otherdir,work))
1513                 {
1514                         return true;
1515                 }
1516                 return false;
1517         }
1518         else return false;
1519 }
1520
1521 std::string GetFullProgDir(char** argv, int argc)
1522 {
1523         char work[MAXBUF];
1524         char buffer[MAXBUF], otherdir[MAXBUF];
1525         strlcpy(work,argv[0],MAXBUF);
1526         int p = strlen(work);
1527         // we just want the dir
1528         while (*work)
1529         {
1530                 if (work[p] == '/')
1531                 {
1532                         work[p] = '\0';
1533                         break;
1534                 }
1535                 work[p--] = '\0';
1536         }
1537         // Get the current working directory
1538         if( getcwd( buffer, MAXBUF ) == NULL )
1539                 return "";
1540         chdir(work);
1541         if( getcwd( otherdir, MAXBUF ) == NULL )
1542                 return "";
1543         chdir(buffer);
1544         return otherdir;
1545 }
1546
1547 int InsertMode(std::string &output, const char* mode, unsigned short section)
1548 {
1549         unsigned short currsection = 1;
1550         unsigned int pos = output.find("CHANMODES=", 0) + 10; // +10 for the length of "CHANMODES="
1551         
1552         if(section > 4 || section == 0)
1553         {
1554                 log(DEBUG, "InsertMode: CHANMODES doesn't have a section %dh :/", section);
1555                 return 0;
1556         }
1557         
1558         for(; pos < output.size(); pos++)
1559         {
1560                 if(section == currsection)
1561                         break;
1562                         
1563                 if(output[pos] == ',')
1564                         currsection++;
1565         }
1566         
1567         output.insert(pos, mode);
1568         return 1;
1569 }
1570
1571 bool IsValidChannelName(const char *chname)
1572 {
1573                 char *c;
1574
1575                 /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
1576                 if (!chname || *chname != '#')
1577                 {
1578                                 return false;
1579                 }
1580
1581                 c = (char *)chname + 1;
1582                 while (*c)
1583                 {
1584                                 switch (*c)
1585                                 {
1586                                                 case ' ':
1587                                                 case ',':
1588                                                 case 7:
1589                                                                 return false;
1590                                 }
1591
1592                                 c++;
1593                 }
1594                 
1595                 /* too long a name - note funky pointer arithmetic here. */
1596                 if ((c - chname) > CHANMAX)
1597                 {
1598                                 return false;
1599                 }
1600
1601                 return true;
1602 }
1603
1604 inline int charlcat(char* x,char y,int z)
1605 {
1606         char* x__n = x;
1607         int v = 0;
1608         while(*x__n++)
1609                 v++;
1610         if (v < z - 1)
1611         {
1612                 *--x__n = y;
1613                 *++x__n = 0;
1614         }
1615         return v;
1616 }
1617
1618 bool charremove(char* mp, char remove)
1619 {
1620         char* mptr = mp;
1621         bool shift_down = false;
1622         while (*mptr)
1623         {
1624                 if (*mptr == remove)
1625                         shift_down = true;
1626                 if (shift_down)
1627                         *mptr = *(mptr+1);
1628                 *mptr++;
1629         }
1630         return shift_down;
1631 }
1632