]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/helperfuncs.cpp
Added extra 'looking up your hostname lollerskates' output :p
[user/henk/code/inspircd.git] / src / helperfuncs.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 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[MAX_DESCRIPTORS];
60 static char already_sent[MAX_DESCRIPTORS];
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, const 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, const 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, const char* text)
236 {
237         if ((sock < 0) || (!text) || (!user))
238                 return;
239         char tb[MAXBUF];
240         int bytes = snprintf(tb,MAXBUF,":%s %s\r\n",user->GetFullHost(),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\r\n",user->GetFullHost(),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_NoFormat(dest->fd,textbuffer);
312         }
313         else
314         {
315                 WriteFrom_NoFormat(dest->fd,source,textbuffer);
316         }
317 }
318
319 void WriteTo_NoFormat(userrec *source, userrec *dest, const char *data)
320 {
321         if ((!dest) || (!data))
322                 return;
323         if (!source)
324         {
325                 WriteServ_NoFormat(dest->fd,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::map<char*,char*> *ulist= Ptr->GetUsers();
350         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
351         {
352                 char* o = i->second;
353                 userrec* otheruser = (userrec*)o;
354                 if (otheruser->fd != FD_MAGIC_NUMBER)
355                         WriteTo_NoFormat(user,otheruser,textbuffer);
356         }
357 }
358
359 void WriteChannel_NoFormat(chanrec* Ptr, userrec* user, const char* text)
360 {
361         if ((!Ptr) || (!user) || (!text))
362         {
363                 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
364                 return;
365         }
366         std::map<char*,char*> *ulist= Ptr->GetUsers();
367         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
368         {
369                 char* o = i->second;
370                 userrec* otheruser = (userrec*)o;
371                 if (otheruser->fd != FD_MAGIC_NUMBER)
372                         WriteTo_NoFormat(user,otheruser,text);
373         }
374 }
375
376
377 /* write formatted text from a source user to all users on a channel
378  * including the sender (NOT for privmsg, notice etc!) doesnt send to
379  * users on remote servers */
380
381 void WriteChannelLocal(chanrec* Ptr, userrec* user, char* text, ...)
382 {
383         if ((!Ptr) || (!text))
384         {
385                 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
386                 return;
387         }
388         char textbuffer[MAXBUF];
389         va_list argsPtr;
390         va_start (argsPtr, text);
391         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
392         va_end(argsPtr);
393
394         std::map<char*,char*> *ulist= Ptr->GetUsers();
395         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
396         {
397                 char* o = i->second;
398                 userrec* otheruser = (userrec*)o;
399                 if ((otheruser->fd != FD_MAGIC_NUMBER) && (otheruser != user))
400                 {
401                         if (!user)
402                         {
403                                 WriteServ_NoFormat(otheruser->fd,textbuffer);
404                         }
405                         else
406                         {
407                                 WriteTo_NoFormat(user,otheruser,textbuffer);
408                         }
409                 }
410         }
411 }
412
413 void WriteChannelLocal_NoFormat(chanrec* Ptr, userrec* user, const char* text)
414 {
415         if ((!Ptr) || (!text))
416         {
417                 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
418                 return;
419         }
420         std::map<char*,char*> *ulist= Ptr->GetUsers();
421         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
422         {
423                 char* o = i->second;
424                 userrec* otheruser = (userrec*)o;
425                 if ((otheruser->fd != FD_MAGIC_NUMBER) && (otheruser != user))
426                 {
427                         if (!user)
428                         {
429                                 WriteServ_NoFormat(otheruser->fd,text);
430                         }
431                         else
432                         {
433                                 WriteTo_NoFormat(user,otheruser,text);
434                         }
435                 }
436         }
437 }
438
439
440
441 void WriteChannelWithServ(char* ServName, chanrec* Ptr, char* text, ...)
442 {
443         if ((!Ptr) || (!text))
444         {
445                 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
446                 return;
447         }
448         char textbuffer[MAXBUF];
449         va_list argsPtr;
450         va_start (argsPtr, text);
451         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
452         va_end(argsPtr);
453
454
455         std::map<char*,char*> *ulist= Ptr->GetUsers();
456         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
457         {
458                 char* o = i->second;
459                 userrec* otheruser = (userrec*)o;
460                 if (IS_LOCAL(otheruser))
461                         WriteServ_NoFormat(otheruser->fd,textbuffer);
462         }
463 }
464
465 void WriteChannelWithServ_NoFormat(char* ServName, chanrec* Ptr, const char* text)
466 {
467         if ((!Ptr) || (!text))
468         {
469                 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
470                 return;
471         }
472         std::map<char*,char*> *ulist= Ptr->GetUsers();
473         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
474         {
475                 char* o = i->second;
476                 userrec* otheruser = (userrec*)o;
477                 if (IS_LOCAL(otheruser))
478                         WriteServ_NoFormat(otheruser->fd,text);
479         }
480 }
481
482
483
484 /* write formatted text from a source user to all users on a channel except
485  * for the sender (for privmsg etc) */
486
487 void ChanExceptSender(chanrec* Ptr, userrec* user, char* text, ...)
488 {
489         if ((!Ptr) || (!user) || (!text))
490         {
491                 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
492                 return;
493         }
494         char textbuffer[MAXBUF];
495         va_list argsPtr;
496         va_start (argsPtr, text);
497         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
498         va_end(argsPtr);
499
500         std::map<char*,char*> *ulist= Ptr->GetUsers();
501         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
502         {
503                 char* o = i->second;
504                 userrec* otheruser = (userrec*)o;
505                 if ((IS_LOCAL(otheruser)) && (user != otheruser))
506                         WriteFrom_NoFormat(otheruser->fd,user,textbuffer);
507         }
508 }
509
510 void ChanExceptSender_NoFormat(chanrec* Ptr, userrec* user, const char* text)
511 {
512         if ((!Ptr) || (!user) || (!text))
513         {
514                 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
515                 return;
516         }
517         std::map<char*,char*> *ulist= Ptr->GetUsers();
518         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
519         {
520                 char* o = i->second;
521                 userrec* otheruser = (userrec*)o;
522                 if ((IS_LOCAL(otheruser)) && (user != otheruser))
523                         WriteFrom_NoFormat(otheruser->fd,user,text);
524         }
525 }
526
527 std::string GetServerDescription(char* servername)
528 {
529         std::string description = "";
530         FOREACH_MOD(I_OnGetServerDescription,OnGetServerDescription(servername,description));
531         if (description != "")
532         {
533                 return description;
534         }
535         else
536         {
537                 return Config->ServerDesc; // not a remote server that can be found, it must be me.
538         }
539 }
540
541 /* write a formatted string to all users who share at least one common
542  * channel, including the source user e.g. for use in NICK */
543
544 void WriteCommon(userrec *u, char* text, ...)
545 {
546         if (!u)
547         {
548                 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
549                 return;
550         }
551
552         if (u->registered != 7) {
553                 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
554                 return;
555         }
556
557         char textbuffer[MAXBUF];
558         va_list argsPtr;
559         va_start (argsPtr, text);
560         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
561         va_end(argsPtr);
562
563         // FIX: Stops a message going to the same person more than once
564         memset(&already_sent,0,MAX_DESCRIPTORS);
565
566         bool sent_to_at_least_one = false;
567
568         unsigned int y = u->chans.size();
569         for (unsigned int i = 0; i < y; i++)
570         {
571                 if (u->chans[i].channel)
572                 {
573                         std::map<char*,char*> *ulist= u->chans[i].channel->GetUsers();
574                         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
575                         {
576                                 char* o = i->second;
577                                 userrec* otheruser = (userrec*)o;
578                                 if ((otheruser->fd > -1) && (!already_sent[otheruser->fd]))
579                                 {
580                                         already_sent[otheruser->fd] = 1;
581                                         WriteFrom_NoFormat(otheruser->fd,u,textbuffer);
582                                         sent_to_at_least_one = true;
583                                 }
584                         }
585                 }
586         }
587         // if the user was not in any channels, no users will receive the text. Make sure the user
588         // receives their OWN message for WriteCommon
589         if (!sent_to_at_least_one)
590         {
591                 WriteFrom_NoFormat(u->fd,u,textbuffer);
592         }
593 }
594
595 void WriteCommon_NoFormat(userrec *u, const char* text)
596 {
597         if (!u)
598         {
599                 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
600                 return;
601         }
602                 
603         if (u->registered != 7) {
604                 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
605                 return;
606         }
607         // FIX: Stops a message going to the same person more than once
608         memset(&already_sent,0,MAX_DESCRIPTORS);
609                 
610         bool sent_to_at_least_one = false;
611                         
612         unsigned int y = u->chans.size();
613         for (unsigned int i = 0; i < y; i++)
614         {
615                 if (u->chans[i].channel)
616                 {
617                         std::map<char*,char*> *ulist= u->chans[i].channel->GetUsers();
618                         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
619                         {
620                                 char* o = i->second;
621                                 userrec* otheruser = (userrec*)o;
622                                 if ((otheruser->fd > -1) && (!already_sent[otheruser->fd]))
623                                 {
624                                         already_sent[otheruser->fd] = 1;
625                                         WriteFrom_NoFormat(otheruser->fd,u,text);
626                                         sent_to_at_least_one = true;
627                                 }
628                         }
629                 }
630         }
631         // if the user was not in any channels, no users will receive the text. Make sure the user
632         // receives their OWN message for WriteCommon
633         if (!sent_to_at_least_one)
634         {
635                 WriteFrom_NoFormat(u->fd,u,text);
636         }
637 }
638
639
640 /* write a formatted string to all users who share at least one common
641  * channel, NOT including the source user e.g. for use in QUIT */
642
643 void WriteCommonExcept(userrec *u, char* text, ...)
644 {
645         if (!u)
646         {
647                 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
648                 return;
649         }
650
651         if (u->registered != 7) {
652                 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
653                 return;
654         }
655
656         char textbuffer[MAXBUF];
657         char oper_quit[MAXBUF];
658         bool quit_munge = false;
659
660         va_list argsPtr;
661         va_start (argsPtr, text);
662         int total = vsnprintf(textbuffer, MAXBUF, text, argsPtr);
663         va_end(argsPtr);
664
665         if ((Config->HideSplits) && (total > 6))
666         {
667                 /* Yeah yeah, this is ugly. But its fast, live with it. */
668                 char* check = textbuffer;
669                 if ((*check++ == 'Q') && (*check++ == 'U') && (*check++ == 'I') && (*check++ == 'T') && (*check++ == ' ') && (*check++ == ':'))
670                 {
671                         std::stringstream split(check);
672                         std::string server_one;
673                         std::string server_two;
674                         split >> server_one;
675                         split >> server_two;
676                         if ((FindServerName(server_one)) && (FindServerName(server_two)))
677                         {
678                                 strlcpy(oper_quit,textbuffer,MAXBUF);
679                                 strlcpy(check,"*.net *.split",MAXQUIT);
680                                 quit_munge = true;
681                         }
682                 }
683         }
684
685         memset(&already_sent,0,MAX_DESCRIPTORS);
686
687         unsigned int y = u->chans.size();
688         for (unsigned int i = 0; i < y; i++)
689         {
690                 if (u->chans[i].channel)
691                 {
692                         std::map<char*,char*> *ulist= u->chans[i].channel->GetUsers();
693                         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
694                         {
695                                 char* o = i->second;
696                                 userrec* otheruser = (userrec*)o;
697                                 if (u != otheruser)
698                                 {
699                                         if ((otheruser->fd > -1) && (!already_sent[otheruser->fd]))
700                                         {
701                                                 already_sent[otheruser->fd] = 1;
702                                                 if (quit_munge)
703                                                 {
704                                                         if (*otheruser->oper)
705                                                         {
706                                                                 WriteFrom_NoFormat(otheruser->fd,u,oper_quit);
707                                                         }
708                                                         else
709                                                         {
710                                                                 WriteFrom_NoFormat(otheruser->fd,u,textbuffer);
711                                                         }
712                                                 }
713                                                 else WriteFrom_NoFormat(otheruser->fd,u,textbuffer);
714                                         }
715                                 }
716                         }
717                 }
718         }
719 }
720
721 void WriteCommonExcept_NoFormat(userrec *u, const char* text)
722 {
723         if (!u)
724         {
725                 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
726                 return;
727         }
728          
729         if (u->registered != 7) {
730                 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
731                 return;
732         }
733
734         memset(&already_sent,0,MAX_DESCRIPTORS);
735
736         unsigned int y = u->chans.size();
737         for (unsigned int i = 0; i < y; i++)
738         {
739                 if (u->chans[i].channel)
740                 {
741                         std::map<char*,char*> *ulist= u->chans[i].channel->GetUsers();
742                         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
743                         {
744                                 char* o = i->second;
745                                 userrec* otheruser = (userrec*)o;
746                                 if (u != otheruser)
747                                 {
748                                         if ((otheruser->fd > -1) && (!already_sent[otheruser->fd]))
749                                         {
750                                                 already_sent[otheruser->fd] = 1;
751                                                 WriteFrom_NoFormat(otheruser->fd,u,text);
752                                         }
753                                 }
754                         }
755                 }
756         }
757 }
758
759
760
761 void WriteOpers(char* text, ...)
762 {
763         if (!text)
764         {
765                 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
766                 return;
767         }
768
769         char textbuffer[MAXBUF];
770         va_list argsPtr;
771         va_start (argsPtr, text);
772         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
773         va_end(argsPtr);
774
775         for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
776         {
777                 userrec* a = *i;
778                 if (IS_LOCAL(a))
779                 {
780                         if (strchr(a->modes,'s'))
781                         {
782                                 // send server notices to all with +s
783                                 WriteServ(a->fd,"NOTICE %s :%s",a->nick,textbuffer);
784                         }
785                 }
786         }
787 }
788
789 void ServerNoticeAll(char* text, ...)
790 {
791         if (!text)
792                 return;
793
794         char textbuffer[MAXBUF];
795         va_list argsPtr;
796         va_start (argsPtr, text);
797         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
798         va_end(argsPtr);
799
800         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
801         {
802                 userrec* t = (userrec*)(*i);
803                 WriteServ(t->fd,"NOTICE $%s :%s",Config->ServerName,textbuffer);
804         }
805 }
806
807 void ServerPrivmsgAll(char* text, ...)
808 {
809         if (!text)
810                 return;
811
812         char textbuffer[MAXBUF];
813         va_list argsPtr;
814         va_start (argsPtr, text);
815         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
816         va_end(argsPtr);
817
818         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
819         {
820                 userrec* t = (userrec*)(*i);
821                 WriteServ(t->fd,"PRIVMSG $%s :%s",Config->ServerName,textbuffer);
822         }
823 }
824
825 void WriteMode(const char* modes, int flags, const char* text, ...)
826 {
827         if ((!text) || (!modes) || (!flags))
828         {
829                 log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
830                 return;
831         }
832
833         char textbuffer[MAXBUF];
834         va_list argsPtr;
835         va_start (argsPtr, text);
836         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
837         va_end(argsPtr);
838         int modelen = strlen(modes);
839
840         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
841         {
842                 userrec* t = (userrec*)(*i);
843                 bool send_to_user = false;
844                 if (flags == WM_AND)
845                 {
846                         send_to_user = true;
847                         for (int n = 0; n < modelen; n++)
848                         {
849                                 if (!hasumode(t,modes[n]))
850                                 {
851                                         send_to_user = false;
852                                         break;
853                                 }
854                         }
855                 }
856                 else if (flags == WM_OR)
857                 {
858                         send_to_user = false;
859                         for (int n = 0; n < modelen; n++)
860                         {
861                                 if (hasumode(t,modes[n]))
862                                 {
863                                         send_to_user = true;
864                                         break;
865                                 }
866                         }
867                 }
868                 if (send_to_user)
869                 {
870                         WriteServ(t->fd,"NOTICE %s :%s",t->nick,textbuffer);
871                 }
872         }
873 }
874
875 void NoticeAll(userrec *source, bool local_only, char* text, ...)
876 {
877         if ((!text) || (!source))
878         {
879                 log(DEFAULT,"*** BUG *** NoticeAll was given an invalid parameter");
880                 return;
881         }
882
883         char textbuffer[MAXBUF];
884         va_list argsPtr;
885         va_start (argsPtr, text);
886         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
887         va_end(argsPtr);
888
889         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
890         {
891                 userrec* t = (userrec*)(*i);
892                 WriteFrom(t->fd,source,"NOTICE $* :%s",textbuffer);
893         }
894
895 }
896
897
898 void WriteWallOps(userrec *source, bool local_only, char* text, ...)
899 {
900         if ((!text) || (!source))
901         {
902                 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
903                 return;
904         }
905
906         char textbuffer[MAXBUF];
907         va_list argsPtr;
908         va_start (argsPtr, text);
909         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
910         va_end(argsPtr);
911
912         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
913         {
914                 userrec* t = (userrec*)(*i);
915                 if ((IS_LOCAL(t)) && (strchr(t->modes,'w')))
916                 {
917                         WriteTo(source,t,"WALLOPS :%s",textbuffer);
918                 }
919         }
920 }
921
922 /* convert a string to lowercase. Note following special circumstances
923  * taken from RFC 1459. Many "official" server branches still hold to this
924  * rule so i will too;
925  *
926  *  Because of IRC's scandanavian origin, the characters {}| are
927  *  considered to be the lower case equivalents of the characters []\,
928  *  respectively. This is a critical issue when determining the
929  *  equivalence of two nicknames.
930  */
931
932 void strlower(char *n)
933 {
934         if (n)
935         {
936                 for (char* t = n; *t; t++)
937                         *t = lowermap[(unsigned)*t];
938         }
939 }
940
941 /* Find a user record by nickname and return a pointer to it */
942
943 userrec* Find(std::string nick)
944 {
945         user_hash::iterator iter = clientlist.find(nick);
946
947         if (iter == clientlist.end())
948                 /* Couldn't find it */
949                 return NULL;
950
951         return iter->second;
952 }
953
954 /* find a channel record by channel name and return a pointer to it */
955
956 chanrec* FindChan(const char* chan)
957 {
958         if (!chan)
959         {
960                 log(DEFAULT,"*** BUG *** Findchan was given an invalid parameter");
961                 return NULL;
962         }
963
964         chan_hash::iterator iter = chanlist.find(chan);
965
966         if (iter == chanlist.end())
967                 /* Couldn't find it */
968                 return NULL;
969
970         return iter->second;
971 }
972
973
974 long GetMaxBans(char* name)
975 {
976         char CM[MAXBUF];
977         for (int count = 0; count < Config->ConfValueEnum("banlist",&Config->config_f); count++)
978         {
979                 Config->ConfValue("banlist","chan",count,CM,&Config->config_f);
980                 if (match(name,CM))
981                 {
982                         Config->ConfValue("banlist","limit",count,CM,&Config->config_f);
983                         return atoi(CM);
984                 }
985         }
986         return 64;
987 }
988
989 void purge_empty_chans(userrec* u)
990 {
991
992         int purge = 0;
993
994         // firstly decrement the count on each channel
995         for (unsigned int f = 0; f < u->chans.size(); f++)
996         {
997                 if (u->chans[f].channel)
998                 {
999                         u->chans[f].channel->DelUser((char*)u);
1000                 }
1001         }
1002
1003         for (unsigned int i = 0; i < u->chans.size(); i++)
1004         {
1005                 if (u->chans[i].channel)
1006                 {
1007                         if (!usercount(u->chans[i].channel))
1008                         {
1009                                 chan_hash::iterator i2 = chanlist.find(u->chans[i].channel->name);
1010                                 /* kill the record */
1011                                 if (i2 != chanlist.end())
1012                                 {
1013                                         log(DEBUG,"del_channel: destroyed: %s",i2->second->name);
1014                                         if (i2->second)
1015                                         {
1016                                                 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(i2->second));
1017                                                 delete i2->second;
1018                                         }
1019                                         chanlist.erase(i2);
1020                                         purge++;
1021                                         u->chans[i].channel = NULL;
1022                                 }
1023                         }
1024                         else
1025                         {
1026                                 log(DEBUG,"skipped purge for %s",u->chans[i].channel->name);
1027                         }
1028                 }
1029         }
1030         log(DEBUG,"completed channel purge, killed %lu",(unsigned long)purge);
1031
1032         DeleteOper(u);
1033 }
1034
1035
1036 char* chanmodes(chanrec *chan, bool showkey)
1037 {
1038         static char scratch[MAXBUF];
1039         static char sparam[MAXBUF];
1040         char* offset = scratch;
1041
1042         if (!chan)
1043         {
1044                 log(DEFAULT,"*** BUG *** chanmodes was given an invalid parameter");
1045                 *scratch = '\0';
1046                 return scratch;
1047         }
1048
1049         *scratch = '\0';
1050         *sparam = '\0';
1051         if (chan->binarymodes & CM_NOEXTERNAL)
1052                 *offset++ = 'n';
1053         if (chan->binarymodes & CM_TOPICLOCK)
1054                 *offset++ = 't';
1055         if (*chan->key)
1056                 *offset++ = 'k';
1057         if (chan->limit)
1058                 *offset++ = 'l';
1059         if (chan->binarymodes & CM_INVITEONLY)
1060                 *offset++ = 'i';
1061         if (chan->binarymodes & CM_MODERATED)
1062                 *offset++ = 'm';
1063         if (chan->binarymodes & CM_SECRET)
1064                 *offset++ = 's';
1065         if (chan->binarymodes & CM_PRIVATE)
1066                 *offset++ = 'p';
1067         if (*chan->key)
1068         {
1069                 snprintf(sparam,MAXBUF," %s",showkey ? chan->key : "<key>");
1070         }
1071         if (chan->limit)
1072         {
1073                 char foo[24];
1074                 sprintf(foo," %lu",(unsigned long)chan->limit);
1075                 strlcat(sparam,foo,MAXBUF);
1076         }
1077         if (*chan->custom_modes)
1078         {
1079                 for (char* t = chan->custom_modes; *t; t++)
1080                         *offset++ = *t;
1081                 for (int z = 0; chan->custom_modes[z]; z++)
1082                 {
1083                         std::string extparam = chan->GetModeParameter(chan->custom_modes[z]);
1084                         if (extparam != "")
1085                         {
1086                                 strlcat(sparam," ",MAXBUF);
1087                                 strlcat(sparam,extparam.c_str(),MAXBUF);
1088                         }
1089                 }
1090         }
1091         /* Null terminate scratch */
1092         *offset = '\0';
1093         strlcat(scratch,sparam,MAXMODES);
1094         return scratch;
1095 }
1096
1097
1098 /* compile a userlist of a channel into a string, each nick seperated by
1099  * spaces and op, voice etc status shown as @ and + */
1100
1101 void userlist(userrec *user,chanrec *c)
1102 {
1103         if ((!c) || (!user))
1104         {
1105                 log(DEFAULT,"*** BUG *** userlist was given an invalid parameter");
1106                 return;
1107         }
1108
1109         snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1110
1111         std::map<char*,char*> *ulist= c->GetUsers();
1112         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
1113         {
1114                 char* o = i->second;
1115                 userrec* otheruser = (userrec*)o;
1116                 if ((!has_channel(user,c)) && (strchr(otheruser->modes,'i')))
1117                 {
1118                         /* user is +i, and source not on the channel, does not show
1119                          * nick in NAMES list */
1120                         continue;
1121                 }
1122                 strlcat(list,cmode(otheruser,c),MAXBUF);
1123                 strlcat(list,otheruser->nick,MAXBUF);
1124                 strlcat(list," ",MAXBUF);
1125                 if (strlen(list)>(480-NICKMAX))
1126                 {
1127                         /* list overflowed into
1128                          * multiple numerics */
1129                         WriteServ_NoFormat(user->fd,list);
1130                         snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1131                 }
1132         }
1133         /* if whats left in the list isnt empty, send it */
1134         if (list[strlen(list)-1] != ':')
1135         {
1136                 WriteServ_NoFormat(user->fd,list);
1137         }
1138 }
1139
1140 /* return a count of the users on a specific channel accounting for
1141  * invisible users who won't increase the count. e.g. for /LIST */
1142
1143 int usercount_i(chanrec *c)
1144 {
1145         int count = 0;
1146
1147         if (!c)
1148         {
1149                 log(DEFAULT,"*** BUG *** usercount_i was given an invalid parameter");
1150                 return 0;
1151         }
1152
1153         for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1154         {
1155                         if (has_channel(i->second,c))
1156                         {
1157                                 if (i->second->registered == 7)
1158                                 {
1159                                         if ((!has_channel(i->second,c)) && (strchr(i->second->modes,'i')))
1160                                         {
1161                                                 /* user is +i, and source not on the channel, does not show
1162                                                  * nick in NAMES list */
1163                                                 continue;
1164                                         }
1165                                         count++;
1166                                 }
1167                         }
1168         }
1169         log(DEBUG,"usercount_i: %s %lu",c->name,(unsigned long)count);
1170         return count;
1171 }
1172
1173
1174 int usercount(chanrec *c)
1175 {
1176         return (c ? c->GetUserCounter() : 0);
1177 }
1178
1179
1180 // looks up a users password for their connection class (<ALLOW>/<DENY> tags)
1181
1182 ConnectClass GetClass(userrec *user)
1183 {
1184         for (ClassVector::iterator i = Config->Classes.begin(); i != Config->Classes.end(); i++)
1185         {
1186                 if (match(user->host,i->host.c_str()))
1187                 {
1188                         return *i;
1189                 }
1190         }
1191         return *(Config->Classes.begin());
1192 }
1193
1194 /* sends out an error notice to all connected clients (not to be used
1195  * lightly!) */
1196
1197 void send_error(char *s)
1198 {
1199         log(DEBUG,"send_error: %s",s);
1200         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1201         {
1202                 userrec* t = (userrec*)(*i);
1203                 if (t->registered == 7)
1204                 {
1205                         WriteServ(t->fd,"NOTICE %s :%s",t->nick,s);
1206                 }
1207                 else
1208                 {
1209                         // fix - unregistered connections receive ERROR, not NOTICE
1210                         Write(t->fd,"ERROR :%s",s);
1211                 }
1212         }
1213 }
1214
1215 void Error(int status)
1216 {
1217         signal (SIGALRM, SIG_IGN);
1218         signal (SIGPIPE, SIG_IGN);
1219         signal (SIGTERM, SIG_IGN);
1220         signal (SIGABRT, SIG_IGN);
1221         signal (SIGSEGV, SIG_IGN);
1222         signal (SIGURG, SIG_IGN);
1223         signal (SIGKILL, SIG_IGN);
1224         log(DEFAULT,"*** fell down a pothole in the road to perfection ***");
1225         send_error("Error! Segmentation fault! save meeeeeeeeeeeeee *splat!*");
1226         Exit(status);
1227 }
1228
1229 // this function counts all users connected, wether they are registered or NOT.
1230 int usercnt(void)
1231 {
1232         return clientlist.size();
1233 }
1234
1235 // this counts only registered users, so that the percentages in /MAP don't mess up when users are sitting in an unregistered state
1236 int registered_usercount(void)
1237 {
1238         int c = 0;
1239         for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1240         {
1241                 if (i->second->registered == 7) c++;
1242         }
1243         return c;
1244 }
1245
1246 int usercount_invisible(void)
1247 {
1248         int c = 0;
1249         for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1250         {
1251                 if ((i->second->registered == 7) && (strchr(i->second->modes,'i'))) c++;
1252         }
1253         return c;
1254 }
1255
1256 int usercount_opers(void)
1257 {
1258         return all_opers.size();
1259 }
1260
1261 int usercount_unknown(void)
1262 {
1263         int c = 0;
1264         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1265         {
1266                 userrec* t = (userrec*)(*i);
1267                 if (t->registered != 7)
1268                         c++;
1269         }
1270         return c;
1271 }
1272
1273 long chancount(void)
1274 {
1275         return chanlist.size();
1276 }
1277
1278 long local_count()
1279 {
1280         int c = 0;
1281         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1282         {
1283                 userrec* t = (userrec*)(*i);
1284                 if (t->registered == 7) c++;
1285         }
1286         return c;
1287 }
1288
1289 void ShowMOTD(userrec *user)
1290 {
1291         static char mbuf[MAXBUF];
1292         static char crud[MAXBUF];
1293         std::string WholeMOTD = "";
1294         if (!Config->MOTD.size())
1295         {
1296                 WriteServ(user->fd,"422 %s :Message of the day file is missing.",user->nick);
1297                 return;
1298         }
1299         snprintf(crud,MAXBUF,":%s 372 %s :- ", Config->ServerName, user->nick);
1300         snprintf(mbuf,MAXBUF,":%s 375 %s :- %s message of the day\r\n", Config->ServerName, user->nick, Config->ServerName);
1301         WholeMOTD = WholeMOTD + mbuf;
1302         for (unsigned int i = 0; i < Config->MOTD.size(); i++)
1303                 WholeMOTD = WholeMOTD + std::string(crud) + Config->MOTD[i].c_str() + std::string("\r\n");
1304         snprintf(mbuf,MAXBUF,":%s 376 %s :End of message of the day.\r\n", Config->ServerName, user->nick);
1305         WholeMOTD = WholeMOTD + mbuf;
1306         // only one write operation
1307         if (Config->GetIOHook(user->port))
1308         {
1309                 Config->GetIOHook(user->port)->OnRawSocketWrite(user->fd,(char*)WholeMOTD.c_str(),WholeMOTD.length());
1310         }
1311         else
1312         {
1313                 user->AddWriteBuf(WholeMOTD);
1314         }
1315         ServerInstance->stats->statsSent += WholeMOTD.length();
1316 }
1317
1318 void ShowRULES(userrec *user)
1319 {
1320         if (!Config->RULES.size())
1321         {
1322                 WriteServ(user->fd,"NOTICE %s :Rules file is missing.",user->nick);
1323                 return;
1324         }
1325         WriteServ(user->fd,"NOTICE %s :%s rules",user->nick,Config->ServerName);
1326         for (unsigned int i = 0; i < Config->RULES.size(); i++)
1327                 WriteServ(user->fd,"NOTICE %s :%s",user->nick,Config->RULES[i].c_str());
1328         WriteServ(user->fd,"NOTICE %s :End of %s rules.",user->nick,Config->ServerName);
1329 }
1330
1331 // this returns 1 when all modules are satisfied that the user should be allowed onto the irc server
1332 // (until this returns true, a user will block in the waiting state, waiting to connect up to the
1333 // registration timeout maximum seconds)
1334 bool AllModulesReportReady(userrec* user)
1335 {
1336         if (!Config->global_implementation[I_OnCheckReady])
1337                 return true;
1338         for (int i = 0; i <= MODCOUNT; i++)
1339         {
1340                 if (Config->implement_lists[i][I_OnCheckReady])
1341                 {
1342                         int res = modules[i]->OnCheckReady(user);
1343                         if (!res)
1344                                 return false;
1345                 }
1346         }
1347         return true;
1348 }
1349
1350 bool DirValid(char* dirandfile)
1351 {
1352         char work[MAXBUF];
1353         char buffer[MAXBUF], otherdir[MAXBUF];
1354         strlcpy(work,dirandfile,MAXBUF);
1355         int p = strlen(work);
1356         // we just want the dir
1357         while (*work)
1358         {
1359                 if (work[p] == '/')
1360                 {
1361                         work[p] = '\0';
1362                         break;
1363                 }
1364                 work[p--] = '\0';
1365         }
1366         // Get the current working directory
1367         if( getcwd( buffer, MAXBUF ) == NULL )
1368                 return false;
1369         chdir(work);
1370         if( getcwd( otherdir, MAXBUF ) == NULL )
1371                 return false;
1372         chdir(buffer);
1373         if (strlen(otherdir) >= strlen(work))
1374         {
1375                 otherdir[strlen(work)] = '\0';
1376                 if (!strcmp(otherdir,work))
1377                 {
1378                         return true;
1379                 }
1380                 return false;
1381         }
1382         else return false;
1383 }
1384
1385 std::string GetFullProgDir(char** argv, int argc)
1386 {
1387         char work[MAXBUF];
1388         char buffer[MAXBUF], otherdir[MAXBUF];
1389         strlcpy(work,argv[0],MAXBUF);
1390         int p = strlen(work);
1391         // we just want the dir
1392         while (*work)
1393         {
1394                 if (work[p] == '/')
1395                 {
1396                         work[p] = '\0';
1397                         break;
1398                 }
1399                 work[p--] = '\0';
1400         }
1401         // Get the current working directory
1402         if( getcwd( buffer, MAXBUF ) == NULL )
1403                 return "";
1404         chdir(work);
1405         if( getcwd( otherdir, MAXBUF ) == NULL )
1406                 return "";
1407         chdir(buffer);
1408         return otherdir;
1409 }
1410
1411 int InsertMode(std::string &output, const char* mode, unsigned short section)
1412 {
1413         unsigned short currsection = 1;
1414         unsigned int pos = output.find("CHANMODES=", 0) + 10; // +10 for the length of "CHANMODES="
1415         
1416         if(section > 4 || section == 0)
1417         {
1418                 log(DEBUG, "InsertMode: CHANMODES doesn't have a section %dh :/", section);
1419                 return 0;
1420         }
1421         
1422         for(; pos < output.size(); pos++)
1423         {
1424                 if(section == currsection)
1425                         break;
1426                         
1427                 if(output[pos] == ',')
1428                         currsection++;
1429         }
1430         
1431         output.insert(pos, mode);
1432         return 1;
1433 }