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