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