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