]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/commands.cpp
Fixed some craq++
[user/henk/code/inspircd.git] / src / commands.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
19 #include "inspircd_config.h"
20 #include "inspircd.h"
21 #include "inspircd_io.h"
22 #include "inspircd_util.h"
23 #include <unistd.h>
24 #include <sys/errno.h>
25 #include <sys/ioctl.h>
26 #include <sys/utsname.h>
27 #include <cstdio>
28 #include <time.h>
29 #include <string>
30 #ifdef GCC3
31 #include <ext/hash_map>
32 #else
33 #include <hash_map>
34 #endif
35 #include <map>
36 #include <sstream>
37 #include <vector>
38 #include <deque>
39 #include <sys/types.h>
40 #include <sys/time.h>
41 #include <sys/resource.h>
42 #ifdef THREADED_DNS
43 #include <pthread.h>
44 #endif
45 #ifndef RUSAGE_SELF
46 #define   RUSAGE_SELF     0
47 #define   RUSAGE_CHILDREN     -1
48 #endif
49 #include "users.h"
50 #include "ctables.h"
51 #include "globals.h"
52 #include "modules.h"
53 #include "dynamic.h"
54 #include "wildcard.h"
55 #include "message.h"
56 #include "commands.h"
57 #include "mode.h"
58 #include "xline.h"
59 #include "inspstring.h"
60 #include "dnsqueue.h"
61 #include "helperfuncs.h"
62 #include "hashcomp.h"
63 #include "socketengine.h"
64 #include "typedefs.h"
65
66 extern SocketEngine* SE;
67 extern ServerConfig* Config;
68
69 extern int MODCOUNT;
70 extern std::vector<Module*> modules;
71 extern std::vector<ircd_module*> factory;
72 extern int WHOWAS_STALE;
73 extern int WHOWAS_MAX;
74 extern time_t startup_time;
75 extern time_t TIME;
76 extern int boundPortCount;
77 extern int portCount;
78 extern int ports[MAXSOCKS];
79
80 const long duration_m = 60;
81 const long duration_h = duration_m * 60;
82 const long duration_d = duration_h * 24;
83 const long duration_w = duration_d * 7;
84 const long duration_y = duration_w * 52;
85
86 extern user_hash clientlist;
87 extern chan_hash chanlist;
88 extern whowas_hash whowas;
89 extern command_table cmdlist;
90 extern address_cache IP;
91
92 extern std::vector<userrec*> all_opers;
93
94 // This table references users by file descriptor.
95 // its an array to make it VERY fast, as all lookups are referenced
96 // by an integer, meaning there is no need for a scan/search operation.
97 extern userrec* fd_ref_table[65536];
98
99 extern serverstats* stats;
100
101 void handle_join(char **parameters, int pcnt, userrec *user)
102 {
103         chanrec* Ptr;
104         
105         if (loop_call(handle_join,parameters,pcnt,user,0,0,1))
106                 return;
107         if (parameters[0][0] == '#')
108         {
109                 Ptr = add_channel(user,parameters[0],parameters[1],false);
110         }
111 }
112
113
114 void handle_part(char **parameters, int pcnt, userrec *user)
115 {
116         if (pcnt > 1)
117         {
118                 if (loop_call(handle_part,parameters,pcnt,user,0,pcnt-2,0))
119                         return;
120                 del_channel(user,parameters[0],parameters[1],false);
121         }
122         else
123         {
124                 if (loop_call(handle_part,parameters,pcnt,user,0,pcnt-1,0))
125                         return;
126                 del_channel(user,parameters[0],NULL,false);
127         }
128 }
129
130 void handle_commands(char **parameters, int pcnt, userrec *user)
131 {
132         for (unsigned int i = 0; i < cmdlist.size(); i++)
133         {
134                 WriteServ(user->fd,"902 %s :%s %s %d",user->nick,cmdlist[i].command,cmdlist[i].source,cmdlist[i].min_params);
135         }
136         WriteServ(user->fd,"903 %s :End of COMMANDS list",user->nick);
137 }
138
139 void handle_kick(char **parameters, int pcnt, userrec *user)
140 {
141         chanrec* Ptr = FindChan(parameters[0]);
142         userrec* u   = Find(parameters[1]);
143
144         if ((!u) || (!Ptr))
145         {
146                 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
147                 return;
148         }
149         
150         if ((!has_channel(user,Ptr)) && (!is_uline(user->server)))
151         {
152                 WriteServ(user->fd,"442 %s %s :You're not on that channel!",user->nick, parameters[0]);
153                 return;
154         }
155         
156         char reason[MAXBUF];
157         
158         if (pcnt > 2)
159         {
160                 strlcpy(reason,parameters[2],MAXBUF);
161                 if (strlen(reason)>MAXKICK)
162                 {
163                         reason[MAXKICK-1] = '\0';
164                 }
165
166                 kick_channel(user,u,Ptr,reason);
167         }
168         else
169         {
170                 strlcpy(reason,user->nick,MAXBUF);
171                 kick_channel(user,u,Ptr,reason);
172         }
173         
174 }
175
176 void handle_loadmodule(char **parameters, int pcnt, userrec *user)
177 {
178         if (LoadModule(parameters[0]))
179         {
180                 WriteOpers("*** NEW MODULE: %s",parameters[0]);
181                 WriteServ(user->fd,"975 %s %s :Module successfully loaded.",user->nick, parameters[0]);
182         }
183         else
184         {
185                 WriteServ(user->fd,"974 %s %s :Failed to load module: %s",user->nick, parameters[0],ModuleError());
186         }
187 }
188
189 void handle_unloadmodule(char **parameters, int pcnt, userrec *user)
190 {
191         if (UnloadModule(parameters[0]))
192         {
193                 WriteOpers("*** MODULE UNLOADED: %s",parameters[0]);
194                 WriteServ(user->fd,"973 %s %s :Module successfully unloaded.",user->nick, parameters[0]);
195         }
196         else
197         {
198                 WriteServ(user->fd,"972 %s %s :Failed to unload module: %s",user->nick, parameters[0],ModuleError());
199         }
200 }
201
202 void handle_die(char **parameters, int pcnt, userrec *user)
203 {
204         log(DEBUG,"die: %s",user->nick);
205         if (!strcmp(parameters[0],Config->diepass))
206         {
207                 WriteOpers("*** DIE command from %s!%s@%s, terminating...",user->nick,user->ident,user->host);
208                 sleep(Config->DieDelay);
209                 Exit(ERROR);
210         }
211         else
212         {
213                 WriteOpers("*** Failed DIE Command from %s!%s@%s.",user->nick,user->ident,user->host);
214         }
215 }
216
217 void handle_restart(char **parameters, int pcnt, userrec *user)
218 {
219         char *argv[32];
220         log(DEFAULT,"Restart: %s",user->nick);
221         if (!strcmp(parameters[0],Config->restartpass))
222         {
223                 WriteOpers("*** RESTART command from %s!%s@%s, restarting server.",user->nick,user->ident,user->host);
224
225                 argv[0] = Config->MyExecutable;
226                 argv[1] = "-wait";
227                 if (Config->nofork)
228                 {
229                         argv[2] = "-nofork";
230                 }
231                 else
232                 {
233                         argv[2] = NULL;
234                 }
235                 argv[3] = NULL;
236                 
237                 // close ALL file descriptors
238                 send_error("Server restarting.");
239                 sleep(1);
240                 for (int i = 0; i < 65536; i++)
241                 {
242                         shutdown(i,2);
243                         close(i);
244                 }
245                 sleep(2);
246                 
247                 execv(Config->MyExecutable,argv);
248
249                 exit(0);
250         }
251         else
252         {
253                 WriteOpers("*** Failed RESTART Command from %s!%s@%s.",user->nick,user->ident,user->host);
254         }
255 }
256
257 void handle_kill(char **parameters, int pcnt, userrec *user)
258 {
259         userrec *u = Find(parameters[0]);
260         char killreason[MAXBUF];
261
262         log(DEBUG,"kill: %s %s",parameters[0],parameters[1]);
263         if (u)
264         {
265                 log(DEBUG,"into kill mechanism");
266                 int MOD_RESULT = 0;
267                 FOREACH_RESULT(OnKill(user,u,parameters[1]));
268                 if (MOD_RESULT) {
269                         log(DEBUG,"A module prevented the kill with result %d",MOD_RESULT);
270                         return;
271                 }
272
273                 if (u->fd < 0)
274                 {
275                         // remote kill
276                         WriteOpers("*** Remote kill by %s: %s!%s@%s (%s)",user->nick,u->nick,u->ident,u->host,parameters[1]);
277                         snprintf(killreason,MAXBUF,"[%s] Killed (%s (%s))",Config->ServerName,user->nick,parameters[1]);
278                         WriteCommonExcept(u,"QUIT :%s",killreason);
279
280                         FOREACH_MOD OnRemoteKill(user,u,killreason);
281                         
282                         user_hash::iterator iter = clientlist.find(u->nick);
283                         if (iter != clientlist.end())
284                         {
285                                 log(DEBUG,"deleting user hash value %d",iter->second);
286                                 clientlist.erase(iter);
287                         }
288                         if (u->registered == 7)
289                         {
290                                 purge_empty_chans(u);
291                         }
292                         if (u->fd > -1)
293                                 fd_ref_table[u->fd] = NULL;
294                         delete u;
295                 }
296                 else
297                 {
298                         // local kill
299                         log(DEFAULT,"LOCAL KILL: %s :%s!%s!%s (%s)", u->nick, Config->ServerName,user->dhost,user->nick,parameters[1]);
300                         WriteTo(user, u, "KILL %s :%s!%s!%s (%s)", u->nick, Config->ServerName,user->dhost,user->nick,parameters[1]);
301                         WriteOpers("*** Local Kill by %s: %s!%s@%s (%s)",user->nick,u->nick,u->ident,u->host,parameters[1]);
302                         snprintf(killreason,MAXBUF,"Killed (%s (%s))",user->nick,parameters[1]);
303                         kill_link(u,killreason);
304                 }
305         }
306         else
307         {
308                 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
309         }
310 }
311
312 void handle_summon(char **parameters, int pcnt, userrec *user)
313 {
314         WriteServ(user->fd,"445 %s :SUMMON has been disabled (depreciated command)",user->nick);
315 }
316
317 void handle_users(char **parameters, int pcnt, userrec *user)
318 {
319         WriteServ(user->fd,"445 %s :USERS has been disabled (depreciated command)",user->nick);
320 }
321
322 void handle_pass(char **parameters, int pcnt, userrec *user)
323 {
324         // Check to make sure they havnt registered -- Fix by FCS
325         if (user->registered == 7)
326         {
327                 WriteServ(user->fd,"462 %s :You may not reregister",user->nick);
328                 return;
329         }
330         strlcpy(user->password,parameters[0],MAXBUF);
331         if (!strcasecmp(parameters[0],Passwd(user)))
332         {
333                 user->haspassed = true;
334         }
335 }
336
337 void handle_invite(char **parameters, int pcnt, userrec *user)
338 {
339         if (pcnt == 2)
340         {
341                 userrec* u = Find(parameters[0]);
342                 chanrec* c = FindChan(parameters[1]);
343
344                 if ((!c) || (!u))
345                 {
346                         if (!c)
347                         {
348                                 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[1]);
349                         }
350                         else
351                         {
352                                 if (c->binarymodes & CM_INVITEONLY)
353                                 {
354                                         WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
355                                 }
356                         }
357
358                         return;
359                 }
360
361                 if (c->binarymodes & CM_INVITEONLY)
362                 {
363                         if (cstatus(user,c) < STATUS_HOP)
364                         {
365                                 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, c->name);
366                                 return;
367                         }
368                 }
369                 if (has_channel(u,c))
370                 {
371                         WriteServ(user->fd,"443 %s %s %s :Is already on channel %s",user->nick,u->nick,c->name,c->name);
372                         return;
373                 }
374                 if (!has_channel(user,c))
375                 {
376                         WriteServ(user->fd,"442 %s %s :You're not on that channel!",user->nick, c->name);
377                         return;
378                 }
379
380                 int MOD_RESULT = 0;
381                 FOREACH_RESULT(OnUserPreInvite(user,u,c));
382                 if (MOD_RESULT == 1) {
383                         return;
384                 }
385
386                 u->InviteTo(c->name);
387                 WriteFrom(u->fd,user,"INVITE %s :%s",u->nick,c->name);
388                 WriteServ(user->fd,"341 %s %s %s",user->nick,u->nick,c->name);
389                 FOREACH_MOD OnUserInvite(user,u,c);
390         }
391         else
392         {
393                 // pinched from ircu - invite with not enough parameters shows channels
394                 // youve been invited to but haven't joined yet.
395                 InvitedList* il = user->GetInviteList();
396                 for (InvitedList::iterator i = il->begin(); i != il->end(); i++)
397                 {
398                         if (i->channel) {
399                                 WriteServ(user->fd,"346 %s :%s",user->nick,i->channel);
400                         }
401                 }
402                 WriteServ(user->fd,"347 %s :End of INVITE list",user->nick);
403         }
404 }
405
406 void handle_topic(char **parameters, int pcnt, userrec *user)
407 {
408         chanrec* Ptr;
409
410         if (pcnt == 1)
411         {
412                 if (strlen(parameters[0]) <= CHANMAX)
413                 {
414                         Ptr = FindChan(parameters[0]);
415                         if (Ptr)
416                         {
417                                 if (((Ptr) && (!has_channel(user,Ptr))) && (Ptr->binarymodes & CM_SECRET))
418                                 {
419                                         WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, Ptr->name);
420                                         return;
421                                 }
422                                 if (Ptr->topicset)
423                                 {
424                                         WriteServ(user->fd,"332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
425                                         WriteServ(user->fd,"333 %s %s %s %d", user->nick, Ptr->name, Ptr->setby, Ptr->topicset);
426                                 }
427                                 else
428                                 {
429                                         WriteServ(user->fd,"331 %s %s :No topic is set.", user->nick, Ptr->name);
430                                 }
431                         }
432                         else
433                         {
434                                 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
435                         }
436                 }
437                 return;
438         }
439         else if (pcnt>1)
440         {
441                 if (strlen(parameters[0]) <= CHANMAX)
442                 {
443                         Ptr = FindChan(parameters[0]);
444                         if (Ptr)
445                         {
446                                 if ((Ptr) && (!has_channel(user,Ptr)))
447                                 {
448                                         WriteServ(user->fd,"442 %s %s :You're not on that channel!",user->nick, Ptr->name);
449                                         return;
450                                 }
451                                 if ((Ptr->binarymodes & CM_TOPICLOCK) && (cstatus(user,Ptr)<STATUS_HOP))
452                                 {
453                                         WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel", user->nick, Ptr->name);
454                                         return;
455                                 }
456
457                                 char topic[MAXBUF];
458                                 strlcpy(topic,parameters[1],MAXBUF);
459                                 if (strlen(topic)>MAXTOPIC)
460                                 {
461                                         topic[MAXTOPIC] = '\0';
462                                 }
463
464                                 if (user->fd > -1)
465                                 {
466                                         int MOD_RESULT = 0;
467                                         FOREACH_RESULT(OnLocalTopicChange(user,Ptr,topic));
468                                         if (MOD_RESULT)
469                                                 return;
470                                 }
471
472                                 strlcpy(Ptr->topic,topic,MAXTOPIC);
473                                 strlcpy(Ptr->setby,user->nick,NICKMAX);
474                                 Ptr->topicset = TIME;
475                                 WriteChannel(Ptr,user,"TOPIC %s :%s",Ptr->name, Ptr->topic);
476                                 if (user->fd > -1)
477                                 {
478                                         FOREACH_MOD OnPostLocalTopicChange(user,Ptr,topic);
479                                 }
480                         }
481                         else
482                         {
483                                 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
484                         }
485                 }
486         }
487 }
488
489 void handle_names(char **parameters, int pcnt, userrec *user)
490 {
491         chanrec* c;
492
493         if (!pcnt)
494         {
495                 WriteServ(user->fd,"366 %s * :End of /NAMES list.",user->nick);
496                 return;
497         }
498
499         if (loop_call(handle_names,parameters,pcnt,user,0,pcnt-1,0))
500                 return;
501         c = FindChan(parameters[0]);
502         if (c)
503         {
504                 if (((c) && (!has_channel(user,c))) && (c->binarymodes & CM_SECRET))
505                 {
506                       WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, c->name);
507                       return;
508                 }
509                 userlist(user,c);
510                 WriteServ(user->fd,"366 %s %s :End of /NAMES list.", user->nick, c->name);
511         }
512         else
513         {
514                 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
515         }
516 }
517
518 void handle_privmsg(char **parameters, int pcnt, userrec *user)
519 {
520         userrec *dest;
521         chanrec *chan;
522
523         user->idle_lastmsg = TIME;
524         
525         if (loop_call(handle_privmsg,parameters,pcnt,user,0,pcnt-2,0))
526                 return;
527         if (parameters[0][0] == '$')
528         {
529                 // notice to server mask
530                 char* servermask = parameters[0];
531                 servermask++;
532                 if (match(Config->ServerName,servermask))
533                 {
534                         ServerPrivmsgAll("%s",parameters[1]);
535                 }
536                 return;
537         }
538         else if (parameters[0][0] == '#')
539         {
540                 chan = FindChan(parameters[0]);
541                 if (chan)
542                 {
543                         if ((chan->binarymodes & CM_NOEXTERNAL) && (!has_channel(user,chan)))
544                         {
545                                 WriteServ(user->fd,"404 %s %s :Cannot send to channel (no external messages)", user->nick, chan->name);
546                                 return;
547                         }
548                         if ((chan->binarymodes & CM_MODERATED) && (cstatus(user,chan)<STATUS_VOICE))
549                         {
550                                 WriteServ(user->fd,"404 %s %s :Cannot send to channel (+m)", user->nick, chan->name);
551                                 return;
552                         }
553                         
554                         int MOD_RESULT = 0;
555
556                         std::string temp = parameters[1];
557                         FOREACH_RESULT(OnUserPreMessage(user,chan,TYPE_CHANNEL,temp));
558                         if (MOD_RESULT) {
559                                 return;
560                         }
561                         parameters[1] = (char*)temp.c_str();
562
563                         if (temp == "")
564                         {
565                                 WriteServ(user->fd,"412 %s No text to send", user->nick);
566                                 return;
567                         }
568                         
569                         ChanExceptSender(chan, user, "PRIVMSG %s :%s", chan->name, parameters[1]);
570                         FOREACH_MOD OnUserMessage(user,chan,TYPE_CHANNEL,parameters[1]);
571                 }
572                 else
573                 {
574                         /* no such nick/channel */
575                         WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
576                 }
577                 return;
578         }
579
580         dest = Find(parameters[0]);
581         if (dest)
582         {
583                 if (strcmp(dest->awaymsg,""))
584                 {
585                         /* auto respond with aweh msg */
586                         WriteServ(user->fd,"301 %s %s :%s",user->nick,dest->nick,dest->awaymsg);
587                 }
588
589                 int MOD_RESULT = 0;
590                 
591                 std::string temp = parameters[1];
592                 FOREACH_RESULT(OnUserPreMessage(user,dest,TYPE_USER,temp));
593                 if (MOD_RESULT) {
594                         return;
595                 }
596                 parameters[1] = (char*)temp.c_str();
597
598                 if (dest->fd > -1)
599                 {
600                         // direct write, same server
601                         WriteTo(user, dest, "PRIVMSG %s :%s", dest->nick, parameters[1]);
602                 }
603
604                 FOREACH_MOD OnUserMessage(user,dest,TYPE_USER,parameters[1]);
605         }
606         else
607         {
608                 /* no such nick/channel */
609                 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
610         }
611 }
612
613 void handle_notice(char **parameters, int pcnt, userrec *user)
614 {
615         userrec *dest;
616         chanrec *chan;
617
618         user->idle_lastmsg = TIME;
619         
620         if (loop_call(handle_notice,parameters,pcnt,user,0,pcnt-2,0))
621                 return;
622         if (parameters[0][0] == '$')
623         {
624                 // notice to server mask
625                 char* servermask = parameters[0];
626                 servermask++;
627                 if (match(Config->ServerName,servermask))
628                 {
629                         NoticeAll(user, true, "%s",parameters[1]);
630                 }
631                 return;
632         }
633         else if (parameters[0][0] == '#')
634         {
635                 chan = FindChan(parameters[0]);
636                 if (chan)
637                 {
638                         if ((chan->binarymodes & CM_NOEXTERNAL) && (!has_channel(user,chan)))
639                         {
640                                 WriteServ(user->fd,"404 %s %s :Cannot send to channel (no external messages)", user->nick, chan->name);
641                                 return;
642                         }
643                         if ((chan->binarymodes & CM_MODERATED) && (cstatus(user,chan)<STATUS_VOICE))
644                         {
645                                 WriteServ(user->fd,"404 %s %s :Cannot send to channel (+m)", user->nick, chan->name);
646                                 return;
647                         }
648
649                         int MOD_RESULT = 0;
650
651                         std::string temp = parameters[1];
652                         FOREACH_RESULT(OnUserPreNotice(user,chan,TYPE_CHANNEL,temp));
653                         if (MOD_RESULT) {
654                                 return;
655                         }
656                         parameters[1] = (char*)temp.c_str();
657
658                         if (temp == "")
659                         {
660                                 WriteServ(user->fd,"412 %s No text to send", user->nick);
661                                 return;
662                         }
663
664                         ChanExceptSender(chan, user, "NOTICE %s :%s", chan->name, parameters[1]);
665
666                         FOREACH_MOD OnUserNotice(user,chan,TYPE_CHANNEL,parameters[1]);
667                 }
668                 else
669                 {
670                         /* no such nick/channel */
671                         WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
672                 }
673                 return;
674         }
675         
676         dest = Find(parameters[0]);
677         if (dest)
678         {
679                 int MOD_RESULT = 0;
680                 
681                 std::string temp = parameters[1];
682                 FOREACH_RESULT(OnUserPreNotice(user,dest,TYPE_USER,temp));
683                 if (MOD_RESULT) {
684                         return;
685                 }
686                 parameters[1] = (char*)temp.c_str();
687
688                 if (dest->fd > -1)
689                 {
690                         // direct write, same server
691                         WriteTo(user, dest, "NOTICE %s :%s", dest->nick, parameters[1]);
692                 }
693
694                 FOREACH_MOD OnUserNotice(user,dest,TYPE_USER,parameters[1]);
695         }
696         else
697         {
698                 /* no such nick/channel */
699                 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
700         }
701 }
702
703 void handle_server(char **parameters, int pcnt, userrec *user)
704 {
705         WriteServ(user->fd,"666 %s :You cannot identify as a server, you are a USER. IRC Operators informed.",user->nick);
706         WriteOpers("*** WARNING: %s attempted to issue a SERVER command and is registered as a user!",user->nick);
707 }
708
709 void handle_info(char **parameters, int pcnt, userrec *user)
710 {
711         WriteServ(user->fd,"371 %s :. o O (The Inspire Internet Relay Chat Server) O o .",user->nick);
712         WriteServ(user->fd,"371 %s : ",user->nick);
713         WriteServ(user->fd,"371 %s :Core developers: Craig Edwards (Brain)",user->nick);
714         WriteServ(user->fd,"371 %s :                 Craig McLure",user->nick);
715         WriteServ(user->fd,"371 %s : ",user->nick);
716         WriteServ(user->fd,"371 %s :Contributors:    typobox43",user->nick);
717         WriteServ(user->fd,"371 %s :                 w00t",user->nick);
718         WriteServ(user->fd,"371 %s :                 Om",user->nick);
719         WriteServ(user->fd,"371 %s :                 Jazza",user->nick);
720         WriteServ(user->fd,"371 %s : ",user->nick);
721         WriteServ(user->fd,"371 %s :Testers:         CC",user->nick);
722         WriteServ(user->fd,"371 %s :                 Om",user->nick);
723         WriteServ(user->fd,"371 %s :                 Piggles",user->nick);
724         WriteServ(user->fd,"371 %s :                 Foamy",user->nick);
725         WriteServ(user->fd,"371 %s :                 Hart",user->nick);
726         WriteServ(user->fd,"371 %s :                 RageD",user->nick);
727         WriteServ(user->fd,"371 %s :                 [ed]",user->nick);
728         WriteServ(user->fd,"371 %s :                 Azhrarn",user->nick);
729         WriteServ(user->fd,"371 %s :                 nenolod",user->nick);
730         WriteServ(user->fd,"371 %s :                 luigiman",user->nick);
731         WriteServ(user->fd,"371 %s :                 Chu",user->nick);
732         WriteServ(user->fd,"371 %s :                 aquanight",user->nick);
733         WriteServ(user->fd,"371 %s :                 xptek",user->nick);
734         WriteServ(user->fd,"371 %s :                 Grantlinks",user->nick);
735         WriteServ(user->fd,"371 %s :                 Rob",user->nick);
736         WriteServ(user->fd,"371 %s :                 angelic",user->nick);
737         WriteServ(user->fd,"371 %s :                 Jason",user->nick);
738         WriteServ(user->fd,"371 %s :                 ThaPrince",user->nick);
739         WriteServ(user->fd,"371 %s : ",user->nick);
740         WriteServ(user->fd,"371 %s :Thanks to irc-junkie and searchirc",user->nick);
741         WriteServ(user->fd,"371 %s :for the nice comments and the help",user->nick);
742         WriteServ(user->fd,"371 %s :you gave us in attracting users to",user->nick);
743         WriteServ(user->fd,"371 %s :this software.",user->nick);
744         WriteServ(user->fd,"371 %s : ",user->nick);
745         WriteServ(user->fd,"371 %s :Best experienced with: An IRC client.",user->nick);
746         FOREACH_MOD OnInfo(user);
747         WriteServ(user->fd,"374 %s :End of /INFO list",user->nick);
748 }
749
750 void handle_time(char **parameters, int pcnt, userrec *user)
751 {
752         time_t rawtime;
753         struct tm * timeinfo;
754
755         time(&rawtime);
756         timeinfo = localtime(&rawtime);
757         WriteServ(user->fd,"391 %s %s :%s",user->nick,Config->ServerName,asctime(timeinfo));
758   
759 }
760
761 void handle_whois(char **parameters, int pcnt, userrec *user)
762 {
763         userrec *dest;
764         if (loop_call(handle_whois,parameters,pcnt,user,0,pcnt-1,0))
765                 return;
766         dest = Find(parameters[0]);
767         if (dest)
768         {
769                 do_whois(user,dest,0,0,parameters[0]);
770         }
771         else
772         {
773                 /* no such nick/channel */
774                 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
775                 WriteServ(user->fd,"318 %s %s :End of /WHOIS list.",user->nick, parameters[0]);
776         }
777 }
778
779 void split_chlist(userrec* user, userrec* dest, std::string &cl)
780 {
781         std::stringstream channels(cl);
782         std::string line = "";
783         std::string cname = "";
784         while (!channels.eof())
785         {
786                 channels >> cname;
787                 line = line + cname + " ";
788                 if (line.length() > 400)
789                 {
790                         WriteServ(user->fd,"319 %s %s :%s",user->nick, dest->nick, line.c_str());
791                         line = "";
792                 }
793         }
794         if (line.length())
795         {
796                 WriteServ(user->fd,"319 %s %s :%s",user->nick, dest->nick, line.c_str());
797         }
798 }
799
800 void do_whois(userrec* user, userrec* dest,unsigned long signon, unsigned long idle, char* nick)
801 {
802         // bug found by phidjit - were able to whois an incomplete connection if it had sent a NICK or USER
803         if (dest->registered == 7)
804         {
805                 WriteServ(user->fd,"311 %s %s %s %s * :%s",user->nick, dest->nick, dest->ident, dest->dhost, dest->fullname);
806                 if ((user == dest) || (strchr(user->modes,'o')))
807                 {
808                         WriteServ(user->fd,"378 %s %s :is connecting from *@%s %s",user->nick, dest->nick, dest->host, dest->ip);
809                 }
810                 std::string cl = chlist(dest,user);
811                 if (cl.length())
812                 {
813                         if (cl.length() > 400)
814                         {
815                                 split_chlist(user,dest,cl);
816                         }
817                         else
818                         {
819                                 WriteServ(user->fd,"319 %s %s :%s",user->nick, dest->nick, cl.c_str());
820                         }
821                 }
822                 WriteServ(user->fd,"312 %s %s %s :%s",user->nick, dest->nick, dest->server, GetServerDescription(dest->server).c_str());
823                 if (*dest->awaymsg)
824                 {
825                         WriteServ(user->fd,"301 %s %s :%s",user->nick, dest->nick, dest->awaymsg);
826                 }
827                 if (strchr(dest->modes,'o'))
828                 {
829                         if (*dest->oper)
830                         {
831                                 WriteServ(user->fd,"313 %s %s :is %s %s on %s",user->nick, dest->nick, (strchr("aeiou",dest->oper[0]) ? "an" : "a"),dest->oper, Config->Network);
832                         }
833                         else
834                         {
835                                 WriteServ(user->fd,"313 %s %s :is opered but has an unknown type",user->nick, dest->nick);
836                         }
837                 }
838                 if ((!signon) && (!idle))
839                 {
840                         FOREACH_MOD OnWhois(user,dest);
841                 }
842                 if (!strcasecmp(user->server,dest->server))
843                 {
844                         // idle time and signon line can only be sent if youre on the same server (according to RFC)
845                         WriteServ(user->fd,"317 %s %s %d %d :seconds idle, signon time",user->nick, dest->nick, abs((dest->idle_lastmsg)-TIME), dest->signon);
846                 }
847                 else
848                 {
849                         if ((idle) || (signon))
850                                 WriteServ(user->fd,"317 %s %s %d %d :seconds idle, signon time",user->nick, dest->nick, idle, signon);
851                 }
852                 WriteServ(user->fd,"318 %s %s :End of /WHOIS list.",user->nick, dest->nick);
853         }
854         else
855         {
856                 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, nick);
857                 WriteServ(user->fd,"318 %s %s :End of /WHOIS list.",user->nick, nick);
858         }
859 }
860
861 void handle_quit(char **parameters, int pcnt, userrec *user)
862 {
863         user_hash::iterator iter = clientlist.find(user->nick);
864         char* reason;
865
866         if (user->registered == 7)
867         {
868                 /* theres more to do here, but for now just close the socket */
869                 if (pcnt == 1)
870                 {
871                         if (parameters[0][0] == ':')
872                         {
873                                 *parameters[0]++;
874                         }
875                         reason = parameters[0];
876
877                         if (strlen(reason)>MAXQUIT)
878                         {
879                                 reason[MAXQUIT-1] = '\0';
880                         }
881
882                         /* We should only prefix the quit for a local user. Remote users have
883                          * already been prefixed, where neccessary, by the upstream server.
884                          */
885                         if (user->fd > -1)
886                         {
887                                 Write(user->fd,"ERROR :Closing link (%s@%s) [%s%s]",user->ident,user->host,Config->PrefixQuit,parameters[0]);
888                                 WriteOpers("*** Client exiting: %s!%s@%s [%s%s]",user->nick,user->ident,user->host,Config->PrefixQuit,parameters[0]);
889                                 WriteCommonExcept(user,"QUIT :%s%s",Config->PrefixQuit,parameters[0]);
890                         }
891                         else
892                         {
893                                 WriteOpers("*** Client exiting at %s: %s!%s@%s [%s]",user->server,user->nick,user->ident,user->host,parameters[0]);
894                                 WriteCommonExcept(user,"QUIT :%s",parameters[0]);
895                         }
896                         FOREACH_MOD OnUserQuit(user,std::string(Config->PrefixQuit)+std::string(parameters[0]));
897
898                 }
899                 else
900                 {
901                         Write(user->fd,"ERROR :Closing link (%s@%s) [QUIT]",user->ident,user->host);
902                         WriteOpers("*** Client exiting: %s!%s@%s [Client exited]",user->nick,user->ident,user->host);
903                         WriteCommonExcept(user,"QUIT :Client exited");
904                         FOREACH_MOD OnUserQuit(user,"Client exited");
905
906                 }
907                 AddWhoWas(user);
908         }
909
910         FOREACH_MOD OnUserDisconnect(user);
911
912         /* push the socket on a stack of sockets due to be closed at the next opportunity */
913         if (user->fd > -1)
914         {
915                 SE->DelFd(user->fd);
916                 user->CloseSocket();
917         }
918         
919         if (iter != clientlist.end())
920         {
921                 clientlist.erase(iter);
922         }
923
924         if (user->registered == 7) {
925                 purge_empty_chans(user);
926         }
927         if (user->fd > -1)
928                 fd_ref_table[user->fd] = NULL;
929         delete user;
930 }
931
932 void handle_who(char **parameters, int pcnt, userrec *user)
933 {
934         chanrec* Ptr = NULL;
935         char tmp[10];
936         
937         /* theres more to do here, but for now just close the socket */
938         if (pcnt == 1)
939         {
940                 if ((!strcmp(parameters[0],"0")) || (!strcmp(parameters[0],"*")))
941                 {
942                         if ((user->chans.size()) && (user->chans[0].channel))
943                         {
944                                 int n_list = 0;
945                                 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
946                                 {
947                                         Ptr = i->second->chans[0].channel;
948                                         // suggested by phidjit and FCS
949                                         if ((!common_channels(user,i->second)) && (isnick(i->second->nick)))
950                                         {
951                                                 // Bug Fix #29
952                                                 strcpy(tmp, "");
953                                                 if (strcmp(i->second->awaymsg, "")) {
954                                                         strlcat(tmp, "G", 9);
955                                                 } else {
956                                                         strlcat(tmp, "H", 9);
957                                                 }
958                                                 if (strchr(i->second->modes,'o')) { strlcat(tmp, "*", 9); }
959                                                 WriteServ(user->fd,"352 %s %s %s %s %s %s %s :0 %s",user->nick, Ptr ? Ptr->name : "*", i->second->ident, i->second->dhost, i->second->server, i->second->nick, tmp, i->second->fullname);
960                                                 n_list++;
961                                                 if (n_list > Config->MaxWhoResults)
962                                                 {
963                                                         WriteServ(user->fd,"523 %s WHO :Command aborted: More results than configured limit",user->nick);
964                                                         break;
965                                                 }
966                                         }
967                                 }
968                         }
969                         if (Ptr)
970                         {
971                                 WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick , parameters[0]);
972                         }
973                         else
974                         {
975                                 WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick, parameters[0]);
976                         }
977                         return;
978                 }
979                 if (parameters[0][0] == '#')
980                 {
981                         Ptr = FindChan(parameters[0]);
982                         if (Ptr)
983                         {
984                                 int n_list = 0;
985                                 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
986                                 {
987                                         if ((has_channel(i->second,Ptr)) && (isnick(i->second->nick)))
988                                         {
989                                                 // Fix Bug #29 - Part 2..
990                                                 strcpy(tmp, "");
991                                                 if (strcmp(i->second->awaymsg, "")) {
992                                                         strlcat(tmp, "G", 9);
993                                                 } else {
994                                                         strlcat(tmp, "H", 9);
995                                                 }
996                                                 if (strchr(i->second->modes,'o')) { strlcat(tmp, "*", 9); }
997                                                 strlcat(tmp, cmode(i->second, Ptr),5);
998                                                 WriteServ(user->fd,"352 %s %s %s %s %s %s %s :0 %s",user->nick, Ptr->name, i->second->ident, i->second->dhost, i->second->server, i->second->nick, tmp, i->second->fullname);
999                                                 n_list++;
1000                                                 if (n_list > Config->MaxWhoResults)
1001                                                 {
1002                                                         WriteServ(user->fd,"523 %s WHO :Command aborted: More results than configured limit",user->nick);
1003                                                         break;
1004                                                 }
1005
1006                                         }
1007                                 }
1008                                 WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick, parameters[0]);
1009                         }
1010                         else
1011                         {
1012                                 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
1013                         }
1014                 }
1015                 else
1016                 {
1017                         userrec* u = Find(parameters[0]);
1018                         if (u)
1019                         {
1020                                 // Bug Fix #29 -- Part 29..
1021                                 strcpy(tmp, "");
1022                                 if (strcmp(u->awaymsg, "")) {
1023                                         strlcat(tmp, "G" ,9);
1024                                 } else {
1025                                         strlcat(tmp, "H" ,9);
1026                                 }
1027                                 if (strchr(u->modes,'o')) { strlcat(tmp, "*" ,9); }
1028                                 WriteServ(user->fd,"352 %s %s %s %s %s %s %s :0 %s",user->nick, u->chans.size() ? u->chans[0].channel->name
1029                                 : "*", u->ident, u->dhost, u->server, u->nick, tmp, u->fullname);
1030                         }
1031                         WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick, parameters[0]);
1032                 }
1033         }
1034         if (pcnt == 2)
1035         {
1036                 if ((!strcmp(parameters[0],"0")) || (!strcmp(parameters[0],"*")) && (!strcmp(parameters[1],"o")))
1037                 {
1038                         for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
1039                         {
1040                                 // If i were a rich man.. I wouldn't need to me making these bugfixes..
1041                                 // But i'm a poor bastard with nothing better to do.
1042                                 userrec* oper = *i;
1043                                 strcpy(tmp, "");
1044                                 if (strcmp(oper->awaymsg, "")) {
1045                                         strlcat(tmp, "G" ,9);
1046                                 } else {
1047                                         strlcat(tmp, "H" ,9);
1048                                 }
1049                                 WriteServ(user->fd,"352 %s %s %s %s %s %s %s* :0 %s", user->nick, oper->chans.size() ? oper->chans[0].channel->name 
1050                                 : "*", oper->ident, oper->dhost, oper->server, oper->nick, tmp, oper->fullname);
1051                         }
1052                         WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick, parameters[0]);
1053                         return;
1054                 }
1055         }
1056 }
1057
1058 void handle_wallops(char **parameters, int pcnt, userrec *user)
1059 {
1060         WriteWallOps(user,false,"%s",parameters[0]);
1061         FOREACH_MOD OnWallops(user,parameters[0]);
1062 }
1063
1064 void handle_list(char **parameters, int pcnt, userrec *user)
1065 {
1066         WriteServ(user->fd,"321 %s Channel :Users Name",user->nick);
1067         for (chan_hash::const_iterator i = chanlist.begin(); i != chanlist.end(); i++)
1068         {
1069                 // if the channel is not private/secret, OR the user is on the channel anyway
1070                 if (((!(i->second->binarymodes & CM_PRIVATE)) && (!(i->second->binarymodes & CM_SECRET))) || (has_channel(user,i->second)))
1071                 {
1072                         WriteServ(user->fd,"322 %s %s %d :[+%s] %s",user->nick,i->second->name,usercount_i(i->second),chanmodes(i->second),i->second->topic);
1073                 }
1074         }
1075         WriteServ(user->fd,"323 %s :End of channel list.",user->nick);
1076 }
1077
1078
1079 void handle_rehash(char **parameters, int pcnt, userrec *user)
1080 {
1081         WriteServ(user->fd,"382 %s %s :Rehashing",user->nick,CleanFilename(CONFIG_FILE));
1082         std::string parameter = "";
1083         if (pcnt)
1084         {
1085                 parameter = parameters[0];
1086         }
1087         else
1088         {
1089                 WriteOpers("%s is rehashing config file %s",user->nick,CleanFilename(CONFIG_FILE));
1090                 Config->Read(false,user);
1091         }
1092         FOREACH_MOD OnRehash(parameter);
1093 }
1094
1095 void handle_lusers(char **parameters, int pcnt, userrec *user)
1096 {
1097         // this lusers command shows one server at all times because
1098         // a protocol module must override it to show those stats.
1099         WriteServ(user->fd,"251 %s :There are %d users and %d invisible on 1 server",user->nick,usercnt()-usercount_invisible(),usercount_invisible());
1100         WriteServ(user->fd,"252 %s %d :operator(s) online",user->nick,usercount_opers());
1101         WriteServ(user->fd,"253 %s %d :unknown connections",user->nick,usercount_unknown());
1102         WriteServ(user->fd,"254 %s %d :channels formed",user->nick,chancount());
1103         WriteServ(user->fd,"254 %s :I have %d clients and 0 servers",user->nick,local_count());
1104 }
1105
1106 void handle_admin(char **parameters, int pcnt, userrec *user)
1107 {
1108         WriteServ(user->fd,"256 %s :Administrative info for %s",user->nick,Config->ServerName);
1109         WriteServ(user->fd,"257 %s :Name     - %s",user->nick,Config->AdminName);
1110         WriteServ(user->fd,"258 %s :Nickname - %s",user->nick,Config->AdminNick);
1111         WriteServ(user->fd,"258 %s :E-Mail   - %s",user->nick,Config->AdminEmail);
1112 }
1113
1114 void handle_ping(char **parameters, int pcnt, userrec *user)
1115 {
1116         WriteServ(user->fd,"PONG %s :%s",Config->ServerName,parameters[0]);
1117 }
1118
1119 void handle_pong(char **parameters, int pcnt, userrec *user)
1120 {
1121         // set the user as alive so they survive to next ping
1122         user->lastping = 1;
1123 }
1124
1125 void handle_motd(char **parameters, int pcnt, userrec *user)
1126 {
1127         ShowMOTD(user);
1128 }
1129
1130 void handle_rules(char **parameters, int pcnt, userrec *user)
1131 {
1132         ShowRULES(user);
1133 }
1134
1135 void handle_user(char **parameters, int pcnt, userrec *user)
1136 {
1137         if (user->registered < 3)
1138         {
1139                 if (isident(parameters[0]) == 0) {
1140                         // This kinda Sucks, According to the RFC thou, its either this,
1141                         // or "You have already registered" :p -- Craig
1142                         WriteServ(user->fd,"461 %s USER :Not enough parameters",user->nick);
1143                 }
1144                 else {
1145                         /* We're not checking ident, but I'm not sure I like the idea of '~' prefixing.. */
1146                         /* XXX - Should this IDENTMAX + 1 be IDENTMAX - 1? Ok, users.h has it defined as
1147                          * char ident[IDENTMAX+2]; - WTF?
1148                          */
1149                         snprintf(user->ident, IDENTMAX+1, "~%s", parameters[0]);
1150                         strlcpy(user->fullname,parameters[3],MAXGECOS);
1151                         user->registered = (user->registered | 1);
1152                 }
1153         }
1154         else
1155         {
1156                 WriteServ(user->fd,"462 %s :You may not reregister",user->nick);
1157                 return;
1158         }
1159         /* parameters 2 and 3 are local and remote hosts, ignored when sent by client connection */
1160         if (user->registered == 3)
1161         {
1162                 /* user is registered now, bit 0 = USER command, bit 1 = sent a NICK command */
1163                 FOREACH_MOD OnUserRegister(user);
1164                 ConnectUser(user);
1165         }
1166 }
1167
1168 void handle_userhost(char **parameters, int pcnt, userrec *user)
1169 {
1170         char Return[MAXBUF],junk[MAXBUF];
1171         snprintf(Return,MAXBUF,"302 %s :",user->nick);
1172         for (int i = 0; i < pcnt; i++)
1173         {
1174                 userrec *u = Find(parameters[i]);
1175                 if (u)
1176                 {
1177                         if (strchr(u->modes,'o'))
1178                         {
1179                                 snprintf(junk,MAXBUF,"%s*=+%s@%s ",u->nick,u->ident,u->host);
1180                                 strlcat(Return,junk,MAXBUF);
1181                         }
1182                         else
1183                         {
1184                                 snprintf(junk,MAXBUF,"%s=+%s@%s ",u->nick,u->ident,u->host);
1185                                 strlcat(Return,junk,MAXBUF);
1186                         }
1187                 }
1188         }
1189         WriteServ(user->fd,Return);
1190 }
1191
1192
1193 void handle_ison(char **parameters, int pcnt, userrec *user)
1194 {
1195         char Return[MAXBUF];
1196         snprintf(Return,MAXBUF,"303 %s :",user->nick);
1197         for (int i = 0; i < pcnt; i++)
1198         {
1199                 userrec *u = Find(parameters[i]);
1200                 if (u)
1201                 {
1202                         strlcat(Return,u->nick,MAXBUF);
1203                         strlcat(Return," ",MAXBUF);
1204                 }
1205         }
1206         WriteServ(user->fd,Return);
1207 }
1208
1209
1210 void handle_away(char **parameters, int pcnt, userrec *user)
1211 {
1212         if (pcnt)
1213         {
1214                 strlcpy(user->awaymsg,parameters[0],MAXAWAY);
1215                 WriteServ(user->fd,"306 %s :You have been marked as being away",user->nick);
1216         }
1217         else
1218         {
1219                 strlcpy(user->awaymsg,"",MAXAWAY);
1220                 WriteServ(user->fd,"305 %s :You are no longer marked as being away",user->nick);
1221         }
1222 }
1223
1224 void handle_whowas(char **parameters, int pcnt, userrec* user)
1225 {
1226         whowas_hash::iterator i = whowas.find(parameters[0]);
1227
1228         if (i == whowas.end())
1229         {
1230                 WriteServ(user->fd,"406 %s %s :There was no such nickname",user->nick,parameters[0]);
1231                 WriteServ(user->fd,"369 %s %s :End of WHOWAS",user->nick,parameters[0]);
1232         }
1233         else
1234         {
1235                 time_t rawtime = i->second->signon;
1236                 tm *timeinfo;
1237                 char b[MAXBUF];
1238                 
1239                 timeinfo = localtime(&rawtime);
1240                 strlcpy(b,asctime(timeinfo),MAXBUF);
1241                 b[strlen(b)-1] = '\0';
1242                 
1243                 WriteServ(user->fd,"314 %s %s %s %s * :%s",user->nick,i->second->nick,i->second->ident,i->second->dhost,i->second->fullname);
1244                 WriteServ(user->fd,"312 %s %s %s :%s",user->nick,i->second->nick,i->second->server,b);
1245                 WriteServ(user->fd,"369 %s %s :End of WHOWAS",user->nick,parameters[0]);
1246         }
1247
1248 }
1249
1250 void handle_trace(char **parameters, int pcnt, userrec *user)
1251 {
1252         for (user_hash::iterator i = clientlist.begin(); i != clientlist.end(); i++)
1253         {
1254                 if (i->second)
1255                 {
1256                         if (isnick(i->second->nick))
1257                         {
1258                                 if (strchr(i->second->modes,'o'))
1259                                 {
1260                                         WriteServ(user->fd,"205 %s :Oper 0 %s",user->nick,i->second->nick);
1261                                 }
1262                                 else
1263                                 {
1264                                         WriteServ(user->fd,"204 %s :User 0 %s",user->nick,i->second->nick);
1265                                 }
1266                         }
1267                         else
1268                         {
1269                                 WriteServ(user->fd,"203 %s :???? 0 [%s]",user->nick,i->second->host);
1270                         }
1271                 }
1272         }
1273 }
1274
1275 void handle_modules(char **parameters, int pcnt, userrec *user)
1276 {
1277         for (unsigned int i = 0; i < Config->module_names.size(); i++)
1278         {
1279                 Version V = modules[i]->GetVersion();
1280                 char modulename[MAXBUF];
1281                 char flagstate[MAXBUF];
1282                 strcpy(flagstate,"");
1283                 if (V.Flags & VF_STATIC)
1284                         strlcat(flagstate,", static",MAXBUF);
1285                 if (V.Flags & VF_VENDOR)
1286                         strlcat(flagstate,", vendor",MAXBUF);
1287                 if (V.Flags & VF_COMMON)
1288                         strlcat(flagstate,", common",MAXBUF);
1289                 if (V.Flags & VF_SERVICEPROVIDER)
1290                         strlcat(flagstate,", service provider",MAXBUF);
1291                 if (!flagstate[0])
1292                         strcpy(flagstate,"  <no flags>");
1293                 strlcpy(modulename,Config->module_names[i].c_str(),256);
1294                 if (strchr(user->modes,'o'))
1295                 {
1296                         WriteServ(user->fd,"900 %s :0x%08lx %d.%d.%d.%d %s (%s)",user->nick,modules[i],V.Major,V.Minor,V.Revision,V.Build,CleanFilename(modulename),flagstate+2);
1297                 }
1298                 else
1299                 {
1300                         WriteServ(user->fd,"900 %s :%s",user->nick,CleanFilename(modulename));
1301                 }
1302         }
1303         WriteServ(user->fd,"901 %s :End of MODULES list",user->nick);
1304 }
1305
1306 void handle_stats(char **parameters, int pcnt, userrec *user)
1307 {
1308         if (pcnt != 1)
1309         {
1310                 return;
1311         }
1312         if (strlen(parameters[0])>1)
1313         {
1314                 /* make the stats query 1 character long */
1315                 parameters[0][1] = '\0';
1316         }
1317
1318
1319         FOREACH_MOD OnStats(*parameters[0]);
1320
1321         if (*parameters[0] == 'c')
1322         {
1323                 /* This stats symbol must be handled by a linking module */
1324         }
1325         
1326         if (*parameters[0] == 'i')
1327         {
1328                 int idx = 0;
1329                 for (ClassVector::iterator i = Config->Classes.begin(); i != Config->Classes.end(); i++)
1330                 {
1331                         WriteServ(user->fd,"215 %s I * * * %d %d %s *",user->nick,MAXCLIENTS,idx,Config->ServerName);
1332                         idx++;
1333                 }
1334         }
1335         
1336         if (*parameters[0] == 'y')
1337         {
1338                 int idx = 0;
1339                 for (ClassVector::iterator i = Config->Classes.begin(); i != Config->Classes.end(); i++)
1340                 {
1341                         WriteServ(user->fd,"218 %s Y %d %d 0 %d %d",user->nick,idx,120,i->flood,i->registration_timeout);
1342                         idx++;
1343                 }
1344         }
1345
1346         if (*parameters[0] == 'U')
1347         {
1348                 char ulined[MAXBUF];
1349                 for (int i = 0; i < Config->ConfValueEnum("uline",&Config->config_f); i++)
1350                 {
1351                         Config->ConfValue("uline","server",i,ulined,&Config->config_f);
1352                         WriteServ(user->fd,"248 %s U %s",user->nick,ulined);
1353                 }
1354         }
1355         
1356         if (*parameters[0] == 'P')
1357         {
1358                 int idx = 0;
1359                 for (user_hash::iterator i = clientlist.begin(); i != clientlist.end(); i++)
1360                 {
1361                         if (strchr(i->second->modes,'o'))
1362                         {
1363                                 WriteServ(user->fd,"249 %s :%s (%s@%s) Idle: %d",user->nick,i->second->nick,i->second->ident,i->second->dhost,(TIME - i->second->idle_lastmsg));
1364                                 idx++;
1365                         }
1366                 }
1367                 WriteServ(user->fd,"249 %s :%d OPER(s)",user->nick,idx);
1368         }
1369         
1370         if (*parameters[0] == 'k')
1371         {
1372                 stats_k(user);
1373         }
1374
1375         if (*parameters[0] == 'g')
1376         {
1377                 stats_g(user);
1378         }
1379
1380         if (*parameters[0] == 'q')
1381         {
1382                 stats_q(user);
1383         }
1384
1385         if (*parameters[0] == 'Z')
1386         {
1387                 stats_z(user);
1388         }
1389
1390         if (*parameters[0] == 'e')
1391         {
1392                 stats_e(user);
1393         }
1394
1395         /* stats m (list number of times each command has been used, plus bytecount) */
1396         if (*parameters[0] == 'm')
1397         {
1398                 for (unsigned int i = 0; i < cmdlist.size(); i++)
1399                 {
1400                         if (cmdlist[i].handler_function)
1401                         {
1402                                 if (cmdlist[i].use_count)
1403                                 {
1404                                         /* RPL_STATSCOMMANDS */
1405                                         WriteServ(user->fd,"212 %s %s %d %d",user->nick,cmdlist[i].command,cmdlist[i].use_count,cmdlist[i].total_bytes);
1406                                 }
1407                         }
1408                 }
1409                         
1410         }
1411
1412         /* stats z (debug and memory info) */
1413         if (*parameters[0] == 'z')
1414         {
1415                 rusage R;
1416                 WriteServ(user->fd,"249 %s :Users(HASH_MAP) %d (%d bytes, %d buckets)",user->nick,clientlist.size(),clientlist.size()*sizeof(userrec),clientlist.bucket_count());
1417                 WriteServ(user->fd,"249 %s :Channels(HASH_MAP) %d (%d bytes, %d buckets)",user->nick,chanlist.size(),chanlist.size()*sizeof(chanrec),chanlist.bucket_count());
1418                 WriteServ(user->fd,"249 %s :Commands(VECTOR) %d (%d bytes)",user->nick,cmdlist.size(),cmdlist.size()*sizeof(command_t));
1419                 WriteServ(user->fd,"249 %s :MOTD(VECTOR) %d, RULES(VECTOR) %d",user->nick,Config->MOTD.size(),Config->RULES.size());
1420                 WriteServ(user->fd,"249 %s :address_cache(HASH_MAP) %d (%d buckets)",user->nick,IP.size(),IP.bucket_count());
1421                 WriteServ(user->fd,"249 %s :Modules(VECTOR) %d (%d)",user->nick,modules.size(),modules.size()*sizeof(Module));
1422                 WriteServ(user->fd,"249 %s :ClassFactories(VECTOR) %d (%d)",user->nick,factory.size(),factory.size()*sizeof(ircd_module));
1423                 WriteServ(user->fd,"249 %s :Ports(STATIC_ARRAY) %d",user->nick,boundPortCount);
1424                 if (!getrusage(RUSAGE_SELF,&R))
1425                 {
1426                         WriteServ(user->fd,"249 %s :Total allocation: %luK (0x%lx)",user->nick,R.ru_maxrss,R.ru_maxrss);
1427                         WriteServ(user->fd,"249 %s :Signals:          %lu  (0x%lx)",user->nick,R.ru_nsignals,R.ru_nsignals);
1428                         WriteServ(user->fd,"249 %s :Page faults:      %lu  (0x%lx)",user->nick,R.ru_majflt,R.ru_majflt);
1429                         WriteServ(user->fd,"249 %s :Swaps:            %lu  (0x%lx)",user->nick,R.ru_nswap,R.ru_nswap);
1430                         WriteServ(user->fd,"249 %s :Context Switches: %lu  (0x%lx)",user->nick,R.ru_nvcsw+R.ru_nivcsw,R.ru_nvcsw+R.ru_nivcsw);
1431                 }
1432         }
1433
1434         if (*parameters[0] == 'T')
1435         {
1436                 WriteServ(user->fd,"249 Brain :accepts %d refused %d",stats->statsAccept,stats->statsRefused);
1437                 WriteServ(user->fd,"249 Brain :unknown commands %d",stats->statsUnknown);
1438                 WriteServ(user->fd,"249 Brain :nick collisions %d",stats->statsCollisions);
1439                 WriteServ(user->fd,"249 Brain :dns requests %d succeeded %d failed %d",stats->statsDns,stats->statsDnsGood,stats->statsDnsBad);
1440                 WriteServ(user->fd,"249 Brain :connections %d",stats->statsConnects);
1441                 WriteServ(user->fd,"249 Brain :bytes sent %dK recv %dK",(stats->statsSent / 1024),(stats->statsRecv / 1024));
1442         }
1443         
1444         /* stats o */
1445         if (*parameters[0] == 'o')
1446         {
1447                 for (int i = 0; i < Config->ConfValueEnum("oper",&Config->config_f); i++)
1448                 {
1449                         char LoginName[MAXBUF];
1450                         char HostName[MAXBUF];
1451                         char OperType[MAXBUF];
1452                         Config->ConfValue("oper","name",i,LoginName,&Config->config_f);
1453                         Config->ConfValue("oper","host",i,HostName,&Config->config_f);
1454                         Config->ConfValue("oper","type",i,OperType,&Config->config_f);
1455                         WriteServ(user->fd,"243 %s O %s * %s %s 0",user->nick,HostName,LoginName,OperType);
1456                 }
1457         }
1458         
1459         /* stats l (show user I/O stats) */
1460         if (*parameters[0] == 'l')
1461         {
1462                 WriteServ(user->fd,"211 %s :server:port nick bytes_in cmds_in bytes_out cmds_out",user->nick);
1463                 for (user_hash::iterator i = clientlist.begin(); i != clientlist.end(); i++)
1464                 {
1465                         if (isnick(i->second->nick))
1466                         {
1467                                 WriteServ(user->fd,"211 %s :%s:%d %s %d %d %d %d",user->nick,i->second->server,i->second->port,i->second->nick,i->second->bytes_in,i->second->cmds_in,i->second->bytes_out,i->second->cmds_out);
1468                         }
1469                         else
1470                         {
1471                                 WriteServ(user->fd,"211 %s :%s:%d (unknown@%d) %d %d %d %d",user->nick,i->second->server,i->second->port,i->second->fd,i->second->bytes_in,i->second->cmds_in,i->second->bytes_out,i->second->cmds_out);
1472                         }
1473                         
1474                 }
1475         }
1476         
1477         /* stats u (show server uptime) */
1478         if (*parameters[0] == 'u')
1479         {
1480                 time_t current_time = 0;
1481                 current_time = TIME;
1482                 time_t server_uptime = current_time - startup_time;
1483                 struct tm* stime;
1484                 stime = gmtime(&server_uptime);
1485                 /* i dont know who the hell would have an ircd running for over a year nonstop, but
1486                  * Craig suggested this, and it seemed a good idea so in it went */
1487                 if (stime->tm_year > 70)
1488                 {
1489                         WriteServ(user->fd,"242 %s :Server up %d years, %d days, %.2d:%.2d:%.2d",user->nick,(stime->tm_year-70),stime->tm_yday,stime->tm_hour,stime->tm_min,stime->tm_sec);
1490                 }
1491                 else
1492                 {
1493                         WriteServ(user->fd,"242 %s :Server up %d days, %.2d:%.2d:%.2d",user->nick,stime->tm_yday,stime->tm_hour,stime->tm_min,stime->tm_sec);
1494                 }
1495         }
1496
1497         WriteServ(user->fd,"219 %s %s :End of /STATS report",user->nick,parameters[0]);
1498         WriteOpers("*** Notice: Stats '%s' requested by %s (%s@%s)",parameters[0],user->nick,user->ident,user->host);
1499         
1500 }
1501
1502
1503 void handle_connect(char **parameters, int pcnt, userrec *user)
1504 {
1505 }
1506
1507 void handle_squit(char **parameters, int pcnt, userrec *user)
1508 {
1509 }
1510
1511 void handle_links(char **parameters, int pcnt, userrec *user)
1512 {
1513         WriteServ(user->fd,"364 %s %s %s :0 %s",user->nick,Config->ServerName,Config->ServerName,Config->ServerDesc);
1514         WriteServ(user->fd,"365 %s * :End of /LINKS list.",user->nick);
1515 }
1516
1517 void handle_map(char **parameters, int pcnt, userrec *user)
1518 {
1519         // as with /LUSERS this does nothing without a linking
1520         // module to override its behaviour and display something
1521         // better.
1522         WriteServ(user->fd,"006 %s :%s",user->nick,Config->ServerName);
1523         WriteServ(user->fd,"007 %s :End of /MAP",user->nick);
1524 }
1525
1526 bool is_uline(const char* server)
1527 {
1528         char ServName[MAXBUF];
1529
1530         if (!server)
1531                 return false;
1532         if (!(*server))
1533                 return true;
1534
1535         for (int i = 0; i < Config->ConfValueEnum("uline",&Config->config_f); i++)
1536         {
1537                 Config->ConfValue("uline","server",i,ServName,&Config->config_f);
1538                 if (!strcasecmp(server,ServName))
1539                 {
1540                         return true;
1541                 }
1542         }
1543         return false;
1544 }
1545 int operstrcmp(char* data,char* input)
1546 {
1547         int MOD_RESULT = 0;
1548         FOREACH_RESULT(OnOperCompare(data,input))
1549         log(DEBUG,"operstrcmp: %d",MOD_RESULT);
1550         if (MOD_RESULT == 1)
1551                 return 0;
1552         if (MOD_RESULT == -1)
1553                 return 1;
1554         log(DEBUG,"strcmp fallback: '%s' '%s' %d",data,input,strcmp(data,input));
1555         return strcmp(data,input);
1556 }
1557
1558 void handle_oper(char **parameters, int pcnt, userrec *user)
1559 {
1560         char LoginName[MAXBUF];
1561         char Password[MAXBUF];
1562         char OperType[MAXBUF];
1563         char TypeName[MAXBUF];
1564         char HostName[MAXBUF];
1565         char TheHost[MAXBUF];
1566         int j;
1567         bool found = false;
1568         bool fail2 = false;
1569
1570         snprintf(TheHost,MAXBUF,"%s@%s",user->ident,user->host);
1571
1572         for (int i = 0; i < Config->ConfValueEnum("oper",&Config->config_f); i++)
1573         {
1574                 Config->ConfValue("oper","name",i,LoginName,&Config->config_f);
1575                 Config->ConfValue("oper","password",i,Password,&Config->config_f);
1576                 Config->ConfValue("oper","type",i,OperType,&Config->config_f);
1577                 Config->ConfValue("oper","host",i,HostName,&Config->config_f);
1578                 if ((!strcmp(LoginName,parameters[0])) && (!operstrcmp(Password,parameters[1])) && (match(TheHost,HostName)))
1579                 {
1580                         fail2 = true;
1581                         for (j =0; j < Config->ConfValueEnum("type",&Config->config_f); j++)
1582                         {
1583                                 Config->ConfValue("type","name",j,TypeName,&Config->config_f);
1584
1585                                 if (!strcmp(TypeName,OperType))
1586                                 {
1587                                         /* found this oper's opertype */
1588                                         Config->ConfValue("type","host",j,HostName,&Config->config_f);
1589                                         if (*HostName)
1590                                                 ChangeDisplayedHost(user,HostName);
1591                                         strlcpy(user->oper,TypeName,NICKMAX);
1592                                         found = true;
1593                                         fail2 = false;
1594                                         break;
1595                                 }
1596                         }
1597                 }
1598                 if (found)
1599                         break;
1600         }
1601         if (found)
1602         {
1603                 /* correct oper credentials */
1604                 WriteOpers("*** %s (%s@%s) is now an IRC operator of type %s",user->nick,user->ident,user->host,OperType);
1605                 WriteServ(user->fd,"381 %s :You are now an IRC operator of type %s",user->nick,OperType);
1606                 if (!strchr(user->modes,'o'))
1607                 {
1608                         strcat(user->modes,"o");
1609                         WriteServ(user->fd,"MODE %s :+o",user->nick);
1610                         FOREACH_MOD OnOper(user,OperType);
1611                         log(DEFAULT,"OPER: %s!%s@%s opered as type: %s",user->nick,user->ident,user->host,OperType);
1612                         AddOper(user);
1613                 }
1614         }
1615         else
1616         {
1617                 if (!fail2)
1618                 {
1619                         WriteServ(user->fd,"491 %s :Invalid oper credentials",user->nick);
1620                         WriteOpers("*** WARNING! Failed oper attempt by %s!%s@%s!",user->nick,user->ident,user->host);
1621                         log(DEFAULT,"OPER: Failed oper attempt by %s!%s@%s: user, host or password did not match.",user->nick,user->ident,user->host);
1622                 }
1623                 else
1624                 {
1625                         WriteServ(user->fd,"491 %s :Your oper block does not have a valid opertype associated with it",user->nick);
1626                         WriteOpers("*** CONFIGURATION ERROR! Oper block mismatch for OperType %s",OperType);
1627                         log(DEFAULT,"OPER: Failed oper attempt by %s!%s@%s: credentials valid, but oper type nonexistent.",user->nick,user->ident,user->host);
1628                 }
1629         }
1630         return;
1631 }
1632
1633 void handle_nick(char **parameters, int pcnt, userrec *user)
1634 {
1635         if (pcnt < 1) 
1636         {
1637                 log(DEBUG,"not enough params for handle_nick");
1638                 return;
1639         }
1640         if (!parameters[0])
1641         {
1642                 log(DEBUG,"invalid parameter passed to handle_nick");
1643                 return;
1644         }
1645         if (!parameters[0][0])
1646         {
1647                 log(DEBUG,"zero length new nick passed to handle_nick");
1648                 return;
1649         }
1650         if (!user)
1651         {
1652                 log(DEBUG,"invalid user passed to handle_nick");
1653                 return;
1654         }
1655         if (!user->nick)
1656         {
1657                 log(DEBUG,"invalid old nick passed to handle_nick");
1658                 return;
1659         }
1660         if (!strcasecmp(user->nick,parameters[0]))
1661         {
1662                 log(DEBUG,"old nick is new nick, skipping");
1663                 return;
1664         }
1665         else
1666         {
1667                 if (strlen(parameters[0]) > 1)
1668                 {
1669                         if (parameters[0][0] == ':')
1670                         {
1671                                 *parameters[0]++;
1672                         }
1673                 }
1674                 if (matches_qline(parameters[0]))
1675                 {
1676                         WriteOpers("*** Q-Lined nickname %s from %s!%s@%s: %s",parameters[0],user->nick,user->ident,user->host,matches_qline(parameters[0]));
1677                         WriteServ(user->fd,"432 %s %s :Invalid nickname: %s",user->nick,parameters[0],matches_qline(parameters[0]));
1678                         return;
1679                 }
1680                 if ((Find(parameters[0])) && (Find(parameters[0]) != user))
1681                 {
1682                         WriteServ(user->fd,"433 %s %s :Nickname is already in use.",user->nick,parameters[0]);
1683                         return;
1684                 }
1685         }
1686         if (isnick(parameters[0]) == 0)
1687         {
1688                 WriteServ(user->fd,"432 %s %s :Erroneous Nickname",user->nick,parameters[0]);
1689                 return;
1690         }
1691
1692         if (user->registered == 7)
1693         {
1694                 int MOD_RESULT = 0;
1695                 FOREACH_RESULT(OnUserPreNick(user,parameters[0]));
1696                 if (MOD_RESULT) {
1697                         // if a module returns true, the nick change is silently forbidden.
1698                         return;
1699                 }
1700
1701                 WriteCommon(user,"NICK %s",parameters[0]);
1702                 
1703         }
1704         
1705         char oldnick[NICKMAX];
1706         strlcpy(oldnick,user->nick,NICKMAX);
1707
1708         /* change the nick of the user in the users_hash */
1709         user = ReHashNick(user->nick, parameters[0]);
1710         /* actually change the nick within the record */
1711         if (!user) return;
1712         if (!user->nick) return;
1713
1714         strlcpy(user->nick, parameters[0],NICKMAX);
1715
1716         log(DEBUG,"new nick set: %s",user->nick);
1717         
1718         if (user->registered < 3)
1719         {
1720                 user->registered = (user->registered | 2);
1721                 // dont attempt to look up the dns until they pick a nick... because otherwise their pointer WILL change
1722                 // and unless we're lucky we'll get a duff one later on.
1723                 //user->dns_done = (!lookup_dns(user->nick));
1724                 //if (user->dns_done)
1725                 //      log(DEBUG,"Aborting dns lookup of %s because dns server experienced a failure.",user->nick);
1726
1727 #ifdef THREADED_DNS
1728                 // initialize their dns lookup thread
1729                 if (pthread_create(&user->dnsthread, NULL, dns_task, (void *)user) != 0)
1730                 {
1731                         log(DEBUG,"Failed to create DNS lookup thread for user %s",user->nick);
1732                 }
1733 #else
1734                 user->dns_done = (!lookup_dns(user->nick));
1735                 if (user->dns_done)
1736                         log(DEBUG,"Aborting dns lookup of %s because dns server experienced a failure.",user->nick);
1737 #endif
1738         
1739         }
1740         if (user->registered == 3)
1741         {
1742                 /* user is registered now, bit 0 = USER command, bit 1 = sent a NICK command */
1743                 FOREACH_MOD OnUserRegister(user);
1744                 ConnectUser(user);
1745         }
1746         if (user->registered == 7)
1747         {
1748                 FOREACH_MOD OnUserPostNick(user,oldnick);
1749         }
1750 }
1751
1752 long duration(const char* str)
1753 {
1754         char n_field[MAXBUF];
1755         long total = 0;
1756         const char* str_end = str + strlen(str);
1757         n_field[0] = 0;
1758
1759         if ((!strchr(str,'s')) && (!strchr(str,'m')) && (!strchr(str,'h')) && (!strchr(str,'d')) && (!strchr(str,'w')) && (!strchr(str,'y')))
1760         {
1761                 std::string n = str;
1762                 n = n + "s";
1763                 return duration(n.c_str());
1764         }
1765         
1766         for (char* i = (char*)str; i < str_end; i++)
1767         {
1768                 // if we have digits, build up a string for the value in n_field,
1769                 // up to 10 digits in size.
1770                 if ((*i >= '0') && (*i <= '9'))
1771                 {
1772                         strlcat(n_field,i,10);
1773                 }
1774                 else
1775                 {
1776                         // we dont have a digit, check for numeric tokens
1777                         switch (tolower(*i))
1778                         {
1779                                 case 's':
1780                                         total += atoi(n_field);
1781                                 break;
1782
1783                                 case 'm':
1784                                         total += (atoi(n_field)*duration_m);
1785                                 break;
1786
1787                                 case 'h':
1788                                         total += (atoi(n_field)*duration_h);
1789                                 break;
1790
1791                                 case 'd':
1792                                         total += (atoi(n_field)*duration_d);
1793                                 break;
1794
1795                                 case 'w':
1796                                         total += (atoi(n_field)*duration_w);
1797                                 break;
1798
1799                                 case 'y':
1800                                         total += (atoi(n_field)*duration_y);
1801                                 break;
1802                         }
1803                         n_field[0] = 0;
1804                 }
1805         }
1806         // add trailing seconds
1807         total += atoi(n_field);
1808         
1809         return total;
1810 }
1811
1812 /* All other ircds when doing this check usually just look for a string of *@* or *. We're smarter than that, though. */
1813
1814 bool host_matches_everyone(std::string mask, userrec* user)
1815 {
1816         char insanemasks[MAXBUF];
1817         char buffer[MAXBUF];
1818         char itrigger[MAXBUF];
1819         Config->ConfValue("insane","hostmasks",0,insanemasks,&Config->config_f);
1820         Config->ConfValue("insane","trigger",0,itrigger,&Config->config_f);
1821         if (*itrigger == 0)
1822                 strlcpy(itrigger,"95.5",MAXBUF);
1823         if ((*insanemasks == 'y') || (*insanemasks == 't') || (*insanemasks == '1'))
1824                 return false;
1825         long matches = 0;
1826         for (user_hash::iterator u = clientlist.begin(); u != clientlist.end(); u++)
1827         {
1828                 strlcpy(buffer,u->second->ident,MAXBUF);
1829                 strlcat(buffer,"@",MAXBUF);
1830                 strlcat(buffer,u->second->host,MAXBUF);
1831                 if (match(buffer,mask.c_str()))
1832                         matches++;
1833         }
1834         float percent = ((float)matches / (float)clientlist.size()) * 100;
1835         if (percent > (float)atof(itrigger))
1836         {
1837                 WriteOpers("*** \2WARNING\2: %s tried to set a G/K/E line mask of %s, which covers %.2f%% of the network!",user->nick,mask.c_str(),percent);
1838                 return true;
1839         }
1840         return false;
1841 }
1842
1843 bool ip_matches_everyone(std::string ip, userrec* user)
1844 {
1845         char insanemasks[MAXBUF];
1846         char itrigger[MAXBUF];
1847         Config->ConfValue("insane","ipmasks",0,insanemasks,&Config->config_f);
1848         Config->ConfValue("insane","trigger",0,itrigger,&Config->config_f);
1849         if (*itrigger == 0)
1850                 strlcpy(itrigger,"95.5",MAXBUF);
1851         if ((*insanemasks == 'y') || (*insanemasks == 't') || (*insanemasks == '1'))
1852                 return false;
1853         long matches = 0;
1854         for (user_hash::iterator u = clientlist.begin(); u != clientlist.end(); u++)
1855         {
1856                 if (match(u->second->ip,ip.c_str()))
1857                         matches++;
1858         }
1859         float percent = ((float)matches / (float)clientlist.size()) * 100;
1860         if (percent > (float)atof(itrigger))
1861         {
1862                 WriteOpers("*** \2WARNING\2: %s tried to set a Z line mask of %s, which covers %.2f%% of the network!",user->nick,ip.c_str(),percent);
1863                 return true;
1864         }
1865         return false;
1866 }
1867
1868 bool nick_matches_everyone(std::string nick, userrec* user)
1869 {
1870         char insanemasks[MAXBUF];
1871         char itrigger[MAXBUF];
1872         Config->ConfValue("insane","nickmasks",0,insanemasks,&Config->config_f);
1873         Config->ConfValue("insane","trigger",0,itrigger,&Config->config_f);
1874         if (*itrigger == 0)
1875                 strlcpy(itrigger,"95.5",MAXBUF);
1876         if ((*insanemasks == 'y') || (*insanemasks == 't') || (*insanemasks == '1'))
1877                 return false;
1878         long matches = 0;
1879         for (user_hash::iterator u = clientlist.begin(); u != clientlist.end(); u++)
1880         {
1881                 if (match(u->second->nick,nick.c_str()))
1882                         matches++;
1883         }
1884         float percent = ((float)matches / (float)clientlist.size()) * 100;
1885         if (percent > (float)atof(itrigger))
1886         {
1887                 WriteOpers("*** \2WARNING\2: %s tried to set a Q line mask of %s, which covers %.2f%% of the network!",user->nick,nick.c_str(),percent);
1888                 return true;
1889         }
1890         return false;
1891 }
1892
1893 void handle_kline(char **parameters, int pcnt, userrec *user)
1894 {
1895         if (pcnt >= 3)
1896         {
1897                 if (host_matches_everyone(parameters[0],user))
1898                         return;
1899                 add_kline(duration(parameters[1]),user->nick,parameters[2],parameters[0]);
1900                 FOREACH_MOD OnAddKLine(duration(parameters[1]), user, parameters[2], parameters[0]);
1901                 if (!duration(parameters[1]))
1902                 {
1903                         WriteOpers("*** %s added permenant K-line for %s.",user->nick,parameters[0]);
1904                 }
1905                 else
1906                 {
1907                         WriteOpers("*** %s added timed K-line for %s, expires in %d seconds.",user->nick,parameters[0],duration(parameters[1]));
1908                 }
1909                 apply_lines(APPLY_KLINES);
1910         }
1911         else
1912         {
1913                 if (del_kline(parameters[0]))
1914                 {
1915                         FOREACH_MOD OnDelKLine(user, parameters[0]);
1916                         WriteOpers("*** %s Removed K-line on %s.",user->nick,parameters[0]);
1917                 }
1918                 else
1919                 {
1920                         WriteServ(user->fd,"NOTICE %s :*** K-Line %s not found in list, try /stats k.",user->nick,parameters[0]);
1921                 }
1922         }
1923 }
1924
1925 void handle_eline(char **parameters, int pcnt, userrec *user)
1926 {
1927         if (pcnt >= 3)
1928         {
1929                 if (host_matches_everyone(parameters[0],user))
1930                         return;
1931                 add_eline(duration(parameters[1]),user->nick,parameters[2],parameters[0]);
1932                 FOREACH_MOD OnAddELine(duration(parameters[1]), user, parameters[2], parameters[0]);
1933                 if (!duration(parameters[1]))
1934                 {
1935                         WriteOpers("*** %s added permenant E-line for %s.",user->nick,parameters[0]);
1936                 }
1937                 else
1938                 {
1939                         WriteOpers("*** %s added timed E-line for %s, expires in %d seconds.",user->nick,parameters[0],duration(parameters[1]));
1940                 }
1941         }
1942         else
1943         {
1944                 if (del_eline(parameters[0]))
1945                 {
1946                         FOREACH_MOD OnDelELine(user, parameters[0]);
1947                         WriteOpers("*** %s Removed E-line on %s.",user->nick,parameters[0]);
1948                 }
1949                 else
1950                 {
1951                         WriteServ(user->fd,"NOTICE %s :*** E-Line %s not found in list, try /stats e.",user->nick,parameters[0]);
1952                 }
1953         }
1954         // no need to apply the lines for an eline
1955 }
1956
1957 void handle_gline(char **parameters, int pcnt, userrec *user)
1958 {
1959         if (pcnt >= 3)
1960         {
1961                 if (host_matches_everyone(parameters[0],user))
1962                         return;
1963                 add_gline(duration(parameters[1]),user->nick,parameters[2],parameters[0]);
1964                 FOREACH_MOD OnAddGLine(duration(parameters[1]), user, parameters[2], parameters[0]);
1965                 if (!duration(parameters[1]))
1966                 {
1967                         WriteOpers("*** %s added permenant G-line for %s.",user->nick,parameters[0]);
1968                 }
1969                 else
1970                 {
1971                         WriteOpers("*** %s added timed G-line for %s, expires in %d seconds.",user->nick,parameters[0],duration(parameters[1]));
1972                 }
1973                 apply_lines(APPLY_GLINES);
1974         }
1975         else
1976         {
1977                 if (del_gline(parameters[0]))
1978                 {
1979                         FOREACH_MOD OnDelGLine(user, parameters[0]);
1980                         WriteOpers("*** %s Removed G-line on %s.",user->nick,parameters[0]);
1981                 }
1982                 else
1983                 {
1984                         WriteServ(user->fd,"NOTICE %s :*** G-Line %s not found in list, try /stats g.",user->nick,parameters[0]);
1985                 }
1986         }
1987 }
1988
1989 void handle_zline(char **parameters, int pcnt, userrec *user)
1990 {
1991         if (pcnt >= 3)
1992         {
1993                 if (strchr(parameters[0],'@'))
1994                 {
1995                         WriteServ(user->fd,"NOTICE %s :*** You cannot include a username in a zline, a zline must ban only an IP mask",user->nick);
1996                         return;
1997                 }
1998                 if (ip_matches_everyone(parameters[0],user))
1999                         return;
2000                 add_zline(duration(parameters[1]),user->nick,parameters[2],parameters[0]);
2001                 FOREACH_MOD OnAddZLine(duration(parameters[1]), user, parameters[2], parameters[0]);
2002                 if (!duration(parameters[1]))
2003                 {
2004                         WriteOpers("*** %s added permenant Z-line for %s.",user->nick,parameters[0]);
2005                 }
2006                 else
2007                 {
2008                         WriteOpers("*** %s added timed Z-line for %s, expires in %d seconds.",user->nick,parameters[0],duration(parameters[1]));
2009                 }
2010                 apply_lines(APPLY_ZLINES);
2011         }
2012         else
2013         {
2014                 if (del_zline(parameters[0]))
2015                 {
2016                         FOREACH_MOD OnDelZLine(user, parameters[0]);
2017                         WriteOpers("*** %s Removed Z-line on %s.",user->nick,parameters[0]);
2018                 }
2019                 else
2020                 {
2021                         WriteServ(user->fd,"NOTICE %s :*** Z-Line %s not found in list, try /stats Z.",user->nick,parameters[0]);
2022                 }
2023         }
2024 }
2025
2026 void handle_qline(char **parameters, int pcnt, userrec *user)
2027 {
2028         if (pcnt >= 3)
2029         {
2030                 if (nick_matches_everyone(parameters[0],user))
2031                         return;
2032                 add_qline(duration(parameters[1]),user->nick,parameters[2],parameters[0]);
2033                 FOREACH_MOD OnAddQLine(duration(parameters[1]), user, parameters[2], parameters[0]);
2034                 if (!duration(parameters[1]))
2035                 {
2036                         WriteOpers("*** %s added permenant Q-line for %s.",user->nick,parameters[0]);
2037                 }
2038                 else
2039                 {
2040                         WriteOpers("*** %s added timed Q-line for %s, expires in %d seconds.",user->nick,parameters[0],duration(parameters[1]));
2041                 }
2042                 apply_lines(APPLY_QLINES);
2043         }
2044         else
2045         {
2046                 if (del_qline(parameters[0]))
2047                 {
2048                         FOREACH_MOD OnDelQLine(user, parameters[0]);
2049                         WriteOpers("*** %s Removed Q-line on %s.",user->nick,parameters[0]);
2050                 }
2051                 else
2052                 {
2053                         WriteServ(user->fd,"NOTICE %s :*** Q-Line %s not found in list, try /stats q.",user->nick,parameters[0]);
2054                 }
2055         }
2056 }
2057
2058