]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/mode.cpp
Fixes to all kinds of annoying problems
[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; parameters[param][i] != 0; 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; parameters[1][i] != 0; i++)
1227                 {
1228                         if (parameters[1][i] == '+')
1229                         {
1230                                 if (direction != 1)
1231                                 {
1232                                         int t = strlen(outpars)-1;
1233                                         if ((outpars[t] == '+') || (outpars[t] == '-'))
1234                                         {
1235                                                 outpars[t] = '+';
1236                                         }
1237                                         else
1238                                         {
1239                                                 strcat(outpars,"+");
1240                                         }
1241                                 }
1242                                 direction = 1;
1243                         }
1244                         else
1245                         if (parameters[1][i] == '-')
1246                         {
1247                                 if (direction != 0)
1248                                 {
1249                                         int t = strlen(outpars)-1;
1250                                         if ((outpars[t] == '+') || (outpars[t] == '-'))
1251                                         {
1252                                                 outpars[t] = '-';
1253                                         }
1254                                         else
1255                                         {
1256                                                 strcat(outpars,"-");
1257                                         }
1258                                 }
1259                                 direction = 0;
1260                         }
1261                         else
1262                         {
1263                                 can_change = 0;
1264                                 if (strchr(user->modes,'o'))
1265                                 {
1266                                         can_change = 1;
1267                                 }
1268                                 else
1269                                 {
1270                                         if ((parameters[1][i] == 'i') || (parameters[1][i] == 'w') || (parameters[1][i] == 's') || (allowed_umode(parameters[1][i],user->modes,direction)))
1271                                         {
1272                                                 can_change = 1;
1273                                         }
1274                                 }
1275                                 if (can_change)
1276                                 {
1277                                         if (direction == 1)
1278                                         {
1279                                                 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true)))
1280                                                 {
1281                                                         char umode = parameters[1][i];
1282                                                         if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1283                                                         {
1284                                                                 int q = strlen(dmodes);
1285                                                                 int r = strlen(outpars);
1286                                                                 dmodes[q+1]='\0';
1287                                                                 dmodes[q] = parameters[1][i];
1288                                                                 outpars[r+1]='\0';
1289                                                                 outpars[r] = parameters[1][i];
1290                                                                 if (parameters[1][i] == 'o')
1291                                                                 {
1292                                                                         FOREACH_MOD OnGlobalOper(dest);
1293                                                                 }
1294                                                         }
1295                                                 }
1296                                         }
1297                                         else
1298                                         {
1299                                                 if ((allowed_umode(parameters[1][i],user->modes,false)) && (strchr(dmodes,parameters[1][i])))
1300                                                 {
1301                                                         char umode = parameters[1][i];
1302                                                         if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1303                                                         {
1304                                                                 int q = 0;
1305                                                                 char temp[MAXBUF];      
1306                                                                 char moo[MAXBUF];       
1307
1308                                                                 int r = strlen(outpars);
1309                                                                 outpars[r+1]='\0';
1310                                                                 outpars[r] = parameters[1][i];
1311                                                         
1312                                                                 strcpy(temp,"");
1313                                                                 for (q = 0; dmodes[q] != 0; q++)
1314                                                                 {
1315                                                                         if (dmodes[q] != parameters[1][i])
1316                                                                         {
1317                                                                                 moo[0] = dmodes[q];
1318                                                                                 moo[1] = '\0';
1319                                                                                 strlcat(temp,moo,MAXBUF);
1320                                                                         }
1321                                                                 }
1322                                                                 strlcpy(dmodes,temp,MAXBUF);
1323
1324                                                                 if (umode == 'o')
1325                                                                         DeleteOper(dest);
1326                                                         }
1327                                                 }
1328                                         }
1329                                 }
1330                         }
1331                 }
1332                 if (outpars[0])
1333                 {
1334                         char b[MAXBUF];
1335                         strlcpy(b,"",MAXBUF);
1336                         int z = 0;
1337                         int i = 0;
1338                         while (i < strlen (outpars))
1339                         {
1340                                 b[z++] = outpars[i++];
1341                                 b[z] = '\0';
1342                                 if (i<strlen(outpars)-1)
1343                                 {
1344                                         if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1345                                         {
1346                                                 // someones playing silly buggers and trying
1347                                                 // to put a +- or -+ into the line...
1348                                                 i++;
1349                                         }
1350                                 }
1351                                 if (i == strlen(outpars)-1)
1352                                 {
1353                                         if ((outpars[i] == '-') || (outpars[i] == '+'))
1354                                         {
1355                                                 i++;
1356                                         }
1357                                 }
1358                         }
1359
1360                         z = strlen(b)-1;
1361                         if ((b[z] == '-') || (b[z] == '+'))
1362                                 b[z] = '\0';
1363
1364                         if ((!b[0]) || (!strcmp(b,"+")) || (!strcmp(b,"-")))
1365                                 return;
1366
1367                         WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1368
1369                         // M token for a usermode must go to all servers
1370                         char buffer[MAXBUF];
1371                         snprintf(buffer,MAXBUF,"m %s %s %s",user->nick, dest->nick, b);
1372                         NetSendToAll(buffer);
1373
1374                         if (strlen(dmodes)>MAXMODES)
1375                         {
1376                                 dmodes[MAXMODES-1] = '\0';
1377                         }
1378                         log(DEBUG,"Stripped mode line");
1379                         log(DEBUG,"Line dest is now %s",dmodes);
1380                         strlcpy(dest->modes,dmodes,MAXMODES);
1381
1382                 }
1383
1384                 return;
1385         }
1386         
1387         Ptr = FindChan(parameters[0]);
1388         if (Ptr)
1389         {
1390                 if (pcnt == 1)
1391                 {
1392                         /* just /modes #channel */
1393                         WriteServ(user->fd,"324 %s %s +%s",user->nick, Ptr->name, chanmodes(Ptr));
1394                         WriteServ(user->fd,"329 %s %s %d", user->nick, Ptr->name, Ptr->created);
1395                         return;
1396                 }
1397                 else
1398                 if (pcnt == 2)
1399                 {
1400                         char* mode = parameters[1];
1401                         if (*mode == '+')
1402                                 mode++;
1403                         int MOD_RESULT = 0;
1404                         FOREACH_RESULT(OnRawMode(user, Ptr, *mode, "", false, 0));
1405                         if (!MOD_RESULT)
1406                         {
1407                                 if (*mode == 'b')
1408                                 {
1409
1410                                         for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
1411                                         {
1412                                                 WriteServ(user->fd,"367 %s %s %s %s %d",user->nick, Ptr->name, i->data, i->set_by, i->set_time);
1413                                         }
1414                                         WriteServ(user->fd,"368 %s %s :End of channel ban list",user->nick, Ptr->name);
1415                                         return;
1416                                 }
1417                                 if ((ModeDefined(*mode,MT_CHANNEL)) && (ModeIsListMode(*mode,MT_CHANNEL)))
1418                                 {
1419                                         // list of items for an extmode
1420                                         log(DEBUG,"Calling OnSendList for all modules, list output for mode %c",*mode);
1421                                         FOREACH_MOD OnSendList(user,Ptr,*mode);
1422                                         return;
1423                                 }
1424                         }
1425                 }
1426
1427                 if ((Ptr) && (!has_channel(user,Ptr)))
1428                 {
1429                         WriteServ(user->fd,"442 %s %s :You're not on that channel!",user->nick, Ptr->name);
1430                         return;
1431                 }
1432
1433                 if (Ptr)
1434                 {
1435                         int MOD_RESULT = 0;
1436                         FOREACH_RESULT(OnAccessCheck(user,NULL,Ptr,AC_GENERAL_MODE));
1437                         
1438                         if (MOD_RESULT == ACR_DENY)
1439                                 return;
1440                         if (MOD_RESULT == ACR_DEFAULT)
1441                         {
1442                                 if (cstatus(user,Ptr) < STATUS_HOP)
1443                                 {
1444                                         WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, Ptr->name);
1445                                         return;
1446                                 }
1447                         }
1448
1449                         process_modes(parameters,user,Ptr,cstatus(user,Ptr),pcnt,false,false,false);
1450                 }
1451         }
1452         else
1453         {
1454                 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
1455         }
1456 }
1457
1458
1459
1460
1461 void server_mode(char **parameters, int pcnt, userrec *user)
1462 {
1463         chanrec* Ptr;
1464         userrec* dest;
1465         int can_change;
1466         int direction = 1;
1467         char outpars[MAXBUF];
1468
1469         dest = Find(parameters[0]);
1470         
1471         // fix: ChroNiCk found this - we cant use this as debug if its null!
1472         if (dest)
1473         {
1474                 log(DEBUG,"server_mode on %s",dest->nick);
1475         }
1476
1477         if ((dest) && (pcnt > 1))
1478         {
1479                 log(DEBUG,"params > 1");
1480
1481                 char dmodes[MAXBUF];
1482                 strlcpy(dmodes,dest->modes,MAXBUF);
1483
1484                 strcpy(outpars,"+");
1485                 direction = 1;
1486
1487                 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1488                         return;
1489
1490                 for (int i = 0; parameters[1][i] != 0; i++)
1491                 {
1492                         if (parameters[1][i] == '+')
1493                         {
1494                                 if (direction != 1)
1495                                 {
1496                                         int t = strlen(outpars)-1;
1497                                         if ((outpars[t] == '+') || (outpars[t] == '-'))
1498                                         {
1499                                                 outpars[t] = '+';
1500                                         }
1501                                         else
1502                                         {
1503                                                 strcat(outpars,"+");
1504                                         }
1505                                 }
1506                                 direction = 1;
1507                         }
1508                         else
1509                         if (parameters[1][i] == '-')
1510                         {
1511                                 if (direction != 0)
1512                                 {
1513                                         int t = strlen(outpars)-1;
1514                                         if ((outpars[t] == '+') || (outpars[t] == '-'))
1515                                         {
1516                                                 outpars[t] = '-';
1517                                         }
1518                                         else
1519                                         {
1520                                                 strcat(outpars,"-");
1521                                         }
1522                                 }
1523                                 direction = 0;
1524                         }
1525                         else
1526                         {
1527                                 log(DEBUG,"begin mode processing entry");
1528                                 can_change = 1;
1529                                 if (can_change)
1530                                 {
1531                                         if (direction == 1)
1532                                         {
1533                                                 log(DEBUG,"umode %c being added",parameters[1][i]);
1534                                                 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true)))
1535                                                 {
1536                                                         char umode = parameters[1][i];
1537                                                         log(DEBUG,"umode %c is an allowed umode",umode);
1538                                                         if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1539                                                         {
1540                                                                 int v1 = strlen(dmodes);
1541                                                                 int v2 = strlen(outpars);
1542                                                                 dmodes[v1+1]='\0';
1543                                                                 dmodes[v1] = parameters[1][i];
1544                                                                 outpars[v2+1]='\0';
1545                                                                 outpars[v2] = parameters[1][i];
1546                                                         }
1547                                                 }
1548                                         }
1549                                         else
1550                                         {
1551                                                 // can only remove a mode they already have
1552                                                 log(DEBUG,"umode %c being removed",parameters[1][i]);
1553                                                 if ((allowed_umode(parameters[1][i],user->modes,false)) && (strchr(dmodes,parameters[1][i])))
1554                                                 {
1555                                                         char umode = parameters[1][i];
1556                                                         log(DEBUG,"umode %c is an allowed umode",umode);
1557                                                         if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1558                                                         {
1559                                                                 int q = 0;
1560                                                                 char temp[MAXBUF];
1561                                                                 char moo[MAXBUF];       
1562
1563                                                                 int v1 = strlen(outpars);
1564                                                                 outpars[v1+1]='\0';
1565                                                                 outpars[v1] = parameters[1][i];
1566                                                         
1567                                                                 strcpy(temp,"");
1568                                                                 for (q = 0; dmodes[q] != 0; q++)
1569                                                                 {
1570                                                                         if (dmodes[q] != parameters[1][i])
1571                                                                         {
1572                                                                                 moo[0] = dmodes[q];
1573                                                                                 moo[1] = '\0';
1574                                                                                 strlcat(temp,moo,MAXBUF);
1575                                                                         }
1576                                                                 }
1577                                                                 strlcpy(dmodes,temp,MAXBUF);
1578                                                         }
1579                                                 }
1580                                         }
1581                                 }
1582                         }
1583                 }
1584                 if (outpars[0])
1585                 {
1586                         char b[MAXBUF];
1587                         strlcpy(b,"",MAXBUF);
1588                         int z = 0;
1589                         int i = 0;
1590                         while (i < strlen (outpars))
1591                         {
1592                                 b[z++] = outpars[i++];
1593                                 b[z] = '\0';
1594                                 if (i<strlen(outpars)-1)
1595                                 {
1596                                         if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1597                                         {
1598                                                 // someones playing silly buggers and trying
1599                                                 // to put a +- or -+ into the line...
1600                                                 i++;
1601                                         }
1602                                 }
1603                                 if (i == strlen(outpars)-1)
1604                                 {
1605                                         if ((outpars[i] == '-') || (outpars[i] == '+'))
1606                                         {
1607                                                 i++;
1608                                         }
1609                                 }
1610                         }
1611
1612                         z = strlen(b)-1;
1613                         if ((b[z] == '-') || (b[z] == '+'))
1614                                 b[z] = '\0';
1615
1616                         if ((!strcmp(b,"+")) || (!strcmp(b,"-")))
1617                                 return;
1618
1619                         WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1620
1621                         // M token for a usermode must go to all servers
1622                         char buffer[MAXBUF];
1623                         snprintf(buffer,MAXBUF,"m %s %s %s",user->nick, dest->nick, b);
1624                         NetSendToAll(buffer);
1625                         
1626                         if (strlen(dmodes)>MAXMODES)
1627                         {
1628                                 dmodes[MAXMODES-1] = '\0';
1629                         }
1630                         log(DEBUG,"Stripped mode line");
1631                         log(DEBUG,"Line dest is now %s",dmodes);
1632                         strlcpy(dest->modes,dmodes,MAXMODES);
1633
1634                 }
1635
1636                 return;
1637         }
1638         
1639         Ptr = FindChan(parameters[0]);
1640         if (Ptr)
1641         {
1642                 process_modes(parameters,user,Ptr,STATUS_OP,pcnt,true,false,false);
1643         }
1644         else
1645         {
1646                 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
1647         }
1648 }
1649
1650
1651
1652 void merge_mode(char **parameters, int pcnt)
1653 {
1654         chanrec* Ptr;
1655         userrec* dest;
1656         int can_change;
1657         int direction = 1;
1658         char outpars[MAXBUF];
1659
1660         dest = Find(parameters[0]);
1661         
1662         // fix: ChroNiCk found this - we cant use this as debug if its null!
1663         if (dest)
1664         {
1665                 log(DEBUG,"merge_mode on %s",dest->nick);
1666         }
1667
1668         if ((dest) && (pcnt > 1))
1669         {
1670                 log(DEBUG,"params > 1");
1671
1672                 char dmodes[MAXBUF];
1673                 strlcpy(dmodes,dest->modes,MAXBUF);
1674
1675                 strcpy(outpars,"+");
1676                 direction = 1;
1677
1678                 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1679                         return;
1680
1681                 for (int i = 0; parameters[1][0] != 0; i++)
1682                 {
1683                         if (parameters[1][i] == '+')
1684                         {
1685                                 if (direction != 1)
1686                                 {
1687                                         int t = strlen(outpars)-1;
1688                                         if ((outpars[t] == '+') || (outpars[t] == '-'))
1689                                         {
1690                                                 outpars[t] = '+';
1691                                         }
1692                                         else
1693                                         {
1694                                                 strcat(outpars,"+");
1695                                         }
1696                                 }
1697                                 direction = 1;
1698                         }
1699                         else
1700                         if (parameters[1][i] == '-')
1701                         {
1702                                 if (direction != 0)
1703                                 {
1704                                         int t = strlen(outpars)-1;
1705                                         if ((outpars[t] == '+') || (outpars[t] == '-'))
1706                                         {
1707                                                 outpars[t] = '-';
1708                                         }
1709                                         else
1710                                         {
1711                                                 strcat(outpars,"-");
1712                                         }
1713                                 }
1714                                 direction = 0;
1715                         }
1716                         else
1717                         {
1718                                 log(DEBUG,"begin mode processing entry");
1719                                 can_change = 1;
1720                                 if (can_change)
1721                                 {
1722                                         if (direction == 1)
1723                                         {
1724                                                 log(DEBUG,"umode %c being added",parameters[1][i]);
1725                                                 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],"o",true)))
1726                                                 {
1727                                                         char umode = parameters[1][i];
1728                                                         log(DEBUG,"umode %c is an allowed umode",umode);
1729                                                         if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1730                                                         {
1731                                                                 int v1 = strlen(dmodes);
1732                                                                 int v2 = strlen(outpars);
1733                                                                 dmodes[v1+1]='\0';
1734                                                                 dmodes[v1] = parameters[1][i];
1735                                                                 outpars[v2+1]='\0';
1736                                                                 outpars[v2] = parameters[1][i];
1737                                                         }
1738                                                 }
1739                                         }
1740                                         else
1741                                         {
1742                                                 // can only remove a mode they already have
1743                                                 log(DEBUG,"umode %c being removed",parameters[1][i]);
1744                                                 if ((allowed_umode(parameters[1][i],"o",false)) && (strchr(dmodes,parameters[1][i])))
1745                                                 {
1746                                                         char umode = parameters[1][i];
1747                                                         log(DEBUG,"umode %c is an allowed umode",umode);
1748                                                         if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1749                                                         {
1750                                                                 int q = 0;
1751                                                                 char temp[MAXBUF];
1752                                                                 char moo[MAXBUF];       
1753
1754                                                                 int v1 = strlen(outpars);
1755                                                                 outpars[v1+1]='\0';
1756                                                                 outpars[v1] = parameters[1][i];
1757                                                         
1758                                                                 strcpy(temp,"");
1759                                                                 for (q = 0; dmodes[q] != 0; q++)
1760                                                                 {
1761                                                                         if (dmodes[q] != parameters[1][i])
1762                                                                         {
1763                                                                                 moo[0] = dmodes[q];
1764                                                                                 moo[1] = '\0';
1765                                                                                 strlcat(temp,moo,MAXBUF);
1766                                                                         }
1767                                                                 }
1768                                                                 strlcpy(dmodes,temp,MAXBUF);
1769                                                         }
1770                                                 }
1771                                         }
1772                                 }
1773                         }
1774                 }
1775                 if (outpars[0])
1776                 {
1777                         char b[MAXBUF];
1778                         strcpy(b,"");
1779                         int z = 0;
1780                         int i = 0;
1781                         while (i < strlen (outpars))
1782                         {
1783                                 b[z++] = outpars[i++];
1784                                 b[z] = '\0';
1785                                 if (i<strlen(outpars)-1)
1786                                 {
1787                                         if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1788                                         {
1789                                                 // someones playing silly buggers and trying
1790                                                 // to put a +- or -+ into the line...
1791                                                 i++;
1792                                         }
1793                                 }
1794                                 if (i == strlen(outpars)-1)
1795                                 {
1796                                         if ((outpars[i] == '-') || (outpars[i] == '+'))
1797                                         {
1798                                                 i++;
1799                                         }
1800                                 }
1801                         }
1802
1803                         z = strlen(b)-1;
1804                         if ((b[z] == '-') || (b[z] == '+'))
1805                                 b[z] = '\0';
1806
1807                         if ((!strcmp(b,"+")) || (!strcmp(b,"-")))
1808                                 return;
1809
1810                         if (strlen(dmodes)>MAXMODES)
1811                         {
1812                                 dmodes[MAXMODES-1] = '\0';
1813                         }
1814                         log(DEBUG,"Stripped mode line");
1815                         log(DEBUG,"Line dest is now %s",dmodes);
1816                         strlcpy(dest->modes,dmodes,MAXMODES);
1817
1818                 }
1819
1820                 return;
1821         }
1822         
1823         Ptr = FindChan(parameters[0]);
1824         if (Ptr)
1825         {
1826                 userrec s2;
1827                 strlcpy(s2.nick,ServerName,NICKMAX);
1828                 strcpy(s2.modes,"o");
1829                 s2.fd = -1;
1830                 process_modes(parameters,&s2,Ptr,STATUS_OP,pcnt,true,true,false);
1831         }
1832 }
1833
1834
1835 void merge_mode2(char **parameters, int pcnt, userrec* user)
1836 {
1837         chanrec* Ptr;
1838         userrec* dest;
1839         int can_change;
1840         int direction = 1;
1841         char outpars[MAXBUF];
1842
1843         dest = Find(parameters[0]);
1844         
1845         // fix: ChroNiCk found this - we cant use this as debug if its null!
1846         if (dest)
1847         {
1848                 log(DEBUG,"merge_mode on %s",dest->nick);
1849         }
1850
1851         if ((dest) && (pcnt > 1))
1852         {
1853                 log(DEBUG,"params > 1");
1854
1855                 char dmodes[MAXBUF];
1856                 strlcpy(dmodes,dest->modes,MAXBUF);
1857
1858                 strcpy(outpars,"+");
1859                 direction = 1;
1860
1861                 if ((parameters[1][0] == ':') && (strlen(parameters[1])>1))
1862                 {
1863                         // some stupid 3rd party things (such as services packages) put a colon on the mode list...
1864                         log(DEBUG,"Some muppet put a colon on the modelist! changed to '%s'",++parameters[1]);
1865                 }
1866                 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1867                 return;
1868
1869                 for (int i = 0; parameters[1][i] != 0; i++)
1870                 {
1871                         if (parameters[1][i] == '+')
1872                         {
1873                                 if (direction != 1)
1874                                 {
1875                                         int t = strlen(outpars)-1;
1876                                         if ((outpars[t] == '+') || (outpars[t] == '-'))
1877                                         {
1878                                                 outpars[t] = '+';
1879                                         }
1880                                         else
1881                                         {
1882                                                 strcat(outpars,"+");
1883                                         }
1884                                 }
1885                                 direction = 1;
1886                         }
1887                         else
1888                         if (parameters[1][i] == '-')
1889                         {
1890                                 if (direction != 0)
1891                                 {
1892                                         int t = strlen(outpars)-1;
1893                                         if ((outpars[t] == '+') || (outpars[t] == '-'))
1894                                         {
1895                                                 outpars[t] = '-';
1896                                         }
1897                                         else
1898                                         {
1899                                                 strcat(outpars,"-");
1900                                         }
1901                                 }
1902                                 direction = 0;
1903                         }
1904                         else
1905                         {
1906                                 log(DEBUG,"begin mode processing entry");
1907                                 can_change = 1;
1908                                 if (can_change)
1909                                 {
1910                                         if (direction == 1)
1911                                         {
1912                                                 log(DEBUG,"umode %c being added",parameters[1][i]);
1913                                                 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true)))
1914                                                 {
1915                                                         char umode = parameters[1][i];
1916                                                         log(DEBUG,"umode %c is an allowed umode",umode);
1917                                                         if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1918                                                         {
1919                                                                 int v1 = strlen(dmodes);
1920                                                                 int v2 = strlen(outpars);
1921                                                                 dmodes[v1+1]='\0';
1922                                                                 dmodes[v1] = parameters[1][i];
1923                                                                 outpars[v2+1]='\0';
1924                                                                 outpars[v2] = parameters[1][i];
1925                                                                 log(DEBUG,"OUTPARS='%s', DMODES='%s'",outpars,dmodes);
1926                                                         }
1927                                                 }
1928                                         }
1929                                         else
1930                                         {
1931                                                 // can only remove a mode they already have
1932                                                 log(DEBUG,"umode %c being removed",parameters[1][i]);
1933                                                 if ((allowed_umode(parameters[1][i],user->modes,false)) && (strchr(dmodes,parameters[1][i])))
1934                                                 {
1935                                                         char umode = parameters[1][i];
1936                                                         log(DEBUG,"umode %c is an allowed umode",umode);
1937                                                         if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1938                                                         {
1939                                                                 int q = 0;
1940                                                                 char temp[MAXBUF];
1941                                                                 char moo[MAXBUF];       
1942
1943                                                                 int v1 = strlen(outpars);
1944                                                                 outpars[v1+1]='\0';
1945                                                                 outpars[v1] = parameters[1][i];
1946                                                         
1947                                                                 strcpy(temp,"");
1948                                                                 for (q = 0; dmodes[q] != 0; q++)
1949                                                                 {
1950                                                                         if (dmodes[q] != parameters[1][i])
1951                                                                         {
1952                                                                                 moo[0] = dmodes[q];
1953                                                                                 moo[1] = '\0';
1954                                                                                 strlcat(temp,moo,MAXBUF);
1955                                                                         }
1956                                                                 }
1957                                                                 strlcpy(dmodes,temp,MAXBUF);
1958                                                         }
1959                                                 }
1960                                         }
1961                                 }
1962                         }
1963                 }
1964                 log(DEBUG,"DONE! OUTPARS='%s', DMODES='%s'",outpars,dmodes);
1965                 if (outpars[0])
1966                 {
1967                         char b[MAXBUF];
1968                         strcpy(b,"");
1969                         int z = 0;
1970                         int i = 0;
1971                         while (i < strlen (outpars))
1972                         {
1973                                 b[z++] = outpars[i++];
1974                                 b[z] = '\0';
1975                                 if (i<strlen(outpars)-1)
1976                                 {
1977                                         if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1978                                         {
1979                                                 // someones playing silly buggers and trying
1980                                                 // to put a +- or -+ into the line...
1981                                                 i++;
1982                                         }
1983                                 }
1984                                 if (i == strlen(outpars)-1)
1985                                 {
1986                                         if ((outpars[i] == '-') || (outpars[i] == '+'))
1987                                         {
1988                                                 i++;
1989                                         }
1990                                 }
1991                         }
1992
1993                         z = strlen(b)-1;
1994                         if ((b[z] == '-') || (b[z] == '+'))
1995                                 b[z] = '\0';
1996
1997                         if ((!strcmp(b,"+")) || (!strcmp(b,"-")))
1998                                 return;
1999
2000                         // was sending 'b'
2001                         WriteTo(user,dest,"MODE %s :%s",dest->nick,b);
2002                         log(DEBUG,"Sent: :%s MODE %s",user->nick,b);
2003
2004                         if (strlen(dmodes)>MAXMODES)
2005                         {
2006                                 dmodes[MAXMODES-1] = '\0';
2007                         }
2008                         log(DEBUG,"Stripped mode line");
2009                         log(DEBUG,"Line dest is now %s",dmodes);
2010                         strlcpy(dest->modes,dmodes,MAXMODES);
2011
2012                 }
2013
2014                 return;
2015         }
2016         
2017         Ptr = FindChan(parameters[0]);
2018         if (Ptr)
2019         {
2020                 log(DEBUG,"merge_mode2: found channel %s",Ptr->name);
2021                 if (Ptr)
2022                 {
2023                         if ((cstatus(user,Ptr) < STATUS_HOP) && (!is_uline(user->server)))
2024                         {
2025                                 return;
2026                         }
2027                         process_modes(parameters,user,Ptr,cstatus(user,Ptr),pcnt,false,false,true);
2028                 }
2029         }
2030 }
2031
2032