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