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