]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/helperfuncs.cpp
d76c708b2e996b8fed4dc131195ec68c0107c933
[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
641 /* write a formatted string to all users who share at least one common
642  * channel, NOT including the source user e.g. for use in QUIT */
643
644 void WriteCommonExcept(userrec *u, char* text, ...)
645 {
646         if (!u)
647         {
648                 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
649                 return;
650         }
651
652         if (u->registered != 7) {
653                 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
654                 return;
655         }
656
657         char textbuffer[MAXBUF];
658         va_list argsPtr;
659         va_start (argsPtr, text);
660         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
661         va_end(argsPtr);
662
663         memset(&already_sent,0,MAX_DESCRIPTORS);
664
665         unsigned int y = u->chans.size();
666         for (unsigned int i = 0; i < y; i++)
667         {
668                 if (u->chans[i].channel)
669                 {
670                         std::map<char*,char*> *ulist= u->chans[i].channel->GetUsers();
671                         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
672                         {
673                                 char* o = i->second;
674                                 userrec* otheruser = (userrec*)o;
675                                 if (u != otheruser)
676                                 {
677                                         if ((otheruser->fd > -1) && (!already_sent[otheruser->fd]))
678                                         {
679                                                 already_sent[otheruser->fd] = 1;
680                                                 WriteFrom_NoFormat(otheruser->fd,u,textbuffer);
681                                         }
682                                 }
683                         }
684                 }
685         }
686 }
687
688 void WriteCommonExcept_NoFormat(userrec *u, const char* text)
689 {
690         if (!u)
691         {
692                 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
693                 return;
694         }
695          
696         if (u->registered != 7) {
697                 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
698                 return;
699         }
700
701         memset(&already_sent,0,MAX_DESCRIPTORS);
702
703         unsigned int y = u->chans.size();
704         for (unsigned int i = 0; i < y; i++)
705         {
706                 if (u->chans[i].channel)
707                 {
708                         std::map<char*,char*> *ulist= u->chans[i].channel->GetUsers();
709                         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
710                         {
711                                 char* o = i->second;
712                                 userrec* otheruser = (userrec*)o;
713                                 if (u != otheruser)
714                                 {
715                                         if ((otheruser->fd > -1) && (!already_sent[otheruser->fd]))
716                                         {
717                                                 already_sent[otheruser->fd] = 1;
718                                                 WriteFrom_NoFormat(otheruser->fd,u,text);
719                                         }
720                                 }
721                         }
722                 }
723         }
724 }
725
726
727
728 void WriteOpers(char* text, ...)
729 {
730         if (!text)
731         {
732                 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
733                 return;
734         }
735
736         char textbuffer[MAXBUF];
737         va_list argsPtr;
738         va_start (argsPtr, text);
739         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
740         va_end(argsPtr);
741
742         for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
743         {
744                 userrec* a = *i;
745                 if (IS_LOCAL(a))
746                 {
747                         if (strchr(a->modes,'s'))
748                         {
749                                 // send server notices to all with +s
750                                 WriteServ(a->fd,"NOTICE %s :%s",a->nick,textbuffer);
751                         }
752                 }
753         }
754 }
755
756 void ServerNoticeAll(char* text, ...)
757 {
758         if (!text)
759                 return;
760
761         char textbuffer[MAXBUF];
762         va_list argsPtr;
763         va_start (argsPtr, text);
764         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
765         va_end(argsPtr);
766
767         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
768         {
769                 userrec* t = (userrec*)(*i);
770                 WriteServ(t->fd,"NOTICE $%s :%s",Config->ServerName,textbuffer);
771         }
772 }
773
774 void ServerPrivmsgAll(char* text, ...)
775 {
776         if (!text)
777                 return;
778
779         char textbuffer[MAXBUF];
780         va_list argsPtr;
781         va_start (argsPtr, text);
782         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
783         va_end(argsPtr);
784
785         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
786         {
787                 userrec* t = (userrec*)(*i);
788                 WriteServ(t->fd,"PRIVMSG $%s :%s",Config->ServerName,textbuffer);
789         }
790 }
791
792 void WriteMode(const char* modes, int flags, const char* text, ...)
793 {
794         if ((!text) || (!modes) || (!flags))
795         {
796                 log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
797                 return;
798         }
799
800         char textbuffer[MAXBUF];
801         va_list argsPtr;
802         va_start (argsPtr, text);
803         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
804         va_end(argsPtr);
805         int modelen = strlen(modes);
806
807         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
808         {
809                 userrec* t = (userrec*)(*i);
810                 bool send_to_user = false;
811                 if (flags == WM_AND)
812                 {
813                         send_to_user = true;
814                         for (int n = 0; n < modelen; n++)
815                         {
816                                 if (!hasumode(t,modes[n]))
817                                 {
818                                         send_to_user = false;
819                                         break;
820                                 }
821                         }
822                 }
823                 else if (flags == WM_OR)
824                 {
825                         send_to_user = false;
826                         for (int n = 0; n < modelen; n++)
827                         {
828                                 if (hasumode(t,modes[n]))
829                                 {
830                                         send_to_user = true;
831                                         break;
832                                 }
833                         }
834                 }
835                 if (send_to_user)
836                 {
837                         WriteServ(t->fd,"NOTICE %s :%s",t->nick,textbuffer);
838                 }
839         }
840 }
841
842 void NoticeAll(userrec *source, bool local_only, char* text, ...)
843 {
844         if ((!text) || (!source))
845         {
846                 log(DEFAULT,"*** BUG *** NoticeAll was given an invalid parameter");
847                 return;
848         }
849
850         char textbuffer[MAXBUF];
851         va_list argsPtr;
852         va_start (argsPtr, text);
853         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
854         va_end(argsPtr);
855
856         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
857         {
858                 userrec* t = (userrec*)(*i);
859                 WriteFrom(t->fd,source,"NOTICE $* :%s",textbuffer);
860         }
861
862 }
863
864
865 void WriteWallOps(userrec *source, bool local_only, char* text, ...)
866 {
867         if ((!text) || (!source))
868         {
869                 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
870                 return;
871         }
872
873         char textbuffer[MAXBUF];
874         va_list argsPtr;
875         va_start (argsPtr, text);
876         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
877         va_end(argsPtr);
878
879         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
880         {
881                 userrec* t = (userrec*)(*i);
882                 if ((IS_LOCAL(t)) && (strchr(t->modes,'w')))
883                 {
884                         WriteTo(source,t,"WALLOPS :%s",textbuffer);
885                 }
886         }
887 }
888
889 /* convert a string to lowercase. Note following special circumstances
890  * taken from RFC 1459. Many "official" server branches still hold to this
891  * rule so i will too;
892  *
893  *  Because of IRC's scandanavian origin, the characters {}| are
894  *  considered to be the lower case equivalents of the characters []\,
895  *  respectively. This is a critical issue when determining the
896  *  equivalence of two nicknames.
897  */
898
899 void strlower(char *n)
900 {
901         if (n)
902         {
903                 for (char* t = n; *t; t++)
904                         *t = lowermap[(unsigned)*t];
905         }
906 }
907
908 /* Find a user record by nickname and return a pointer to it */
909
910 userrec* Find(std::string nick)
911 {
912         user_hash::iterator iter = clientlist.find(nick);
913
914         if (iter == clientlist.end())
915                 /* Couldn't find it */
916                 return NULL;
917
918         return iter->second;
919 }
920
921 /* find a channel record by channel name and return a pointer to it */
922
923 chanrec* FindChan(const char* chan)
924 {
925         if (!chan)
926         {
927                 log(DEFAULT,"*** BUG *** Findchan was given an invalid parameter");
928                 return NULL;
929         }
930
931         chan_hash::iterator iter = chanlist.find(chan);
932
933         if (iter == chanlist.end())
934                 /* Couldn't find it */
935                 return NULL;
936
937         return iter->second;
938 }
939
940
941 long GetMaxBans(char* name)
942 {
943         char CM[MAXBUF];
944         for (int count = 0; count < Config->ConfValueEnum("banlist",&Config->config_f); count++)
945         {
946                 Config->ConfValue("banlist","chan",count,CM,&Config->config_f);
947                 if (match(name,CM))
948                 {
949                         Config->ConfValue("banlist","limit",count,CM,&Config->config_f);
950                         return atoi(CM);
951                 }
952         }
953         return 64;
954 }
955
956 void purge_empty_chans(userrec* u)
957 {
958
959         int purge = 0;
960
961         // firstly decrement the count on each channel
962         for (unsigned int f = 0; f < u->chans.size(); f++)
963         {
964                 if (u->chans[f].channel)
965                 {
966                         u->chans[f].channel->DelUser((char*)u);
967                 }
968         }
969
970         for (unsigned int i = 0; i < u->chans.size(); i++)
971         {
972                 if (u->chans[i].channel)
973                 {
974                         if (!usercount(u->chans[i].channel))
975                         {
976                                 chan_hash::iterator i2 = chanlist.find(u->chans[i].channel->name);
977                                 /* kill the record */
978                                 if (i2 != chanlist.end())
979                                 {
980                                         log(DEBUG,"del_channel: destroyed: %s",i2->second->name);
981                                         if (i2->second)
982                                         {
983                                                 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(i2->second));
984                                                 delete i2->second;
985                                         }
986                                         chanlist.erase(i2);
987                                         purge++;
988                                         u->chans[i].channel = NULL;
989                                 }
990                         }
991                         else
992                         {
993                                 log(DEBUG,"skipped purge for %s",u->chans[i].channel->name);
994                         }
995                 }
996         }
997         log(DEBUG,"completed channel purge, killed %lu",(unsigned long)purge);
998
999         DeleteOper(u);
1000 }
1001
1002
1003 char* chanmodes(chanrec *chan)
1004 {
1005         static char scratch[MAXBUF];
1006         static char sparam[MAXBUF];
1007         char* offset = scratch;
1008
1009         if (!chan)
1010         {
1011                 log(DEFAULT,"*** BUG *** chanmodes was given an invalid parameter");
1012                 *scratch = '\0';
1013                 return scratch;
1014         }
1015
1016         *scratch = '\0';
1017         *sparam = '\0';
1018         if (chan->binarymodes & CM_NOEXTERNAL)
1019                 *offset++ = 'n';
1020         if (chan->binarymodes & CM_TOPICLOCK)
1021                 *offset++ = 't';
1022         if (*chan->key)
1023                 *offset++ = 'k';
1024         if (chan->limit)
1025                 *offset++ = 'l';
1026         if (chan->binarymodes & CM_INVITEONLY)
1027                 *offset++ = 'i';
1028         if (chan->binarymodes & CM_MODERATED)
1029                 *offset++ = 'm';
1030         if (chan->binarymodes & CM_SECRET)
1031                 *offset++ = 's';
1032         if (chan->binarymodes & CM_PRIVATE)
1033                 *offset++ = 'p';
1034         if (*chan->key)
1035                 snprintf(sparam,MAXBUF," %s",chan->key);
1036         if (chan->limit)
1037         {
1038                 char foo[24];
1039                 sprintf(foo," %lu",(unsigned long)chan->limit);
1040                 strlcat(sparam,foo,MAXBUF);
1041         }
1042         if (*chan->custom_modes)
1043         {
1044                 for (char* t = chan->custom_modes; *t; t++)
1045                         *offset++ = *t;
1046                 for (int z = 0; chan->custom_modes[z]; z++)
1047                 {
1048                         std::string extparam = chan->GetModeParameter(chan->custom_modes[z]);
1049                         if (extparam != "")
1050                         {
1051                                 strlcat(sparam," ",MAXBUF);
1052                                 strlcat(sparam,extparam.c_str(),MAXBUF);
1053                         }
1054                 }
1055         }
1056         /* Null terminate scratch */
1057         *offset = '\0';
1058         strlcat(scratch,sparam,MAXMODES);
1059         return scratch;
1060 }
1061
1062
1063 /* compile a userlist of a channel into a string, each nick seperated by
1064  * spaces and op, voice etc status shown as @ and + */
1065
1066 void userlist(userrec *user,chanrec *c)
1067 {
1068         if ((!c) || (!user))
1069         {
1070                 log(DEFAULT,"*** BUG *** userlist was given an invalid parameter");
1071                 return;
1072         }
1073
1074         snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1075
1076         std::map<char*,char*> *ulist= c->GetUsers();
1077         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
1078         {
1079                 char* o = i->second;
1080                 userrec* otheruser = (userrec*)o;
1081                 if ((!has_channel(user,c)) && (strchr(otheruser->modes,'i')))
1082                 {
1083                         /* user is +i, and source not on the channel, does not show
1084                          * nick in NAMES list */
1085                         continue;
1086                 }
1087                 strlcat(list,cmode(otheruser,c),MAXBUF);
1088                 strlcat(list,otheruser->nick,MAXBUF);
1089                 strlcat(list," ",MAXBUF);
1090                 if (strlen(list)>(480-NICKMAX))
1091                 {
1092                         /* list overflowed into
1093                          * multiple numerics */
1094                         WriteServ_NoFormat(user->fd,list);
1095                         snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1096                 }
1097         }
1098         /* if whats left in the list isnt empty, send it */
1099         if (list[strlen(list)-1] != ':')
1100         {
1101                 WriteServ_NoFormat(user->fd,list);
1102         }
1103 }
1104
1105 /* return a count of the users on a specific channel accounting for
1106  * invisible users who won't increase the count. e.g. for /LIST */
1107
1108 int usercount_i(chanrec *c)
1109 {
1110         int count = 0;
1111
1112         if (!c)
1113         {
1114                 log(DEFAULT,"*** BUG *** usercount_i was given an invalid parameter");
1115                 return 0;
1116         }
1117
1118         for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1119         {
1120                         if (has_channel(i->second,c))
1121                         {
1122                                 if (i->second->registered == 7)
1123                                 {
1124                                         if ((!has_channel(i->second,c)) && (strchr(i->second->modes,'i')))
1125                                         {
1126                                                 /* user is +i, and source not on the channel, does not show
1127                                                  * nick in NAMES list */
1128                                                 continue;
1129                                         }
1130                                         count++;
1131                                 }
1132                         }
1133         }
1134         log(DEBUG,"usercount_i: %s %lu",c->name,(unsigned long)count);
1135         return count;
1136 }
1137
1138
1139 int usercount(chanrec *c)
1140 {
1141         return (c ? c->GetUserCounter() : 0);
1142 }
1143
1144
1145 // looks up a users password for their connection class (<ALLOW>/<DENY> tags)
1146
1147 ConnectClass GetClass(userrec *user)
1148 {
1149         for (ClassVector::iterator i = Config->Classes.begin(); i != Config->Classes.end(); i++)
1150         {
1151                 if (match(user->host,i->host.c_str()))
1152                 {
1153                         return *i;
1154                 }
1155         }
1156         return *(Config->Classes.begin());
1157 }
1158
1159 /* sends out an error notice to all connected clients (not to be used
1160  * lightly!) */
1161
1162 void send_error(char *s)
1163 {
1164         log(DEBUG,"send_error: %s",s);
1165         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1166         {
1167                 userrec* t = (userrec*)(*i);
1168                 if (t->registered == 7)
1169                 {
1170                         WriteServ(t->fd,"NOTICE %s :%s",t->nick,s);
1171                 }
1172                 else
1173                 {
1174                         // fix - unregistered connections receive ERROR, not NOTICE
1175                         Write(t->fd,"ERROR :%s",s);
1176                 }
1177         }
1178 }
1179
1180 void Error(int status)
1181 {
1182         signal (SIGALRM, SIG_IGN);
1183         signal (SIGPIPE, SIG_IGN);
1184         signal (SIGTERM, SIG_IGN);
1185         signal (SIGABRT, SIG_IGN);
1186         signal (SIGSEGV, SIG_IGN);
1187         signal (SIGURG, SIG_IGN);
1188         signal (SIGKILL, SIG_IGN);
1189         log(DEFAULT,"*** fell down a pothole in the road to perfection ***");
1190         send_error("Error! Segmentation fault! save meeeeeeeeeeeeee *splat!*");
1191         Exit(status);
1192 }
1193
1194 // this function counts all users connected, wether they are registered or NOT.
1195 int usercnt(void)
1196 {
1197         return clientlist.size();
1198 }
1199
1200 // this counts only registered users, so that the percentages in /MAP don't mess up when users are sitting in an unregistered state
1201 int registered_usercount(void)
1202 {
1203         int c = 0;
1204         for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1205         {
1206                 if (i->second->registered == 7) c++;
1207         }
1208         return c;
1209 }
1210
1211 int usercount_invisible(void)
1212 {
1213         int c = 0;
1214         for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1215         {
1216                 if ((i->second->registered == 7) && (strchr(i->second->modes,'i'))) c++;
1217         }
1218         return c;
1219 }
1220
1221 int usercount_opers(void)
1222 {
1223         return all_opers.size();
1224 }
1225
1226 int usercount_unknown(void)
1227 {
1228         int c = 0;
1229         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1230         {
1231                 userrec* t = (userrec*)(*i);
1232                 if (t->registered != 7)
1233                         c++;
1234         }
1235         return c;
1236 }
1237
1238 long chancount(void)
1239 {
1240         return chanlist.size();
1241 }
1242
1243 long local_count()
1244 {
1245         int c = 0;
1246         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1247         {
1248                 userrec* t = (userrec*)(*i);
1249                 if (t->registered == 7) c++;
1250         }
1251         return c;
1252 }
1253
1254 void ShowMOTD(userrec *user)
1255 {
1256         static char mbuf[MAXBUF];
1257         static char crud[MAXBUF];
1258         std::string WholeMOTD = "";
1259         if (!Config->MOTD.size())
1260         {
1261                 WriteServ(user->fd,"422 %s :Message of the day file is missing.",user->nick);
1262                 return;
1263         }
1264         snprintf(crud,MAXBUF,":%s 372 %s :- ", Config->ServerName, user->nick);
1265         snprintf(mbuf,MAXBUF,":%s 375 %s :- %s message of the day\r\n", Config->ServerName, user->nick, Config->ServerName);
1266         WholeMOTD = WholeMOTD + mbuf;
1267         for (unsigned int i = 0; i < Config->MOTD.size(); i++)
1268                 WholeMOTD = WholeMOTD + std::string(crud) + Config->MOTD[i].c_str() + std::string("\r\n");
1269         snprintf(mbuf,MAXBUF,":%s 376 %s :End of message of the day.\r\n", Config->ServerName, user->nick);
1270         WholeMOTD = WholeMOTD + mbuf;
1271         // only one write operation
1272         if (Config->GetIOHook(user->port))
1273         {
1274                 Config->GetIOHook(user->port)->OnRawSocketWrite(user->fd,(char*)WholeMOTD.c_str(),WholeMOTD.length());
1275         }
1276         else
1277         {
1278                 user->AddWriteBuf(WholeMOTD);
1279         }
1280         ServerInstance->stats->statsSent += WholeMOTD.length();
1281 }
1282
1283 void ShowRULES(userrec *user)
1284 {
1285         if (!Config->RULES.size())
1286         {
1287                 WriteServ(user->fd,"NOTICE %s :Rules file is missing.",user->nick);
1288                 return;
1289         }
1290         WriteServ(user->fd,"NOTICE %s :%s rules",user->nick,Config->ServerName);
1291         for (unsigned int i = 0; i < Config->RULES.size(); i++)
1292                 WriteServ(user->fd,"NOTICE %s :%s",user->nick,Config->RULES[i].c_str());
1293         WriteServ(user->fd,"NOTICE %s :End of %s rules.",user->nick,Config->ServerName);
1294 }
1295
1296 // this returns 1 when all modules are satisfied that the user should be allowed onto the irc server
1297 // (until this returns true, a user will block in the waiting state, waiting to connect up to the
1298 // registration timeout maximum seconds)
1299 bool AllModulesReportReady(userrec* user)
1300 {
1301         if (!Config->global_implementation[I_OnCheckReady])
1302                 return true;
1303         for (int i = 0; i <= MODCOUNT; i++)
1304         {
1305                 if (Config->implement_lists[i][I_OnCheckReady])
1306                 {
1307                         int res = modules[i]->OnCheckReady(user);
1308                         if (!res)
1309                                 return false;
1310                 }
1311         }
1312         return true;
1313 }
1314
1315 bool DirValid(char* dirandfile)
1316 {
1317         char work[MAXBUF];
1318         char buffer[MAXBUF], otherdir[MAXBUF];
1319         strlcpy(work,dirandfile,MAXBUF);
1320         int p = strlen(work);
1321         // we just want the dir
1322         while (*work)
1323         {
1324                 if (work[p] == '/')
1325                 {
1326                         work[p] = '\0';
1327                         break;
1328                 }
1329                 work[p--] = '\0';
1330         }
1331         // Get the current working directory
1332         if( getcwd( buffer, MAXBUF ) == NULL )
1333                 return false;
1334         chdir(work);
1335         if( getcwd( otherdir, MAXBUF ) == NULL )
1336                 return false;
1337         chdir(buffer);
1338         if (strlen(otherdir) >= strlen(work))
1339         {
1340                 otherdir[strlen(work)] = '\0';
1341                 if (!strcmp(otherdir,work))
1342                 {
1343                         return true;
1344                 }
1345                 return false;
1346         }
1347         else return false;
1348 }
1349
1350 std::string GetFullProgDir(char** argv, int argc)
1351 {
1352         char work[MAXBUF];
1353         char buffer[MAXBUF], otherdir[MAXBUF];
1354         strlcpy(work,argv[0],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 "";
1369         chdir(work);
1370         if( getcwd( otherdir, MAXBUF ) == NULL )
1371                 return "";
1372         chdir(buffer);
1373         return otherdir;
1374 }
1375
1376 int InsertMode(std::string &output, const char* mode, unsigned short section)
1377 {
1378         unsigned short currsection = 1;
1379         unsigned int pos = output.find("CHANMODES=", 0) + 10; // +10 for the length of "CHANMODES="
1380         
1381         if(section > 4 || section == 0)
1382         {
1383                 log(DEBUG, "InsertMode: CHANMODES doesn't have a section %dh :/", section);
1384                 return 0;
1385         }
1386         
1387         for(; pos < output.size(); pos++)
1388         {
1389                 if(section == currsection)
1390                         break;
1391                         
1392                 if(output[pos] == ',')
1393                         currsection++;
1394         }
1395         
1396         output.insert(pos, mode);
1397         return 1;
1398 }