]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/helperfuncs.cpp
Removed deprecated direct reference to file_cache MOTD, RULES
[user/henk/code/inspircd.git] / src / helperfuncs.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  Inspire is copyright (C) 2002-2004 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 "inspircd_util.h"
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include <sys/errno.h>
26 #include <time.h>
27 #include <string>
28 #ifdef GCC3
29 #include <ext/hash_map>
30 #else
31 #include <hash_map>
32 #endif
33 #include <sstream>
34 #include <vector>
35 #include <deque>
36 #include <stdarg.h>
37 #include "connection.h"
38 #include "users.h"
39 #include "ctables.h"
40 #include "globals.h"
41 #include "modules.h"
42 #include "dynamic.h"
43 #include "wildcard.h"
44 #include "message.h"
45 #include "mode.h"
46 #include "xline.h"
47 #include "commands.h"
48 #include "inspstring.h"
49 #include "helperfuncs.h"
50 #include "hashcomp.h"
51 #include "typedefs.h"
52
53 extern int MODCOUNT;
54 extern std::vector<Module*> modules;
55 extern ServerConfig *Config;
56 extern time_t TIME;
57 extern char lowermap[255];
58 static char list[MAXBUF];
59 extern userrec* fd_ref_table[65536];
60 extern serverstats* stats;
61 static char already_sent[65536];
62 extern std::vector<userrec*> all_opers;
63 extern ClassVector Classes;
64 extern user_hash clientlist;
65 extern chan_hash chanlist;
66 extern command_table cmdlist;
67
68 void log(int level,char *text, ...)
69 {
70         char textbuffer[MAXBUF];
71         va_list argsPtr;
72         time_t rawtime;
73         struct tm * timeinfo;
74         if (level < Config->LogLevel)
75                 return;
76
77         time(&rawtime);
78         timeinfo = localtime(&rawtime);
79
80         if (Config->log_file)
81         {
82                 char b[MAXBUF];
83                 va_start (argsPtr, text);
84                 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
85                 va_end(argsPtr);
86                 strlcpy(b,asctime(timeinfo),MAXBUF);
87                 b[24] = ':';    // we know this is the end of the time string
88                 if (Config->log_file)
89                         fprintf(Config->log_file,"%s %s\n",b,textbuffer);
90                 if (Config->nofork)
91                 {
92                         // nofork enabled? display it on terminal too
93                         printf("%s %s\n",b,textbuffer);
94                 }
95         }
96 }
97
98 void readfile(file_cache &F, const char* fname)
99 {
100         FILE* file;
101         char linebuf[MAXBUF];
102
103         log(DEBUG,"readfile: loading %s",fname);
104         F.clear();
105         file =  fopen(fname,"r");
106         if (file)
107         {
108                 while (!feof(file))
109                 {
110                         fgets(linebuf,sizeof(linebuf),file);
111                         linebuf[strlen(linebuf)-1]='\0';
112                         if (!*linebuf)
113                         {
114                                 strcpy(linebuf,"  ");
115                         }
116                         if (!feof(file))
117                         {
118                                 F.push_back(linebuf);
119                         }
120                 }
121                 fclose(file);
122         }
123         else
124         {
125                 log(DEBUG,"readfile: failed to load file: %s",fname);
126         }
127         log(DEBUG,"readfile: loaded %s, %lu lines",fname,(unsigned long)F.size());
128 }
129
130 void Write(int sock,char *text, ...)
131 {
132         if (sock < 0)
133                 return;
134         if (!text)
135         {
136                 log(DEFAULT,"*** BUG *** Write was given an invalid parameter");
137                 return;
138         }
139         char textbuffer[MAXBUF];
140         va_list argsPtr;
141         char tb[MAXBUF];
142
143         va_start (argsPtr, text);
144         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
145         va_end(argsPtr);
146         int bytes = snprintf(tb,MAXBUF,"%s\r\n",textbuffer);
147         chop(tb);
148         if (fd_ref_table[sock])
149         {
150                 int MOD_RESULT = 0;
151                 FOREACH_RESULT(OnRawSocketWrite(sock,tb,bytes));
152                 fd_ref_table[sock]->AddWriteBuf(tb);
153                 stats->statsSent += bytes;
154         }
155         else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
156 }
157
158 /* write a server formatted numeric response to a single socket */
159
160 void WriteServ(int sock, char* text, ...)
161 {
162         if (sock < 0)
163                 return;
164         if (!text)
165         {
166                 log(DEFAULT,"*** BUG *** WriteServ was given an invalid parameter");
167                 return;
168         }
169         char textbuffer[MAXBUF],tb[MAXBUF];
170         va_list argsPtr;
171         va_start (argsPtr, text);
172
173         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
174         va_end(argsPtr);
175         int bytes = snprintf(tb,MAXBUF,":%s %s\r\n",Config->ServerName,textbuffer);
176         chop(tb);
177         if (fd_ref_table[sock])
178         {
179                 int MOD_RESULT = 0;
180                 FOREACH_RESULT(OnRawSocketWrite(sock,tb,bytes));
181                 fd_ref_table[sock]->AddWriteBuf(tb);
182                 stats->statsSent += bytes;
183         }
184         else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
185 }
186
187 /* write text from an originating user to originating user */
188
189 void WriteFrom(int sock, userrec *user,char* text, ...)
190 {
191         if (sock < 0)
192                 return;
193         if ((!text) || (!user))
194         {
195                 log(DEFAULT,"*** BUG *** WriteFrom was given an invalid parameter");
196                 return;
197         }
198         char textbuffer[MAXBUF],tb[MAXBUF];
199         va_list argsPtr;
200         va_start (argsPtr, text);
201
202         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
203         va_end(argsPtr);
204         int bytes = snprintf(tb,MAXBUF,":%s!%s@%s %s\r\n",user->nick,user->ident,user->dhost,textbuffer);
205         chop(tb);
206         if (fd_ref_table[sock])
207         {
208                 int MOD_RESULT = 0;
209                 FOREACH_RESULT(OnRawSocketWrite(sock,tb,bytes));
210                 fd_ref_table[sock]->AddWriteBuf(tb);
211                 stats->statsSent += bytes;
212         }
213         else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
214 }
215
216 /* write text to an destination user from a source user (e.g. user privmsg) */
217
218 void WriteTo(userrec *source, userrec *dest,char *data, ...)
219 {
220         if ((!dest) || (!data))
221         {
222                 log(DEFAULT,"*** BUG *** WriteTo was given an invalid parameter");
223                 return;
224         }
225         if (dest->fd == FD_MAGIC_NUMBER)
226                 return;
227         char textbuffer[MAXBUF],tb[MAXBUF];
228         va_list argsPtr;
229         va_start (argsPtr, data);
230         vsnprintf(textbuffer, MAXBUF, data, argsPtr);
231         va_end(argsPtr);
232         chop(tb);
233
234         // if no source given send it from the server.
235         if (!source)
236         {
237                 WriteServ(dest->fd,":%s %s",Config->ServerName,textbuffer);
238         }
239         else
240         {
241                 WriteFrom(dest->fd,source,"%s",textbuffer);
242         }
243 }
244
245 /* write formatted text from a source user to all users on a channel
246  * including the sender (NOT for privmsg, notice etc!) */
247
248 void WriteChannel(chanrec* Ptr, userrec* user, char* text, ...)
249 {
250         if ((!Ptr) || (!user) || (!text))
251         {
252                 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
253                 return;
254         }
255         char textbuffer[MAXBUF];
256         va_list argsPtr;
257         va_start (argsPtr, text);
258         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
259         va_end(argsPtr);
260
261         std::vector<char*> *ulist = Ptr->GetUsers();
262         for (unsigned int j = 0; j < ulist->size(); j++)
263         {
264                 char* o = (*ulist)[j];
265                 userrec* otheruser = (userrec*)o;
266                 if (otheruser->fd != FD_MAGIC_NUMBER)
267                         WriteTo(user,otheruser,"%s",textbuffer);
268         }
269 }
270
271 /* write formatted text from a source user to all users on a channel
272  * including the sender (NOT for privmsg, notice etc!) doesnt send to
273  * users on remote servers */
274
275 void WriteChannelLocal(chanrec* Ptr, userrec* user, char* text, ...)
276 {
277         if ((!Ptr) || (!text))
278         {
279                 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
280                 return;
281         }
282         char textbuffer[MAXBUF];
283         va_list argsPtr;
284         va_start (argsPtr, text);
285         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
286         va_end(argsPtr);
287
288         std::vector<char*> *ulist = Ptr->GetUsers();
289         for (unsigned int j = 0; j < ulist->size(); j++)
290         {
291                 char* o = (*ulist)[j];
292                 userrec* otheruser = (userrec*)o;
293                 if ((otheruser->fd != FD_MAGIC_NUMBER) && (otheruser->fd != -1) && (otheruser != user))
294                 {
295                         if (!user)
296                         {
297                                 WriteServ(otheruser->fd,"%s",textbuffer);
298                         }
299                         else
300                         {
301                                 WriteTo(user,otheruser,"%s",textbuffer);
302                         }
303                 }
304         }
305 }
306
307 void WriteChannelWithServ(char* ServName, chanrec* Ptr, char* text, ...)
308 {
309         if ((!Ptr) || (!text))
310         {
311                 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
312                 return;
313         }
314         char textbuffer[MAXBUF];
315         va_list argsPtr;
316         va_start (argsPtr, text);
317         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
318         va_end(argsPtr);
319
320
321         std::vector<char*> *ulist = Ptr->GetUsers();
322         for (unsigned int j = 0; j < ulist->size(); j++)
323         {
324                 char* o = (*ulist)[j];
325                 userrec* otheruser = (userrec*)o;
326                 if (otheruser->fd != FD_MAGIC_NUMBER)
327                         WriteServ(otheruser->fd,"%s",textbuffer);
328         }
329 }
330
331 /* write formatted text from a source user to all users on a channel except
332  * for the sender (for privmsg etc) */
333
334 void ChanExceptSender(chanrec* Ptr, userrec* user, char* text, ...)
335 {
336         if ((!Ptr) || (!user) || (!text))
337         {
338                 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
339                 return;
340         }
341         char textbuffer[MAXBUF];
342         va_list argsPtr;
343         va_start (argsPtr, text);
344         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
345         va_end(argsPtr);
346
347         std::vector<char*> *ulist = Ptr->GetUsers();
348         for (unsigned int j = 0; j < ulist->size(); j++)
349         {
350                 char* o = (*ulist)[j];
351                 userrec* otheruser = (userrec*)o;
352                 if ((otheruser->fd != FD_MAGIC_NUMBER) && (user != otheruser))
353                         WriteFrom(otheruser->fd,user,"%s",textbuffer);
354         }
355 }
356
357 std::string GetServerDescription(char* servername)
358 {
359         std::string description = "";
360         FOREACH_MOD OnGetServerDescription(servername,description);
361         if (description != "")
362         {
363                 return description;
364         }
365         else
366         {
367                 return Config->ServerDesc; // not a remote server that can be found, it must be me.
368         }
369 }
370
371 /* write a formatted string to all users who share at least one common
372  * channel, including the source user e.g. for use in NICK */
373
374 void WriteCommon(userrec *u, char* text, ...)
375 {
376         if (!u)
377         {
378                 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
379                 return;
380         }
381
382         if (u->registered != 7) {
383                 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
384                 return;
385         }
386
387         char textbuffer[MAXBUF];
388         va_list argsPtr;
389         va_start (argsPtr, text);
390         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
391         va_end(argsPtr);
392
393         // FIX: Stops a message going to the same person more than once
394         memset(&already_sent,0,65536);
395
396         bool sent_to_at_least_one = false;
397
398         for (unsigned int i = 0; i < u->chans.size(); i++)
399         {
400                 if (u->chans[i].channel)
401                 {
402                         std::vector<char*> *ulist = u->chans[i].channel->GetUsers();
403                         for (unsigned int j = 0; j < ulist->size(); j++)
404                         {
405                                 char* o = (*ulist)[j];
406                                 userrec* otheruser = (userrec*)o;
407                                 if ((otheruser->fd > 0) && (!already_sent[otheruser->fd]))
408                                 {
409                                         already_sent[otheruser->fd] = 1;
410                                         WriteFrom(otheruser->fd,u,"%s",textbuffer);
411                                         sent_to_at_least_one = true;
412                                 }
413                         }
414                 }
415         }
416         // if the user was not in any channels, no users will receive the text. Make sure the user
417         // receives their OWN message for WriteCommon
418         if (!sent_to_at_least_one)
419         {
420                 WriteFrom(u->fd,u,"%s",textbuffer);
421         }
422 }
423
424 /* write a formatted string to all users who share at least one common
425  * channel, NOT including the source user e.g. for use in QUIT */
426
427 void WriteCommonExcept(userrec *u, char* text, ...)
428 {
429         if (!u)
430         {
431                 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
432                 return;
433         }
434
435         if (u->registered != 7) {
436                 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
437                 return;
438         }
439
440         char textbuffer[MAXBUF];
441         va_list argsPtr;
442         va_start (argsPtr, text);
443         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
444         va_end(argsPtr);
445
446         memset(&already_sent,0,65536);
447
448         for (unsigned int i = 0; i < u->chans.size(); i++)
449         {
450                 if (u->chans[i].channel)
451                 {
452                         std::vector<char*> *ulist = u->chans[i].channel->GetUsers();
453                         for (unsigned int j = 0; j < ulist->size(); j++)
454                         {
455                                 char* o = (*ulist)[j];
456                                 userrec* otheruser = (userrec*)o;
457                                 if (u != otheruser)
458                                 {
459                                         if ((otheruser->fd > 0) && (!already_sent[otheruser->fd]))
460                                         {
461                                                 already_sent[otheruser->fd] = 1;
462                                                 WriteFrom(otheruser->fd,u,"%s",textbuffer);
463                                         }
464                                 }
465                         }
466                 }
467         }
468 }
469
470 void WriteOpers(char* text, ...)
471 {
472         if (!text)
473         {
474                 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
475                 return;
476         }
477
478         char textbuffer[MAXBUF];
479         va_list argsPtr;
480         va_start (argsPtr, text);
481         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
482         va_end(argsPtr);
483
484         for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
485         {
486                 userrec* a = *i;
487                 if ((a) && (a->fd != FD_MAGIC_NUMBER))
488                 {
489                         if (strchr(a->modes,'s'))
490                         {
491                                 // send server notices to all with +s
492                                 WriteServ(a->fd,"NOTICE %s :%s",a->nick,textbuffer);
493                         }
494                 }
495         }
496 }
497
498 void ServerNoticeAll(char* text, ...)
499 {
500         if (!text)
501                 return;
502
503         char textbuffer[MAXBUF];
504         va_list argsPtr;
505         va_start (argsPtr, text);
506         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
507         va_end(argsPtr);
508
509         for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
510         {
511                 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
512                 {
513                         WriteServ(i->second->fd,"NOTICE $%s :%s",Config->ServerName,textbuffer);
514                 }
515         }
516 }
517
518 void ServerPrivmsgAll(char* text, ...)
519 {
520         if (!text)
521                 return;
522
523         char textbuffer[MAXBUF];
524         va_list argsPtr;
525         va_start (argsPtr, text);
526         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
527         va_end(argsPtr);
528
529         for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
530         {
531                 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
532                 {
533                         WriteServ(i->second->fd,"PRIVMSG $%s :%s",Config->ServerName,textbuffer);
534                 }
535         }
536 }
537
538 void NoticeAllOpers(userrec *source, bool local_only, char* text, ...)
539 {
540         if ((!text) || (!source))
541         {
542                 log(DEFAULT,"*** BUG *** NoticeAllOpers was given an invalid parameter");
543                 return;
544         }
545
546         char textbuffer[MAXBUF];
547         va_list argsPtr;
548         va_start (argsPtr, text);
549         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
550         va_end(argsPtr);
551
552         for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
553         {
554                 userrec* a = *i;
555                 if ((a) && (a->fd != FD_MAGIC_NUMBER))
556                 {
557                         if (strchr(a->modes,'s'))
558                         {
559                                 // send server notices to all with +s
560                                 WriteServ(a->fd,"NOTICE %s :*** Notice From %s: %s",a->nick,source->nick,textbuffer);
561                         }
562                 }
563         }
564
565 }
566 // returns TRUE of any users on channel C occupy server 'servername'.
567
568 bool ChanAnyOnThisServer(chanrec *c,char* servername)
569 {
570         log(DEBUG,"ChanAnyOnThisServer");
571
572         std::vector<char*> *ulist = c->GetUsers();
573         for (unsigned int j = 0; j < ulist->size(); j++)
574         {
575                 char* o = (*ulist)[j];
576                 userrec* user = (userrec*)o;
577                 if (!strcasecmp(user->server,servername))
578                         return true;
579         }
580         return false;
581 }
582
583 // returns true if user 'u' shares any common channels with any users on server 'servername'
584
585 bool CommonOnThisServer(userrec* u,const char* servername)
586 {
587         log(DEBUG,"ChanAnyOnThisServer");
588
589         for (unsigned int i = 0; i < u->chans.size(); i++)
590         {
591                 if (u->chans[i].channel)
592                 {
593                         std::vector<char*> *ulist = u->chans[i].channel->GetUsers();
594                         for (unsigned int j = 0; j < ulist->size(); j++)
595                         {
596                                 char* o = (*ulist)[j];
597                                 userrec* user = (userrec*)o;
598                                 if (!strcasecmp(user->server,servername))
599                                         return true;
600                         }
601                 }
602         }
603         return false;
604 }
605
606
607 void WriteMode(const char* modes, int flags, const char* text, ...)
608 {
609         if ((!text) || (!modes) || (!flags))
610         {
611                 log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
612                 return;
613         }
614
615         char textbuffer[MAXBUF];
616         va_list argsPtr;
617         va_start (argsPtr, text);
618         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
619         va_end(argsPtr);
620         int modelen = strlen(modes);
621
622         for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
623         {
624                 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
625                 {
626                         bool send_to_user = false;
627
628                         if (flags == WM_AND)
629                         {
630                                 send_to_user = true;
631                                 for (int n = 0; n < modelen; n++)
632                                 {
633                                         if (!hasumode(i->second,modes[n]))
634                                         {
635                                                 send_to_user = false;
636                                                 break;
637                                         }
638                                 }
639                         }
640                         else if (flags == WM_OR)
641                         {
642                                 send_to_user = false;
643                                 for (int n = 0; n < modelen; n++)
644                                 {
645                                         if (hasumode(i->second,modes[n]))
646                                         {
647                                                 send_to_user = true;
648                                                 break;
649                                         }
650                                 }
651                         }
652
653                         if (send_to_user)
654                         {
655                                 WriteServ(i->second->fd,"NOTICE %s :%s",i->second->nick,textbuffer);
656                         }
657                 }
658         }
659 }
660
661 void NoticeAll(userrec *source, bool local_only, char* text, ...)
662 {
663         if ((!text) || (!source))
664         {
665                 log(DEFAULT,"*** BUG *** NoticeAll was given an invalid parameter");
666                 return;
667         }
668
669         char textbuffer[MAXBUF];
670         va_list argsPtr;
671         va_start (argsPtr, text);
672         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
673         va_end(argsPtr);
674
675         for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
676         {
677                 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
678                 {
679                         WriteFrom(i->second->fd,source,"NOTICE $* :%s",textbuffer);
680                 }
681         }
682
683 }
684
685
686 void WriteWallOps(userrec *source, bool local_only, char* text, ...)
687 {
688         if ((!text) || (!source))
689         {
690                 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
691                 return;
692         }
693
694         char textbuffer[MAXBUF];
695         va_list argsPtr;
696         va_start (argsPtr, text);
697         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
698         va_end(argsPtr);
699
700         for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
701         {
702                 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
703                 {
704                         if (strchr(i->second->modes,'w'))
705                         {
706                                 WriteTo(source,i->second,"WALLOPS :%s",textbuffer);
707                         }
708                 }
709         }
710 }
711
712 /* convert a string to lowercase. Note following special circumstances
713  * taken from RFC 1459. Many "official" server branches still hold to this
714  * rule so i will too;
715  *
716  *  Because of IRC's scandanavian origin, the characters {}| are
717  *  considered to be the lower case equivalents of the characters []\,
718  *  respectively. This is a critical issue when determining the
719  *  equivalence of two nicknames.
720  */
721
722 void strlower(char *n)
723 {
724         if (n)
725         {
726                 for (char* t = n; *t; t++)
727                         *t = lowermap[(unsigned)*t];
728         }
729 }
730
731 /* Find a user record by nickname and return a pointer to it */
732
733 userrec* Find(std::string nick)
734 {
735         user_hash::iterator iter = clientlist.find(nick);
736
737         if (iter == clientlist.end())
738                 /* Couldn't find it */
739                 return NULL;
740
741         return iter->second;
742 }
743
744 /* find a channel record by channel name and return a pointer to it */
745
746 chanrec* FindChan(const char* chan)
747 {
748         if (!chan)
749         {
750                 log(DEFAULT,"*** BUG *** Findchan was given an invalid parameter");
751                 return NULL;
752         }
753
754         chan_hash::iterator iter = chanlist.find(chan);
755
756         if (iter == chanlist.end())
757                 /* Couldn't find it */
758                 return NULL;
759
760         return iter->second;
761 }
762
763
764 long GetMaxBans(char* name)
765 {
766         char CM[MAXBUF];
767         for (int count = 0; count < ConfValueEnum("banlist",&Config->config_f); count++)
768         {
769                 ConfValue("banlist","chan",count,CM,&Config->config_f);
770                 if (match(name,CM))
771                 {
772                         ConfValue("banlist","limit",count,CM,&Config->config_f);
773                         return atoi(CM);
774                 }
775         }
776         return 64;
777 }
778
779 void purge_empty_chans(userrec* u)
780 {
781
782         int go_again = 1, purge = 0;
783
784         // firstly decrement the count on each channel
785         for (unsigned int f = 0; f < u->chans.size(); f++)
786         {
787                 if (u->chans[f].channel)
788                 {
789                         u->chans[f].channel->DelUser((char*)u);
790                 }
791         }
792
793         for (unsigned int i = 0; i < u->chans.size(); i++)
794         {
795                 if (u->chans[i].channel)
796                 {
797                         if (!usercount(u->chans[i].channel))
798                         {
799                                 chan_hash::iterator i2 = chanlist.find(u->chans[i].channel->name);
800                                 /* kill the record */
801                                 if (i2 != chanlist.end())
802                                 {
803                                         log(DEBUG,"del_channel: destroyed: %s",i2->second->name);
804                                         if (i2->second)
805                                                 delete i2->second;
806                                         chanlist.erase(i2);
807                                         go_again = 1;
808                                         purge++;
809                                         u->chans[i].channel = NULL;
810                                 }
811                         }
812                         else
813                         {
814                                 log(DEBUG,"skipped purge for %s",u->chans[i].channel->name);
815                         }
816                 }
817         }
818         log(DEBUG,"completed channel purge, killed %lu",(unsigned long)purge);
819
820         DeleteOper(u);
821 }
822
823
824 char scratch[MAXBUF];
825 char sparam[MAXBUF];
826
827 char* chanmodes(chanrec *chan)
828 {
829         if (!chan)
830         {
831                 log(DEFAULT,"*** BUG *** chanmodes was given an invalid parameter");
832                 strcpy(scratch,"");
833                 return scratch;
834         }
835
836         strcpy(scratch,"");
837         strcpy(sparam,"");
838         if (chan->binarymodes & CM_NOEXTERNAL)
839         {
840                 strlcat(scratch,"n",MAXMODES);
841         }
842         if (chan->binarymodes & CM_TOPICLOCK)
843         {
844                 strlcat(scratch,"t",MAXMODES);
845         }
846         if (*chan->key)
847         {
848                 strlcat(scratch,"k",MAXMODES);
849         }
850         if (chan->limit)
851         {
852                 strlcat(scratch,"l",MAXMODES);
853         }
854         if (chan->binarymodes & CM_INVITEONLY)
855         {
856                 strlcat(scratch,"i",MAXMODES);
857         }
858         if (chan->binarymodes & CM_MODERATED)
859         {
860                 strlcat(scratch,"m",MAXMODES);
861         }
862         if (chan->binarymodes & CM_SECRET)
863         {
864                 strlcat(scratch,"s",MAXMODES);
865         }
866         if (chan->binarymodes & CM_PRIVATE)
867         {
868                 strlcat(scratch,"p",MAXMODES);
869         }
870         if (*chan->key)
871         {
872                 strlcat(sparam," ",MAXBUF);
873                 strlcat(sparam,chan->key,MAXBUF);
874         }
875         if (chan->limit)
876         {
877                 char foo[24];
878                 sprintf(foo," %lu",(unsigned long)chan->limit);
879                 strlcat(sparam,foo,MAXBUF);
880         }
881         if (*chan->custom_modes)
882         {
883                 strlcat(scratch,chan->custom_modes,MAXMODES);
884                 for (int z = 0; chan->custom_modes[z] != 0; z++)
885                 {
886                         std::string extparam = chan->GetModeParameter(chan->custom_modes[z]);
887                         if (extparam != "")
888                         {
889                                 strlcat(sparam," ",MAXBUF);
890                                 strlcat(sparam,extparam.c_str(),MAXBUF);
891                         }
892                 }
893         }
894         log(DEBUG,"chanmodes: %s %s%s",chan->name,scratch,sparam);
895         strlcat(scratch,sparam,MAXMODES);
896         return scratch;
897 }
898
899
900 /* compile a userlist of a channel into a string, each nick seperated by
901  * spaces and op, voice etc status shown as @ and + */
902
903 void userlist(userrec *user,chanrec *c)
904 {
905         if ((!c) || (!user))
906         {
907                 log(DEFAULT,"*** BUG *** userlist was given an invalid parameter");
908                 return;
909         }
910
911         snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
912
913         std::vector<char*> *ulist = c->GetUsers();
914         for (unsigned int i = 0; i < ulist->size(); i++)
915         {
916                 char* o = (*ulist)[i];
917                 userrec* otheruser = (userrec*)o;
918                 if ((!has_channel(user,c)) && (strchr(otheruser->modes,'i')))
919                 {
920                         /* user is +i, and source not on the channel, does not show
921                          * nick in NAMES list */
922                         continue;
923                 }
924                 strlcat(list,cmode(otheruser,c),MAXBUF);
925                 strlcat(list,otheruser->nick,MAXBUF);
926                 strlcat(list," ",MAXBUF);
927                 if (strlen(list)>(480-NICKMAX))
928                 {
929                         /* list overflowed into
930                          * multiple numerics */
931                         WriteServ(user->fd,"%s",list);
932                         snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
933                 }
934         }
935         /* if whats left in the list isnt empty, send it */
936         if (list[strlen(list)-1] != ':')
937         {
938                 WriteServ(user->fd,"%s",list);
939         }
940 }
941
942 /* return a count of the users on a specific channel accounting for
943  * invisible users who won't increase the count. e.g. for /LIST */
944
945 int usercount_i(chanrec *c)
946 {
947         int count = 0;
948
949         if (!c)
950         {
951                 log(DEFAULT,"*** BUG *** usercount_i was given an invalid parameter");
952                 return 0;
953         }
954
955         for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
956         {
957                 if (i->second)
958                 {
959                         if (has_channel(i->second,c))
960                         {
961                                 if (isnick(i->second->nick))
962                                 {
963                                         if ((!has_channel(i->second,c)) && (strchr(i->second->modes,'i')))
964                                         {
965                                                 /* user is +i, and source not on the channel, does not show
966                                                  * nick in NAMES list */
967                                                 continue;
968                                         }
969                                         count++;
970                                 }
971                         }
972                 }
973         }
974         log(DEBUG,"usercount_i: %s %lu",c->name,(unsigned long)count);
975         return count;
976 }
977
978
979 int usercount(chanrec *c)
980 {
981         if (!c)
982         {
983                 log(DEFAULT,"*** BUG *** usercount was given an invalid parameter");
984                 return 0;
985         }
986         int count = c->GetUserCounter();
987         log(DEBUG,"usercount: %s %lu",c->name,(unsigned long)count);
988         return count;
989 }
990
991
992 // looks up a users password for their connection class (<ALLOW>/<DENY> tags)
993
994 char* Passwd(userrec *user)
995 {
996         for (ClassVector::iterator i = Classes.begin(); i != Classes.end(); i++)
997         {
998                 if (match(user->host,i->host) && (i->type == CC_ALLOW))
999                 {
1000                         return i->pass;
1001                 }
1002         }
1003         return "";
1004 }
1005
1006 bool IsDenied(userrec *user)
1007 {
1008         for (ClassVector::iterator i = Classes.begin(); i != Classes.end(); i++)
1009         {
1010                 if (match(user->host,i->host) && (i->type == CC_DENY))
1011                 {
1012                         return true;
1013                 }
1014         }
1015         return false;
1016 }
1017
1018
1019
1020
1021 /* sends out an error notice to all connected clients (not to be used
1022  * lightly!) */
1023
1024 void send_error(char *s)
1025 {
1026         log(DEBUG,"send_error: %s",s);
1027         for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1028         {
1029                 if (isnick(i->second->nick))
1030                 {
1031                         WriteServ(i->second->fd,"NOTICE %s :%s",i->second->nick,s);
1032                 }
1033                 else
1034                 {
1035                         // fix - unregistered connections receive ERROR, not NOTICE
1036                         Write(i->second->fd,"ERROR :%s",s);
1037                 }
1038         }
1039 }
1040
1041 void Error(int status)
1042 {
1043         signal (SIGALRM, SIG_IGN);
1044         signal (SIGPIPE, SIG_IGN);
1045         signal (SIGTERM, SIG_IGN);
1046         signal (SIGABRT, SIG_IGN);
1047         signal (SIGSEGV, SIG_IGN);
1048         signal (SIGURG, SIG_IGN);
1049         signal (SIGKILL, SIG_IGN);
1050         log(DEFAULT,"*** fell down a pothole in the road to perfection ***");
1051         send_error("Error! Segmentation fault! save meeeeeeeeeeeeee *splat!*");
1052         Exit(status);
1053 }
1054
1055 // this function counts all users connected, wether they are registered or NOT.
1056 int usercnt(void)
1057 {
1058         return clientlist.size();
1059 }
1060
1061 // this counts only registered users, so that the percentages in /MAP don't mess up when users are sitting in an unregistered state
1062 int registered_usercount(void)
1063 {
1064         int c = 0;
1065         for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1066         {
1067                 if (i->second->registered == 7) c++;
1068         }
1069         return c;
1070 }
1071
1072 int usercount_invisible(void)
1073 {
1074         int c = 0;
1075         for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1076         {
1077                 if ((isnick(i->second->nick)) && (strchr(i->second->modes,'i'))) c++;
1078         }
1079         return c;
1080 }
1081
1082 int usercount_opers(void)
1083 {
1084         int c = 0;
1085         for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1086         {
1087                 if ((isnick(i->second->nick)) && (strchr(i->second->modes,'o'))) c++;
1088         }
1089         return c;
1090 }
1091
1092 int usercount_unknown(void)
1093 {
1094         int c = 0;
1095
1096         for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1097         {
1098                 if ((i->second->fd > -1) && (i->second->registered != 7))
1099                         c++;
1100         }
1101         return c;
1102 }
1103
1104 long chancount(void)
1105 {
1106         return chanlist.size();
1107 }
1108
1109 long local_count()
1110 {
1111         int c = 0;
1112         for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1113         {
1114                 if ((isnick(i->second->nick)) && (i->second->fd > -1)) c++;
1115         }
1116         return c;
1117 }
1118
1119 void ShowMOTD(userrec *user)
1120 {
1121         char buf[65536];
1122         std::string WholeMOTD = "";
1123         if (!MOTD.size())
1124         {
1125                 WriteServ(user->fd,"422 %s :Message of the day file is missing.",user->nick);
1126                 return;
1127         }
1128         snprintf(buf,65535,":%s 375 %s :- %s message of the day\r\n", Config->ServerName, user->nick, Config->ServerName);
1129         WholeMOTD = WholeMOTD + buf;
1130         for (unsigned int i = 0; i != MOTD.size(); i++)
1131         {
1132                 snprintf(buf,65535,":%s 372 %s :- %s\r\n", Config->ServerName, user->nick, MOTD[i].c_str());
1133                 WholeMOTD = WholeMOTD + buf;
1134         }
1135         snprintf(buf,65535,":%s 376 %s :End of message of the day.\r\n", Config->ServerName, user->nick);
1136         WholeMOTD = WholeMOTD + buf;
1137         // only one write operation
1138         user->AddWriteBuf(WholeMOTD);
1139         stats->statsSent += WholeMOTD.length();
1140 }
1141
1142 void ShowRULES(userrec *user)
1143 {
1144         if (!RULES.size())
1145         {
1146                 WriteServ(user->fd,"NOTICE %s :Rules file is missing.",user->nick);
1147                 return;
1148         }
1149         WriteServ(user->fd,"NOTICE %s :%s rules",user->nick,Config->ServerName);
1150         for (unsigned int i = 0; i != RULES.size(); i++)
1151         {
1152                                 WriteServ(user->fd,"NOTICE %s :%s",user->nick,RULES[i].c_str());
1153         }
1154         WriteServ(user->fd,"NOTICE %s :End of %s rules.",user->nick,Config->ServerName);
1155 }
1156
1157 // this returns 1 when all modules are satisfied that the user should be allowed onto the irc server
1158 // (until this returns true, a user will block in the waiting state, waiting to connect up to the
1159 // registration timeout maximum seconds)
1160 bool AllModulesReportReady(userrec* user)
1161 {
1162         for (int i = 0; i <= MODCOUNT; i++)
1163         {
1164                 int res = modules[i]->OnCheckReady(user);
1165                         if (!res)
1166                                 return false;
1167         }
1168         return true;
1169 }
1170
1171 void createcommand(char* cmd, handlerfunc f, char flags, int minparams,char* source)
1172 {
1173         command_t comm;
1174         /* create the command and push it onto the table */
1175         strlcpy(comm.command,cmd,MAXBUF);
1176         strlcpy(comm.source,source,MAXBUF);
1177         comm.handler_function = f;
1178         comm.flags_needed = flags;
1179         comm.min_params = minparams;
1180         comm.use_count = 0;
1181         comm.total_bytes = 0;
1182         cmdlist.push_back(comm);
1183         log(DEBUG,"Added command %s (%lu parameters)",cmd,(unsigned long)minparams);
1184 }
1185
1186
1187 void SetupCommandTable(void)
1188 {
1189         createcommand("USER",handle_user,0,4,"<core>");
1190         createcommand("NICK",handle_nick,0,1,"<core>");
1191         createcommand("QUIT",handle_quit,0,0,"<core>");
1192         createcommand("VERSION",handle_version,0,0,"<core>");
1193         createcommand("PING",handle_ping,0,1,"<core>");
1194         createcommand("PONG",handle_pong,0,1,"<core>");
1195         createcommand("ADMIN",handle_admin,0,0,"<core>");
1196         createcommand("PRIVMSG",handle_privmsg,0,2,"<core>");
1197         createcommand("INFO",handle_info,0,0,"<core>");
1198         createcommand("TIME",handle_time,0,0,"<core>");
1199         createcommand("WHOIS",handle_whois,0,1,"<core>");
1200         createcommand("WALLOPS",handle_wallops,'o',1,"<core>");
1201         createcommand("NOTICE",handle_notice,0,2,"<core>");
1202         createcommand("JOIN",handle_join,0,1,"<core>");
1203         createcommand("NAMES",handle_names,0,0,"<core>");
1204         createcommand("PART",handle_part,0,1,"<core>");
1205         createcommand("KICK",handle_kick,0,2,"<core>");
1206         createcommand("MODE",handle_mode,0,1,"<core>");
1207         createcommand("TOPIC",handle_topic,0,1,"<core>");
1208         createcommand("WHO",handle_who,0,1,"<core>");
1209         createcommand("MOTD",handle_motd,0,0,"<core>");
1210         createcommand("RULES",handle_rules,0,0,"<core>");
1211         createcommand("OPER",handle_oper,0,2,"<core>");
1212         createcommand("LIST",handle_list,0,0,"<core>");
1213         createcommand("DIE",handle_die,'o',1,"<core>");
1214         createcommand("RESTART",handle_restart,'o',1,"<core>");
1215         createcommand("KILL",handle_kill,'o',2,"<core>");
1216         createcommand("REHASH",handle_rehash,'o',0,"<core>");
1217         createcommand("LUSERS",handle_lusers,0,0,"<core>");
1218         createcommand("STATS",handle_stats,0,1,"<core>");
1219         createcommand("USERHOST",handle_userhost,0,1,"<core>");
1220         createcommand("AWAY",handle_away,0,0,"<core>");
1221         createcommand("ISON",handle_ison,0,0,"<core>");
1222         createcommand("SUMMON",handle_summon,0,0,"<core>");
1223         createcommand("USERS",handle_users,0,0,"<core>");
1224         createcommand("INVITE",handle_invite,0,0,"<core>");
1225         createcommand("PASS",handle_pass,0,1,"<core>");
1226         createcommand("TRACE",handle_trace,'o',0,"<core>");
1227         createcommand("WHOWAS",handle_whowas,0,1,"<core>");
1228         createcommand("CONNECT",handle_connect,'o',1,"<core>");
1229         createcommand("SQUIT",handle_squit,'o',0,"<core>");
1230         createcommand("MODULES",handle_modules,0,0,"<core>");
1231         createcommand("LINKS",handle_links,0,0,"<core>");
1232         createcommand("MAP",handle_map,0,0,"<core>");
1233         createcommand("KLINE",handle_kline,'o',1,"<core>");
1234         createcommand("GLINE",handle_gline,'o',1,"<core>");
1235         createcommand("ZLINE",handle_zline,'o',1,"<core>");
1236         createcommand("QLINE",handle_qline,'o',1,"<core>");
1237         createcommand("ELINE",handle_eline,'o',1,"<core>");
1238         createcommand("LOADMODULE",handle_loadmodule,'o',1,"<core>");
1239         createcommand("UNLOADMODULE",handle_unloadmodule,'o',1,"<core>");
1240         createcommand("SERVER",handle_server,0,0,"<core>");
1241         createcommand("COMMANDS",handle_commands,0,0,"<core>");
1242 }
1243
1244 bool DirValid(char* dirandfile)
1245 {
1246         char work[MAXBUF];
1247         strlcpy(work,dirandfile,MAXBUF);
1248         int p = strlen(work);
1249         // we just want the dir
1250         while (strlen(work))
1251         {
1252                 if (work[p] == '/')
1253                 {
1254                         work[p] = '\0';
1255                         break;
1256                 }
1257                 work[p--] = '\0';
1258         }
1259         char buffer[MAXBUF], otherdir[MAXBUF];
1260         // Get the current working directory
1261         if( getcwd( buffer, MAXBUF ) == NULL )
1262                 return false;
1263         chdir(work);
1264         if( getcwd( otherdir, MAXBUF ) == NULL )
1265                 return false;
1266         chdir(buffer);
1267         if (strlen(otherdir) >= strlen(work))
1268         {
1269                 otherdir[strlen(work)] = '\0';
1270                 if (!strcmp(otherdir,work))
1271                 {
1272                         return true;
1273                 }
1274                 return false;
1275         }
1276         else return false;
1277 }
1278
1279 std::string GetFullProgDir(char** argv, int argc)
1280 {
1281         char work[MAXBUF];
1282         strlcpy(work,argv[0],MAXBUF);
1283         int p = strlen(work);
1284         // we just want the dir
1285         while (strlen(work))
1286         {
1287                 if (work[p] == '/')
1288                 {
1289                         work[p] = '\0';
1290                         break;
1291                 }
1292                 work[p--] = '\0';
1293         }
1294         char buffer[MAXBUF], otherdir[MAXBUF];
1295         // Get the current working directory
1296         if( getcwd( buffer, MAXBUF ) == NULL )
1297                 return "";
1298         chdir(work);
1299         if( getcwd( otherdir, MAXBUF ) == NULL )
1300                 return "";
1301         chdir(buffer);
1302         return otherdir;
1303 }
1304