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