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