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