]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/helperfuncs.cpp
Typo
[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         va_list argsPtr;
993         va_start (argsPtr, text);
994         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
995         va_end(argsPtr);
996
997         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
998         {
999                 userrec* t = (userrec*)(*i);
1000                 WriteServ(t->fd,"NOTICE $%s :%s",Config->ServerName,textbuffer);
1001         }
1002 }
1003
1004 void ServerPrivmsgAll(char* text, ...)
1005 {
1006         if (!text)
1007                 return;
1008
1009         char textbuffer[MAXBUF];
1010         va_list argsPtr;
1011         va_start (argsPtr, text);
1012         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1013         va_end(argsPtr);
1014
1015         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1016         {
1017                 userrec* t = (userrec*)(*i);
1018                 WriteServ(t->fd,"PRIVMSG $%s :%s",Config->ServerName,textbuffer);
1019         }
1020 }
1021
1022 void WriteMode(const char* modes, int flags, const char* text, ...)
1023 {
1024         char textbuffer[MAXBUF];
1025         int modelen;
1026         va_list argsPtr;
1027
1028         if ((!text) || (!modes) || (!flags))
1029         {
1030                 log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
1031                 return;
1032         }
1033
1034         va_start(argsPtr, text);
1035         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1036         va_end(argsPtr);
1037         modelen = strlen(modes);
1038
1039         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1040         {
1041                 userrec* t = (userrec*)(*i);
1042                 bool send_to_user = false;
1043
1044                 if (flags == WM_AND)
1045                 {
1046                         send_to_user = true;
1047
1048                         for (int n = 0; n < modelen; n++)
1049                         {
1050                                 if (!hasumode(t,modes[n]))
1051                                 {
1052                                         send_to_user = false;
1053                                         break;
1054                                 }
1055                         }
1056                 }
1057                 else if (flags == WM_OR)
1058                 {
1059                         send_to_user = false;
1060
1061                         for (int n = 0; n < modelen; n++)
1062                         {
1063                                 if (hasumode(t,modes[n]))
1064                                 {
1065                                         send_to_user = true;
1066                                         break;
1067                                 }
1068                         }
1069                 }
1070
1071                 if (send_to_user)
1072                 {
1073                         WriteServ(t->fd,"NOTICE %s :%s",t->nick,textbuffer);
1074                 }
1075         }
1076 }
1077
1078 void NoticeAll(userrec *source, bool local_only, char* text, ...)
1079 {
1080         char textbuffer[MAXBUF];
1081         va_list argsPtr;
1082
1083         if ((!text) || (!source))
1084         {
1085                 log(DEFAULT,"*** BUG *** NoticeAll was given an invalid parameter");
1086                 return;
1087         }
1088
1089         va_start(argsPtr, text);
1090         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1091         va_end(argsPtr);
1092
1093         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1094         {
1095                 userrec* t = (userrec*)(*i);
1096                 WriteFrom(t->fd,source,"NOTICE $* :%s",textbuffer);
1097         }
1098 }
1099
1100
1101 void WriteWallOps(userrec *source, bool local_only, char* text, ...)
1102 {
1103         char textbuffer[MAXBUF];
1104         va_list argsPtr;
1105
1106         if ((!text) || (!source))
1107         {
1108                 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
1109                 return;
1110         }
1111
1112         va_start(argsPtr, text);
1113         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1114         va_end(argsPtr);
1115
1116         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1117         {
1118                 userrec* t = (userrec*)(*i);
1119
1120                 if ((IS_LOCAL(t)) && (t->modebits & UM_WALLOPS))
1121                 {
1122                         WriteTo(source,t,"WALLOPS :%s",textbuffer);
1123                 }
1124         }
1125 }
1126
1127 /* convert a string to lowercase. Note following special circumstances
1128  * taken from RFC 1459. Many "official" server branches still hold to this
1129  * rule so i will too;
1130  *
1131  *  Because of IRC's scandanavian origin, the characters {}| are
1132  *  considered to be the lower case equivalents of the characters []\,
1133  *  respectively. This is a critical issue when determining the
1134  *  equivalence of two nicknames.
1135  */
1136 void strlower(char *n)
1137 {
1138         if (n)
1139         {
1140                 for (char* t = n; *t; t++)
1141                         *t = lowermap[(unsigned)*t];
1142         }
1143 }
1144
1145 /* Find a user record by nickname and return a pointer to it */
1146
1147 userrec* Find(const std::string &nick)
1148 {
1149         user_hash::iterator iter = clientlist.find(nick);
1150
1151         if (iter == clientlist.end())
1152                 /* Couldn't find it */
1153                 return NULL;
1154
1155         return iter->second;
1156 }
1157
1158 userrec* Find(const char* nick)
1159 {
1160         user_hash::iterator iter;
1161
1162         if (!nick)
1163                 return NULL;
1164
1165         iter = clientlist.find(nick);
1166         
1167         if (iter == clientlist.end())
1168                 return NULL;
1169
1170         return iter->second;
1171 }
1172
1173 /* find a channel record by channel name and return a pointer to it */
1174
1175 chanrec* FindChan(const char* chan)
1176 {
1177         chan_hash::iterator iter;
1178
1179         if (!chan)
1180         {
1181                 log(DEFAULT,"*** BUG *** Findchan was given an invalid parameter");
1182                 return NULL;
1183         }
1184
1185         iter = chanlist.find(chan);
1186
1187         if (iter == chanlist.end())
1188                 /* Couldn't find it */
1189                 return NULL;
1190
1191         return iter->second;
1192 }
1193
1194
1195 long GetMaxBans(char* name)
1196 {
1197         std::string x;
1198         for (std::map<std::string,int>::iterator n = Config->maxbans.begin(); n != Config->maxbans.end(); n++)
1199         {
1200                 x = n->first;
1201                 if (match(name,x.c_str()))
1202                 {
1203                         return n->second;
1204                 }
1205         }
1206         return 64;
1207 }
1208
1209 void purge_empty_chans(userrec* u)
1210 {
1211         std::vector<chanrec*> to_delete;
1212
1213         // firstly decrement the count on each channel
1214         for (std::vector<ucrec*>::iterator f = u->chans.begin(); f != u->chans.end(); f++)
1215         {
1216                 if (((ucrec*)(*f))->channel)
1217                 {
1218                         if (((ucrec*)(*f))->channel->DelUser(u) == 0)
1219                         {
1220                                 /* No users left in here, mark it for deletion */
1221                                 to_delete.push_back(((ucrec*)(*f))->channel);
1222                                 ((ucrec*)(*f))->channel = NULL;
1223                         }
1224                 }
1225         }
1226
1227         log(DEBUG,"purge_empty_chans: %d channels to delete",to_delete.size());
1228
1229         for (std::vector<chanrec*>::iterator n = to_delete.begin(); n != to_delete.end(); n++)
1230         {
1231                 chanrec* thischan = (chanrec*)*n;
1232                 chan_hash::iterator i2 = chanlist.find(thischan->name);
1233                 if (i2 != chanlist.end())
1234                 {
1235                         FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(i2->second));
1236                         delete i2->second;
1237                         chanlist.erase(i2);
1238                 }
1239         }
1240
1241         if (*u->oper)
1242                 DeleteOper(u);
1243 }
1244
1245
1246 char* chanmodes(chanrec *chan, bool showkey)
1247 {
1248         static char scratch[MAXBUF];
1249         static char sparam[MAXBUF];
1250         char* offset = scratch;
1251
1252         if (!chan)
1253         {
1254                 log(DEFAULT,"*** BUG *** chanmodes was given an invalid parameter");
1255                 *scratch = '\0';
1256                 return scratch;
1257         }
1258
1259         *scratch = '\0';
1260         *sparam = '\0';
1261
1262         if (chan->binarymodes & CM_NOEXTERNAL)
1263                 *offset++ = 'n';
1264         if (chan->binarymodes & CM_TOPICLOCK)
1265                 *offset++ = 't';
1266         if (*chan->key)
1267                 *offset++ = 'k';
1268         if (chan->limit)
1269                 *offset++ = 'l';
1270         if (chan->binarymodes & CM_INVITEONLY)
1271                 *offset++ = 'i';
1272         if (chan->binarymodes & CM_MODERATED)
1273                 *offset++ = 'm';
1274         if (chan->binarymodes & CM_SECRET)
1275                 *offset++ = 's';
1276         if (chan->binarymodes & CM_PRIVATE)
1277                 *offset++ = 'p';
1278
1279         if (*chan->key)
1280         {
1281                 snprintf(sparam,MAXBUF," %s",showkey ? chan->key : "<key>");
1282         }
1283
1284         if (chan->limit)
1285         {
1286                 char foo[24];
1287                 sprintf(foo," %lu",(unsigned long)chan->limit);
1288                 strlcat(sparam,foo,MAXBUF);
1289         }
1290
1291         /* This was still iterating up to 190, chanrec::custom_modes is only 64 elements -- Om */
1292         for(int n = 0; n < 64; n++)
1293         {
1294                 if(chan->custom_modes[n])
1295                 {
1296                         *offset++ = n+65;
1297                         std::string extparam = chan->GetModeParameter(n+65);
1298
1299                         if (extparam != "")
1300                         {
1301                                 charlcat(sparam,' ',MAXBUF);
1302                                 strlcat(sparam,extparam.c_str(),MAXBUF);
1303                         }
1304                 }
1305         }
1306
1307         /* Null terminate scratch */
1308         *offset = '\0';
1309         strlcat(scratch,sparam,MAXMODES);
1310         return scratch;
1311 }
1312
1313
1314 /* compile a userlist of a channel into a string, each nick seperated by
1315  * spaces and op, voice etc status shown as @ and + */
1316
1317 void userlist(userrec *user,chanrec *c)
1318 {
1319         if ((!c) || (!user))
1320         {
1321                 log(DEFAULT,"*** BUG *** userlist was given an invalid parameter");
1322                 return;
1323         }
1324
1325         char list[MAXBUF];
1326         size_t dlen, curlen;
1327
1328         dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1329
1330         int numusers = 0;
1331         char* ptr = list + dlen;
1332
1333         CUList *ulist= c->GetUsers();
1334
1335         /* Improvement by Brain - this doesnt change in value, so why was it inside
1336          * the loop?
1337          */
1338         bool has_user = c->HasUser(user);
1339
1340         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
1341         {
1342                 if ((!has_user) && (i->second->modebits & UM_INVISIBLE))
1343                 {
1344                         /*
1345                          * user is +i, and source not on the channel, does not show
1346                          * nick in NAMES list
1347                          */
1348                         continue;
1349                 }
1350
1351                 size_t ptrlen = snprintf(ptr, MAXBUF, "%s%s ", cmode(i->second, c), i->second->nick);
1352
1353                 curlen += ptrlen;
1354                 ptr += ptrlen;
1355
1356                 numusers++;
1357
1358                 if (curlen > (480-NICKMAX))
1359                 {
1360                         /* list overflowed into multiple numerics */
1361                         WriteServ_NoFormat(user->fd,list);
1362
1363                         /* reset our lengths */
1364                         dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1365                         ptr = list + dlen;
1366
1367                         ptrlen = 0;
1368                         numusers = 0;
1369                 }
1370         }
1371
1372         /* if whats left in the list isnt empty, send it */
1373         if (numusers)
1374         {
1375                 WriteServ_NoFormat(user->fd,list);
1376         }
1377 }
1378
1379 /*
1380  * return a count of the users on a specific channel accounting for
1381  * invisible users who won't increase the count. e.g. for /LIST
1382  */
1383 int usercount_i(chanrec *c)
1384 {
1385         int count = 0;
1386
1387         if (!c)
1388                 return 0;
1389
1390         CUList *ulist= c->GetUsers();
1391         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
1392         {
1393                 if (!(i->second->modebits & UM_INVISIBLE))
1394                         count++;
1395         }
1396
1397         return count;
1398 }
1399
1400 int usercount(chanrec *c)
1401 {
1402         return (c ? c->GetUserCounter() : 0);
1403 }
1404
1405
1406 /* looks up a users password for their connection class (<ALLOW>/<DENY> tags) */
1407 ConnectClass GetClass(userrec *user)
1408 {
1409         for (ClassVector::iterator i = Config->Classes.begin(); i != Config->Classes.end(); i++)
1410         {
1411                 if (match(user->host,i->host.c_str()))
1412                 {
1413                         return *i;
1414                 }
1415         }
1416
1417         return *(Config->Classes.begin());
1418 }
1419
1420 /*
1421  * sends out an error notice to all connected clients (not to be used
1422  * lightly!)
1423  */
1424 void send_error(char *s)
1425 {
1426         log(DEBUG,"send_error: %s",s);
1427
1428         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1429         {
1430                 userrec* t = (userrec*)(*i);
1431                 if (t->registered == 7)
1432                 {
1433                         WriteServ(t->fd,"NOTICE %s :%s",t->nick,s);
1434                 }
1435                 else
1436                 {
1437                         // fix - unregistered connections receive ERROR, not NOTICE
1438                         Write(t->fd,"ERROR :%s",s);
1439                 }
1440         }
1441 }
1442
1443 void Error(int status)
1444 {
1445         signal(SIGALRM, SIG_IGN);
1446         signal(SIGPIPE, SIG_IGN);
1447         signal(SIGTERM, SIG_IGN);
1448         signal(SIGABRT, SIG_IGN);
1449         signal(SIGSEGV, SIG_IGN);
1450         signal(SIGURG, SIG_IGN);
1451         signal(SIGKILL, SIG_IGN);
1452         log(DEFAULT,"*** fell down a pothole in the road to perfection ***");
1453         send_error("Error! Segmentation fault! save meeeeeeeeeeeeee *splat!*");
1454         Exit(status);
1455 }
1456
1457 // this function counts all users connected, wether they are registered or NOT.
1458 int usercnt(void)
1459 {
1460         return clientlist.size();
1461 }
1462
1463 // this counts only registered users, so that the percentages in /MAP don't mess up when users are sitting in an unregistered state
1464 int registered_usercount(void)
1465 {
1466         int c = 0;
1467
1468         for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1469         {
1470                 if (i->second->registered == 7) c++;
1471         }
1472
1473         return c;
1474 }
1475
1476 int usercount_invisible(void)
1477 {
1478         int c = 0;
1479
1480         for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1481         {
1482                 if ((i->second->registered == 7) && (i->second->modebits & UM_INVISIBLE))
1483                         c++;
1484         }
1485
1486         return c;
1487 }
1488
1489 int usercount_opers(void)
1490 {
1491         int c = 0;
1492
1493         for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1494         {
1495                 if (i->second->oper)
1496                         c++;
1497         }
1498         return c;
1499 }
1500
1501 int usercount_unknown(void)
1502 {
1503         int c = 0;
1504
1505         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1506         {
1507                 userrec* t = (userrec*)(*i);
1508                 if (t->registered != 7)
1509                         c++;
1510         }
1511
1512         return c;
1513 }
1514
1515 long chancount(void)
1516 {
1517         return chanlist.size();
1518 }
1519
1520 long local_count()
1521 {
1522         int c = 0;
1523
1524         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1525         {
1526                 userrec* t = (userrec*)(*i);
1527                 if (t->registered == 7)
1528                         c++;
1529         }
1530
1531         return c;
1532 }
1533
1534 void ShowMOTD(userrec *user)
1535 {
1536         static char mbuf[MAXBUF];
1537         static char crud[MAXBUF];
1538         std::string WholeMOTD = "";
1539
1540         if (!Config->MOTD.size())
1541         {
1542                 WriteServ(user->fd,"422 %s :Message of the day file is missing.",user->nick);
1543                 return;
1544         }
1545
1546         snprintf(crud,MAXBUF,":%s 372 %s :- ", Config->ServerName, user->nick);
1547         snprintf(mbuf,MAXBUF,":%s 375 %s :- %s message of the day\r\n", Config->ServerName, user->nick, Config->ServerName);
1548         WholeMOTD = WholeMOTD + mbuf;
1549
1550         for (unsigned int i = 0; i < Config->MOTD.size(); i++)
1551                 WholeMOTD = WholeMOTD + std::string(crud) + Config->MOTD[i].c_str() + std::string("\r\n");
1552
1553         snprintf(mbuf,MAXBUF,":%s 376 %s :End of message of the day.\r\n", Config->ServerName, user->nick);
1554         WholeMOTD = WholeMOTD + mbuf;
1555
1556         // only one write operation
1557         if (Config->GetIOHook(user->port))
1558         {
1559                 try
1560                 {
1561                         Config->GetIOHook(user->port)->OnRawSocketWrite(user->fd,(char*)WholeMOTD.c_str(),WholeMOTD.length());
1562                 }
1563                 catch (ModuleException& modexcept)
1564                 {
1565                         log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
1566                 }
1567         }
1568         else
1569         {
1570                 user->AddWriteBuf(WholeMOTD);
1571         }
1572
1573         ServerInstance->stats->statsSent += WholeMOTD.length();
1574 }
1575
1576 void ShowRULES(userrec *user)
1577 {
1578         if (!Config->RULES.size())
1579         {
1580                 WriteServ(user->fd,"NOTICE %s :Rules file is missing.",user->nick);
1581                 return;
1582         }
1583         WriteServ(user->fd,"NOTICE %s :%s rules",user->nick,Config->ServerName);
1584
1585         for (unsigned int i = 0; i < Config->RULES.size(); i++)
1586                 WriteServ(user->fd,"NOTICE %s :%s",user->nick,Config->RULES[i].c_str());
1587
1588         WriteServ(user->fd,"NOTICE %s :End of %s rules.",user->nick,Config->ServerName);
1589 }
1590
1591 // this returns 1 when all modules are satisfied that the user should be allowed onto the irc server
1592 // (until this returns true, a user will block in the waiting state, waiting to connect up to the
1593 // registration timeout maximum seconds)
1594 bool AllModulesReportReady(userrec* user)
1595 {
1596         if (!Config->global_implementation[I_OnCheckReady])
1597                 return true;
1598
1599         for (int i = 0; i <= MODCOUNT; i++)
1600         {
1601                 if (Config->implement_lists[i][I_OnCheckReady])
1602                 {
1603                         int res = modules[i]->OnCheckReady(user);
1604                         if (!res)
1605                                 return false;
1606                 }
1607         }
1608
1609         return true;
1610 }
1611
1612 bool DirValid(char* dirandfile)
1613 {
1614         char work[MAXBUF];
1615         char buffer[MAXBUF];
1616         char otherdir[MAXBUF];
1617         int p;
1618
1619         strlcpy(work, dirandfile, MAXBUF);
1620         p = strlen(work);
1621
1622         // we just want the dir
1623         while (*work)
1624         {
1625                 if (work[p] == '/')
1626                 {
1627                         work[p] = '\0';
1628                         break;
1629                 }
1630
1631                 work[p--] = '\0';
1632         }
1633
1634         // Get the current working directory
1635         if (getcwd(buffer, MAXBUF ) == NULL )
1636                 return false;
1637
1638         chdir(work);
1639
1640         if (getcwd(otherdir, MAXBUF ) == NULL )
1641                 return false;
1642
1643         chdir(buffer);
1644
1645         size_t t = strlen(work);
1646
1647         if (strlen(otherdir) >= t)
1648         {
1649                 otherdir[t] = '\0';
1650
1651                 if (!strcmp(otherdir,work))
1652                 {
1653                         return true;
1654                 }
1655
1656                 return false;
1657         }
1658         else
1659         {
1660                 return false;
1661         }
1662 }
1663
1664 std::string GetFullProgDir(char** argv, int argc)
1665 {
1666         char work[MAXBUF];
1667         char buffer[MAXBUF];
1668         char otherdir[MAXBUF];
1669         int p;
1670
1671         strlcpy(work,argv[0],MAXBUF);
1672         p = strlen(work);
1673
1674         // we just want the dir
1675         while (*work)
1676         {
1677                 if (work[p] == '/')
1678                 {
1679                         work[p] = '\0';
1680                         break;
1681                 }
1682
1683                 work[p--] = '\0';
1684         }
1685
1686         // Get the current working directory
1687         if (getcwd(buffer, MAXBUF) == NULL)
1688                 return "";
1689
1690         chdir(work);
1691
1692         if (getcwd(otherdir, MAXBUF) == NULL)
1693                 return "";
1694
1695         chdir(buffer);
1696         return otherdir;
1697 }
1698
1699 int InsertMode(std::string &output, const char* mode, unsigned short section)
1700 {
1701         unsigned short currsection = 1;
1702         unsigned int pos = output.find("CHANMODES=", 0) + 10; // +10 for the length of "CHANMODES="
1703         
1704         if(section > 4 || section == 0)
1705         {
1706                 log(DEBUG, "InsertMode: CHANMODES doesn't have a section %dh :/", section);
1707                 return 0;
1708         }
1709         
1710         for(; pos < output.size(); pos++)
1711         {
1712                 if(section == currsection)
1713                         break;
1714                         
1715                 if(output[pos] == ',')
1716                         currsection++;
1717         }
1718         
1719         output.insert(pos, mode);
1720         return 1;
1721 }
1722
1723 bool IsValidChannelName(const char *chname)
1724 {
1725         char *c;
1726
1727         /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
1728         if (!chname || *chname != '#')
1729         {
1730                 return false;
1731         }
1732
1733         c = (char *)chname + 1;
1734         while (*c)
1735         {
1736                 switch (*c)
1737                 {
1738                         case ' ':
1739                         case ',':
1740                         case 7:
1741                                 return false;
1742                 }
1743
1744                 c++;
1745         }
1746                 
1747         /* too long a name - note funky pointer arithmetic here. */
1748         if ((c - chname) > CHANMAX)
1749         {
1750                         return false;
1751         }
1752
1753         return true;
1754 }
1755
1756 inline int charlcat(char* x,char y,int z)
1757 {
1758         char* x__n = x;
1759         int v = 0;
1760
1761         while(*x__n++)
1762                 v++;
1763
1764         if (v < z - 1)
1765         {
1766                 *--x__n = y;
1767                 *++x__n = 0;
1768         }
1769
1770         return v;
1771 }
1772
1773 bool charremove(char* mp, char remove)
1774 {
1775         char* mptr = mp;
1776         bool shift_down = false;
1777
1778         while (*mptr)
1779         {
1780                 if (*mptr == remove)
1781                 shift_down = true;
1782
1783                 if (shift_down)
1784                         *mptr = *(mptr+1);
1785
1786                 mptr++;
1787         }
1788
1789         return shift_down;
1790 }