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