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