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