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