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