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