]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/helperfuncs.cpp
Correctly use iterators and pointer to ucrec
[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 static char TIMESTR[26];
68 static time_t LAST = 0;
69
70 /** log()
71  *  Write a line of text `text' to the logfile (and stdout, if in nofork) if the level `level'
72  *  is greater than the configured loglevel.
73  */
74 void log(int level, char *text, ...)
75 {
76         va_list argsPtr;
77         char textbuffer[MAXBUF];
78
79         if (level < Config->LogLevel)
80                 return;
81
82         if (TIME != LAST)
83         {
84                 struct tm *timeinfo = localtime(&TIME);
85
86                 strlcpy(TIMESTR,asctime(timeinfo),26);
87                 TIMESTR[24] = ':';
88                 LAST = TIME;
89         }
90
91         if (Config->log_file)
92         {
93                 va_start(argsPtr, text);
94                 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
95                 va_end(argsPtr);
96
97                 if (Config->log_file)
98                         fprintf(Config->log_file,"%s %s\n",TIMESTR,textbuffer);
99
100                 if (Config->nofork)
101                 {
102                         printf("%s %s\n", TIMESTR, textbuffer);
103                 }
104         }
105 }
106
107 /** readfile()
108  *  Read the contents of a file located by `fname' into a file_cache pointed at by `F'.
109  *
110  *  XXX - we may want to consider returning a file_cache or pointer to one, less confusing.
111  */
112 void readfile(file_cache &F, const char* fname)
113 {
114         FILE* file;
115         char linebuf[MAXBUF];
116
117         log(DEBUG,"readfile: loading %s",fname);
118         F.clear();
119         file =  fopen(fname,"r");
120
121         if (file)
122         {
123                 while (!feof(file))
124                 {
125                         fgets(linebuf,sizeof(linebuf),file);
126                         linebuf[strlen(linebuf)-1]='\0';
127
128                         if (!*linebuf)
129                         {
130                                 strcpy(linebuf,"  ");
131                         }
132
133                         if (!feof(file))
134                         {
135                                 F.push_back(linebuf);
136                         }
137                 }
138
139                 fclose(file);
140         }
141         else
142         {
143                 log(DEBUG,"readfile: failed to load file: %s",fname);
144         }
145
146         log(DEBUG,"readfile: loaded %s, %lu lines",fname,(unsigned long)F.size());
147 }
148
149 /** Write_NoFormat()
150  *  Writes a given string in `text' to the socket on fd `sock' - only if the socket
151  *  is a valid entry in the local FD table.
152  */
153 void Write_NoFormat(int sock, const char *text)
154 {
155         char tb[MAXBUF];
156         int bytes;
157
158         if ((sock < 0) || (!text) || (sock > MAX_DESCRIPTORS))
159                 return;
160
161         bytes = snprintf(tb,MAXBUF,"%s\r\n",text);
162         chop(tb);
163
164         if (fd_ref_table[sock])
165         {
166                 if (Config->GetIOHook(fd_ref_table[sock]->port))
167                 {
168                         try
169                         {
170                                 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
171                         }
172                         catch (ModuleException& modexcept)
173                         {
174                                 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
175                         }
176                 }
177                 else
178                 {
179                         fd_ref_table[sock]->AddWriteBuf(tb);
180                 }
181                 ServerInstance->stats->statsSent += bytes;
182         }
183         else
184                 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
185 }
186
187 /** Write()
188  *  Same as Write_NoFormat(), but formatted printf() style first.
189  */
190 void Write(int sock, char *text, ...)
191 {
192         va_list argsPtr;
193         char textbuffer[MAXBUF];
194         char tb[MAXBUF];
195         int bytes;
196
197         if ((sock < 0) || (sock > MAX_DESCRIPTORS))
198                 return;
199
200         if (!text)
201         {
202                 log(DEFAULT,"*** BUG *** Write was given an invalid parameter");
203                 return;
204         }
205
206         va_start(argsPtr, text);
207         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
208         va_end(argsPtr);
209         bytes = snprintf(tb,MAXBUF,"%s\r\n",textbuffer);
210         chop(tb);
211
212         if (fd_ref_table[sock])
213         {
214                 if (Config->GetIOHook(fd_ref_table[sock]->port))
215                 {
216                         try
217                         {
218                                 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
219                         }
220                         catch (ModuleException& modexcept)
221                         {
222                                 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
223                         }                                               
224                 }
225                 else
226                 {
227                         fd_ref_table[sock]->AddWriteBuf(tb);
228                 }
229                 ServerInstance->stats->statsSent += bytes;
230         }
231         else
232                 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
233 }
234
235 /** WriteServ_NoFormat()
236  *  Same as Write_NoFormat(), except prefixes `text' with `:server.name '.
237  */
238 void WriteServ_NoFormat(int sock, const char* text)
239 {
240         char tb[MAXBUF];
241         int bytes;
242
243         if ((sock < 0) || (!text) || (sock > MAX_DESCRIPTORS))
244                 return;
245
246         bytes = snprintf(tb,MAXBUF,":%s %s\r\n",Config->ServerName,text);
247         chop(tb);
248
249         if (fd_ref_table[sock])
250         {
251                 if (Config->GetIOHook(fd_ref_table[sock]->port))
252                 {
253                         try
254                         {
255                                 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
256                         }
257                         catch (ModuleException& modexcept)
258                         {
259                                 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
260                         }
261                 }
262                 else
263                 {
264                         fd_ref_table[sock]->AddWriteBuf(tb);
265                 }
266                 ServerInstance->stats->statsSent += bytes;
267         }
268         else
269                 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
270 }
271
272 /** WriteServ()
273  *  Same as Write(), except `text' is prefixed with `:server.name '.
274  */
275 void WriteServ(int sock, char* text, ...)
276 {
277         va_list argsPtr;
278         char textbuffer[MAXBUF];
279         char tb[MAXBUF];
280         int bytes;
281
282         if ((sock < 0) || (sock > MAX_DESCRIPTORS))
283                 return;
284
285         if (!text)
286         {
287                 log(DEFAULT,"*** BUG *** WriteServ was given an invalid parameter");
288                 return;
289         }
290
291         va_start(argsPtr, text);
292         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
293         va_end(argsPtr);
294         bytes = snprintf(tb,MAXBUF,":%s %s\r\n",Config->ServerName,textbuffer);
295         chop(tb);
296
297         if (fd_ref_table[sock])
298         {
299                 if (Config->GetIOHook(fd_ref_table[sock]->port))
300                 {
301                         try
302                         {
303                                 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
304                         }
305                         catch (ModuleException& modexcept)
306                         {
307                                 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
308                         }
309                 }
310                 else
311                 {
312                         fd_ref_table[sock]->AddWriteBuf(tb);
313                 }
314
315                 ServerInstance->stats->statsSent += bytes;
316         }
317         else
318                 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
319 }
320
321 /** WriteFrom_NoFormat()
322  * Write `text' to a socket with fd `sock' prefixed with `:n!u@h' - taken from
323  * the nick, user, and host of `user'.
324  */
325 void WriteFrom_NoFormat(int sock, userrec *user, const char* text)
326 {
327         char tb[MAXBUF];
328         int bytes;
329
330         if ((sock < 0) || (!text) || (!user) || (sock > MAX_DESCRIPTORS))
331                 return;
332
333         bytes = snprintf(tb,MAXBUF,":%s %s\r\n",user->GetFullHost(),text);
334         chop(tb);
335
336         if (fd_ref_table[sock])
337         {
338                 if (Config->GetIOHook(fd_ref_table[sock]->port))
339                 {
340                         try
341                         {
342                                 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
343                         }
344                         catch (ModuleException& modexcept)
345                         {
346                                 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
347                         }
348                 }
349                 else
350                 {
351                         fd_ref_table[sock]->AddWriteBuf(tb);
352                 }
353                 ServerInstance->stats->statsSent += bytes;
354         }
355         else
356                 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
357 }
358
359 /* write text from an originating user to originating user */
360
361 void WriteFrom(int sock, userrec *user,char* text, ...)
362 {
363         va_list argsPtr;
364         char textbuffer[MAXBUF];
365         char tb[MAXBUF];
366         int bytes;
367
368         if ((sock < 0) || (sock > MAX_DESCRIPTORS))
369                 return;
370
371         if ((!text) || (!user))
372         {
373                 log(DEFAULT,"*** BUG *** WriteFrom was given an invalid parameter");
374                 return;
375         }
376
377         va_start(argsPtr, text);
378         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
379         va_end(argsPtr);
380         bytes = snprintf(tb,MAXBUF,":%s %s\r\n",user->GetFullHost(),textbuffer);
381         chop(tb);
382
383         if (fd_ref_table[sock])
384         {
385                 if (Config->GetIOHook(fd_ref_table[sock]->port))
386                 {
387                         try
388                         {
389                                 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
390                         }
391                         catch (ModuleException& modexcept)
392                         {
393                                 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
394                         }
395                 }
396                 else
397                 {
398                         fd_ref_table[sock]->AddWriteBuf(tb);
399                 }
400
401                 ServerInstance->stats->statsSent += bytes;
402         }
403         else
404                 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
405 }
406
407 /* write text to an destination user from a source user (e.g. user privmsg) */
408
409 void WriteTo(userrec *source, userrec *dest,char *data, ...)
410 {
411         char textbuffer[MAXBUF];
412         va_list argsPtr;
413
414         if ((!dest) || (!data))
415         {
416                 log(DEFAULT,"*** BUG *** WriteTo was given an invalid parameter");
417                 return;
418         }
419
420         if (!IS_LOCAL(dest))
421                 return;
422
423         va_start(argsPtr, data);
424         vsnprintf(textbuffer, MAXBUF, data, argsPtr);
425         va_end(argsPtr);
426         chop(textbuffer);
427
428         // if no source given send it from the server.
429         if (!source)
430         {
431                 WriteServ_NoFormat(dest->fd,textbuffer);
432         }
433         else
434         {
435                 WriteFrom_NoFormat(dest->fd,source,textbuffer);
436         }
437 }
438
439 void WriteTo_NoFormat(userrec *source, userrec *dest, const char *data)
440 {
441         if ((!dest) || (!data))
442                 return;
443
444         if (!source)
445         {
446                 WriteServ_NoFormat(dest->fd,data);
447         }
448         else
449         {
450                 WriteFrom_NoFormat(dest->fd,source,data);
451         }
452 }
453
454 /* write formatted text from a source user to all users on a channel
455  * including the sender (NOT for privmsg, notice etc!) */
456
457 void WriteChannel(chanrec* Ptr, userrec* user, char* text, ...)
458 {
459         char textbuffer[MAXBUF];
460         va_list argsPtr;
461         CUList *ulist;
462
463         if ((!Ptr) || (!user) || (!text))
464         {
465                 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
466                 return;
467         }
468
469         va_start(argsPtr, text);
470         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
471         va_end(argsPtr);
472
473         ulist = Ptr->GetUsers();
474
475         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
476         {
477                 if (i->second->fd != FD_MAGIC_NUMBER)
478                         WriteTo_NoFormat(user,i->second,textbuffer);
479         }
480 }
481
482 void WriteChannel_NoFormat(chanrec* Ptr, userrec* user, const char* text)
483 {
484         CUList *ulist;
485
486         if ((!Ptr) || (!user) || (!text))
487         {
488                 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
489                 return;
490         }
491
492         ulist = Ptr->GetUsers();
493
494         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
495         {
496                 if (i->second->fd != FD_MAGIC_NUMBER)
497                         WriteTo_NoFormat(user,i->second,text);
498         }
499 }
500
501
502 /* write formatted text from a source user to all users on a channel
503  * including the sender (NOT for privmsg, notice etc!) doesnt send to
504  * users on remote servers */
505
506 void WriteChannelLocal(chanrec* Ptr, userrec* user, char* text, ...)
507 {
508         char textbuffer[MAXBUF];
509         va_list argsPtr;
510         CUList *ulist;
511
512         if ((!Ptr) || (!text))
513         {
514                 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
515                 return;
516         }
517
518         va_start(argsPtr, text);
519         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
520         va_end(argsPtr);
521
522         ulist = Ptr->GetUsers();
523
524         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
525         {
526                 if ((i->second->fd != FD_MAGIC_NUMBER) && (i->second != user))
527                 {
528                         if (!user)
529                         {
530                                 WriteServ_NoFormat(i->second->fd,textbuffer);
531                         }
532                         else
533                         {
534                                 WriteTo_NoFormat(user,i->second,textbuffer);
535                         }
536                 }
537         }
538 }
539
540 void WriteChannelLocal_NoFormat(chanrec* Ptr, userrec* user, const char* text)
541 {
542         CUList *ulist;
543
544         if ((!Ptr) || (!text))
545         {
546                 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
547                 return;
548         }
549
550         ulist = Ptr->GetUsers();
551
552         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
553         {
554                 if ((i->second->fd != FD_MAGIC_NUMBER) && (i->second != user))
555                 {
556                         if (!user)
557                         {
558                                 WriteServ_NoFormat(i->second->fd,text);
559                         }
560                         else
561                         {
562                                 WriteTo_NoFormat(user,i->second,text);
563                         }
564                 }
565         }
566 }
567
568
569
570 void WriteChannelWithServ(char* ServName, chanrec* Ptr, char* text, ...)
571 {
572         char textbuffer[MAXBUF];
573         va_list argsPtr;
574         CUList *ulist;
575
576         if ((!Ptr) || (!text))
577         {
578                 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
579                 return;
580         }
581
582         va_start(argsPtr, text);
583         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
584         va_end(argsPtr);
585
586         ulist = Ptr->GetUsers();
587
588         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
589         {
590                 if (IS_LOCAL(i->second))
591                         WriteServ_NoFormat(i->second->fd,textbuffer);
592         }
593 }
594
595 void WriteChannelWithServ_NoFormat(char* ServName, chanrec* Ptr, const char* text)
596 {
597         CUList *ulist;
598
599         if ((!Ptr) || (!text))
600         {
601                 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
602                 return;
603         }
604
605         ulist = Ptr->GetUsers();
606
607         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
608         {
609                 if (IS_LOCAL(i->second))
610                         WriteServ_NoFormat(i->second->fd,text);
611         }
612 }
613
614
615
616 /* write formatted text from a source user to all users on a channel except
617  * for the sender (for privmsg etc) */
618
619 void ChanExceptSender(chanrec* Ptr, userrec* user, char status, char* text, ...)
620 {
621         char textbuffer[MAXBUF];
622         va_list argsPtr;
623         CUList *ulist;
624
625         if ((!Ptr) || (!user) || (!text))
626         {
627                 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
628                 return;
629         }
630
631         va_start(argsPtr, text);
632         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
633         va_end(argsPtr);
634
635         switch (status)
636         {
637                 case '@':
638                         ulist = Ptr->GetOppedUsers();
639                         break;
640                 case '%':
641                         ulist = Ptr->GetHalfoppedUsers();
642                         break;
643                 case '+':
644                         ulist = Ptr->GetVoicedUsers();
645                         break;
646                 default:
647                         ulist = Ptr->GetUsers();
648                         break;
649         }
650
651         log(DEBUG,"%d users to write to",ulist->size());
652
653         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
654         {
655                 if ((IS_LOCAL(i->second)) && (user != i->second))
656                         WriteFrom_NoFormat(i->second->fd,user,textbuffer);
657         }
658 }
659
660 void ChanExceptSender_NoFormat(chanrec* Ptr, userrec* user, char status, const char* text)
661 {
662         CUList *ulist;
663
664         if ((!Ptr) || (!user) || (!text))
665         {
666                 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
667                 return;
668         }
669
670         switch (status)
671         {
672                 case '@':
673                         ulist = Ptr->GetOppedUsers();
674                         break;  
675                 case '%':
676                         ulist = Ptr->GetHalfoppedUsers();
677                         break;
678                 case '+':
679                         ulist = Ptr->GetVoicedUsers();
680                         break;
681                 default:
682                         ulist = Ptr->GetUsers();
683                         break;
684         }
685
686         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
687         {
688                 if ((IS_LOCAL(i->second)) && (user != i->second))
689                         WriteFrom_NoFormat(i->second->fd,user,text);
690         }
691 }
692
693 std::string GetServerDescription(char* servername)
694 {
695         std::string description = "";
696
697         FOREACH_MOD(I_OnGetServerDescription,OnGetServerDescription(servername,description));
698
699         if (description != "")
700         {
701                 return description;
702         }
703         else
704         {
705                 // not a remote server that can be found, it must be me.
706                 return Config->ServerDesc;
707         }
708 }
709
710 /* write a formatted string to all users who share at least one common
711  * channel, including the source user e.g. for use in NICK */
712
713 void WriteCommon(userrec *u, char* text, ...)
714 {
715         char textbuffer[MAXBUF];
716         va_list argsPtr;
717         bool sent_to_at_least_one = false;
718
719         if (!u)
720         {
721                 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
722                 return;
723         }
724
725         if (u->registered != 7)
726         {
727                 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
728                 return;
729         }
730
731         va_start(argsPtr, text);
732         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
733         va_end(argsPtr);
734
735         // FIX: Stops a message going to the same person more than once
736         memset(&already_sent,0,MAX_DESCRIPTORS);
737
738         for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
739         {
740                 if (((ucrec*)(*v))->channel)
741                 {
742                         CUList *ulist= ((ucrec*)(*v))->channel->GetUsers();
743
744                         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
745                         {
746                                 if ((i->second->fd > -1) && (!already_sent[i->second->fd]))
747                                 {
748                                         already_sent[i->second->fd] = 1;
749                                         WriteFrom_NoFormat(i->second->fd,u,textbuffer);
750                                         sent_to_at_least_one = true;
751                                 }
752                         }
753                 }
754         }
755
756         /*
757          * if the user was not in any channels, no users will receive the text. Make sure the user
758          * receives their OWN message for WriteCommon
759          */
760         if (!sent_to_at_least_one)
761         {
762                 WriteFrom_NoFormat(u->fd,u,textbuffer);
763         }
764 }
765
766 void WriteCommon_NoFormat(userrec *u, const char* text)
767 {
768         bool sent_to_at_least_one = false;
769
770         if (!u)
771         {
772                 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
773                 return;
774         }
775
776         if (u->registered != 7)
777         {
778                 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
779                 return;
780         }
781
782         // FIX: Stops a message going to the same person more than once
783         memset(&already_sent,0,MAX_DESCRIPTORS);
784
785         for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
786         {
787                 if (((ucrec*)(*v))->channel)
788                 {
789                         CUList *ulist= ((ucrec*)(*v))->channel->GetUsers();
790
791                         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
792                         {
793                                 if ((i->second->fd > -1) && (!already_sent[i->second->fd]))
794                                 {
795                                         already_sent[i->second->fd] = 1;
796                                         WriteFrom_NoFormat(i->second->fd,u,text);
797                                         sent_to_at_least_one = true;
798                                 }
799                         }
800                 }
801         }
802
803         /*
804          * if the user was not in any channels, no users will receive the text. Make sure the user
805          * receives their OWN message for WriteCommon
806          */
807         if (!sent_to_at_least_one)
808         {
809                 WriteFrom_NoFormat(u->fd,u,text);
810         }
811 }
812
813
814 /* write a formatted string to all users who share at least one common
815  * channel, NOT including the source user e.g. for use in QUIT
816  */
817
818 void WriteCommonExcept(userrec *u, char* text, ...)
819 {
820         char textbuffer[MAXBUF];
821         char oper_quit[MAXBUF];
822         bool quit_munge = false;
823         va_list argsPtr;
824         int total;
825
826         if (!u)
827         {
828                 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
829                 return;
830         }
831
832         if (u->registered != 7)
833         {
834                 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
835                 return;
836         }
837
838         va_start(argsPtr, text);
839         total = vsnprintf(textbuffer, MAXBUF, text, argsPtr);
840         va_end(argsPtr);
841
842         if ((Config->HideSplits) && (total > 6))
843         {
844                 /* Yeah yeah, this is ugly. But its fast, live with it. */
845                 char* check = textbuffer;
846
847                 if ((*check++ == 'Q') && (*check++ == 'U') && (*check++ == 'I') && (*check++ == 'T') && (*check++ == ' ') && (*check++ == ':'))
848                 {
849                         std::stringstream split(check);
850                         std::string server_one;
851                         std::string server_two;
852
853                         split >> server_one;
854                         split >> server_two;
855
856                         if ((FindServerName(server_one)) && (FindServerName(server_two)))
857                         {
858                                 strlcpy(oper_quit,textbuffer,MAXQUIT);
859                                 strlcpy(check,"*.net *.split",MAXQUIT);
860                                 quit_munge = true;
861                         }
862                 }
863         }
864
865         if ((Config->HideBans) && (total > 13) && (!quit_munge))
866         {
867                 char* check = textbuffer;
868
869                 /* XXX - as above */
870                 if ((*check++ == 'Q') && (*check++ == 'U') && (*check++ == 'I') && (*check++ == 'T') && (*check++ == ' ') && (*check++ == ':'))
871                 {
872                         check++;
873
874                         if ((*check++ == '-') && (*check++ == 'L') && (*check++ == 'i') && (*check++ == 'n') && (*check++ == 'e') && (*check++ == 'd') && (*check++ == ':'))
875                         {
876                                 strlcpy(oper_quit,textbuffer,MAXQUIT);
877                                 *(--check) = 0;         // We don't need to strlcpy, we just chop it from the :
878                                 quit_munge = true;
879                         }
880                 }
881         }
882
883         memset(&already_sent,0,MAX_DESCRIPTORS);
884
885         for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
886         {
887                 if (((ucrec*)(*v))->channel)
888                 {
889                         CUList *ulist= ((ucrec*)(*v))->channel->GetUsers();
890
891                         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
892                         {
893                                 if (u != i->second)
894                                 {
895                                         if ((i->second->fd > -1) && (!already_sent[i->second->fd]))
896                                         {
897                                                 already_sent[i->second->fd] = 1;
898
899                                                 if (quit_munge)
900                                                 {
901                                                         WriteFrom_NoFormat(i->second->fd,u,*i->second->oper ? oper_quit : textbuffer);
902                                                 }
903                                                 else
904                                                         WriteFrom_NoFormat(i->second->fd,u,textbuffer);
905                                         }
906                                 }
907                         }
908                 }
909         }
910 }
911
912 void WriteCommonExcept_NoFormat(userrec *u, const char* text)
913 {
914         if (!u)
915         {
916                 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
917                 return;
918         }
919  
920         if (u->registered != 7)
921         {
922                 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
923                 return;
924         }
925
926         memset(&already_sent,0,MAX_DESCRIPTORS);
927
928         for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
929         {
930                 if (((ucrec*)(*v))->channel)
931                 {
932                         CUList *ulist= ((ucrec*)(*v))->channel->GetUsers();
933
934                         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
935                         {
936                                 if (u != i->second)
937                                 {
938                                         if ((i->second->fd > -1) && (!already_sent[i->second->fd]))
939                                         {
940                                                 already_sent[i->second->fd] = 1;
941                                                 WriteFrom_NoFormat(i->second->fd,u,text);
942                                         }
943                                 }
944                         }
945                 }
946         }
947 }
948
949
950 /* XXX - We don't use WriteMode for this because WriteMode is very slow and
951  * this isnt. Basically WriteMode has to iterate ALL the users 'n' times for
952  * the number of modes provided, e.g. if you send WriteMode 'og' to write to
953  * opers with globops, and you have 2000 users, thats 4000 iterations. WriteOpers
954  * uses the oper list, which means if you have 2000 users but only 5 opers,
955  * it iterates 5 times.
956  */
957 void WriteOpers(char* text, ...)
958 {
959         char textbuffer[MAXBUF];
960         va_list argsPtr;
961
962         if (!text)
963         {
964                 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
965                 return;
966         }
967
968         va_start(argsPtr, text);
969         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
970         va_end(argsPtr);
971
972         for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
973         {
974                 userrec* a = *i;
975
976                 if (IS_LOCAL(a))
977                 {
978                         if (strchr(a->modes,'s'))
979                         {
980                                 // send server notices to all with +s
981                                 WriteServ(a->fd,"NOTICE %s :%s",a->nick,textbuffer);
982                         }
983                 }
984         }
985 }
986
987 void ServerNoticeAll(char* text, ...)
988 {
989         if (!text)
990                 return;
991
992         char textbuffer[MAXBUF];
993         va_list argsPtr;
994         va_start (argsPtr, text);
995         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
996         va_end(argsPtr);
997
998         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
999         {
1000                 userrec* t = (userrec*)(*i);
1001                 WriteServ(t->fd,"NOTICE $%s :%s",Config->ServerName,textbuffer);
1002         }
1003 }
1004
1005 void ServerPrivmsgAll(char* text, ...)
1006 {
1007         if (!text)
1008                 return;
1009
1010         char textbuffer[MAXBUF];
1011         va_list argsPtr;
1012         va_start (argsPtr, text);
1013         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1014         va_end(argsPtr);
1015
1016         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1017         {
1018                 userrec* t = (userrec*)(*i);
1019                 WriteServ(t->fd,"PRIVMSG $%s :%s",Config->ServerName,textbuffer);
1020         }
1021 }
1022
1023 void WriteMode(const char* modes, int flags, const char* text, ...)
1024 {
1025         char textbuffer[MAXBUF];
1026         int modelen;
1027         va_list argsPtr;
1028
1029         if ((!text) || (!modes) || (!flags))
1030         {
1031                 log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
1032                 return;
1033         }
1034
1035         va_start(argsPtr, text);
1036         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1037         va_end(argsPtr);
1038         modelen = strlen(modes);
1039
1040         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1041         {
1042                 userrec* t = (userrec*)(*i);
1043                 bool send_to_user = false;
1044
1045                 if (flags == WM_AND)
1046                 {
1047                         send_to_user = true;
1048
1049                         for (int n = 0; n < modelen; n++)
1050                         {
1051                                 if (!hasumode(t,modes[n]))
1052                                 {
1053                                         send_to_user = false;
1054                                         break;
1055                                 }
1056                         }
1057                 }
1058                 else if (flags == WM_OR)
1059                 {
1060                         send_to_user = false;
1061
1062                         for (int n = 0; n < modelen; n++)
1063                         {
1064                                 if (hasumode(t,modes[n]))
1065                                 {
1066                                         send_to_user = true;
1067                                         break;
1068                                 }
1069                         }
1070                 }
1071
1072                 if (send_to_user)
1073                 {
1074                         WriteServ(t->fd,"NOTICE %s :%s",t->nick,textbuffer);
1075                 }
1076         }
1077 }
1078
1079 void NoticeAll(userrec *source, bool local_only, char* text, ...)
1080 {
1081         char textbuffer[MAXBUF];
1082         va_list argsPtr;
1083
1084         if ((!text) || (!source))
1085         {
1086                 log(DEFAULT,"*** BUG *** NoticeAll was given an invalid parameter");
1087                 return;
1088         }
1089
1090         va_start(argsPtr, text);
1091         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1092         va_end(argsPtr);
1093
1094         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1095         {
1096                 userrec* t = (userrec*)(*i);
1097                 WriteFrom(t->fd,source,"NOTICE $* :%s",textbuffer);
1098         }
1099 }
1100
1101
1102 void WriteWallOps(userrec *source, bool local_only, char* text, ...)
1103 {
1104         char textbuffer[MAXBUF];
1105         va_list argsPtr;
1106
1107         if ((!text) || (!source))
1108         {
1109                 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
1110                 return;
1111         }
1112
1113         va_start(argsPtr, text);
1114         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1115         va_end(argsPtr);
1116
1117         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1118         {
1119                 userrec* t = (userrec*)(*i);
1120
1121                 if ((IS_LOCAL(t)) && (strchr(t->modes,'w')))
1122                 {
1123                         WriteTo(source,t,"WALLOPS :%s",textbuffer);
1124                 }
1125         }
1126 }
1127
1128 /* convert a string to lowercase. Note following special circumstances
1129  * taken from RFC 1459. Many "official" server branches still hold to this
1130  * rule so i will too;
1131  *
1132  *  Because of IRC's scandanavian origin, the characters {}| are
1133  *  considered to be the lower case equivalents of the characters []\,
1134  *  respectively. This is a critical issue when determining the
1135  *  equivalence of two nicknames.
1136  */
1137 void strlower(char *n)
1138 {
1139         if (n)
1140         {
1141                 for (char* t = n; *t; t++)
1142                         *t = lowermap[(unsigned)*t];
1143         }
1144 }
1145
1146 /* Find a user record by nickname and return a pointer to it */
1147
1148 userrec* Find(std::string nick)
1149 {
1150         user_hash::iterator iter = clientlist.find(nick);
1151
1152         if (iter == clientlist.end())
1153                 /* Couldn't find it */
1154                 return NULL;
1155
1156         return iter->second;
1157 }
1158
1159 userrec* Find(const char* nick)
1160 {
1161         user_hash::iterator iter;
1162
1163         if (!nick)
1164                 return NULL;
1165
1166         iter = clientlist.find(nick);
1167         
1168         if (iter == clientlist.end())
1169                 return NULL;
1170
1171         return iter->second;
1172 }
1173
1174 /* find a channel record by channel name and return a pointer to it */
1175
1176 chanrec* FindChan(const char* chan)
1177 {
1178         chan_hash::iterator iter;
1179
1180         if (!chan)
1181         {
1182                 log(DEFAULT,"*** BUG *** Findchan was given an invalid parameter");
1183                 return NULL;
1184         }
1185
1186         iter = chanlist.find(chan);
1187
1188         if (iter == chanlist.end())
1189                 /* Couldn't find it */
1190                 return NULL;
1191
1192         return iter->second;
1193 }
1194
1195
1196 long GetMaxBans(char* name)
1197 {
1198         std::string x;
1199         for (std::map<std::string,int>::iterator n = Config->maxbans.begin(); n != Config->maxbans.end(); n++)
1200         {
1201                 x = n->first;
1202                 if (match(name,x.c_str()))
1203                 {
1204                         return n->second;
1205                 }
1206         }
1207         return 64;
1208 }
1209
1210 void purge_empty_chans(userrec* u)
1211 {
1212         std::vector<chanrec*> to_delete;
1213
1214         // firstly decrement the count on each channel
1215         for (std::vector<ucrec*>::iterator f = u->chans.begin(); f != u->chans.end(); f++)
1216         {
1217                 if (((ucrec*)(*f))->channel)
1218                 {
1219                         if (((ucrec*)(*f))->channel->DelUser(u) == 0)
1220                         {
1221                                 /* No users left in here, mark it for deletion */
1222                                 to_delete.push_back(((ucrec*)(*f))->channel);
1223                                 ((ucrec*)(*f))->channel = NULL;
1224                         }
1225                 }
1226         }
1227
1228         log(DEBUG,"purge_empty_chans: %d channels to delete",to_delete.size());
1229
1230         for (std::vector<chanrec*>::iterator n = to_delete.begin(); n != to_delete.end(); n++)
1231         {
1232                 chanrec* thischan = (chanrec*)*n;
1233                 chan_hash::iterator i2 = chanlist.find(thischan->name);
1234                 if (i2 != chanlist.end())
1235                 {
1236                         FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(i2->second));
1237                         delete i2->second;
1238                         chanlist.erase(i2);
1239                 }
1240         }
1241
1242         if (*u->oper)
1243                 DeleteOper(u);
1244 }
1245
1246
1247 char* chanmodes(chanrec *chan, bool showkey)
1248 {
1249         static char scratch[MAXBUF];
1250         static char sparam[MAXBUF];
1251         char* offset = scratch;
1252
1253         if (!chan)
1254         {
1255                 log(DEFAULT,"*** BUG *** chanmodes was given an invalid parameter");
1256                 *scratch = '\0';
1257                 return scratch;
1258         }
1259
1260         *scratch = '\0';
1261         *sparam = '\0';
1262
1263         if (chan->binarymodes & CM_NOEXTERNAL)
1264                 *offset++ = 'n';
1265         if (chan->binarymodes & CM_TOPICLOCK)
1266                 *offset++ = 't';
1267         if (*chan->key)
1268                 *offset++ = 'k';
1269         if (chan->limit)
1270                 *offset++ = 'l';
1271         if (chan->binarymodes & CM_INVITEONLY)
1272                 *offset++ = 'i';
1273         if (chan->binarymodes & CM_MODERATED)
1274                 *offset++ = 'm';
1275         if (chan->binarymodes & CM_SECRET)
1276                 *offset++ = 's';
1277         if (chan->binarymodes & CM_PRIVATE)
1278                 *offset++ = 'p';
1279
1280         if (*chan->key)
1281         {
1282                 snprintf(sparam,MAXBUF," %s",showkey ? chan->key : "<key>");
1283         }
1284
1285         if (chan->limit)
1286         {
1287                 char foo[24];
1288                 sprintf(foo," %lu",(unsigned long)chan->limit);
1289                 strlcat(sparam,foo,MAXBUF);
1290         }
1291
1292         /* This was still iterating up to 190, chanrec::custom_modes is only 64 elements -- Om */
1293         for(int n = 0; n < 64; n++)
1294         {
1295                 if(chan->custom_modes[n])
1296                 {
1297                         *offset++ = n+65;
1298                         std::string extparam = chan->GetModeParameter(n+65);
1299
1300                         if (extparam != "")
1301                         {
1302                                 charlcat(sparam,' ',MAXBUF);
1303                                 strlcat(sparam,extparam.c_str(),MAXBUF);
1304                         }
1305                 }
1306         }
1307
1308         /* Null terminate scratch */
1309         *offset = '\0';
1310         strlcat(scratch,sparam,MAXMODES);
1311         return scratch;
1312 }
1313
1314
1315 /* compile a userlist of a channel into a string, each nick seperated by
1316  * spaces and op, voice etc status shown as @ and + */
1317
1318 void userlist(userrec *user,chanrec *c)
1319 {
1320         if ((!c) || (!user))
1321         {
1322                 log(DEFAULT,"*** BUG *** userlist was given an invalid parameter");
1323                 return;
1324         }
1325
1326         size_t dlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1327         size_t initial = dlen;
1328
1329         CUList *ulist= c->GetUsers();
1330
1331         /* Improvement by Brain - this doesnt change in value, so why was it inside
1332          * the loop?
1333          */
1334         bool has_user = c->HasUser(user);
1335
1336         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
1337         {
1338                 if ((!has_user) && (strchr(i->second->modes,'i')))
1339                 {
1340                         /*
1341                          * user is +i, and source not on the channel, does not show
1342                          * nick in NAMES list
1343                          */
1344                         continue;
1345                 }
1346
1347                 dlen += strlcat(list,cmode(i->second,c),MAXBUF);
1348                 dlen += strlcat(list,i->second->nick,MAXBUF);
1349                 charlcat(list,' ',MAXBUF);
1350                 dlen++;
1351
1352                 if (dlen > (480-NICKMAX))
1353                 {
1354                         /* list overflowed into multiple numerics */
1355                         WriteServ_NoFormat(user->fd,list);
1356                         dlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1357                 }
1358         }
1359
1360         /* if whats left in the list isnt empty, send it */
1361         if (dlen != initial)
1362         {
1363                 WriteServ_NoFormat(user->fd,list);
1364         }
1365 }
1366
1367 /*
1368  * return a count of the users on a specific channel accounting for
1369  * invisible users who won't increase the count. e.g. for /LIST
1370  */
1371 int usercount_i(chanrec *c)
1372 {
1373         int count = 0;
1374
1375         if (!c)
1376                 return 0;
1377
1378         CUList *ulist= c->GetUsers();
1379         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
1380         {
1381                 if (!strchr(i->second->modes,'i'))
1382                         count++;
1383         }
1384
1385         return count;
1386 }
1387
1388 int usercount(chanrec *c)
1389 {
1390         return (c ? c->GetUserCounter() : 0);
1391 }
1392
1393
1394 /* looks up a users password for their connection class (<ALLOW>/<DENY> tags) */
1395 ConnectClass GetClass(userrec *user)
1396 {
1397         for (ClassVector::iterator i = Config->Classes.begin(); i != Config->Classes.end(); i++)
1398         {
1399                 if (match(user->host,i->host.c_str()))
1400                 {
1401                         return *i;
1402                 }
1403         }
1404
1405         return *(Config->Classes.begin());
1406 }
1407
1408 /*
1409  * sends out an error notice to all connected clients (not to be used
1410  * lightly!)
1411  */
1412 void send_error(char *s)
1413 {
1414         log(DEBUG,"send_error: %s",s);
1415
1416         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1417         {
1418                 userrec* t = (userrec*)(*i);
1419                 if (t->registered == 7)
1420                 {
1421                         WriteServ(t->fd,"NOTICE %s :%s",t->nick,s);
1422                 }
1423                 else
1424                 {
1425                         // fix - unregistered connections receive ERROR, not NOTICE
1426                         Write(t->fd,"ERROR :%s",s);
1427                 }
1428         }
1429 }
1430
1431 void Error(int status)
1432 {
1433         signal(SIGALRM, SIG_IGN);
1434         signal(SIGPIPE, SIG_IGN);
1435         signal(SIGTERM, SIG_IGN);
1436         signal(SIGABRT, SIG_IGN);
1437         signal(SIGSEGV, SIG_IGN);
1438         signal(SIGURG, SIG_IGN);
1439         signal(SIGKILL, SIG_IGN);
1440         log(DEFAULT,"*** fell down a pothole in the road to perfection ***");
1441         send_error("Error! Segmentation fault! save meeeeeeeeeeeeee *splat!*");
1442         Exit(status);
1443 }
1444
1445 // this function counts all users connected, wether they are registered or NOT.
1446 int usercnt(void)
1447 {
1448         return clientlist.size();
1449 }
1450
1451 // this counts only registered users, so that the percentages in /MAP don't mess up when users are sitting in an unregistered state
1452 int registered_usercount(void)
1453 {
1454         int c = 0;
1455
1456         for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1457         {
1458                 if (i->second->registered == 7) c++;
1459         }
1460
1461         return c;
1462 }
1463
1464 int usercount_invisible(void)
1465 {
1466         int c = 0;
1467
1468         for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1469         {
1470                 if ((i->second->registered == 7) && (strchr(i->second->modes,'i')))
1471                         c++;
1472         }
1473
1474         return c;
1475 }
1476
1477 int usercount_opers(void)
1478 {
1479         int c = 0;
1480
1481         for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1482         {
1483                 if (i->second->oper)
1484                         c++;
1485         }
1486         return c;
1487 }
1488
1489 int usercount_unknown(void)
1490 {
1491         int c = 0;
1492
1493         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1494         {
1495                 userrec* t = (userrec*)(*i);
1496                 if (t->registered != 7)
1497                         c++;
1498         }
1499
1500         return c;
1501 }
1502
1503 long chancount(void)
1504 {
1505         return chanlist.size();
1506 }
1507
1508 long local_count()
1509 {
1510         int c = 0;
1511
1512         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1513         {
1514                 userrec* t = (userrec*)(*i);
1515                 if (t->registered == 7)
1516                         c++;
1517         }
1518
1519         return c;
1520 }
1521
1522 void ShowMOTD(userrec *user)
1523 {
1524         static char mbuf[MAXBUF];
1525         static char crud[MAXBUF];
1526         std::string WholeMOTD = "";
1527
1528         if (!Config->MOTD.size())
1529         {
1530                 WriteServ(user->fd,"422 %s :Message of the day file is missing.",user->nick);
1531                 return;
1532         }
1533
1534         snprintf(crud,MAXBUF,":%s 372 %s :- ", Config->ServerName, user->nick);
1535         snprintf(mbuf,MAXBUF,":%s 375 %s :- %s message of the day\r\n", Config->ServerName, user->nick, Config->ServerName);
1536         WholeMOTD = WholeMOTD + mbuf;
1537
1538         for (unsigned int i = 0; i < Config->MOTD.size(); i++)
1539                 WholeMOTD = WholeMOTD + std::string(crud) + Config->MOTD[i].c_str() + std::string("\r\n");
1540
1541         snprintf(mbuf,MAXBUF,":%s 376 %s :End of message of the day.\r\n", Config->ServerName, user->nick);
1542         WholeMOTD = WholeMOTD + mbuf;
1543
1544         // only one write operation
1545         if (Config->GetIOHook(user->port))
1546         {
1547                 try
1548                 {
1549                         Config->GetIOHook(user->port)->OnRawSocketWrite(user->fd,(char*)WholeMOTD.c_str(),WholeMOTD.length());
1550                 }
1551                 catch (ModuleException& modexcept)
1552                 {
1553                         log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
1554                 }
1555         }
1556         else
1557         {
1558                 user->AddWriteBuf(WholeMOTD);
1559         }
1560
1561         ServerInstance->stats->statsSent += WholeMOTD.length();
1562 }
1563
1564 void ShowRULES(userrec *user)
1565 {
1566         if (!Config->RULES.size())
1567         {
1568                 WriteServ(user->fd,"NOTICE %s :Rules file is missing.",user->nick);
1569                 return;
1570         }
1571         WriteServ(user->fd,"NOTICE %s :%s rules",user->nick,Config->ServerName);
1572
1573         for (unsigned int i = 0; i < Config->RULES.size(); i++)
1574                 WriteServ(user->fd,"NOTICE %s :%s",user->nick,Config->RULES[i].c_str());
1575
1576         WriteServ(user->fd,"NOTICE %s :End of %s rules.",user->nick,Config->ServerName);
1577 }
1578
1579 // this returns 1 when all modules are satisfied that the user should be allowed onto the irc server
1580 // (until this returns true, a user will block in the waiting state, waiting to connect up to the
1581 // registration timeout maximum seconds)
1582 bool AllModulesReportReady(userrec* user)
1583 {
1584         if (!Config->global_implementation[I_OnCheckReady])
1585                 return true;
1586
1587         for (int i = 0; i <= MODCOUNT; i++)
1588         {
1589                 if (Config->implement_lists[i][I_OnCheckReady])
1590                 {
1591                         int res = modules[i]->OnCheckReady(user);
1592                         if (!res)
1593                                 return false;
1594                 }
1595         }
1596
1597         return true;
1598 }
1599
1600 bool DirValid(char* dirandfile)
1601 {
1602         char work[MAXBUF];
1603         char buffer[MAXBUF];
1604         char otherdir[MAXBUF];
1605         int p;
1606
1607         strlcpy(work, dirandfile, MAXBUF);
1608         p = strlen(work);
1609
1610         // we just want the dir
1611         while (*work)
1612         {
1613                 if (work[p] == '/')
1614                 {
1615                         work[p] = '\0';
1616                         break;
1617                 }
1618
1619                 work[p--] = '\0';
1620         }
1621
1622         // Get the current working directory
1623         if (getcwd(buffer, MAXBUF ) == NULL )
1624                 return false;
1625
1626         chdir(work);
1627
1628         if (getcwd(otherdir, MAXBUF ) == NULL )
1629                 return false;
1630
1631         chdir(buffer);
1632
1633         size_t t = strlen(work);
1634
1635         if (strlen(otherdir) >= t)
1636         {
1637                 otherdir[t] = '\0';
1638
1639                 if (!strcmp(otherdir,work))
1640                 {
1641                         return true;
1642                 }
1643
1644                 return false;
1645         }
1646         else
1647         {
1648                 return false;
1649         }
1650 }
1651
1652 std::string GetFullProgDir(char** argv, int argc)
1653 {
1654         char work[MAXBUF];
1655         char buffer[MAXBUF];
1656         char otherdir[MAXBUF];
1657         int p;
1658
1659         strlcpy(work,argv[0],MAXBUF);
1660         p = strlen(work);
1661
1662         // we just want the dir
1663         while (*work)
1664         {
1665                 if (work[p] == '/')
1666                 {
1667                         work[p] = '\0';
1668                         break;
1669                 }
1670
1671                 work[p--] = '\0';
1672         }
1673
1674         // Get the current working directory
1675         if (getcwd(buffer, MAXBUF) == NULL)
1676                 return "";
1677
1678         chdir(work);
1679
1680         if (getcwd(otherdir, MAXBUF) == NULL)
1681                 return "";
1682
1683         chdir(buffer);
1684         return otherdir;
1685 }
1686
1687 int InsertMode(std::string &output, const char* mode, unsigned short section)
1688 {
1689         unsigned short currsection = 1;
1690         unsigned int pos = output.find("CHANMODES=", 0) + 10; // +10 for the length of "CHANMODES="
1691         
1692         if(section > 4 || section == 0)
1693         {
1694                 log(DEBUG, "InsertMode: CHANMODES doesn't have a section %dh :/", section);
1695                 return 0;
1696         }
1697         
1698         for(; pos < output.size(); pos++)
1699         {
1700                 if(section == currsection)
1701                         break;
1702                         
1703                 if(output[pos] == ',')
1704                         currsection++;
1705         }
1706         
1707         output.insert(pos, mode);
1708         return 1;
1709 }
1710
1711 bool IsValidChannelName(const char *chname)
1712 {
1713         char *c;
1714
1715         /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
1716         if (!chname || *chname != '#')
1717         {
1718                 return false;
1719         }
1720
1721         c = (char *)chname + 1;
1722         while (*c)
1723         {
1724                 switch (*c)
1725                 {
1726                         case ' ':
1727                         case ',':
1728                         case 7:
1729                                 return false;
1730                 }
1731
1732                 c++;
1733         }
1734                 
1735         /* too long a name - note funky pointer arithmetic here. */
1736         if ((c - chname) > CHANMAX)
1737         {
1738                         return false;
1739         }
1740
1741         return true;
1742 }
1743
1744 inline int charlcat(char* x,char y,int z)
1745 {
1746         char* x__n = x;
1747         int v = 0;
1748
1749         while(*x__n++)
1750                 v++;
1751
1752         if (v < z - 1)
1753         {
1754                 *--x__n = y;
1755                 *++x__n = 0;
1756         }
1757
1758         return v;
1759 }
1760
1761 bool charremove(char* mp, char remove)
1762 {
1763         char* mptr = mp;
1764         bool shift_down = false;
1765
1766         while (*mptr)
1767         {
1768                 if (*mptr == remove)
1769                 shift_down = true;
1770
1771                 if (shift_down)
1772                         *mptr = *(mptr+1);
1773
1774                 mptr++;
1775         }
1776
1777         return shift_down;
1778 }