]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/mode.cpp
5a8c47102d7c11dd29d859b5377a21744afad090
[user/henk/code/inspircd.git] / src / mode.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 #include "inspircd.h"
18 #include "inspircd_io.h"
19 #include "inspircd_util.h"
20 #include "inspircd_config.h"
21 #include <unistd.h>
22 #include <fcntl.h>
23 #include <sys/errno.h>
24 #include <sys/ioctl.h>
25 #include <sys/utsname.h>
26 #include <cstdio>
27 #include <time.h>
28 #include <string>
29 #ifdef GCC3
30 #include <ext/hash_map>
31 #else
32 #include <hash_map>
33 #endif
34 #include <map>
35 #include <sstream>
36 #include <vector>
37 #include <errno.h>
38 #include <deque>
39 #include <errno.h>
40 #include <unistd.h>
41 #include <sched.h>
42 #include "connection.h"
43 #include "users.h"
44 #include "servers.h"
45 #include "ctables.h"
46 #include "globals.h"
47 #include "modules.h"
48 #include "dynamic.h"
49 #include "wildcard.h"
50 #include "message.h"
51 #include "commands.h"
52 #include "xline.h"
53 #include "inspstring.h"
54
55 using namespace std;
56
57 extern int MODCOUNT;
58 extern std::vector<Module*> modules;
59 extern std::vector<ircd_module*> factory;
60 extern std::vector<std::string> module_names;
61
62
63 extern int LogLevel;
64 extern char ServerName[MAXBUF];
65 extern char Network[MAXBUF];
66 extern char ServerDesc[MAXBUF];
67 extern char AdminName[MAXBUF];
68 extern char AdminEmail[MAXBUF];
69 extern char AdminNick[MAXBUF];
70 extern char diepass[MAXBUF];
71 extern char restartpass[MAXBUF];
72 extern char motd[MAXBUF];
73 extern char rules[MAXBUF];
74 extern char list[MAXBUF];
75 extern char PrefixQuit[MAXBUF];
76 extern char DieValue[MAXBUF];
77
78 extern bool AllowHalfop;
79 extern bool AllowProtect;
80 extern bool AllowFounder;
81
82 extern time_t TIME;
83
84 char* give_ops(userrec *user,char *dest,chanrec *chan,int status)
85 {
86         userrec *d;
87         
88         if ((!user) || (!dest) || (!chan))
89         {
90                 log(DEFAULT,"*** BUG *** give_ops was given an invalid parameter");
91                 return NULL;
92         }
93
94         if (!isnick(dest))
95         {
96                 log(DEFAULT,"the target nickname given to give_ops was invalid");
97                 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
98                 return NULL;
99         }
100         d = Find(dest);
101         if (!d)
102         {
103                 log(DEFAULT,"the target nickname given to give_ops couldnt be found");
104                 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
105                 return NULL;
106         }
107         else
108         {
109
110                 int MOD_RESULT = 0;
111                 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_OP));
112                 
113                 if (MOD_RESULT == ACR_DENY)
114                         return NULL;
115                 if (MOD_RESULT == ACR_DEFAULT)
116                 {
117                         if ((status < STATUS_OP) && (!is_uline(user->server)))
118                         {
119                                 log(DEBUG,"%s cant give ops to %s because they nave status %d and needs %d",user->nick,dest,status,STATUS_OP);
120                                 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
121                                 return NULL;
122                         }
123                 }
124
125
126                 for (int i = 0; i != MAXCHANS; i++)
127                 {
128                         if ((d->chans[i].channel != NULL) && (chan != NULL))
129                         if (!strcasecmp(d->chans[i].channel->name,chan->name))
130                         {
131                         if (d->chans[i].uc_modes & UCMODE_OP)
132                                 {
133                                         /* mode already set on user, dont allow multiple */
134                                         log(DEFAULT,"The target user given to give_ops was already opped on the channel");
135                                         return NULL;
136                                 }
137                                 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_OP;
138                                 log(DEBUG,"gave ops: %s %s",d->chans[i].channel->name,d->nick);
139                                 return d->nick;
140                         }
141                 }
142                 log(DEFAULT,"The target channel given to give_ops was not in the users mode list");
143         }
144         return NULL;
145 }
146
147 char* give_hops(userrec *user,char *dest,chanrec *chan,int status)
148 {
149         userrec *d;
150         
151         if ((!user) || (!dest) || (!chan))
152         {
153                 log(DEFAULT,"*** BUG *** give_hops was given an invalid parameter");
154                 return NULL;
155         }
156
157         d = Find(dest);
158         if (!isnick(dest))
159         {
160                 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
161                 return NULL;
162         }
163         if (!d)
164         {
165                 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
166                 return NULL;
167         }
168         else
169         {
170                 int MOD_RESULT = 0;
171                 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_HALFOP));
172                 
173                 if (MOD_RESULT == ACR_DENY)
174                         return NULL;
175                 if (MOD_RESULT == ACR_DEFAULT)
176                 {
177                         if ((status < STATUS_OP) && (!is_uline(user->server)))
178                         {
179                                 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
180                                 return NULL;
181                         }
182                 }
183
184                 for (int i = 0; i != MAXCHANS; i++)
185                 {
186                         if ((d->chans[i].channel != NULL) && (chan != NULL))
187                         if (!strcasecmp(d->chans[i].channel->name,chan->name))
188                         {
189                                 if (d->chans[i].uc_modes & UCMODE_HOP)
190                                 {
191                                         /* mode already set on user, dont allow multiple */
192                                         return NULL;
193                                 }
194                                 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_HOP;
195                                 log(DEBUG,"gave h-ops: %s %s",d->chans[i].channel->name,d->nick);
196                                 return d->nick;
197                         }
198                 }
199         }
200         return NULL;
201 }
202
203 char* give_voice(userrec *user,char *dest,chanrec *chan,int status)
204 {
205         userrec *d;
206         
207         if ((!user) || (!dest) || (!chan))
208         {
209                 log(DEFAULT,"*** BUG *** give_voice was given an invalid parameter");
210                 return NULL;
211         }
212
213         d = Find(dest);
214         if (!isnick(dest))
215         {
216                 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
217                 return NULL;
218         }
219         if (!d)
220         {
221                 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
222                 return NULL;
223         }
224         else
225         {
226                 int MOD_RESULT = 0;
227                 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_VOICE));
228                 
229                 if (MOD_RESULT == ACR_DENY)
230                         return NULL;
231                 if (MOD_RESULT == ACR_DEFAULT)
232                 {
233                         if ((status < STATUS_HOP) && (!is_uline(user->server)))
234                         {
235                                 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
236                                 return NULL;
237                         }
238                 }
239
240                 for (int i = 0; i != MAXCHANS; i++)
241                 {
242                         if ((d->chans[i].channel != NULL) && (chan != NULL))
243                         if (!strcasecmp(d->chans[i].channel->name,chan->name))
244                         {
245                                 if (d->chans[i].uc_modes & UCMODE_VOICE)
246                                 {
247                                         /* mode already set on user, dont allow multiple */
248                                         return NULL;
249                                 }
250                                 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_VOICE;
251                                 log(DEBUG,"gave voice: %s %s",d->chans[i].channel->name,d->nick);
252                                 return d->nick;
253                         }
254                 }
255         }
256         return NULL;
257 }
258
259 char* take_ops(userrec *user,char *dest,chanrec *chan,int status)
260 {
261         userrec *d;
262         
263         if ((!user) || (!dest) || (!chan))
264         {
265                 log(DEFAULT,"*** BUG *** take_ops was given an invalid parameter");
266                 return NULL;
267         }
268
269         d = Find(dest);
270         if (!isnick(dest))
271         {
272                 log(DEBUG,"take_ops was given an invalid target nickname of %s",dest);
273                 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
274                 return NULL;
275         }
276         if (!d)
277         {
278                 log(DEBUG,"take_ops couldnt resolve the target nickname: %s",dest);
279                 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
280                 return NULL;
281         }
282         else
283         {
284                 int MOD_RESULT = 0;
285                 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_DEOP));
286                 
287                 if (MOD_RESULT == ACR_DENY)
288                         return NULL;
289                 if (MOD_RESULT == ACR_DEFAULT)
290                 {
291                         if ((status < STATUS_OP) && (!is_uline(user->server)))
292                         {
293                                 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
294                                 return NULL;
295                         }
296                 }
297
298                 for (int i = 0; i != MAXCHANS; i++)
299                 {
300                         if ((d->chans[i].channel != NULL) && (chan != NULL))
301                         if (!strcasecmp(d->chans[i].channel->name,chan->name))
302                         {
303                                 if ((d->chans[i].uc_modes & UCMODE_OP) == 0)
304                                 {
305                                         /* mode already set on user, dont allow multiple */
306                                         return NULL;
307                                 }
308                                 d->chans[i].uc_modes ^= UCMODE_OP;
309                                 log(DEBUG,"took ops: %s %s",d->chans[i].channel->name,d->nick);
310                                 return d->nick;
311                         }
312                 }
313                 log(DEBUG,"take_ops couldnt locate the target channel in the target users list");
314         }
315         return NULL;
316 }
317
318 char* take_hops(userrec *user,char *dest,chanrec *chan,int status)
319 {
320         userrec *d;
321         
322         if ((!user) || (!dest) || (!chan))
323         {
324                 log(DEFAULT,"*** BUG *** take_hops was given an invalid parameter");
325                 return NULL;
326         }
327
328         d = Find(dest);
329         if (!isnick(dest))
330         {
331                 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
332                 return NULL;
333         }
334         if (!d)
335         {
336                 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
337                 return NULL;
338         }
339         else
340         {
341                 int MOD_RESULT = 0;
342                 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_DEHALFOP));
343                 
344                 if (MOD_RESULT == ACR_DENY)
345                         return NULL;
346                 if (MOD_RESULT == ACR_DEFAULT)
347                 {
348                         if ((status < STATUS_OP) && (!is_uline(user->server)))
349                         {
350                                 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
351                                 return NULL;
352                         }
353                 }
354
355                 for (int i = 0; i != MAXCHANS; i++)
356                 {
357                         if ((d->chans[i].channel != NULL) && (chan != NULL))
358                         if (!strcasecmp(d->chans[i].channel->name,chan->name))
359                         {
360                                 if ((d->chans[i].uc_modes & UCMODE_HOP) == 0)
361                                 {
362                                         /* mode already set on user, dont allow multiple */
363                                         return NULL;
364                                 }
365                                 d->chans[i].uc_modes ^= UCMODE_HOP;
366                                 log(DEBUG,"took h-ops: %s %s",d->chans[i].channel->name,d->nick);
367                                 return d->nick;
368                         }
369                 }
370         }
371         return NULL;
372 }
373
374 char* take_voice(userrec *user,char *dest,chanrec *chan,int status)
375 {
376         userrec *d;
377         
378         if ((!user) || (!dest) || (!chan))
379         {
380                 log(DEFAULT,"*** BUG *** take_voice was given an invalid parameter");
381                 return NULL;
382         }
383
384         d = Find(dest);
385         if (!isnick(dest))
386         {
387                 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
388                 return NULL;
389         }
390         if (!d)
391         {
392                 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
393                 return NULL;
394         }
395         else
396         {
397                 int MOD_RESULT = 0;
398                 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_DEVOICE));
399                 
400                 if (MOD_RESULT == ACR_DENY)
401                         return NULL;
402                 if (MOD_RESULT == ACR_DEFAULT)
403                 {
404                         if ((status < STATUS_HOP) && (!is_uline(user->server)))
405                         {
406                                 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
407                                 return NULL;
408                         }
409                 }
410
411                 for (int i = 0; i != MAXCHANS; i++)
412                 {
413                         if ((d->chans[i].channel != NULL) && (chan != NULL))
414                         if (!strcasecmp(d->chans[i].channel->name,chan->name))
415                         {
416                                 if ((d->chans[i].uc_modes & UCMODE_VOICE) == 0)
417                                 {
418                                         /* mode already set on user, dont allow multiple */
419                                         return NULL;
420                                 }
421                                 d->chans[i].uc_modes ^= UCMODE_VOICE;
422                                 log(DEBUG,"took voice: %s %s",d->chans[i].channel->name,d->nick);
423                                 return d->nick;
424                         }
425                 }
426         }
427         return NULL;
428 }
429
430 char* add_ban(userrec *user,char *dest,chanrec *chan,int status)
431 {
432         if ((!user) || (!dest) || (!chan)) {
433                 log(DEFAULT,"*** BUG *** add_ban was given an invalid parameter");
434                 return NULL;
435         }
436
437         BanItem b;
438         if ((!user) || (!dest) || (!chan))
439                 return NULL;
440         int l = strlen(dest);
441         if (strchr(dest,'!')==0)
442                 return NULL;
443         if (strchr(dest,'@')==0)
444                 return NULL;
445         for (int i = 0; i < l; i++)
446                 if (dest[i] < 32)
447                         return NULL;
448         for (int i = 0; i < l; i++)
449                 if (dest[i] > 126)
450                         return NULL;
451         int c = 0;
452         for (int i = 0; i < l; i++)
453                 if (dest[i] == '!')
454                         c++;
455         if (c>1)
456                 return NULL;
457         c = 0;
458         for (int i = 0; i < l; i++)
459                 if (dest[i] == '@')
460                         c++;
461         if (c>1)
462                 return NULL;
463
464         long maxbans = GetMaxBans(chan->name);
465         if (chan->bans.size() > maxbans)
466         {
467                 WriteServ(user->fd,"478 %s %s :Channel ban list for %s is full (maximum entries for this channel is %d)",user->nick, chan->name,chan->name,maxbans);
468                 return NULL;
469         }
470
471         log(DEBUG,"add_ban: %s %s",chan->name,user->nick);
472
473         TidyBan(dest);
474         for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
475         {
476                 if (!strcasecmp(i->data,dest))
477                 {
478                         // dont allow a user to set the same ban twice
479                         return NULL;
480                 }
481         }
482
483         b.set_time = TIME;
484         strncpy(b.data,dest,MAXBUF);
485         strncpy(b.set_by,user->nick,NICKMAX);
486         chan->bans.push_back(b);
487         return dest;
488 }
489
490 char* take_ban(userrec *user,char *dest,chanrec *chan,int status)
491 {
492         if ((!user) || (!dest) || (!chan)) {
493                 log(DEFAULT,"*** BUG *** take_ban was given an invalid parameter");
494                 return 0;
495         }
496
497         log(DEBUG,"del_ban: %s %s",chan->name,user->nick);
498         for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
499         {
500                 if (!strcasecmp(i->data,dest))
501                 {
502                         chan->bans.erase(i);
503                         return dest;
504                 }
505         }
506         return NULL;
507 }
508
509 void process_modes(char **parameters,userrec* user,chanrec *chan,int status, int pcnt, bool servermode, bool silent, bool local)
510 {
511         if (!parameters) {
512                 log(DEFAULT,"*** BUG *** process_modes was given an invalid parameter");
513                 return;
514         }
515
516         char modelist[MAXBUF];
517         char outlist[MAXBUF];
518         char outstr[MAXBUF];
519         char outpars[32][MAXBUF];
520         int param = 2;
521         int pc = 0;
522         int ptr = 0;
523         int mdir = 1;
524         char* r = NULL;
525         bool k_set = false, l_set = false;
526
527         if (pcnt < 2)
528         {
529                 return;
530         }
531
532         int MOD_RESULT = 0;
533         FOREACH_RESULT(OnAccessCheck(user,NULL,chan,AC_GENERAL_MODE));
534         
535         if (MOD_RESULT == ACR_DENY)
536                 return;
537
538         log(DEBUG,"process_modes: start: parameters=%d",pcnt);
539
540         strlcpy(modelist,parameters[1],MAXBUF); /* mode list, e.g. +oo-o *
541                                                  * parameters[2] onwards are parameters for
542                                                  * modes that require them :) */
543         strlcpy(outlist,"+",MAXBUF);
544         mdir = 1;
545
546         log(DEBUG,"process_modes: modelist: %s",modelist);
547
548         int len = strlen(modelist);
549         while (modelist[len-1] == ' ')
550                 modelist[--len] = '\0';
551         for (ptr = 0; ptr < len; ptr++)
552         {
553                 r = NULL;
554
555                 {
556                         log(DEBUG,"process_modes: modechar: %c",modelist[ptr]);
557
558                         char modechar = modelist[ptr];
559                         switch (modelist[ptr])
560                         {
561                                 case '-':
562                                         if (mdir != 0)
563                                         {
564                                                 int t = strlen(outlist)-1;
565                                                 if ((outlist[t] == '+') || (outlist[t] == '-'))
566                                                 {
567                                                         outlist[t] = '-';
568                                                 }
569                                                 else
570                                                 {
571                                                         strcat(outlist,"-");
572                                                 }
573                                         }
574                                         mdir = 0;
575                                         
576                                 break;                  
577
578                                 case '+':
579                                         if (mdir != 1)
580                                         {
581                                                 int t = strlen(outlist)-1;
582                                                 if ((outlist[t] == '+') || (outlist[t] == '-'))
583                                                 {
584                                                         outlist[t] = '+';
585                                                 }
586                                                 else
587                                                 {
588                                                         strcat(outlist,"+");
589                                                 }
590                                         }
591                                         mdir = 1;
592                                 break;
593
594                                 case 'o':
595                                         log(DEBUG,"Ops");
596                                         if ((param >= pcnt)) break;
597                                         log(DEBUG,"Enough parameters left");
598                                         if (mdir == 1)
599                                         {
600                                                 MOD_RESULT = 0;
601                                                 FOREACH_RESULT(OnRawMode(user, chan, 'o', parameters[param], true, 1));
602                                                 if (!MOD_RESULT)
603                                                 {
604                                                         log(DEBUG,"calling give_ops");
605                                                         r = give_ops(user,parameters[param++],chan,status);
606                                                 }
607                                                 else param++;
608                                         }
609                                         else
610                                         {
611                                                 MOD_RESULT = 0;
612                                                 FOREACH_RESULT(OnRawMode(user, chan, 'o', parameters[param], false, 1));
613                                                 if (!MOD_RESULT)
614                                                 {
615                                                         log(DEBUG,"calling take_ops");
616                                                         r = take_ops(user,parameters[param++],chan,status);
617                                                 }
618                                                 else param++;
619                                         }
620                                         if (r)
621                                         {
622                                                 strlcat(outlist,"o",MAXBUF);
623                                                 strlcpy(outpars[pc++],r,MAXBUF);
624                                         }
625                                 break;
626                         
627                                 case 'h':
628                                         if (((param >= pcnt)) || (!AllowHalfop)) break;
629                                         if (mdir == 1)
630                                         {
631                                                 MOD_RESULT = 0;
632                                                 FOREACH_RESULT(OnRawMode(user, chan, 'h', parameters[param], true, 1));
633                                                 if (!MOD_RESULT)
634                                                 {
635                                                         r = give_hops(user,parameters[param++],chan,status);
636                                                 }
637                                                 else param++;
638                                         }
639                                         else
640                                         {
641                                                 MOD_RESULT = 0;
642                                                 FOREACH_RESULT(OnRawMode(user, chan, 'h', parameters[param], false, 1));
643                                                 if (!MOD_RESULT)
644                                                 {
645                                                         r = take_hops(user,parameters[param++],chan,status);
646                                                 }
647                                                 else param++;
648                                         }
649                                         if (r)
650                                         {
651                                                 strlcat(outlist,"h",MAXBUF);
652                                                 strlcpy(outpars[pc++],r,MAXBUF);
653                                         }
654                                 break;
655                         
656                                 
657                                 case 'v':
658                                         if ((param >= pcnt)) break;
659                                         if (mdir == 1)
660                                         {
661                                                 MOD_RESULT = 0;
662                                                 FOREACH_RESULT(OnRawMode(user, chan, 'v', parameters[param], true, 1));
663                                                 if (!MOD_RESULT)
664                                                 {
665                                                         r = give_voice(user,parameters[param++],chan,status);
666                                                 }
667                                                 else param++;
668                                         }
669                                         else
670                                         {
671                                                 MOD_RESULT = 0;
672                                                 FOREACH_RESULT(OnRawMode(user, chan, 'v', parameters[param], false, 1));
673                                                 if (!MOD_RESULT)
674                                                 {
675                                                         r = take_voice(user,parameters[param++],chan,status);
676                                                 }
677                                                 else param++;
678                                         }
679                                         if (r)
680                                         {
681                                                 strlcat(outlist,"v",MAXBUF);
682                                                 strlcpy(outpars[pc++],r,MAXBUF);
683                                         }
684                                 break;
685                                 
686                                 case 'b':
687                                         if ((param >= pcnt)) break;
688                                         if (mdir == 1)
689                                         {
690                                                 MOD_RESULT = 0;
691                                                 FOREACH_RESULT(OnRawMode(user, chan, 'b', parameters[param], true, 1));
692                                                 if (!MOD_RESULT)
693                                                 {
694                                                         r = add_ban(user,parameters[param++],chan,status);
695                                                 }
696                                                 else param++;
697                                         }
698                                         else
699                                         {
700                                                 MOD_RESULT = 0;
701                                                 FOREACH_RESULT(OnRawMode(user, chan, 'b', parameters[param], false, 1));
702                                                 if (!MOD_RESULT)
703                                                 {
704                                                         r = take_ban(user,parameters[param++],chan,status);
705                                                 }
706                                                 else param++;
707                                         }
708                                         if (r)
709                                         {
710                                                 strlcat(outlist,"b",MAXBUF);
711                                                 strlcpy(outpars[pc++],parameters[param-1],MAXBUF);
712                                         }
713                                 break;
714
715
716                                 case 'k':
717                                         if ((param >= pcnt))
718                                                 break;
719
720                                         if (mdir == 1)
721                                         {
722                                                 if (k_set)
723                                                         break;
724                                                 
725                                                 if (!strcmp(chan->key,""))
726                                                 {
727                                                         MOD_RESULT = 0;
728                                                         FOREACH_RESULT(OnRawMode(user, chan, 'k', parameters[param], true, 1));
729                                                         if (!MOD_RESULT)
730                                                         {
731                                                                 strcat(outlist,"k");
732                                                                 char key[MAXBUF];
733                                                                 strlcpy(key,parameters[param++],32);
734                                                                 strlcpy(outpars[pc++],key,MAXBUF);
735                                                                 strlcpy(chan->key,key,MAXBUF);
736                                                                 k_set = true;
737                                                         }
738                                                         else param++;
739                                                 }
740                                         }
741                                         else
742                                         {
743                                                 /* checks on -k are case sensitive and only accurate to the
744                                                    first 32 characters */
745                                                 char key[MAXBUF];
746                                                 MOD_RESULT = 0;
747                                                 FOREACH_RESULT(OnRawMode(user, chan, 'k', parameters[param], false, 1));
748                                                 if (!MOD_RESULT)
749                                                 {
750                                                         strlcpy(key,parameters[param++],32);
751                                                         /* only allow -k if correct key given */
752                                                         if (!strcmp(chan->key,key))
753                                                         {
754                                                                 strlcat(outlist,"k",MAXBUF);
755                                                                 strlcpy(chan->key,"",MAXBUF);
756                                                                 strlcpy(outpars[pc++],key,MAXBUF);
757                                                         }
758                                                 }
759                                                 else param++;
760                                         }
761                                 break;
762                                 
763                                 case 'l':
764                                         if (mdir == 0)
765                                         {
766                                                 MOD_RESULT = 0;
767                                                 FOREACH_RESULT(OnRawMode(user, chan, 'l', "", false, 0));
768                                                 if (!MOD_RESULT)
769                                                 {
770                                                         if (chan->limit)
771                                                         {
772                                                                 strcat(outlist,"l");
773                                                                 chan->limit = 0;
774                                                         }
775                                                 }
776                                         }
777                                         
778                                         if ((param >= pcnt)) break;
779                                         if (mdir == 1)
780                                         {
781                                                 if (l_set)
782                                                         break;
783                                                 
784                                                 bool invalid = false;
785                                                 for (int i = 0; i < strlen(parameters[param]); i++)
786                                                 {
787                                                         if ((parameters[param][i] < '0') || (parameters[param][i] > '9'))
788                                                         {
789                                                                 invalid = true;
790                                                         }
791                                                 }
792                                                 if (atoi(parameters[param]) < 1)
793                                                 {
794                                                         invalid = true;
795                                                 }
796
797                                                 if (invalid)
798                                                         break;
799
800                                                 MOD_RESULT = 0;
801                                                 FOREACH_RESULT(OnRawMode(user, chan, 'l', parameters[param], true, 1));
802                                                 if (!MOD_RESULT)
803                                                 {
804         
805                                                         chan->limit = atoi(parameters[param]);
806                                                         
807                                                         // reported by mech: large values cause underflow
808                                                         if (chan->limit < 0)
809                                                                 chan->limit = 0x7FFFFF;
810                                                 }
811                                                         
812                                                 if (chan->limit)
813                                                 {
814                                                         strlcat(outlist,"l",MAXBUF);
815                                                         strlcpy(outpars[pc++],parameters[param++],MAXBUF);
816                                                         l_set = true;
817                                                 }
818                                         }
819                                 break;
820                                 
821                                 case 'i':
822                                         MOD_RESULT = 0;
823                                         FOREACH_RESULT(OnRawMode(user, chan, 'i', "", mdir, 0));
824                                         if (!MOD_RESULT)
825                                         {
826                                                 if (chan->inviteonly != mdir)
827                                                 {
828                                                         strlcat(outlist,"i",MAXBUF);
829                                                 }
830                                                 chan->inviteonly = mdir;
831                                         }
832                                 break;
833                                 
834                                 case 't':
835                                         MOD_RESULT = 0;
836                                         FOREACH_RESULT(OnRawMode(user, chan, 't', "", mdir, 0));
837                                         if (!MOD_RESULT)
838                                         {
839                                                 if (chan->topiclock != mdir)
840                                                 {
841                                                         strlcat(outlist,"t",MAXBUF);
842                                                 }
843                                                 chan->topiclock = mdir;
844                                         }
845                                 break;
846                                 
847                                 case 'n':
848                                         MOD_RESULT = 0;
849                                         FOREACH_RESULT(OnRawMode(user, chan, 'n', "", mdir, 0));
850                                         if (!MOD_RESULT)
851                                         {
852                                                 if (chan->noexternal != mdir)
853                                                 {
854                                                         strlcat(outlist,"n",MAXBUF);
855                                                 }
856                                                 chan->noexternal = mdir;
857                                         }
858                                 break;
859                                 
860                                 case 'm':
861                                         MOD_RESULT = 0;
862                                         FOREACH_RESULT(OnRawMode(user, chan, 'm', "", mdir, 0));
863                                         if (!MOD_RESULT)
864                                         {
865                                                 if (chan->moderated != mdir)
866                                                 {
867                                                         strlcat(outlist,"m",MAXBUF);
868                                                 }
869                                                 chan->moderated = mdir;
870                                         }
871                                 break;
872                                 
873                                 case 's':
874                                         MOD_RESULT = 0;
875                                         FOREACH_RESULT(OnRawMode(user, chan, 's', "", mdir, 0));
876                                         if (!MOD_RESULT)
877                                         {
878                                                 if (chan->secret != mdir)
879                                                 {
880                                                         strcat(outlist,"s");
881                                                         if (chan->c_private)
882                                                         {
883                                                                 chan->c_private = 0;
884                                                                 if (mdir)
885                                                                 {
886                                                                         strlcat(outlist,"-p+",MAXBUF);
887                                                                 }
888                                                                 else
889                                                                 {
890                                                                         strlcat(outlist,"+p-",MAXBUF);
891                                                                 }
892                                                         }
893                                                 }
894                                                 chan->secret = mdir;
895                                         }
896                                 break;
897                                 
898                                 case 'p':
899                                         MOD_RESULT = 0;
900                                         FOREACH_RESULT(OnRawMode(user, chan, 'p', "", mdir, 0));
901                                         if (!MOD_RESULT)
902                                         {
903                                                 if (chan->c_private != mdir)
904                                                 {
905                                                         strlcat(outlist,"p",MAXBUF);
906                                                         if (chan->secret)
907                                                         {
908                                                                 chan->secret = 0;
909                                                                 if (mdir)
910                                                                 {
911                                                                         strlcat(outlist,"-s+",MAXBUF);
912                                                                 }
913                                                                 else
914                                                                 {
915                                                                         strlcat(outlist,"+s-",MAXBUF);
916                                                                 }
917                                                         }
918                                                 }
919                                                 chan->c_private = mdir;
920                                         }
921                                 break;
922                                 
923                                 default:
924                                         log(DEBUG,"Preprocessing custom mode %c: modelist: %s",modechar,chan->custom_modes);
925                                         string_list p;
926                                         p.clear();
927                                         if (((!strchr(chan->custom_modes,modechar)) && (!mdir)) || ((strchr(chan->custom_modes,modechar)) && (mdir)))
928                                         {
929                                                 if (!ModeIsListMode(modechar,MT_CHANNEL))
930                                                 {
931                                                         log(DEBUG,"Mode %c isnt set on %s but trying to remove!",modechar,chan->name);
932                                                         break;
933                                                 }
934                                         }
935                                         if (ModeDefined(modechar,MT_CHANNEL))
936                                         {
937                                                 log(DEBUG,"A module has claimed this mode");
938                                                 if (param<pcnt)
939                                                 {
940                                                         if ((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir))
941                                                         {
942                                                                 p.push_back(parameters[param]);
943                                                         }
944                                                         if ((ModeDefinedOff(modechar,MT_CHANNEL)>0) && (!mdir))
945                                                         {
946                                                                 p.push_back(parameters[param]);
947                                                         }
948                                                 }
949                                                 bool handled = false;
950                                                 if (param>=pcnt)
951                                                 {
952                                                         // we're supposed to have a parameter, but none was given... so dont handle the mode.
953                                                         if (((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir)) || ((ModeDefinedOff(modechar,MT_CHANNEL)>0) && (!mdir))) 
954                                                         {
955                                                                 log(DEBUG,"Not enough parameters for module-mode %c",modechar);
956                                                                 handled = true;
957                                                                 param++;
958                                                         }
959                                                 }
960
961                                                 // BIG ASS IDIOTIC CODER WARNING!
962                                                 // Using OnRawMode on another modules mode's behavour 
963                                                 // will confuse the crap out of admins! just because you CAN
964                                                 // do it, doesnt mean you SHOULD!
965                                                 MOD_RESULT = 0;
966                                                 std::string para = "";
967                                                 if (p.size())
968                                                         para = p[0];
969                                                 FOREACH_RESULT(OnRawMode(user, chan, modechar, para, mdir, pcnt));
970                                                 if (!MOD_RESULT)
971                                                 {
972                                                         for (int i = 0; i <= MODCOUNT; i++)
973                                                         {
974                                                                 if (!handled)
975                                                                 {
976                                                                         int t = modules[i]->OnExtendedMode(user,chan,modechar,MT_CHANNEL,mdir,p);
977                                                                         if (t != 0)
978                                                                         {
979                                                                                 log(DEBUG,"OnExtendedMode returned nonzero for a module");
980                                                                                 char app[] = {modechar, 0};
981                                                                                 if (ModeIsListMode(modechar,MT_CHANNEL))
982                                                                                 {
983                                                                                         if (t == -1)
984                                                                                         {
985                                                                                                 pc++;
986                                                                                         }
987                                                                                         else
988                                                                                         {
989                                                                                                 if (ptr>0)
990                                                                                                 {
991                                                                                                         strlcat(outlist, app,MAXBUF);
992                                                                                                 }
993                                                                                                 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
994                                                                                         }
995                                                                                 }
996                                                                                 else
997                                                                                 {
998                                                                                         if (ptr>0)
999                                                                                         {
1000                                                                                                 if ((modelist[ptr-1] == '+') || (modelist[ptr-1] == '-'))
1001                                                                                                 {
1002                                                                                                         strlcat(outlist, app,MAXBUF);
1003                                                                                                 }
1004                                                                                                 else if (!strchr(outlist,modechar))
1005                                                                                                 {
1006                                                                                                         strlcat(outlist, app,MAXBUF);
1007                                                                                                 }
1008                                                                                         }
1009                                                                                         chan->SetCustomMode(modechar,mdir);
1010                                                                                         // include parameters in output if mode has them
1011                                                                                         if ((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir))
1012                                                                                         {
1013                                                                                                 chan->SetCustomModeParam(modelist[ptr],parameters[param],mdir);
1014                                                                                                 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
1015                                                                                         }
1016                                                                                 }
1017                                                                                 // break, because only one module can handle the mode.
1018                                                                                 handled = true;
1019                                                                         }
1020                                                                 }
1021                                                         }
1022                                                 }
1023                                         }
1024                                         else
1025                                         {
1026                                                 WriteServ(user->fd,"472 %s %c :is unknown mode char to me",user->nick,modechar);
1027                                         }
1028                                 break;
1029                                 
1030                         }
1031                 }
1032         }
1033
1034         /* this ensures only the *valid* modes are sent out onto the network */
1035         int xt = strlen(outlist)-1;
1036         while ((outlist[xt] == '-') || (outlist[xt] == '+'))
1037         {
1038                 outlist[xt] = '\0';
1039                 xt = strlen(outlist)-1;
1040         }
1041         if (outlist[0])
1042         {
1043                 strlcpy(outstr,outlist,MAXBUF);
1044                 for (ptr = 0; ptr < pc; ptr++)
1045                 {
1046                         strlcat(outstr," ",MAXBUF);
1047                         strlcat(outstr,outpars[ptr],MAXBUF);
1048                 }
1049                 if (local)
1050                 {
1051                         log(DEBUG,"Local mode change");
1052                         WriteChannelLocal(chan, user, "MODE %s %s",chan->name,outstr);
1053                 }
1054                 else
1055                 {
1056                         if (servermode)
1057                         {
1058                                 if (!silent)
1059                                 {
1060                                         WriteChannelWithServ(ServerName,chan,"MODE %s %s",chan->name,outstr);
1061                                         // M token for a usermode must go to all servers
1062                                         char buffer[MAXBUF];
1063                                         snprintf(buffer,MAXBUF,"M %s %s",chan->name, outstr);
1064                                         NetSendToAll(buffer);
1065                                 }
1066                                         
1067                         }
1068                         else
1069                         {
1070                                 if (!silent)
1071                                 {
1072                                         WriteChannel(chan,user,"MODE %s %s",chan->name,outstr);
1073                                         // M token for a usermode must go to all servers
1074                                         char buffer[MAXBUF];
1075                                         snprintf(buffer,MAXBUF,"m %s %s %s",user->nick,chan->name, outstr);
1076                                         NetSendToAll(buffer);
1077                                 }
1078                         }
1079                 }
1080         }
1081 }
1082
1083 // based on sourcemodes, return true or false to determine if umode is a valid mode a user may set on themselves or others.
1084
1085 bool allowed_umode(char umode, char* sourcemodes,bool adding)
1086 {
1087         log(DEBUG,"Allowed_umode: %c %s",umode,sourcemodes);
1088         // RFC1459 specified modes
1089         if ((umode == 'w') || (umode == 's') || (umode == 'i'))
1090         {
1091                 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1092                 return true;
1093         }
1094         
1095         // user may not +o themselves or others, but an oper may de-oper other opers or themselves
1096         if ((strchr(sourcemodes,'o')) && (!adding))
1097         {
1098                 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1099                 return true;
1100         }
1101         else if (umode == 'o')
1102         {
1103                 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1104                 return false;
1105         }
1106         
1107         // process any module-defined modes that need oper
1108         if ((ModeDefinedOper(umode,MT_CLIENT)) && (strchr(sourcemodes,'o')))
1109         {
1110                 log(DEBUG,"umode %c allowed by module handler (oper only mode)",umode);
1111                 return true;
1112         }
1113         else
1114         if (ModeDefined(umode,MT_CLIENT))
1115         {
1116                 // process any module-defined modes that don't need oper
1117                 log(DEBUG,"umode %c allowed by module handler (non-oper mode)",umode);
1118                 if ((ModeDefinedOper(umode,MT_CLIENT)) && (!strchr(sourcemodes,'o')))
1119                 {
1120                         // no, this mode needs oper, and this user 'aint got what it takes!
1121                         return false;
1122                 }
1123                 return true;
1124         }
1125
1126         // anything else - return false.
1127         log(DEBUG,"umode %c not known by any ruleset",umode);
1128         return false;
1129 }
1130
1131 bool process_module_umode(char umode, userrec* source, void* dest, bool adding)
1132 {
1133         userrec* s2;
1134         bool faked = false;
1135         if (!source)
1136         {
1137                 s2 = new userrec;
1138                 strlcpy(s2->nick,ServerName,NICKMAX);
1139                 strcpy(s2->modes,"o");
1140                 s2->fd = -1;
1141                 source = s2;
1142                 faked = true;
1143         }
1144         string_list p;
1145         p.clear();
1146         if (ModeDefined(umode,MT_CLIENT))
1147         {
1148                 for (int i = 0; i <= MODCOUNT; i++)
1149                 {
1150                         if (modules[i]->OnExtendedMode(source,(void*)dest,umode,MT_CLIENT,adding,p))
1151                         {
1152                                 log(DEBUG,"Module %s claims umode %c",module_names[i].c_str(),umode);
1153                                 return true;
1154                         }
1155                 }
1156                 log(DEBUG,"No module claims umode %c",umode);
1157                 if (faked)
1158                 {
1159                         delete s2;
1160                         source = NULL;
1161                 }
1162                 return false;
1163         }
1164         else
1165         {
1166                 if (faked)
1167                 {
1168                         delete s2;
1169                         source = NULL;
1170                 }
1171                 return false;
1172         }
1173 }
1174
1175 void handle_mode(char **parameters, int pcnt, userrec *user)
1176 {
1177         chanrec* Ptr;
1178         userrec* dest;
1179         int can_change;
1180         int direction = 1;
1181         char outpars[MAXBUF];
1182
1183         dest = Find(parameters[0]);
1184
1185         if (!user)
1186         {
1187                 return;
1188         }
1189
1190         if ((dest) && (pcnt == 1))
1191         {
1192                 WriteServ(user->fd,"221 %s :+%s",dest->nick,dest->modes);
1193                 return;
1194         }
1195
1196         if ((dest) && (pcnt > 1))
1197         {
1198                 char dmodes[MAXBUF];
1199                 strlcpy(dmodes,dest->modes,MAXBUF);
1200                 log(DEBUG,"pulled up dest user modes: %s",dmodes);
1201         
1202                 can_change = 0;
1203                 if (user != dest)
1204                 {
1205                         if (strchr(user->modes,'o'))
1206                         {
1207                                 can_change = 1;
1208                         }
1209                 }
1210                 else
1211                 {
1212                         can_change = 1;
1213                 }
1214                 if (!can_change)
1215                 {
1216                         WriteServ(user->fd,"482 %s :Can't change mode for other users",user->nick);
1217                         return;
1218                 }
1219                 
1220                 strcpy(outpars,"+");
1221                 direction = 1;
1222
1223                 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1224                         return;
1225
1226                 for (int i = 0; i < strlen(parameters[1]); i++)
1227                 {
1228                         if (parameters[1][i] == ' ')
1229                                 continue;
1230                         if (parameters[1][i] == '+')
1231                         {
1232                                 if (direction != 1)
1233                                 {
1234                                         int t = strlen(outpars)-1;
1235                                         if ((outpars[t] == '+') || (outpars[t] == '-'))
1236                                         {
1237                                                 outpars[t] = '+';
1238                                         }
1239                                         else
1240                                         {
1241                                                 strcat(outpars,"+");
1242                                         }
1243                                 }
1244                                 direction = 1;
1245                         }
1246                         else
1247                         if (parameters[1][i] == '-')
1248                         {
1249                                 if (direction != 0)
1250                                 {
1251                                         int t = strlen(outpars)-1;
1252                                         if ((outpars[t] == '+') || (outpars[t] == '-'))
1253                                         {
1254                                                 outpars[t] = '-';
1255                                         }
1256                                         else
1257                                         {
1258                                                 strcat(outpars,"-");
1259                                         }
1260                                 }
1261                                 direction = 0;
1262                         }
1263                         else
1264                         {
1265                                 can_change = 0;
1266                                 if (strchr(user->modes,'o'))
1267                                 {
1268                                         can_change = 1;
1269                                 }
1270                                 else
1271                                 {
1272                                         if ((parameters[1][i] == 'i') || (parameters[1][i] == 'w') || (parameters[1][i] == 's') || (allowed_umode(parameters[1][i],user->modes,direction)))
1273                                         {
1274                                                 can_change = 1;
1275                                         }
1276                                 }
1277                                 if (can_change)
1278                                 {
1279                                         if (direction == 1)
1280                                         {
1281                                                 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true)))
1282                                                 {
1283                                                         char umode = parameters[1][i];
1284                                                         if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1285                                                         {
1286                                                                 int q = strlen(dmodes);
1287                                                                 int r = strlen(outpars);
1288                                                                 dmodes[q+1]='\0';
1289                                                                 dmodes[q] = parameters[1][i];
1290                                                                 outpars[r+1]='\0';
1291                                                                 outpars[r] = parameters[1][i];
1292                                                                 if (parameters[1][i] == 'o')
1293                                                                 {
1294                                                                         FOREACH_MOD OnGlobalOper(dest);
1295                                                                 }
1296                                                         }
1297                                                 }
1298                                         }
1299                                         else
1300                                         {
1301                                                 if ((allowed_umode(parameters[1][i],user->modes,false)) && (strchr(dmodes,parameters[1][i])))
1302                                                 {
1303                                                         char umode = parameters[1][i];
1304                                                         if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1305                                                         {
1306                                                                 int q = 0;
1307                                                                 char temp[MAXBUF];      
1308                                                                 char moo[MAXBUF];       
1309
1310                                                                 int r = strlen(outpars);
1311                                                                 outpars[r+1]='\0';
1312                                                                 outpars[r] = parameters[1][i];
1313                                                         
1314                                                                 strcpy(temp,"");
1315                                                                 for (q = 0; q < strlen(dmodes); q++)
1316                                                                 {
1317                                                                         if (dmodes[q] != parameters[1][i])
1318                                                                         {
1319                                                                                 moo[0] = dmodes[q];
1320                                                                                 moo[1] = '\0';
1321                                                                                 strlcat(temp,moo,MAXBUF);
1322                                                                         }
1323                                                                 }
1324                                                                 strlcpy(dmodes,temp,MAXBUF);
1325
1326                                                                 if (umode == 'o')
1327                                                                         DeleteOper(dest);
1328                                                         }
1329                                                 }
1330                                         }
1331                                 }
1332                         }
1333                 }
1334                 if (outpars[0])
1335                 {
1336                         char b[MAXBUF];
1337                         strlcpy(b,"",MAXBUF);
1338                         int z = 0;
1339                         int i = 0;
1340                         while (i < strlen (outpars))
1341                         {
1342                                 b[z++] = outpars[i++];
1343                                 b[z] = '\0';
1344                                 if (i<strlen(outpars)-1)
1345                                 {
1346                                         if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1347                                         {
1348                                                 // someones playing silly buggers and trying
1349                                                 // to put a +- or -+ into the line...
1350                                                 i++;
1351                                         }
1352                                 }
1353                                 if (i == strlen(outpars)-1)
1354                                 {
1355                                         if ((outpars[i] == '-') || (outpars[i] == '+'))
1356                                         {
1357                                                 i++;
1358                                         }
1359                                 }
1360                         }
1361
1362                         z = strlen(b)-1;
1363                         if ((b[z] == '-') || (b[z] == '+'))
1364                                 b[z] = '\0';
1365
1366                         if ((!b[0]) || (!strcmp(b,"+")) || (!strcmp(b,"-")))
1367                                 return;
1368
1369                         WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1370
1371                         // M token for a usermode must go to all servers
1372                         char buffer[MAXBUF];
1373                         snprintf(buffer,MAXBUF,"m %s %s %s",user->nick, dest->nick, b);
1374                         NetSendToAll(buffer);
1375
1376                         if (strlen(dmodes)>MAXMODES)
1377                         {
1378                                 dmodes[MAXMODES-1] = '\0';
1379                         }
1380                         log(DEBUG,"Stripped mode line");
1381                         log(DEBUG,"Line dest is now %s",dmodes);
1382                         strlcpy(dest->modes,dmodes,MAXMODES);
1383
1384                 }
1385
1386                 return;
1387         }
1388         
1389         Ptr = FindChan(parameters[0]);
1390         if (Ptr)
1391         {
1392                 if (pcnt == 1)
1393                 {
1394                         /* just /modes #channel */
1395                         WriteServ(user->fd,"324 %s %s +%s",user->nick, Ptr->name, chanmodes(Ptr));
1396                         WriteServ(user->fd,"329 %s %s %d", user->nick, Ptr->name, Ptr->created);
1397                         return;
1398                 }
1399                 else
1400                 if (pcnt == 2)
1401                 {
1402                         char* mode = parameters[1];
1403                         if (*mode == '+')
1404                                 mode++;
1405                         int MOD_RESULT = 0;
1406                         FOREACH_RESULT(OnRawMode(user, Ptr, *mode, "", false, 0));
1407                         if (!MOD_RESULT)
1408                         {
1409                                 if (*mode == 'b')
1410                                 {
1411
1412                                         for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
1413                                         {
1414                                                 WriteServ(user->fd,"367 %s %s %s %s %d",user->nick, Ptr->name, i->data, i->set_by, i->set_time);
1415                                         }
1416                                         WriteServ(user->fd,"368 %s %s :End of channel ban list",user->nick, Ptr->name);
1417                                         return;
1418                                 }
1419                                 if ((ModeDefined(*mode,MT_CHANNEL)) && (ModeIsListMode(*mode,MT_CHANNEL)))
1420                                 {
1421                                         // list of items for an extmode
1422                                         log(DEBUG,"Calling OnSendList for all modules, list output for mode %c",*mode);
1423                                         FOREACH_MOD OnSendList(user,Ptr,*mode);
1424                                         return;
1425                                 }
1426                         }
1427                 }
1428
1429                 if ((Ptr) && (!has_channel(user,Ptr)))
1430                 {
1431                         WriteServ(user->fd,"442 %s %s :You're not on that channel!",user->nick, Ptr->name);
1432                         return;
1433                 }
1434
1435                 if (Ptr)
1436                 {
1437                         int MOD_RESULT = 0;
1438                         FOREACH_RESULT(OnAccessCheck(user,NULL,Ptr,AC_GENERAL_MODE));
1439                         
1440                         if (MOD_RESULT == ACR_DENY)
1441                                 return;
1442                         if (MOD_RESULT == ACR_DEFAULT)
1443                         {
1444                                 if (cstatus(user,Ptr) < STATUS_HOP)
1445                                 {
1446                                         WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, Ptr->name);
1447                                         return;
1448                                 }
1449                         }
1450
1451                         process_modes(parameters,user,Ptr,cstatus(user,Ptr),pcnt,false,false,false);
1452                 }
1453         }
1454         else
1455         {
1456                 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
1457         }
1458 }
1459
1460
1461
1462
1463 void server_mode(char **parameters, int pcnt, userrec *user)
1464 {
1465         chanrec* Ptr;
1466         userrec* dest;
1467         int can_change;
1468         int direction = 1;
1469         char outpars[MAXBUF];
1470
1471         dest = Find(parameters[0]);
1472         
1473         // fix: ChroNiCk found this - we cant use this as debug if its null!
1474         if (dest)
1475         {
1476                 log(DEBUG,"server_mode on %s",dest->nick);
1477         }
1478
1479         if ((dest) && (pcnt > 1))
1480         {
1481                 log(DEBUG,"params > 1");
1482
1483                 char dmodes[MAXBUF];
1484                 strlcpy(dmodes,dest->modes,MAXBUF);
1485
1486                 strcpy(outpars,"+");
1487                 direction = 1;
1488
1489                 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1490                         return;
1491
1492                 for (int i = 0; i < strlen(parameters[1]); i++)
1493                 {
1494                         if (parameters[1][i] == ' ')
1495                                 continue;
1496                         if (parameters[1][i] == '+')
1497                         {
1498                                 if (direction != 1)
1499                                 {
1500                                         int t = strlen(outpars)-1;
1501                                         if ((outpars[t] == '+') || (outpars[t] == '-'))
1502                                         {
1503                                                 outpars[t] = '+';
1504                                         }
1505                                         else
1506                                         {
1507                                                 strcat(outpars,"+");
1508                                         }
1509                                 }
1510                                 direction = 1;
1511                         }
1512                         else
1513                         if (parameters[1][i] == '-')
1514                         {
1515                                 if (direction != 0)
1516                                 {
1517                                         int t = strlen(outpars)-1;
1518                                         if ((outpars[t] == '+') || (outpars[t] == '-'))
1519                                         {
1520                                                 outpars[t] = '-';
1521                                         }
1522                                         else
1523                                         {
1524                                                 strcat(outpars,"-");
1525                                         }
1526                                 }
1527                                 direction = 0;
1528                         }
1529                         else
1530                         {
1531                                 log(DEBUG,"begin mode processing entry");
1532                                 can_change = 1;
1533                                 if (can_change)
1534                                 {
1535                                         if (direction == 1)
1536                                         {
1537                                                 log(DEBUG,"umode %c being added",parameters[1][i]);
1538                                                 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true)))
1539                                                 {
1540                                                         char umode = parameters[1][i];
1541                                                         log(DEBUG,"umode %c is an allowed umode",umode);
1542                                                         if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1543                                                         {
1544                                                                 int v1 = strlen(dmodes);
1545                                                                 int v2 = strlen(outpars);
1546                                                                 dmodes[v1+1]='\0';
1547                                                                 dmodes[v1] = parameters[1][i];
1548                                                                 outpars[v2+1]='\0';
1549                                                                 outpars[v2] = parameters[1][i];
1550                                                         }
1551                                                 }
1552                                         }
1553                                         else
1554                                         {
1555                                                 // can only remove a mode they already have
1556                                                 log(DEBUG,"umode %c being removed",parameters[1][i]);
1557                                                 if ((allowed_umode(parameters[1][i],user->modes,false)) && (strchr(dmodes,parameters[1][i])))
1558                                                 {
1559                                                         char umode = parameters[1][i];
1560                                                         log(DEBUG,"umode %c is an allowed umode",umode);
1561                                                         if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1562                                                         {
1563                                                                 int q = 0;
1564                                                                 char temp[MAXBUF];
1565                                                                 char moo[MAXBUF];       
1566
1567                                                                 int v1 = strlen(outpars);
1568                                                                 outpars[v1+1]='\0';
1569                                                                 outpars[v1] = parameters[1][i];
1570                                                         
1571                                                                 strcpy(temp,"");
1572                                                                 for (q = 0; q < strlen(dmodes); q++)
1573                                                                 {
1574                                                                         if (dmodes[q] != parameters[1][i])
1575                                                                         {
1576                                                                                 moo[0] = dmodes[q];
1577                                                                                 moo[1] = '\0';
1578                                                                                 strlcat(temp,moo,MAXBUF);
1579                                                                         }
1580                                                                 }
1581                                                                 strlcpy(dmodes,temp,MAXBUF);
1582                                                         }
1583                                                 }
1584                                         }
1585                                 }
1586                         }
1587                 }
1588                 if (outpars[0])
1589                 {
1590                         char b[MAXBUF];
1591                         strlcpy(b,"",MAXBUF);
1592                         int z = 0;
1593                         int i = 0;
1594                         while (i < strlen (outpars))
1595                         {
1596                                 b[z++] = outpars[i++];
1597                                 b[z] = '\0';
1598                                 if (i<strlen(outpars)-1)
1599                                 {
1600                                         if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1601                                         {
1602                                                 // someones playing silly buggers and trying
1603                                                 // to put a +- or -+ into the line...
1604                                                 i++;
1605                                         }
1606                                 }
1607                                 if (i == strlen(outpars)-1)
1608                                 {
1609                                         if ((outpars[i] == '-') || (outpars[i] == '+'))
1610                                         {
1611                                                 i++;
1612                                         }
1613                                 }
1614                         }
1615
1616                         z = strlen(b)-1;
1617                         if ((b[z] == '-') || (b[z] == '+'))
1618                                 b[z] = '\0';
1619
1620                         if ((!strcmp(b,"+")) || (!strcmp(b,"-")))
1621                                 return;
1622
1623                         WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1624
1625                         // M token for a usermode must go to all servers
1626                         char buffer[MAXBUF];
1627                         snprintf(buffer,MAXBUF,"m %s %s %s",user->nick, dest->nick, b);
1628                         NetSendToAll(buffer);
1629                         
1630                         if (strlen(dmodes)>MAXMODES)
1631                         {
1632                                 dmodes[MAXMODES-1] = '\0';
1633                         }
1634                         log(DEBUG,"Stripped mode line");
1635                         log(DEBUG,"Line dest is now %s",dmodes);
1636                         strlcpy(dest->modes,dmodes,MAXMODES);
1637
1638                 }
1639
1640                 return;
1641         }
1642         
1643         Ptr = FindChan(parameters[0]);
1644         if (Ptr)
1645         {
1646                 process_modes(parameters,user,Ptr,STATUS_OP,pcnt,true,false,false);
1647         }
1648         else
1649         {
1650                 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
1651         }
1652 }
1653
1654
1655
1656 void merge_mode(char **parameters, int pcnt)
1657 {
1658         chanrec* Ptr;
1659         userrec* dest;
1660         int can_change;
1661         int direction = 1;
1662         char outpars[MAXBUF];
1663
1664         dest = Find(parameters[0]);
1665         
1666         // fix: ChroNiCk found this - we cant use this as debug if its null!
1667         if (dest)
1668         {
1669                 log(DEBUG,"merge_mode on %s",dest->nick);
1670         }
1671
1672         if ((dest) && (pcnt > 1))
1673         {
1674                 log(DEBUG,"params > 1");
1675
1676                 char dmodes[MAXBUF];
1677                 strlcpy(dmodes,dest->modes,MAXBUF);
1678
1679                 strcpy(outpars,"+");
1680                 direction = 1;
1681
1682                 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1683                         return;
1684
1685                 for (int i = 0; i < strlen(parameters[1]); i++)
1686                 {
1687                         if (parameters[1][i] == ' ')
1688                                 continue;
1689                         if (parameters[1][i] == '+')
1690                         {
1691                                 if (direction != 1)
1692                                 {
1693                                         int t = strlen(outpars)-1;
1694                                         if ((outpars[t] == '+') || (outpars[t] == '-'))
1695                                         {
1696                                                 outpars[t] = '+';
1697                                         }
1698                                         else
1699                                         {
1700                                                 strcat(outpars,"+");
1701                                         }
1702                                 }
1703                                 direction = 1;
1704                         }
1705                         else
1706                         if (parameters[1][i] == '-')
1707                         {
1708                                 if (direction != 0)
1709                                 {
1710                                         int t = strlen(outpars)-1;
1711                                         if ((outpars[t] == '+') || (outpars[t] == '-'))
1712                                         {
1713                                                 outpars[t] = '-';
1714                                         }
1715                                         else
1716                                         {
1717                                                 strcat(outpars,"-");
1718                                         }
1719                                 }
1720                                 direction = 0;
1721                         }
1722                         else
1723                         {
1724                                 log(DEBUG,"begin mode processing entry");
1725                                 can_change = 1;
1726                                 if (can_change)
1727                                 {
1728                                         if (direction == 1)
1729                                         {
1730                                                 log(DEBUG,"umode %c being added",parameters[1][i]);
1731                                                 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],"o",true)))
1732                                                 {
1733                                                         char umode = parameters[1][i];
1734                                                         log(DEBUG,"umode %c is an allowed umode",umode);
1735                                                         if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1736                                                         {
1737                                                                 int v1 = strlen(dmodes);
1738                                                                 int v2 = strlen(outpars);
1739                                                                 dmodes[v1+1]='\0';
1740                                                                 dmodes[v1] = parameters[1][i];
1741                                                                 outpars[v2+1]='\0';
1742                                                                 outpars[v2] = parameters[1][i];
1743                                                         }
1744                                                 }
1745                                         }
1746                                         else
1747                                         {
1748                                                 // can only remove a mode they already have
1749                                                 log(DEBUG,"umode %c being removed",parameters[1][i]);
1750                                                 if ((allowed_umode(parameters[1][i],"o",false)) && (strchr(dmodes,parameters[1][i])))
1751                                                 {
1752                                                         char umode = parameters[1][i];
1753                                                         log(DEBUG,"umode %c is an allowed umode",umode);
1754                                                         if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1755                                                         {
1756                                                                 int q = 0;
1757                                                                 char temp[MAXBUF];
1758                                                                 char moo[MAXBUF];       
1759
1760                                                                 int v1 = strlen(outpars);
1761                                                                 outpars[v1+1]='\0';
1762                                                                 outpars[v1] = parameters[1][i];
1763                                                         
1764                                                                 strcpy(temp,"");
1765                                                                 for (q = 0; q < strlen(dmodes); q++)
1766                                                                 {
1767                                                                         if (dmodes[q] != parameters[1][i])
1768                                                                         {
1769                                                                                 moo[0] = dmodes[q];
1770                                                                                 moo[1] = '\0';
1771                                                                                 strlcat(temp,moo,MAXBUF);
1772                                                                         }
1773                                                                 }
1774                                                                 strlcpy(dmodes,temp,MAXBUF);
1775                                                         }
1776                                                 }
1777                                         }
1778                                 }
1779                         }
1780                 }
1781                 if (outpars[0])
1782                 {
1783                         char b[MAXBUF];
1784                         strcpy(b,"");
1785                         int z = 0;
1786                         int i = 0;
1787                         while (i < strlen (outpars))
1788                         {
1789                                 b[z++] = outpars[i++];
1790                                 b[z] = '\0';
1791                                 if (i<strlen(outpars)-1)
1792                                 {
1793                                         if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1794                                         {
1795                                                 // someones playing silly buggers and trying
1796                                                 // to put a +- or -+ into the line...
1797                                                 i++;
1798                                         }
1799                                 }
1800                                 if (i == strlen(outpars)-1)
1801                                 {
1802                                         if ((outpars[i] == '-') || (outpars[i] == '+'))
1803                                         {
1804                                                 i++;
1805                                         }
1806                                 }
1807                         }
1808
1809                         z = strlen(b)-1;
1810                         if ((b[z] == '-') || (b[z] == '+'))
1811                                 b[z] = '\0';
1812
1813                         if ((!strcmp(b,"+")) || (!strcmp(b,"-")))
1814                                 return;
1815
1816                         if (strlen(dmodes)>MAXMODES)
1817                         {
1818                                 dmodes[MAXMODES-1] = '\0';
1819                         }
1820                         log(DEBUG,"Stripped mode line");
1821                         log(DEBUG,"Line dest is now %s",dmodes);
1822                         strlcpy(dest->modes,dmodes,MAXMODES);
1823
1824                 }
1825
1826                 return;
1827         }
1828         
1829         Ptr = FindChan(parameters[0]);
1830         if (Ptr)
1831         {
1832                 userrec s2;
1833                 strlcpy(s2.nick,ServerName,NICKMAX);
1834                 strcpy(s2.modes,"o");
1835                 s2.fd = -1;
1836                 process_modes(parameters,&s2,Ptr,STATUS_OP,pcnt,true,true,false);
1837         }
1838 }
1839
1840
1841 void merge_mode2(char **parameters, int pcnt, userrec* user)
1842 {
1843         chanrec* Ptr;
1844         userrec* dest;
1845         int can_change;
1846         int direction = 1;
1847         char outpars[MAXBUF];
1848
1849         dest = Find(parameters[0]);
1850         
1851         // fix: ChroNiCk found this - we cant use this as debug if its null!
1852         if (dest)
1853         {
1854                 log(DEBUG,"merge_mode2 on %s",dest->nick);
1855         }
1856
1857         if ((dest) && (pcnt > 1))
1858         {
1859                 log(DEBUG,"params > 1");
1860
1861                 char dmodes[MAXBUF];
1862                 strlcpy(dmodes,dest->modes,MAXBUF);
1863
1864                 strcpy(outpars,"+");
1865                 direction = 1;
1866
1867                 if ((parameters[1][0] == ':') && (strlen(parameters[1])>1))
1868                 {
1869                         // some stupid 3rd party things (such as services packages) put a colon on the mode list...
1870                         log(DEBUG,"Some muppet put a colon on the modelist! changed to '%s'",++parameters[1]);
1871                 }
1872                 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1873                 return;
1874
1875                 for (int i = 0; i < strlen(parameters[1]); i++)
1876                 {
1877                         if (parameters[1][i] == ' ')
1878                                 continue;
1879                         if (parameters[1][i] == '+')
1880                         {
1881                                 if (direction != 1)
1882                                 {
1883                                         int t = strlen(outpars)-1;
1884                                         if ((outpars[t] == '+') || (outpars[t] == '-'))
1885                                         {
1886                                                 outpars[t] = '+';
1887                                         }
1888                                         else
1889                                         {
1890                                                 strcat(outpars,"+");
1891                                         }
1892                                 }
1893                                 direction = 1;
1894                         }
1895                         else
1896                         if (parameters[1][i] == '-')
1897                         {
1898                                 if (direction != 0)
1899                                 {
1900                                         int t = strlen(outpars)-1;
1901                                         if ((outpars[t] == '+') || (outpars[t] == '-'))
1902                                         {
1903                                                 outpars[t] = '-';
1904                                         }
1905                                         else
1906                                         {
1907                                                 strcat(outpars,"-");
1908                                         }
1909                                 }
1910                                 direction = 0;
1911                         }
1912                         else
1913                         {
1914                                 log(DEBUG,"begin mode processing entry");
1915                                 can_change = 1;
1916                                 if (can_change)
1917                                 {
1918                                         if (direction == 1)
1919                                         {
1920                                                 log(DEBUG,"umode %c being added",parameters[1][i]);
1921                                                 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true)))
1922                                                 {
1923                                                         char umode = parameters[1][i];
1924                                                         log(DEBUG,"umode %c is an allowed umode",umode);
1925                                                         if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1926                                                         {
1927                                                                 int v1 = strlen(dmodes);
1928                                                                 int v2 = strlen(outpars);
1929                                                                 dmodes[v1+1]='\0';
1930                                                                 dmodes[v1] = parameters[1][i];
1931                                                                 outpars[v2+1]='\0';
1932                                                                 outpars[v2] = parameters[1][i];
1933                                                                 log(DEBUG,"OUTPARS='%s', DMODES='%s'",outpars,dmodes);
1934                                                         }
1935                                                 }
1936                                         }
1937                                         else
1938                                         {
1939                                                 // can only remove a mode they already have
1940                                                 log(DEBUG,"umode %c being removed",parameters[1][i]);
1941                                                 if ((allowed_umode(parameters[1][i],user->modes,false)) && (strchr(dmodes,parameters[1][i])))
1942                                                 {
1943                                                         char umode = parameters[1][i];
1944                                                         log(DEBUG,"umode %c is an allowed umode",umode);
1945                                                         if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1946                                                         {
1947                                                                 int q = 0;
1948                                                                 char temp[MAXBUF];
1949                                                                 char moo[MAXBUF];       
1950
1951                                                                 int v1 = strlen(outpars);
1952                                                                 outpars[v1+1]='\0';
1953                                                                 outpars[v1] = parameters[1][i];
1954                                                         
1955                                                                 strcpy(temp,"");
1956                                                                 for (q = 0; q < strlen(dmodes); q++)
1957                                                                 {
1958                                                                         if (dmodes[q] != parameters[1][i])
1959                                                                         {
1960                                                                                 moo[0] = dmodes[q];
1961                                                                                 moo[1] = '\0';
1962                                                                                 strlcat(temp,moo,MAXBUF);
1963                                                                         }
1964                                                                 }
1965                                                                 strlcpy(dmodes,temp,MAXBUF);
1966                                                         }
1967                                                 }
1968                                         }
1969                                 }
1970                         }
1971                 }
1972                 log(DEBUG,"DONE! OUTPARS='%s', DMODES='%s'",outpars,dmodes);
1973                 if (outpars[0])
1974                 {
1975                         char b[MAXBUF];
1976                         strcpy(b,"");
1977                         int z = 0;
1978                         int i = 0;
1979                         while (i < strlen (outpars))
1980                         {
1981                                 b[z++] = outpars[i++];
1982                                 b[z] = '\0';
1983                                 if (i<strlen(outpars)-1)
1984                                 {
1985                                         if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1986                                         {
1987                                                 // someones playing silly buggers and trying
1988                                                 // to put a +- or -+ into the line...
1989                                                 i++;
1990                                         }
1991                                 }
1992                                 if (i == strlen(outpars)-1)
1993                                 {
1994                                         if ((outpars[i] == '-') || (outpars[i] == '+'))
1995                                         {
1996                                                 i++;
1997                                         }
1998                                 }
1999                         }
2000
2001                         z = strlen(b)-1;
2002                         if ((b[z] == '-') || (b[z] == '+'))
2003                                 b[z] = '\0';
2004
2005                         if ((!strcmp(b,"+")) || (!strcmp(b,"-")))
2006                                 return;
2007
2008                         // was sending 'b'
2009                         WriteTo(user,dest,"MODE %s :%s",dest->nick,b);
2010                         log(DEBUG,"Sent: :%s MODE %s",user->nick,b);
2011
2012                         if (strlen(dmodes)>MAXMODES)
2013                         {
2014                                 dmodes[MAXMODES-1] = '\0';
2015                         }
2016                         log(DEBUG,"Stripped mode line");
2017                         log(DEBUG,"Line dest is now %s",dmodes);
2018                         strlcpy(dest->modes,dmodes,MAXMODES);
2019
2020                 }
2021
2022                 return;
2023         }
2024         
2025         Ptr = FindChan(parameters[0]);
2026         if (Ptr)
2027         {
2028                 log(DEBUG,"merge_mode2: found channel %s",Ptr->name);
2029                 if (Ptr)
2030                 {
2031                         if ((cstatus(user,Ptr) < STATUS_HOP) && (!is_uline(user->server)))
2032                         {
2033                                 return;
2034                         }
2035                         process_modes(parameters,user,Ptr,cstatus(user,Ptr),pcnt,false,false,true);
2036                 }
2037         }
2038 }
2039
2040