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