]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/helperfuncs.cpp
It's neat. It's tidy. It has no spaces, and almost matches our guidelines...
[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         std::map<char*,char*> *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 (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
476         {
477                 /* XXX - WHAT THE HELL are we doing here? :P --w00t */
478                 char* o = i->second;
479                 userrec* otheruser = (userrec*)o;
480
481                 if (otheruser->fd != FD_MAGIC_NUMBER)
482                         WriteTo_NoFormat(user,otheruser,textbuffer);
483         }
484 }
485
486 void WriteChannel_NoFormat(chanrec* Ptr, userrec* user, const char* text)
487 {
488         std::map<char*,char*> *ulist;
489
490         if ((!Ptr) || (!user) || (!text))
491         {
492                 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
493                 return;
494         }
495
496         ulist = Ptr->GetUsers();
497
498         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
499         {
500                 /* XXX - .....and here! --w00t */
501                 char* o = i->second;
502                 userrec* otheruser = (userrec*)o;
503
504                 if (otheruser->fd != FD_MAGIC_NUMBER)
505                         WriteTo_NoFormat(user,otheruser,text);
506         }
507 }
508
509
510 /* write formatted text from a source user to all users on a channel
511  * including the sender (NOT for privmsg, notice etc!) doesnt send to
512  * users on remote servers */
513
514 void WriteChannelLocal(chanrec* Ptr, userrec* user, char* text, ...)
515 {
516         char textbuffer[MAXBUF];
517         va_list argsPtr;
518         std::map<char*,char*> *ulist;
519
520         if ((!Ptr) || (!text))
521         {
522                 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
523                 return;
524         }
525
526         va_start(argsPtr, text);
527         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
528         va_end(argsPtr);
529
530         ulist = Ptr->GetUsers();
531
532         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
533         {
534                 /* XXX -- w00t */
535                 char* o = i->second;
536                 userrec* otheruser = (userrec*)o;
537
538                 if ((otheruser->fd != FD_MAGIC_NUMBER) && (otheruser != user))
539                 {
540                         if (!user)
541                         {
542                                 WriteServ_NoFormat(otheruser->fd,textbuffer);
543                         }
544                         else
545                         {
546                                 WriteTo_NoFormat(user,otheruser,textbuffer);
547                         }
548                 }
549         }
550 }
551
552 void WriteChannelLocal_NoFormat(chanrec* Ptr, userrec* user, const char* text)
553 {
554         std::map<char*,char*> *ulist;
555
556         if ((!Ptr) || (!text))
557         {
558                 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
559                 return;
560         }
561
562         ulist = Ptr->GetUsers();
563
564         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
565         {
566                 /* XXX -- w00t */
567                 char* o = i->second;
568                 userrec* otheruser = (userrec*)o;
569
570                 if ((otheruser->fd != FD_MAGIC_NUMBER) && (otheruser != user))
571                 {
572                         if (!user)
573                         {
574                                 WriteServ_NoFormat(otheruser->fd,text);
575                         }
576                         else
577                         {
578                                 WriteTo_NoFormat(user,otheruser,text);
579                         }
580                 }
581         }
582 }
583
584
585
586 void WriteChannelWithServ(char* ServName, chanrec* Ptr, char* text, ...)
587 {
588         char textbuffer[MAXBUF];
589         va_list argsPtr;
590         std::map<char*,char*> *ulist;
591
592         if ((!Ptr) || (!text))
593         {
594                 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
595                 return;
596         }
597
598         va_start(argsPtr, text);
599         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
600         va_end(argsPtr);
601
602         ulist = Ptr->GetUsers();
603
604         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
605         {
606                 /* XXX */
607                 char* o = i->second;
608                 userrec* otheruser = (userrec*)o;
609
610                 if (IS_LOCAL(otheruser))
611                         WriteServ_NoFormat(otheruser->fd,textbuffer);
612         }
613 }
614
615 void WriteChannelWithServ_NoFormat(char* ServName, chanrec* Ptr, const char* text)
616 {
617         std::map<char*,char*> *ulist;
618
619         if ((!Ptr) || (!text))
620         {
621                 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
622                 return;
623         }
624
625         ulist = Ptr->GetUsers();
626
627         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
628         {
629                 /* XXX */
630                 char* o = i->second;
631                 userrec* otheruser = (userrec*)o;
632
633                 if (IS_LOCAL(otheruser))
634                         WriteServ_NoFormat(otheruser->fd,text);
635         }
636 }
637
638
639
640 /* write formatted text from a source user to all users on a channel except
641  * for the sender (for privmsg etc) */
642
643 void ChanExceptSender(chanrec* Ptr, userrec* user, char status, char* text, ...)
644 {
645         char textbuffer[MAXBUF];
646         va_list argsPtr;
647         std::map<char*,char*> *ulist;
648
649         if ((!Ptr) || (!user) || (!text))
650         {
651                 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
652                 return;
653         }
654
655         va_start(argsPtr, text);
656         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
657         va_end(argsPtr);
658
659         switch (status)
660         {
661                 case '@':
662                         ulist = Ptr->GetOppedUsers();
663                         break;
664                 case '%':
665                         ulist = Ptr->GetHalfoppedUsers();
666                         break;
667                 case '+':
668                         ulist = Ptr->GetVoicedUsers();
669                         break;
670                 default:
671                         ulist = Ptr->GetUsers();
672                         break;
673         }
674
675         log(DEBUG,"%d users to write to",ulist->size());
676
677         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
678         {
679                 /* XXX */
680                 char* o = i->second;
681                 userrec* otheruser = (userrec*)o;
682
683                 if ((IS_LOCAL(otheruser)) && (user != otheruser))
684                         WriteFrom_NoFormat(otheruser->fd,user,textbuffer);
685         }
686 }
687
688 void ChanExceptSender_NoFormat(chanrec* Ptr, userrec* user, char status, const char* text)
689 {
690         std::map<char*,char*> *ulist;
691
692         if ((!Ptr) || (!user) || (!text))
693         {
694                 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
695                 return;
696         }
697
698         switch (status)
699         {
700                 case '@':
701                         ulist = Ptr->GetOppedUsers();
702                         break;  
703                 case '%':
704                         ulist = Ptr->GetHalfoppedUsers();
705                         break;
706                 case '+':
707                         ulist = Ptr->GetVoicedUsers();
708                         break;
709                 default:
710                         ulist = Ptr->GetUsers();
711                         break;
712         }
713
714         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
715         {
716                 /* XXX */
717                 char* o = i->second;
718                 userrec* otheruser = (userrec*)o;
719
720                 if ((IS_LOCAL(otheruser)) && (user != otheruser))
721                         WriteFrom_NoFormat(otheruser->fd,user,text);
722         }
723 }
724
725 std::string GetServerDescription(char* servername)
726 {
727         std::string description = "";
728
729         FOREACH_MOD(I_OnGetServerDescription,OnGetServerDescription(servername,description));
730
731         if (description != "")
732         {
733                 return description;
734         }
735         else
736         {
737                 // not a remote server that can be found, it must be me.
738                 return Config->ServerDesc;
739         }
740 }
741
742 /* write a formatted string to all users who share at least one common
743  * channel, including the source user e.g. for use in NICK */
744
745 void WriteCommon(userrec *u, char* text, ...)
746 {
747         char textbuffer[MAXBUF];
748         va_list argsPtr;
749         bool sent_to_at_least_one = false;
750         unsigned int y;
751
752         if (!u)
753         {
754                 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
755                 return;
756         }
757
758         if (u->registered != 7)
759         {
760                 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
761                 return;
762         }
763
764         va_start(argsPtr, text);
765         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
766         va_end(argsPtr);
767
768         // FIX: Stops a message going to the same person more than once
769         memset(&already_sent,0,MAX_DESCRIPTORS);
770         y = u->chans.size();
771
772         for (unsigned int i = 0; i < y; i++)
773         {
774                 if (u->chans[i].channel)
775                 {
776                         std::map<char*,char*> *ulist= u->chans[i].channel->GetUsers();
777
778                         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
779                         {
780                                 char* o = i->second;
781                                 userrec* otheruser = (userrec*)o;
782
783                                 if ((otheruser->fd > -1) && (!already_sent[otheruser->fd]))
784                                 {
785                                         already_sent[otheruser->fd] = 1;
786                                         WriteFrom_NoFormat(otheruser->fd,u,textbuffer);
787                                         sent_to_at_least_one = true;
788                                 }
789                         }
790                 }
791         }
792
793         /*
794          * if the user was not in any channels, no users will receive the text. Make sure the user
795          * receives their OWN message for WriteCommon
796          */
797         if (!sent_to_at_least_one)
798         {
799                 WriteFrom_NoFormat(u->fd,u,textbuffer);
800         }
801 }
802
803 void WriteCommon_NoFormat(userrec *u, const char* text)
804 {
805         bool sent_to_at_least_one = false;
806         unsigned int y;
807
808         if (!u)
809         {
810                 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
811                 return;
812         }
813
814         if (u->registered != 7)
815         {
816                 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
817                 return;
818         }
819
820         // FIX: Stops a message going to the same person more than once
821         memset(&already_sent,0,MAX_DESCRIPTORS);
822         y = u->chans.size();
823
824         for (unsigned int i = 0; i < y; i++)
825         {
826                 if (u->chans[i].channel)
827                 {
828                         std::map<char*,char*> *ulist= u->chans[i].channel->GetUsers();
829
830                         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
831                         {
832                                 char* o = i->second;
833                                 userrec* otheruser = (userrec*)o;
834
835                                 if ((otheruser->fd > -1) && (!already_sent[otheruser->fd]))
836                                 {
837                                         already_sent[otheruser->fd] = 1;
838                                         WriteFrom_NoFormat(otheruser->fd,u,text);
839                                         sent_to_at_least_one = true;
840                                 }
841                         }
842                 }
843         }
844
845         /*
846          * if the user was not in any channels, no users will receive the text. Make sure the user
847          * receives their OWN message for WriteCommon
848          */
849         if (!sent_to_at_least_one)
850         {
851                 WriteFrom_NoFormat(u->fd,u,text);
852         }
853 }
854
855
856 /* write a formatted string to all users who share at least one common
857  * channel, NOT including the source user e.g. for use in QUIT
858  */
859
860 void WriteCommonExcept(userrec *u, char* text, ...)
861 {
862         char textbuffer[MAXBUF];
863         char oper_quit[MAXBUF];
864         bool quit_munge = false;
865         va_list argsPtr;
866         int total;
867         unsigned int y;
868
869         if (!u)
870         {
871                 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
872                 return;
873         }
874
875         if (u->registered != 7)
876         {
877                 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
878                 return;
879         }
880
881         va_start(argsPtr, text);
882         total = vsnprintf(textbuffer, MAXBUF, text, argsPtr);
883         va_end(argsPtr);
884
885         if ((Config->HideSplits) && (total > 6))
886         {
887                 /* Yeah yeah, this is ugly. But its fast, live with it. */
888                 char* check = textbuffer;
889
890                 /* XXX - don't mean to be picky, but I think this could be done better */
891                 if ((*check++ == 'Q') && (*check++ == 'U') && (*check++ == 'I') && (*check++ == 'T') && (*check++ == ' ') && (*check++ == ':'))
892                 {
893                         std::stringstream split(check);
894                         std::string server_one;
895                         std::string server_two;
896
897                         split >> server_one;
898                         split >> server_two;
899
900                         if ((FindServerName(server_one)) && (FindServerName(server_two)))
901                         {
902                                 strlcpy(oper_quit,textbuffer,MAXQUIT);
903                                 strlcpy(check,"*.net *.split",MAXQUIT);
904                                 quit_munge = true;
905                         }
906                 }
907         }
908
909         if ((Config->HideBans) && (total > 13) && (!quit_munge))
910         {
911                 char* check = textbuffer;
912
913                 /* XXX - as above */
914                 if ((*check++ == 'Q') && (*check++ == 'U') && (*check++ == 'I') && (*check++ == 'T') && (*check++ == ' ') && (*check++ == ':'))
915                 {
916                         check++;
917
918                         if ((*check++ == '-') && (*check++ == 'L') && (*check++ == 'i') && (*check++ == 'n') && (*check++ == 'e') && (*check++ == 'd') && (*check++ == ':'))
919                         {
920                                 strlcpy(oper_quit,textbuffer,MAXQUIT);
921                                 *(--check) = 0;         // We don't need to strlcpy, we just chop it from the :
922                                 quit_munge = true;
923                         }
924                 }
925         }
926
927         memset(&already_sent,0,MAX_DESCRIPTORS);
928         y = u->chans.size();
929
930         for (unsigned int i = 0; i < y; i++)
931         {
932                 if (u->chans[i].channel)
933                 {
934                         std::map<char*,char*> *ulist= u->chans[i].channel->GetUsers();
935
936                         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
937                         {
938                                 char* o = i->second;
939                                 userrec* otheruser = (userrec*)o;
940
941                                 if (u != otheruser)
942                                 {
943                                         if ((otheruser->fd > -1) && (!already_sent[otheruser->fd]))
944                                         {
945                                                 already_sent[otheruser->fd] = 1;
946
947                                                 if (quit_munge)
948                                                 {
949                                                         WriteFrom_NoFormat(otheruser->fd,u,*otheruser->oper ? oper_quit : textbuffer);
950                                                 }
951                                                 else
952                                                         WriteFrom_NoFormat(otheruser->fd,u,textbuffer);
953                                         }
954                                 }
955                         }
956                 }
957         }
958 }
959
960 void WriteCommonExcept_NoFormat(userrec *u, const char* text)
961 {
962         unsigned int y;
963
964         if (!u)
965         {
966                 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
967                 return;
968         }
969  
970         if (u->registered != 7)
971         {
972                 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
973                 return;
974         }
975
976         memset(&already_sent,0,MAX_DESCRIPTORS);
977         y = u->chans.size();
978
979         for (unsigned int i = 0; i < y; i++)
980         {
981                 if (u->chans[i].channel)
982                 {
983                         std::map<char*,char*> *ulist= u->chans[i].channel->GetUsers();
984
985                         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
986                         {
987                                 char* o = i->second;
988                                 userrec* otheruser = (userrec*)o;
989
990                                 if (u != otheruser)
991                                 {
992                                         if ((otheruser->fd > -1) && (!already_sent[otheruser->fd]))
993                                         {
994                                                 already_sent[otheruser->fd] = 1;
995                                                 WriteFrom_NoFormat(otheruser->fd,u,text);
996                                         }
997                                 }
998                         }
999                 }
1000         }
1001 }
1002
1003
1004 /* XXX - replace with a call to WriteMode() ? -- w00t */
1005 void WriteOpers(char* text, ...)
1006 {
1007         char textbuffer[MAXBUF];
1008         va_list argsPtr;
1009
1010         if (!text)
1011         {
1012                 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
1013                 return;
1014         }
1015
1016         va_start(argsPtr, text);
1017         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1018         va_end(argsPtr);
1019
1020         for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
1021         {
1022                 userrec* a = *i;
1023
1024                 if (IS_LOCAL(a))
1025                 {
1026                         if (strchr(a->modes,'s'))
1027                         {
1028                                 // send server notices to all with +s
1029                                 WriteServ(a->fd,"NOTICE %s :%s",a->nick,textbuffer);
1030                         }
1031                 }
1032         }
1033 }
1034
1035 void ServerNoticeAll(char* text, ...)
1036 {
1037         if (!text)
1038                 return;
1039
1040         char textbuffer[MAXBUF];
1041         va_list argsPtr;
1042         va_start (argsPtr, text);
1043         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1044         va_end(argsPtr);
1045
1046         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1047         {
1048                 userrec* t = (userrec*)(*i);
1049                 WriteServ(t->fd,"NOTICE $%s :%s",Config->ServerName,textbuffer);
1050         }
1051 }
1052
1053 void ServerPrivmsgAll(char* text, ...)
1054 {
1055         if (!text)
1056                 return;
1057
1058         char textbuffer[MAXBUF];
1059         va_list argsPtr;
1060         va_start (argsPtr, text);
1061         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1062         va_end(argsPtr);
1063
1064         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1065         {
1066                 userrec* t = (userrec*)(*i);
1067                 WriteServ(t->fd,"PRIVMSG $%s :%s",Config->ServerName,textbuffer);
1068         }
1069 }
1070
1071 void WriteMode(const char* modes, int flags, const char* text, ...)
1072 {
1073         char textbuffer[MAXBUF];
1074         int modelen;
1075         va_list argsPtr;
1076
1077         if ((!text) || (!modes) || (!flags))
1078         {
1079                 log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
1080                 return;
1081         }
1082
1083         va_start(argsPtr, text);
1084         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1085         va_end(argsPtr);
1086         modelen = strlen(modes);
1087
1088         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1089         {
1090                 userrec* t = (userrec*)(*i);
1091                 bool send_to_user = false;
1092
1093                 if (flags == WM_AND)
1094                 {
1095                         send_to_user = true;
1096
1097                         for (int n = 0; n < modelen; n++)
1098                         {
1099                                 if (!hasumode(t,modes[n]))
1100                                 {
1101                                         send_to_user = false;
1102                                         break;
1103                                 }
1104                         }
1105                 }
1106                 else if (flags == WM_OR)
1107                 {
1108                         send_to_user = false;
1109
1110                         for (int n = 0; n < modelen; n++)
1111                         {
1112                                 if (hasumode(t,modes[n]))
1113                                 {
1114                                         send_to_user = true;
1115                                         break;
1116                                 }
1117                         }
1118                 }
1119
1120                 if (send_to_user)
1121                 {
1122                         WriteServ(t->fd,"NOTICE %s :%s",t->nick,textbuffer);
1123                 }
1124         }
1125 }
1126
1127 void NoticeAll(userrec *source, bool local_only, char* text, ...)
1128 {
1129         char textbuffer[MAXBUF];
1130         va_list argsPtr;
1131
1132         if ((!text) || (!source))
1133         {
1134                 log(DEFAULT,"*** BUG *** NoticeAll was given an invalid parameter");
1135                 return;
1136         }
1137
1138         va_start(argsPtr, text);
1139         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1140         va_end(argsPtr);
1141
1142         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1143         {
1144                 userrec* t = (userrec*)(*i);
1145                 WriteFrom(t->fd,source,"NOTICE $* :%s",textbuffer);
1146         }
1147 }
1148
1149
1150 void WriteWallOps(userrec *source, bool local_only, char* text, ...)
1151 {
1152         char textbuffer[MAXBUF];
1153         va_list argsPtr;
1154
1155         if ((!text) || (!source))
1156         {
1157                 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
1158                 return;
1159         }
1160
1161         va_start(argsPtr, text);
1162         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1163         va_end(argsPtr);
1164
1165         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1166         {
1167                 userrec* t = (userrec*)(*i);
1168
1169                 if ((IS_LOCAL(t)) && (strchr(t->modes,'w')))
1170                 {
1171                         WriteTo(source,t,"WALLOPS :%s",textbuffer);
1172                 }
1173         }
1174 }
1175
1176 /* convert a string to lowercase. Note following special circumstances
1177  * taken from RFC 1459. Many "official" server branches still hold to this
1178  * rule so i will too;
1179  *
1180  *  Because of IRC's scandanavian origin, the characters {}| are
1181  *  considered to be the lower case equivalents of the characters []\,
1182  *  respectively. This is a critical issue when determining the
1183  *  equivalence of two nicknames.
1184  */
1185 void strlower(char *n)
1186 {
1187         if (n)
1188         {
1189                 for (char* t = n; *t; t++)
1190                         *t = lowermap[(unsigned)*t];
1191         }
1192 }
1193
1194 /* Find a user record by nickname and return a pointer to it */
1195
1196 userrec* Find(std::string nick)
1197 {
1198         user_hash::iterator iter = clientlist.find(nick);
1199
1200         if (iter == clientlist.end())
1201                 /* Couldn't find it */
1202                 return NULL;
1203
1204         return iter->second;
1205 }
1206
1207 userrec* Find(const char* nick)
1208 {
1209         user_hash::iterator iter;
1210
1211         if (!nick)
1212                 return NULL;
1213
1214         iter = clientlist.find(nick);
1215         
1216         if (iter == clientlist.end())
1217                 return NULL;
1218
1219         return iter->second;
1220 }
1221
1222 /* find a channel record by channel name and return a pointer to it */
1223
1224 chanrec* FindChan(const char* chan)
1225 {
1226         chan_hash::iterator iter;
1227
1228         if (!chan)
1229         {
1230                 log(DEFAULT,"*** BUG *** Findchan was given an invalid parameter");
1231                 return NULL;
1232         }
1233
1234         iter = chanlist.find(chan);
1235
1236         if (iter == chanlist.end())
1237                 /* Couldn't find it */
1238                 return NULL;
1239
1240         return iter->second;
1241 }
1242
1243
1244 long GetMaxBans(char* name)
1245 {
1246         std::string x;
1247         for (std::map<std::string,int>::iterator n = Config->maxbans.begin(); n != Config->maxbans.end(); n++)
1248         {
1249                 x = n->first;
1250                 if (match(name,x.c_str()))
1251                 {
1252                         return n->second;
1253                 }
1254         }
1255         return 64;
1256 }
1257
1258 void purge_empty_chans(userrec* u)
1259 {
1260         int purge = 0;
1261
1262         // firstly decrement the count on each channel
1263         for (unsigned int f = 0; f < u->chans.size(); f++)
1264         {
1265                 if (u->chans[f].channel)
1266                 {
1267                         u->chans[f].channel->DelUser((char*)u);
1268                 }
1269         }
1270
1271         for (unsigned int i = 0; i < u->chans.size(); i++)
1272         {
1273                 if (u->chans[i].channel)
1274                 {
1275                         if (!usercount(u->chans[i].channel))
1276                         {
1277                                 chan_hash::iterator i2 = chanlist.find(u->chans[i].channel->name);
1278
1279                                 /* kill the record */
1280                                 if (i2 != chanlist.end())
1281                                 {
1282                                         log(DEBUG,"del_channel: destroyed: %s",i2->second->name);
1283
1284                                         if (i2->second)
1285                                         {
1286                                                 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(i2->second));
1287                                                 delete i2->second;
1288                                         }
1289
1290                                         chanlist.erase(i2);
1291                                         purge++;
1292                                         u->chans[i].channel = NULL;
1293                                 }
1294                         }
1295                         else
1296                         {
1297                                 log(DEBUG,"skipped purge for %s",u->chans[i].channel->name);
1298                         }
1299                 }
1300         }
1301
1302         log(DEBUG,"completed channel purge, killed %lu",(unsigned long)purge);
1303         DeleteOper(u);
1304 }
1305
1306
1307 char* chanmodes(chanrec *chan, bool showkey)
1308 {
1309         static char scratch[MAXBUF];
1310         static char sparam[MAXBUF];
1311         char* offset = scratch;
1312
1313         if (!chan)
1314         {
1315                 log(DEFAULT,"*** BUG *** chanmodes was given an invalid parameter");
1316                 *scratch = '\0';
1317                 return scratch;
1318         }
1319
1320         *scratch = '\0';
1321         *sparam = '\0';
1322
1323         if (chan->binarymodes & CM_NOEXTERNAL)
1324                 *offset++ = 'n';
1325         if (chan->binarymodes & CM_TOPICLOCK)
1326                 *offset++ = 't';
1327         if (*chan->key)
1328                 *offset++ = 'k';
1329         if (chan->limit)
1330                 *offset++ = 'l';
1331         if (chan->binarymodes & CM_INVITEONLY)
1332                 *offset++ = 'i';
1333         if (chan->binarymodes & CM_MODERATED)
1334                 *offset++ = 'm';
1335         if (chan->binarymodes & CM_SECRET)
1336                 *offset++ = 's';
1337         if (chan->binarymodes & CM_PRIVATE)
1338                 *offset++ = 'p';
1339
1340         if (*chan->key)
1341         {
1342                 snprintf(sparam,MAXBUF," %s",showkey ? chan->key : "<key>");
1343         }
1344
1345         if (chan->limit)
1346         {
1347                 char foo[24];
1348                 sprintf(foo," %lu",(unsigned long)chan->limit);
1349                 strlcat(sparam,foo,MAXBUF);
1350         }
1351
1352         for (int n = 0; n < 190; n++)
1353         {
1354                 if (chan->custom_modes[n])
1355                 {
1356                         *offset++ = n+65;
1357                         std::string extparam = chan->GetModeParameter(n+65);
1358
1359                         if (extparam != "")
1360                         {
1361                                 charlcat(sparam,' ',MAXBUF);
1362                                 strlcat(sparam,extparam.c_str(),MAXBUF);
1363                         }
1364                 }
1365         }
1366
1367         /* Null terminate scratch */
1368         *offset = '\0';
1369         strlcat(scratch,sparam,MAXMODES);
1370         return scratch;
1371 }
1372
1373
1374 /* compile a userlist of a channel into a string, each nick seperated by
1375  * spaces and op, voice etc status shown as @ and + */
1376
1377 void userlist(userrec *user,chanrec *c)
1378 {
1379         if ((!c) || (!user))
1380         {
1381                 log(DEFAULT,"*** BUG *** userlist was given an invalid parameter");
1382                 return;
1383         }
1384
1385         size_t dlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1386         size_t initial = dlen;
1387
1388         std::map<char*,char*> *ulist= c->GetUsers();
1389
1390         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
1391         {
1392                 char* o = i->second;
1393                 userrec* otheruser = (userrec*)o;
1394
1395                 if ((!has_channel(user,c)) && (strchr(otheruser->modes,'i')))
1396                 {
1397                         /*
1398                          * user is +i, and source not on the channel, does not show
1399                          * nick in NAMES list
1400                          */
1401                         continue;
1402                 }
1403
1404                 dlen += strlcat(list,cmode(otheruser,c),MAXBUF);
1405                 dlen += strlcat(list,otheruser->nick,MAXBUF);
1406                 charlcat(list,' ',MAXBUF);
1407                 dlen++;
1408
1409                 if (dlen > (480-NICKMAX))
1410                 {
1411                         /* list overflowed into multiple numerics */
1412                         WriteServ_NoFormat(user->fd,list);
1413                         dlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1414                 }
1415         }
1416
1417         /* if whats left in the list isnt empty, send it */
1418         if (dlen != initial)
1419         {
1420                 WriteServ_NoFormat(user->fd,list);
1421         }
1422 }
1423
1424 /*
1425  * return a count of the users on a specific channel accounting for
1426  * invisible users who won't increase the count. e.g. for /LIST
1427  */
1428 int usercount_i(chanrec *c)
1429 {
1430         int count = 0;
1431
1432         if (!c)
1433                 return 0;
1434
1435         std::map<char*,char*> *ulist= c->GetUsers();
1436         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
1437         {
1438                 char* o = i->second;
1439                 userrec* user = (userrec*)o;
1440                 if (!strchr(user->modes,'i'))
1441                         count++;
1442         }
1443
1444         return count;
1445 }
1446
1447 int usercount(chanrec *c)
1448 {
1449         return (c ? c->GetUserCounter() : 0);
1450 }
1451
1452
1453 /* looks up a users password for their connection class (<ALLOW>/<DENY> tags) */
1454 ConnectClass GetClass(userrec *user)
1455 {
1456         for (ClassVector::iterator i = Config->Classes.begin(); i != Config->Classes.end(); i++)
1457         {
1458                 if (match(user->host,i->host.c_str()))
1459                 {
1460                         return *i;
1461                 }
1462         }
1463
1464         return *(Config->Classes.begin());
1465 }
1466
1467 /*
1468  * sends out an error notice to all connected clients (not to be used
1469  * lightly!)
1470  */
1471 void send_error(char *s)
1472 {
1473         log(DEBUG,"send_error: %s",s);
1474
1475         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1476         {
1477                 userrec* t = (userrec*)(*i);
1478                 if (t->registered == 7)
1479                 {
1480                         WriteServ(t->fd,"NOTICE %s :%s",t->nick,s);
1481                 }
1482                 else
1483                 {
1484                         // fix - unregistered connections receive ERROR, not NOTICE
1485                         Write(t->fd,"ERROR :%s",s);
1486                 }
1487         }
1488 }
1489
1490 void Error(int status)
1491 {
1492         signal(SIGALRM, SIG_IGN);
1493         signal(SIGPIPE, SIG_IGN);
1494         signal(SIGTERM, SIG_IGN);
1495         signal(SIGABRT, SIG_IGN);
1496         signal(SIGSEGV, SIG_IGN);
1497         signal(SIGURG, SIG_IGN);
1498         signal(SIGKILL, SIG_IGN);
1499         log(DEFAULT,"*** fell down a pothole in the road to perfection ***");
1500         send_error("Error! Segmentation fault! save meeeeeeeeeeeeee *splat!*");
1501         Exit(status);
1502 }
1503
1504 // this function counts all users connected, wether they are registered or NOT.
1505 int usercnt(void)
1506 {
1507         return clientlist.size();
1508 }
1509
1510 // this counts only registered users, so that the percentages in /MAP don't mess up when users are sitting in an unregistered state
1511 int registered_usercount(void)
1512 {
1513         int c = 0;
1514
1515         for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1516         {
1517                 if (i->second->registered == 7) c++;
1518         }
1519
1520         return c;
1521 }
1522
1523 int usercount_invisible(void)
1524 {
1525         int c = 0;
1526
1527         for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1528         {
1529                 if ((i->second->registered == 7) && (strchr(i->second->modes,'i')))
1530                         c++;
1531         }
1532
1533         return c;
1534 }
1535
1536 int usercount_opers(void)
1537 {
1538         int c = 0;
1539
1540         for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1541         {
1542                 if (i->second->oper)
1543                         c++;
1544         }
1545         return c;
1546 }
1547
1548 int usercount_unknown(void)
1549 {
1550         int c = 0;
1551
1552         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1553         {
1554                 userrec* t = (userrec*)(*i);
1555                 if (t->registered != 7)
1556                         c++;
1557         }
1558
1559         return c;
1560 }
1561
1562 long chancount(void)
1563 {
1564         return chanlist.size();
1565 }
1566
1567 long local_count()
1568 {
1569         int c = 0;
1570
1571         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1572         {
1573                 userrec* t = (userrec*)(*i);
1574                 if (t->registered == 7)
1575                         c++;
1576         }
1577
1578         return c;
1579 }
1580
1581 void ShowMOTD(userrec *user)
1582 {
1583         static char mbuf[MAXBUF];
1584         static char crud[MAXBUF];
1585         std::string WholeMOTD = "";
1586
1587         if (!Config->MOTD.size())
1588         {
1589                 WriteServ(user->fd,"422 %s :Message of the day file is missing.",user->nick);
1590                 return;
1591         }
1592
1593         snprintf(crud,MAXBUF,":%s 372 %s :- ", Config->ServerName, user->nick);
1594         snprintf(mbuf,MAXBUF,":%s 375 %s :- %s message of the day\r\n", Config->ServerName, user->nick, Config->ServerName);
1595         WholeMOTD = WholeMOTD + mbuf;
1596
1597         for (unsigned int i = 0; i < Config->MOTD.size(); i++)
1598                 WholeMOTD = WholeMOTD + std::string(crud) + Config->MOTD[i].c_str() + std::string("\r\n");
1599
1600         snprintf(mbuf,MAXBUF,":%s 376 %s :End of message of the day.\r\n", Config->ServerName, user->nick);
1601         WholeMOTD = WholeMOTD + mbuf;
1602
1603         // only one write operation
1604         if (Config->GetIOHook(user->port))
1605         {
1606                 try
1607                 {
1608                         Config->GetIOHook(user->port)->OnRawSocketWrite(user->fd,(char*)WholeMOTD.c_str(),WholeMOTD.length());
1609                 }
1610                 catch (ModuleException& modexcept)
1611                 {
1612                         log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
1613                 }
1614         }
1615         else
1616         {
1617                 user->AddWriteBuf(WholeMOTD);
1618         }
1619
1620         ServerInstance->stats->statsSent += WholeMOTD.length();
1621 }
1622
1623 void ShowRULES(userrec *user)
1624 {
1625         if (!Config->RULES.size())
1626         {
1627                 WriteServ(user->fd,"NOTICE %s :Rules file is missing.",user->nick);
1628                 return;
1629         }
1630         WriteServ(user->fd,"NOTICE %s :%s rules",user->nick,Config->ServerName);
1631
1632         for (unsigned int i = 0; i < Config->RULES.size(); i++)
1633                 WriteServ(user->fd,"NOTICE %s :%s",user->nick,Config->RULES[i].c_str());
1634
1635         WriteServ(user->fd,"NOTICE %s :End of %s rules.",user->nick,Config->ServerName);
1636 }
1637
1638 // this returns 1 when all modules are satisfied that the user should be allowed onto the irc server
1639 // (until this returns true, a user will block in the waiting state, waiting to connect up to the
1640 // registration timeout maximum seconds)
1641 bool AllModulesReportReady(userrec* user)
1642 {
1643         if (!Config->global_implementation[I_OnCheckReady])
1644                 return true;
1645
1646         for (int i = 0; i <= MODCOUNT; i++)
1647         {
1648                 if (Config->implement_lists[i][I_OnCheckReady])
1649                 {
1650                         int res = modules[i]->OnCheckReady(user);
1651                         if (!res)
1652                                 return false;
1653                 }
1654         }
1655
1656         return true;
1657 }
1658
1659 bool DirValid(char* dirandfile)
1660 {
1661         char work[MAXBUF];
1662         char buffer[MAXBUF];
1663         char otherdir[MAXBUF];
1664         int p;
1665
1666         strlcpy(work, dirandfile, MAXBUF);
1667         p = strlen(work);
1668
1669         // we just want the dir
1670         while (*work)
1671         {
1672                 if (work[p] == '/')
1673                 {
1674                         work[p] = '\0';
1675                         break;
1676                 }
1677
1678                 work[p--] = '\0';
1679         }
1680
1681         // Get the current working directory
1682         if (getcwd(buffer, MAXBUF ) == NULL )
1683                 return false;
1684
1685         chdir(work);
1686
1687         if (getcwd(otherdir, MAXBUF ) == NULL )
1688                 return false;
1689
1690         chdir(buffer);
1691
1692         size_t t = strlen(work);
1693
1694         if (strlen(otherdir) >= t)
1695         {
1696                 otherdir[t] = '\0';
1697
1698                 if (!strcmp(otherdir,work))
1699                 {
1700                         return true;
1701                 }
1702
1703                 return false;
1704         }
1705         else
1706         {
1707                 return false;
1708         }
1709 }
1710
1711 std::string GetFullProgDir(char** argv, int argc)
1712 {
1713         char work[MAXBUF];
1714         char buffer[MAXBUF];
1715         char otherdir[MAXBUF];
1716         int p;
1717
1718         strlcpy(work,argv[0],MAXBUF);
1719         p = strlen(work);
1720
1721         // we just want the dir
1722         while (*work)
1723         {
1724                 if (work[p] == '/')
1725                 {
1726                         work[p] = '\0';
1727                         break;
1728                 }
1729
1730                 work[p--] = '\0';
1731         }
1732
1733         // Get the current working directory
1734         if (getcwd(buffer, MAXBUF) == NULL)
1735                 return "";
1736
1737         chdir(work);
1738
1739         if (getcwd(otherdir, MAXBUF) == NULL)
1740                 return "";
1741
1742         chdir(buffer);
1743         return otherdir;
1744 }
1745
1746 int InsertMode(std::string &output, const char* mode, unsigned short section)
1747 {
1748         unsigned short currsection = 1;
1749         unsigned int pos = output.find("CHANMODES=", 0) + 10; // +10 for the length of "CHANMODES="
1750         
1751         if(section > 4 || section == 0)
1752         {
1753                 log(DEBUG, "InsertMode: CHANMODES doesn't have a section %dh :/", section);
1754                 return 0;
1755         }
1756         
1757         for(; pos < output.size(); pos++)
1758         {
1759                 if(section == currsection)
1760                         break;
1761                         
1762                 if(output[pos] == ',')
1763                         currsection++;
1764         }
1765         
1766         output.insert(pos, mode);
1767         return 1;
1768 }
1769
1770 bool IsValidChannelName(const char *chname)
1771 {
1772         char *c;
1773
1774         /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
1775         if (!chname || *chname != '#')
1776         {
1777                 return false;
1778         }
1779
1780         c = (char *)chname + 1;
1781         while (*c)
1782         {
1783                 switch (*c)
1784                 {
1785                         case ' ':
1786                         case ',':
1787                         case 7:
1788                                 return false;
1789                 }
1790
1791                 c++;
1792         }
1793                 
1794         /* too long a name - note funky pointer arithmetic here. */
1795         if ((c - chname) > CHANMAX)
1796         {
1797                         return false;
1798         }
1799
1800         return true;
1801 }
1802
1803 inline int charlcat(char* x,char y,int z)
1804 {
1805         char* x__n = x;
1806         int v = 0;
1807
1808         while(*x__n++)
1809                 v++;
1810
1811         if (v < z - 1)
1812         {
1813                 *--x__n = y;
1814                 *++x__n = 0;
1815         }
1816
1817         return v;
1818 }
1819
1820 bool charremove(char* mp, char remove)
1821 {
1822         char* mptr = mp;
1823         bool shift_down = false;
1824
1825         while (*mptr)
1826         {
1827                 if (*mptr == remove)
1828                 shift_down = true;
1829
1830                 if (shift_down)
1831                         *mptr = *(mptr+1);
1832
1833                 *mptr++;
1834         }
1835
1836         return shift_down;
1837 }
1838