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