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