]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/helperfuncs.cpp
Initial revision of /check - doesn't do anything yet
[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         std::string x;
1102         for (std::map<std::string,int>::iterator n = Config->maxbans.begin(); n != Config->maxbans.end(); n++)
1103         {
1104                 x = n->first;
1105                 if (match(name,x.c_str()))
1106                 {
1107                         return n->second;
1108                 }
1109         }
1110         return 64;
1111 }
1112
1113 void purge_empty_chans(userrec* u)
1114 {
1115
1116         int purge = 0;
1117
1118         // firstly decrement the count on each channel
1119         for (unsigned int f = 0; f < u->chans.size(); f++)
1120         {
1121                 if (u->chans[f].channel)
1122                 {
1123                         u->chans[f].channel->DelUser((char*)u);
1124                 }
1125         }
1126
1127         for (unsigned int i = 0; i < u->chans.size(); i++)
1128         {
1129                 if (u->chans[i].channel)
1130                 {
1131                         if (!usercount(u->chans[i].channel))
1132                         {
1133                                 chan_hash::iterator i2 = chanlist.find(u->chans[i].channel->name);
1134                                 /* kill the record */
1135                                 if (i2 != chanlist.end())
1136                                 {
1137                                         log(DEBUG,"del_channel: destroyed: %s",i2->second->name);
1138                                         if (i2->second)
1139                                         {
1140                                                 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(i2->second));
1141                                                 delete i2->second;
1142                                         }
1143                                         chanlist.erase(i2);
1144                                         purge++;
1145                                         u->chans[i].channel = NULL;
1146                                 }
1147                         }
1148                         else
1149                         {
1150                                 log(DEBUG,"skipped purge for %s",u->chans[i].channel->name);
1151                         }
1152                 }
1153         }
1154         log(DEBUG,"completed channel purge, killed %lu",(unsigned long)purge);
1155
1156         DeleteOper(u);
1157 }
1158
1159
1160 char* chanmodes(chanrec *chan, bool showkey)
1161 {
1162         static char scratch[MAXBUF];
1163         static char sparam[MAXBUF];
1164         char* offset = scratch;
1165
1166         if (!chan)
1167         {
1168                 log(DEFAULT,"*** BUG *** chanmodes was given an invalid parameter");
1169                 *scratch = '\0';
1170                 return scratch;
1171         }
1172
1173         *scratch = '\0';
1174         *sparam = '\0';
1175         if (chan->binarymodes & CM_NOEXTERNAL)
1176                 *offset++ = 'n';
1177         if (chan->binarymodes & CM_TOPICLOCK)
1178                 *offset++ = 't';
1179         if (*chan->key)
1180                 *offset++ = 'k';
1181         if (chan->limit)
1182                 *offset++ = 'l';
1183         if (chan->binarymodes & CM_INVITEONLY)
1184                 *offset++ = 'i';
1185         if (chan->binarymodes & CM_MODERATED)
1186                 *offset++ = 'm';
1187         if (chan->binarymodes & CM_SECRET)
1188                 *offset++ = 's';
1189         if (chan->binarymodes & CM_PRIVATE)
1190                 *offset++ = 'p';
1191         if (*chan->key)
1192         {
1193                 snprintf(sparam,MAXBUF," %s",showkey ? chan->key : "<key>");
1194         }
1195         if (chan->limit)
1196         {
1197                 char foo[24];
1198                 sprintf(foo," %lu",(unsigned long)chan->limit);
1199                 strlcat(sparam,foo,MAXBUF);
1200         }
1201         if (*chan->custom_modes)
1202         {
1203                 for (char* t = chan->custom_modes; *t; t++)
1204                         *offset++ = *t;
1205                 for (int z = 0; chan->custom_modes[z]; z++)
1206                 {
1207                         std::string extparam = chan->GetModeParameter(chan->custom_modes[z]);
1208                         if (extparam != "")
1209                         {
1210                                 charlcat(sparam,' ',MAXBUF);
1211                                 strlcat(sparam,extparam.c_str(),MAXBUF);
1212                         }
1213                 }
1214         }
1215         /* Null terminate scratch */
1216         *offset = '\0';
1217         strlcat(scratch,sparam,MAXMODES);
1218         return scratch;
1219 }
1220
1221
1222 /* compile a userlist of a channel into a string, each nick seperated by
1223  * spaces and op, voice etc status shown as @ and + */
1224
1225 void userlist(userrec *user,chanrec *c)
1226 {
1227         if ((!c) || (!user))
1228         {
1229                 log(DEFAULT,"*** BUG *** userlist was given an invalid parameter");
1230                 return;
1231         }
1232
1233         snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1234
1235         std::map<char*,char*> *ulist= c->GetUsers();
1236         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
1237         {
1238                 char* o = i->second;
1239                 userrec* otheruser = (userrec*)o;
1240                 if ((!has_channel(user,c)) && (strchr(otheruser->modes,'i')))
1241                 {
1242                         /* user is +i, and source not on the channel, does not show
1243                          * nick in NAMES list */
1244                         continue;
1245                 }
1246                 strlcat(list,cmode(otheruser,c),MAXBUF);
1247                 strlcat(list,otheruser->nick,MAXBUF);
1248                 charlcat(list,' ',MAXBUF);
1249                 if (strlen(list)>(480-NICKMAX))
1250                 {
1251                         /* list overflowed into
1252                          * multiple numerics */
1253                         WriteServ_NoFormat(user->fd,list);
1254                         snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1255                 }
1256         }
1257         /* if whats left in the list isnt empty, send it */
1258         if (list[strlen(list)-1] != ':')
1259         {
1260                 WriteServ_NoFormat(user->fd,list);
1261         }
1262 }
1263
1264 /* return a count of the users on a specific channel accounting for
1265  * invisible users who won't increase the count. e.g. for /LIST */
1266
1267 int usercount_i(chanrec *c)
1268 {
1269         int count = 0;
1270
1271         if (!c)
1272         {
1273                 log(DEFAULT,"*** BUG *** usercount_i was given an invalid parameter");
1274                 return 0;
1275         }
1276
1277         for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1278         {
1279                         if (has_channel(i->second,c))
1280                         {
1281                                 if (i->second->registered == 7)
1282                                 {
1283                                         if ((!has_channel(i->second,c)) && (strchr(i->second->modes,'i')))
1284                                         {
1285                                                 /* user is +i, and source not on the channel, does not show
1286                                                  * nick in NAMES list */
1287                                                 continue;
1288                                         }
1289                                         count++;
1290                                 }
1291                         }
1292         }
1293         log(DEBUG,"usercount_i: %s %lu",c->name,(unsigned long)count);
1294         return count;
1295 }
1296
1297
1298 int usercount(chanrec *c)
1299 {
1300         return (c ? c->GetUserCounter() : 0);
1301 }
1302
1303
1304 // looks up a users password for their connection class (<ALLOW>/<DENY> tags)
1305
1306 ConnectClass GetClass(userrec *user)
1307 {
1308         for (ClassVector::iterator i = Config->Classes.begin(); i != Config->Classes.end(); i++)
1309         {
1310                 if (match(user->host,i->host.c_str()))
1311                 {
1312                         return *i;
1313                 }
1314         }
1315         return *(Config->Classes.begin());
1316 }
1317
1318 /* sends out an error notice to all connected clients (not to be used
1319  * lightly!) */
1320
1321 void send_error(char *s)
1322 {
1323         log(DEBUG,"send_error: %s",s);
1324         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1325         {
1326                 userrec* t = (userrec*)(*i);
1327                 if (t->registered == 7)
1328                 {
1329                         WriteServ(t->fd,"NOTICE %s :%s",t->nick,s);
1330                 }
1331                 else
1332                 {
1333                         // fix - unregistered connections receive ERROR, not NOTICE
1334                         Write(t->fd,"ERROR :%s",s);
1335                 }
1336         }
1337 }
1338
1339 void Error(int status)
1340 {
1341         signal (SIGALRM, SIG_IGN);
1342         signal (SIGPIPE, SIG_IGN);
1343         signal (SIGTERM, SIG_IGN);
1344         signal (SIGABRT, SIG_IGN);
1345         signal (SIGSEGV, SIG_IGN);
1346         signal (SIGURG, SIG_IGN);
1347         signal (SIGKILL, SIG_IGN);
1348         log(DEFAULT,"*** fell down a pothole in the road to perfection ***");
1349         send_error("Error! Segmentation fault! save meeeeeeeeeeeeee *splat!*");
1350         Exit(status);
1351 }
1352
1353 // this function counts all users connected, wether they are registered or NOT.
1354 int usercnt(void)
1355 {
1356         return clientlist.size();
1357 }
1358
1359 // this counts only registered users, so that the percentages in /MAP don't mess up when users are sitting in an unregistered state
1360 int registered_usercount(void)
1361 {
1362         int c = 0;
1363         for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1364         {
1365                 if (i->second->registered == 7) c++;
1366         }
1367         return c;
1368 }
1369
1370 int usercount_invisible(void)
1371 {
1372         int c = 0;
1373         for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1374         {
1375                 if ((i->second->registered == 7) && (strchr(i->second->modes,'i'))) c++;
1376         }
1377         return c;
1378 }
1379
1380 int usercount_opers(void)
1381 {
1382         int c = 0;
1383         for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1384                 if (*i->second->oper) c++;
1385         return c;
1386 }
1387
1388 int usercount_unknown(void)
1389 {
1390         int c = 0;
1391         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1392         {
1393                 userrec* t = (userrec*)(*i);
1394                 if (t->registered != 7)
1395                         c++;
1396         }
1397         return c;
1398 }
1399
1400 long chancount(void)
1401 {
1402         return chanlist.size();
1403 }
1404
1405 long local_count()
1406 {
1407         int c = 0;
1408         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1409         {
1410                 userrec* t = (userrec*)(*i);
1411                 if (t->registered == 7) c++;
1412         }
1413         return c;
1414 }
1415
1416 void ShowMOTD(userrec *user)
1417 {
1418         static char mbuf[MAXBUF];
1419         static char crud[MAXBUF];
1420         std::string WholeMOTD = "";
1421         if (!Config->MOTD.size())
1422         {
1423                 WriteServ(user->fd,"422 %s :Message of the day file is missing.",user->nick);
1424                 return;
1425         }
1426         snprintf(crud,MAXBUF,":%s 372 %s :- ", Config->ServerName, user->nick);
1427         snprintf(mbuf,MAXBUF,":%s 375 %s :- %s message of the day\r\n", Config->ServerName, user->nick, Config->ServerName);
1428         WholeMOTD = WholeMOTD + mbuf;
1429         for (unsigned int i = 0; i < Config->MOTD.size(); i++)
1430                 WholeMOTD = WholeMOTD + std::string(crud) + Config->MOTD[i].c_str() + std::string("\r\n");
1431         snprintf(mbuf,MAXBUF,":%s 376 %s :End of message of the day.\r\n", Config->ServerName, user->nick);
1432         WholeMOTD = WholeMOTD + mbuf;
1433         // only one write operation
1434         if (Config->GetIOHook(user->port))
1435         {
1436                 try
1437                 {
1438                         Config->GetIOHook(user->port)->OnRawSocketWrite(user->fd,(char*)WholeMOTD.c_str(),WholeMOTD.length());
1439                 }
1440                 catch (ModuleException& modexcept)
1441                 {
1442                         log(DEBUG,"Module exception cought: %s",modexcept.GetReason());
1443                 }
1444
1445         }
1446         else
1447         {
1448                 user->AddWriteBuf(WholeMOTD);
1449         }
1450         ServerInstance->stats->statsSent += WholeMOTD.length();
1451 }
1452
1453 void ShowRULES(userrec *user)
1454 {
1455         if (!Config->RULES.size())
1456         {
1457                 WriteServ(user->fd,"NOTICE %s :Rules file is missing.",user->nick);
1458                 return;
1459         }
1460         WriteServ(user->fd,"NOTICE %s :%s rules",user->nick,Config->ServerName);
1461         for (unsigned int i = 0; i < Config->RULES.size(); i++)
1462                 WriteServ(user->fd,"NOTICE %s :%s",user->nick,Config->RULES[i].c_str());
1463         WriteServ(user->fd,"NOTICE %s :End of %s rules.",user->nick,Config->ServerName);
1464 }
1465
1466 // this returns 1 when all modules are satisfied that the user should be allowed onto the irc server
1467 // (until this returns true, a user will block in the waiting state, waiting to connect up to the
1468 // registration timeout maximum seconds)
1469 bool AllModulesReportReady(userrec* user)
1470 {
1471         if (!Config->global_implementation[I_OnCheckReady])
1472                 return true;
1473         for (int i = 0; i <= MODCOUNT; i++)
1474         {
1475                 if (Config->implement_lists[i][I_OnCheckReady])
1476                 {
1477                         int res = modules[i]->OnCheckReady(user);
1478                         if (!res)
1479                                 return false;
1480                 }
1481         }
1482         return true;
1483 }
1484
1485 bool DirValid(char* dirandfile)
1486 {
1487         char work[MAXBUF];
1488         char buffer[MAXBUF], otherdir[MAXBUF];
1489         strlcpy(work,dirandfile,MAXBUF);
1490         int p = strlen(work);
1491         // we just want the dir
1492         while (*work)
1493         {
1494                 if (work[p] == '/')
1495                 {
1496                         work[p] = '\0';
1497                         break;
1498                 }
1499                 work[p--] = '\0';
1500         }
1501         // Get the current working directory
1502         if( getcwd( buffer, MAXBUF ) == NULL )
1503                 return false;
1504         chdir(work);
1505         if( getcwd( otherdir, MAXBUF ) == NULL )
1506                 return false;
1507         chdir(buffer);
1508         if (strlen(otherdir) >= strlen(work))
1509         {
1510                 otherdir[strlen(work)] = '\0';
1511                 if (!strcmp(otherdir,work))
1512                 {
1513                         return true;
1514                 }
1515                 return false;
1516         }
1517         else return false;
1518 }
1519
1520 std::string GetFullProgDir(char** argv, int argc)
1521 {
1522         char work[MAXBUF];
1523         char buffer[MAXBUF], otherdir[MAXBUF];
1524         strlcpy(work,argv[0],MAXBUF);
1525         int p = strlen(work);
1526         // we just want the dir
1527         while (*work)
1528         {
1529                 if (work[p] == '/')
1530                 {
1531                         work[p] = '\0';
1532                         break;
1533                 }
1534                 work[p--] = '\0';
1535         }
1536         // Get the current working directory
1537         if( getcwd( buffer, MAXBUF ) == NULL )
1538                 return "";
1539         chdir(work);
1540         if( getcwd( otherdir, MAXBUF ) == NULL )
1541                 return "";
1542         chdir(buffer);
1543         return otherdir;
1544 }
1545
1546 int InsertMode(std::string &output, const char* mode, unsigned short section)
1547 {
1548         unsigned short currsection = 1;
1549         unsigned int pos = output.find("CHANMODES=", 0) + 10; // +10 for the length of "CHANMODES="
1550         
1551         if(section > 4 || section == 0)
1552         {
1553                 log(DEBUG, "InsertMode: CHANMODES doesn't have a section %dh :/", section);
1554                 return 0;
1555         }
1556         
1557         for(; pos < output.size(); pos++)
1558         {
1559                 if(section == currsection)
1560                         break;
1561                         
1562                 if(output[pos] == ',')
1563                         currsection++;
1564         }
1565         
1566         output.insert(pos, mode);
1567         return 1;
1568 }
1569
1570 bool IsValidChannelName(const char *chname)
1571 {
1572                 char *c;
1573
1574                 /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
1575                 if (!chname || *chname != '#')
1576                 {
1577                                 return false;
1578                 }
1579
1580                 c = (char *)chname + 1;
1581                 while (*c)
1582                 {
1583                                 switch (*c)
1584                                 {
1585                                                 case ' ':
1586                                                 case ',':
1587                                                 case 7:
1588                                                                 return false;
1589                                 }
1590
1591                                 c++;
1592                 }
1593                 
1594                 /* too long a name - note funky pointer arithmetic here. */
1595                 if ((c - chname) > CHANMAX)
1596                 {
1597                                 return false;
1598                 }
1599
1600                 return true;
1601 }
1602
1603 inline int charlcat(char* x,char y,int z)
1604 {
1605         char* x__n = x;
1606         int v = 0;
1607         while(*x__n++)
1608                 v++;
1609         if (v < z - 1)
1610         {
1611                 *--x__n = y;
1612                 *++x__n = 0;
1613         }
1614         return v;
1615 }
1616
1617 bool charremove(char* mp, char remove)
1618 {
1619         char* mptr = mp;
1620         bool shift_down = false;
1621         while (*mptr)
1622         {
1623                 if (*mptr == remove)
1624                         shift_down = true;
1625                 if (shift_down)
1626                         *mptr = *(mptr+1);
1627                 *mptr++;
1628         }
1629         return shift_down;
1630 }
1631