]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/mode.cpp
67f3785d8bc6f4adbee5bcb0ac9bcccb4cd52585
[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         int MOD_RESULT = 0;
474         FOREACH_RESULT(OnAddBan(user,chan,dest));
475         if (MOD_RESULT)
476                 return NULL;
477
478         TidyBan(dest);
479         for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
480         {
481                 if (!strcasecmp(i->data,dest))
482                 {
483                         // dont allow a user to set the same ban twice
484                         return NULL;
485                 }
486         }
487
488         b.set_time = TIME;
489         strncpy(b.data,dest,MAXBUF);
490         strncpy(b.set_by,user->nick,NICKMAX);
491         chan->bans.push_back(b);
492         return dest;
493 }
494
495 char* take_ban(userrec *user,char *dest,chanrec *chan,int status)
496 {
497         if ((!user) || (!dest) || (!chan)) {
498                 log(DEFAULT,"*** BUG *** take_ban was given an invalid parameter");
499                 return 0;
500         }
501
502         log(DEBUG,"del_ban: %s %s",chan->name,user->nick);
503         for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
504         {
505                 if (!strcasecmp(i->data,dest))
506                 {
507                         int MOD_RESULT = 0;
508                         FOREACH_RESULT(OnDelBan(user,chan,dest));
509                         if (MOD_RESULT)
510                                 return NULL;
511                         chan->bans.erase(i);
512                         return dest;
513                 }
514         }
515         return NULL;
516 }
517
518 // tidies up redundant modes, e.g. +nt-nt+i becomes +-+i,
519 // a section further down the chain tidies up the +-+- crap.
520 std::string compress_modes(std::string modes,bool channelmodes)
521 {
522         int counts[127];
523         bool active[127];
524         int delta = 1;
525         memset(counts,0,sizeof(counts));
526         memset(active,0,sizeof(active));
527         log(DEBUG,"compress_modes: %s",modes.c_str());
528         for (int i = 0; i < modes.length(); i++)
529         {
530                 if ((modes[i] == '+') || (modes[i] == '-'))
531                         continue;
532                 if (channelmodes)
533                 {
534                         if ((strchr("itnmsp",modes[i])) || ((ModeDefined(modes[i],MT_CHANNEL)) && (ModeDefinedOn(modes[i],MT_CHANNEL)==0) && (ModeDefinedOff(modes[i],MT_CHANNEL)==0)))
535                         {
536                                 log(DEBUG,"Tidy mode %c",modes[i]);
537                                 counts[(unsigned int)modes[i]]++;
538                                 active[(unsigned int)modes[i]] = true;
539                         }
540                 }
541                 else
542                 {
543                         log(DEBUG,"Tidy mode %c",modes[i]);
544                         counts[(unsigned int)modes[i]]++;
545                         active[(unsigned int)modes[i]] = true;
546                 }
547         }
548         for (int j = 65; j < 127; j++)
549         {
550                 if ((counts[j] > 1) && (active[j] == true))
551                 {
552                         static char v[2];
553                         v[0] = (unsigned char)j;
554                         v[1] = '\0';
555                         std::string mode_str = v;
556                         std::string::size_type pos = modes.find(mode_str);
557                         if (pos != std::string::npos)
558                         {
559                                 log(DEBUG,"all occurances of mode %c to be deleted...",(unsigned char)j);
560                                 while (modes.find(mode_str) != std::string::npos)
561                                         modes.erase(modes.find(mode_str),1);
562                                 log(DEBUG,"New mode line: %s",modes.c_str());
563                         }
564                 }
565         }
566         return modes;
567 }
568
569 void process_modes(char **parameters,userrec* user,chanrec *chan,int status, int pcnt, bool servermode, bool silent, bool local)
570 {
571         if (!parameters) {
572                 log(DEFAULT,"*** BUG *** process_modes was given an invalid parameter");
573                 return;
574         }
575
576         char modelist[MAXBUF];
577         char outlist[MAXBUF];
578         char outstr[MAXBUF];
579         char outpars[32][MAXBUF];
580         int param = 2;
581         int pc = 0;
582         int ptr = 0;
583         int mdir = 1;
584         char* r = NULL;
585         bool k_set = false, l_set = false, previously_set_l = false, previously_unset_l = false, previously_set_k = false, previously_unset_k = false;
586
587         if (pcnt < 2)
588         {
589                 return;
590         }
591
592         int MOD_RESULT = 0;
593         FOREACH_RESULT(OnAccessCheck(user,NULL,chan,AC_GENERAL_MODE));
594         
595         if (MOD_RESULT == ACR_DENY)
596                 return;
597
598         log(DEBUG,"process_modes: start: parameters=%d",pcnt);
599
600         strlcpy(modelist,parameters[1],MAXBUF); /* mode list, e.g. +oo-o *
601                                                  * parameters[2] onwards are parameters for
602                                                  * modes that require them :) */
603         strlcpy(outlist,"+",MAXBUF);
604         mdir = 1;
605
606         log(DEBUG,"process_modes: modelist: %s",modelist);
607
608         std::string tidied = compress_modes(modelist,true);
609         strlcpy(modelist,tidied.c_str(),MAXBUF);
610
611         int len = strlen(modelist);
612         while (modelist[len-1] == ' ')
613                 modelist[--len] = '\0';
614         for (ptr = 0; ptr < len; ptr++)
615         {
616                 r = NULL;
617
618                 {
619                         log(DEBUG,"process_modes: modechar: %c",modelist[ptr]);
620
621                         char modechar = modelist[ptr];
622                         switch (modelist[ptr])
623                         {
624                                 case '-':
625                                         if (mdir != 0)
626                                         {
627                                                 int t = strlen(outlist)-1;
628                                                 if ((outlist[t] == '+') || (outlist[t] == '-'))
629                                                 {
630                                                         outlist[t] = '-';
631                                                 }
632                                                 else
633                                                 {
634                                                         strcat(outlist,"-");
635                                                 }
636                                         }
637                                         mdir = 0;
638                                         
639                                 break;                  
640
641                                 case '+':
642                                         if (mdir != 1)
643                                         {
644                                                 int t = strlen(outlist)-1;
645                                                 if ((outlist[t] == '+') || (outlist[t] == '-'))
646                                                 {
647                                                         outlist[t] = '+';
648                                                 }
649                                                 else
650                                                 {
651                                                         strcat(outlist,"+");
652                                                 }
653                                         }
654                                         mdir = 1;
655                                 break;
656
657                                 case 'o':
658                                         log(DEBUG,"Ops");
659                                         if ((param >= pcnt)) break;
660                                         log(DEBUG,"Enough parameters left");
661                                         if (mdir == 1)
662                                         {
663                                                 MOD_RESULT = 0;
664                                                 FOREACH_RESULT(OnRawMode(user, chan, 'o', parameters[param], true, 1));
665                                                 if (!MOD_RESULT)
666                                                 {
667                                                         log(DEBUG,"calling give_ops");
668                                                         r = give_ops(user,parameters[param++],chan,status);
669                                                 }
670                                                 else param++;
671                                         }
672                                         else
673                                         {
674                                                 MOD_RESULT = 0;
675                                                 FOREACH_RESULT(OnRawMode(user, chan, 'o', parameters[param], false, 1));
676                                                 if (!MOD_RESULT)
677                                                 {
678                                                         log(DEBUG,"calling take_ops");
679                                                         r = take_ops(user,parameters[param++],chan,status);
680                                                 }
681                                                 else param++;
682                                         }
683                                         if (r)
684                                         {
685                                                 strlcat(outlist,"o",MAXBUF);
686                                                 strlcpy(outpars[pc++],r,MAXBUF);
687                                         }
688                                 break;
689                         
690                                 case 'h':
691                                         if (((param >= pcnt)) || (!AllowHalfop)) break;
692                                         if (mdir == 1)
693                                         {
694                                                 MOD_RESULT = 0;
695                                                 FOREACH_RESULT(OnRawMode(user, chan, 'h', parameters[param], true, 1));
696                                                 if (!MOD_RESULT)
697                                                 {
698                                                         r = give_hops(user,parameters[param++],chan,status);
699                                                 }
700                                                 else param++;
701                                         }
702                                         else
703                                         {
704                                                 MOD_RESULT = 0;
705                                                 FOREACH_RESULT(OnRawMode(user, chan, 'h', parameters[param], false, 1));
706                                                 if (!MOD_RESULT)
707                                                 {
708                                                         r = take_hops(user,parameters[param++],chan,status);
709                                                 }
710                                                 else param++;
711                                         }
712                                         if (r)
713                                         {
714                                                 strlcat(outlist,"h",MAXBUF);
715                                                 strlcpy(outpars[pc++],r,MAXBUF);
716                                         }
717                                 break;
718                         
719                                 
720                                 case 'v':
721                                         if ((param >= pcnt)) break;
722                                         if (mdir == 1)
723                                         {
724                                                 MOD_RESULT = 0;
725                                                 FOREACH_RESULT(OnRawMode(user, chan, 'v', parameters[param], true, 1));
726                                                 if (!MOD_RESULT)
727                                                 {
728                                                         r = give_voice(user,parameters[param++],chan,status);
729                                                 }
730                                                 else param++;
731                                         }
732                                         else
733                                         {
734                                                 MOD_RESULT = 0;
735                                                 FOREACH_RESULT(OnRawMode(user, chan, 'v', parameters[param], false, 1));
736                                                 if (!MOD_RESULT)
737                                                 {
738                                                         r = take_voice(user,parameters[param++],chan,status);
739                                                 }
740                                                 else param++;
741                                         }
742                                         if (r)
743                                         {
744                                                 strlcat(outlist,"v",MAXBUF);
745                                                 strlcpy(outpars[pc++],r,MAXBUF);
746                                         }
747                                 break;
748                                 
749                                 case 'b':
750                                         if ((param >= pcnt)) break;
751                                         if (mdir == 1)
752                                         {
753                                                 MOD_RESULT = 0;
754                                                 FOREACH_RESULT(OnRawMode(user, chan, 'b', parameters[param], true, 1));
755                                                 if (!MOD_RESULT)
756                                                 {
757                                                         r = add_ban(user,parameters[param++],chan,status);
758                                                 }
759                                                 else param++;
760                                         }
761                                         else
762                                         {
763                                                 MOD_RESULT = 0;
764                                                 FOREACH_RESULT(OnRawMode(user, chan, 'b', parameters[param], false, 1));
765                                                 if (!MOD_RESULT)
766                                                 {
767                                                         r = take_ban(user,parameters[param++],chan,status);
768                                                 }
769                                                 else param++;
770                                         }
771                                         if (r)
772                                         {
773                                                 strlcat(outlist,"b",MAXBUF);
774                                                 strlcpy(outpars[pc++],parameters[param-1],MAXBUF);
775                                         }
776                                 break;
777
778
779                                 case 'k':
780                                         if ((param >= pcnt))
781                                                 break;
782
783                                         if (mdir == 1)
784                                         {
785                                                 if (k_set)
786                                                         break;
787
788                                                 if (previously_unset_k)
789                                                         break;
790                                                 previously_set_k = true;
791                                                 
792                                                 if (!strcmp(chan->key,""))
793                                                 {
794                                                         MOD_RESULT = 0;
795                                                         FOREACH_RESULT(OnRawMode(user, chan, 'k', parameters[param], true, 1));
796                                                         if (!MOD_RESULT)
797                                                         {
798                                                                 strcat(outlist,"k");
799                                                                 char key[MAXBUF];
800                                                                 strlcpy(key,parameters[param++],32);
801                                                                 strlcpy(outpars[pc++],key,MAXBUF);
802                                                                 strlcpy(chan->key,key,MAXBUF);
803                                                                 k_set = true;
804                                                         }
805                                                         else param++;
806                                                 }
807                                         }
808                                         else
809                                         {
810                                                 /* checks on -k are case sensitive and only accurate to the
811                                                    first 32 characters */
812                                                 if (previously_set_k)
813                                                         break;
814                                                 previously_unset_k = true;
815
816                                                 char key[MAXBUF];
817                                                 MOD_RESULT = 0;
818                                                 FOREACH_RESULT(OnRawMode(user, chan, 'k', parameters[param], false, 1));
819                                                 if (!MOD_RESULT)
820                                                 {
821                                                         strlcpy(key,parameters[param++],32);
822                                                         /* only allow -k if correct key given */
823                                                         if (!strcmp(chan->key,key))
824                                                         {
825                                                                 strlcat(outlist,"k",MAXBUF);
826                                                                 strlcpy(chan->key,"",MAXBUF);
827                                                                 strlcpy(outpars[pc++],key,MAXBUF);
828                                                         }
829                                                 }
830                                                 else param++;
831                                         }
832                                 break;
833                                 
834                                 case 'l':
835                                         if (mdir == 0)
836                                         {
837                                                 if (previously_set_l)
838                                                         break;
839                                                 previously_unset_l = true;
840                                                 MOD_RESULT = 0;
841                                                 FOREACH_RESULT(OnRawMode(user, chan, 'l', "", false, 0));
842                                                 if (!MOD_RESULT)
843                                                 {
844                                                         if (chan->limit)
845                                                         {
846                                                                 strcat(outlist,"l");
847                                                                 chan->limit = 0;
848                                                         }
849                                                 }
850                                         }
851                                         
852                                         if ((param >= pcnt)) break;
853                                         if (mdir == 1)
854                                         {
855                                                 if (l_set)
856                                                         break;
857                                                 if (previously_unset_l)
858                                                         break;
859                                                 previously_set_l = true;
860                                                 bool invalid = false;
861                                                 for (int i = 0; i < strlen(parameters[param]); i++)
862                                                 {
863                                                         if ((parameters[param][i] < '0') || (parameters[param][i] > '9'))
864                                                         {
865                                                                 invalid = true;
866                                                         }
867                                                 }
868                                                 if (atoi(parameters[param]) < 1)
869                                                 {
870                                                         invalid = true;
871                                                 }
872
873                                                 if (invalid)
874                                                         break;
875
876                                                 MOD_RESULT = 0;
877                                                 FOREACH_RESULT(OnRawMode(user, chan, 'l', parameters[param], true, 1));
878                                                 if (!MOD_RESULT)
879                                                 {
880         
881                                                         chan->limit = atoi(parameters[param]);
882                                                         
883                                                         // reported by mech: large values cause underflow
884                                                         if (chan->limit < 0)
885                                                                 chan->limit = 0x7FFF;
886                                                 }
887                                                         
888                                                 if (chan->limit)
889                                                 {
890                                                         strlcat(outlist,"l",MAXBUF);
891                                                         strlcpy(outpars[pc++],parameters[param++],MAXBUF);
892                                                         l_set = true;
893                                                 }
894                                         }
895                                 break;
896                                 
897                                 case 'i':
898                                         MOD_RESULT = 0;
899                                         FOREACH_RESULT(OnRawMode(user, chan, 'i', "", mdir, 0));
900                                         if (!MOD_RESULT)
901                                         {
902                                                 if (mdir)
903                                                 {
904                                                         if (!(chan->binarymodes & CM_INVITEONLY)) strlcat(outlist,"i",MAXBUF);
905                                                         chan->binarymodes |= CM_INVITEONLY;
906                                                 }
907                                                 else
908                                                 {
909                                                         if (chan->binarymodes & CM_INVITEONLY) strlcat(outlist,"i",MAXBUF);
910                                                         chan->binarymodes &= ~CM_INVITEONLY;
911                                                 }
912                                         }
913                                 break;
914                                 
915                                 case 't':
916                                         MOD_RESULT = 0;
917                                         FOREACH_RESULT(OnRawMode(user, chan, 't', "", mdir, 0));
918                                         if (!MOD_RESULT)
919                                         {
920                                                 if (mdir)
921                                                 {
922                                                         if (!(chan->binarymodes & CM_TOPICLOCK)) strlcat(outlist,"t",MAXBUF);
923                                                         chan->binarymodes |= CM_TOPICLOCK;
924                                                 }
925                                                 else
926                                                 {
927                                                         if (chan->binarymodes & CM_NOEXTERNAL) strlcat(outlist,"t",MAXBUF);
928                                                         chan->binarymodes &= ~CM_TOPICLOCK;
929                                                 }
930                                         }
931                                 break;
932                                 
933                                 case 'n':
934                                         MOD_RESULT = 0;
935                                         FOREACH_RESULT(OnRawMode(user, chan, 'n', "", mdir, 0));
936                                         if (!MOD_RESULT)
937                                         {
938                                                 if (mdir)
939                                                 {
940                                                         if (!(chan->binarymodes & CM_NOEXTERNAL)) strlcat(outlist,"n",MAXBUF);
941                                                         chan->binarymodes |= CM_NOEXTERNAL;
942                                                 }
943                                                 else
944                                                 {
945                                                         if (chan->binarymodes & CM_NOEXTERNAL) strlcat(outlist,"n",MAXBUF);
946                                                         chan->binarymodes &= ~CM_NOEXTERNAL;
947                                                 }
948                                         }
949                                 break;
950                                 
951                                 case 'm':
952                                         MOD_RESULT = 0;
953                                         FOREACH_RESULT(OnRawMode(user, chan, 'm', "", mdir, 0));
954                                         if (!MOD_RESULT)
955                                         {
956                                                 if (mdir)
957                                                 {
958                                                         if (!(chan->binarymodes & CM_MODERATED)) strlcat(outlist,"m",MAXBUF);
959                                                         chan->binarymodes |= CM_MODERATED;
960                                                 }
961                                                 else
962                                                 {
963                                                         if (chan->binarymodes & CM_MODERATED) strlcat(outlist,"m",MAXBUF);
964                                                         chan->binarymodes &= ~CM_MODERATED;
965                                                 }
966                                         }
967                                 break;
968                                 
969                                 case 's':
970                                         MOD_RESULT = 0;
971                                         FOREACH_RESULT(OnRawMode(user, chan, 's', "", mdir, 0));
972                                         if (!MOD_RESULT)
973                                         {
974                                                 if (mdir)
975                                                 {
976                                                         if (!(chan->binarymodes & CM_SECRET)) strlcat(outlist,"s",MAXBUF);
977                                                         chan->binarymodes |= CM_SECRET;
978                                                         if (chan->binarymodes & CM_PRIVATE)
979                                                         {
980                                                                 chan->binarymodes &= ~CM_PRIVATE;
981                                                                 if (mdir)
982                                                                 {
983                                                                         strlcat(outlist,"-p+",MAXBUF);
984                                                                 }
985                                                         }
986                                                 }
987                                                 else
988                                                 {
989                                                         if (chan->binarymodes & CM_SECRET) strlcat(outlist,"s",MAXBUF);
990                                                         chan->binarymodes &= ~CM_SECRET;
991                                                 }
992                                         }
993                                 break;
994                                 
995                                 case 'p':
996                                         MOD_RESULT = 0;
997                                         FOREACH_RESULT(OnRawMode(user, chan, 'p', "", mdir, 0));
998                                         if (!MOD_RESULT)
999                                         {
1000                                                 if (mdir)
1001                                                 {
1002                                                         if (!(chan->binarymodes & CM_PRIVATE)) strlcat(outlist,"p",MAXBUF);
1003                                                         chan->binarymodes |= CM_PRIVATE;
1004                                                         if (chan->binarymodes & CM_SECRET)
1005                                                         {
1006                                                                 chan->binarymodes &= ~CM_SECRET;
1007                                                                 if (mdir)
1008                                                                 {
1009                                                                         strlcat(outlist,"-s+",MAXBUF);
1010                                                                 }
1011                                                         }
1012                                                 }
1013                                                 else
1014                                                 {
1015                                                         if (chan->binarymodes & CM_PRIVATE) strlcat(outlist,"p",MAXBUF);
1016                                                         chan->binarymodes &= ~CM_PRIVATE;
1017                                                 }
1018                                         }
1019                                 break;
1020                                 
1021                                 default:
1022                                         log(DEBUG,"Preprocessing custom mode %c: modelist: %s",modechar,chan->custom_modes);
1023                                         string_list p;
1024                                         p.clear();
1025                                         if (((!strchr(chan->custom_modes,modechar)) && (!mdir)) || ((strchr(chan->custom_modes,modechar)) && (mdir)))
1026                                         {
1027                                                 if (!ModeIsListMode(modechar,MT_CHANNEL))
1028                                                 {
1029                                                         log(DEBUG,"Mode %c isnt set on %s but trying to remove!",modechar,chan->name);
1030                                                         break;
1031                                                 }
1032                                         }
1033                                         if (ModeDefined(modechar,MT_CHANNEL))
1034                                         {
1035                                                 log(DEBUG,"A module has claimed this mode");
1036                                                 if (param<pcnt)
1037                                                 {
1038                                                         if ((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir))
1039                                                         {
1040                                                                 p.push_back(parameters[param]);
1041                                                         }
1042                                                         if ((ModeDefinedOff(modechar,MT_CHANNEL)>0) && (!mdir))
1043                                                         {
1044                                                                 p.push_back(parameters[param]);
1045                                                         }
1046                                                 }
1047                                                 bool handled = false;
1048                                                 if (param>=pcnt)
1049                                                 {
1050                                                         // we're supposed to have a parameter, but none was given... so dont handle the mode.
1051                                                         if (((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir)) || ((ModeDefinedOff(modechar,MT_CHANNEL)>0) && (!mdir))) 
1052                                                         {
1053                                                                 log(DEBUG,"Not enough parameters for module-mode %c",modechar);
1054                                                                 handled = true;
1055                                                                 param++;
1056                                                         }
1057                                                 }
1058
1059                                                 // BIG ASS IDIOTIC CODER WARNING!
1060                                                 // Using OnRawMode on another modules mode's behavour 
1061                                                 // will confuse the crap out of admins! just because you CAN
1062                                                 // do it, doesnt mean you SHOULD!
1063                                                 MOD_RESULT = 0;
1064                                                 std::string para = "";
1065                                                 if (p.size())
1066                                                         para = p[0];
1067                                                 FOREACH_RESULT(OnRawMode(user, chan, modechar, para, mdir, pcnt));
1068                                                 if (!MOD_RESULT)
1069                                                 {
1070                                                         for (int i = 0; i <= MODCOUNT; i++)
1071                                                         {
1072                                                                 if (!handled)
1073                                                                 {
1074                                                                         int t = modules[i]->OnExtendedMode(user,chan,modechar,MT_CHANNEL,mdir,p);
1075                                                                         if (t != 0)
1076                                                                         {
1077                                                                                 log(DEBUG,"OnExtendedMode returned nonzero for a module");
1078                                                                                 char app[] = {modechar, 0};
1079                                                                                 if (ModeIsListMode(modechar,MT_CHANNEL))
1080                                                                                 {
1081                                                                                         if (t == -1)
1082                                                                                         {
1083                                                                                                 //pc++;
1084                                                                                                 param++;
1085                                                                                         }
1086                                                                                         else
1087                                                                                         {
1088                                                                                                 if (ptr>0)
1089                                                                                                 {
1090                                                                                                         strlcat(outlist, app,MAXBUF);
1091                                                                                                 }
1092                                                                                                 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
1093                                                                                         }
1094                                                                                 }
1095                                                                                 else
1096                                                                                 {
1097                                                                                         if (ptr>0)
1098                                                                                         {
1099                                                                                                 if ((modelist[ptr-1] == '+') || (modelist[ptr-1] == '-'))
1100                                                                                                 {
1101                                                                                                         strlcat(outlist, app,MAXBUF);
1102                                                                                                 }
1103                                                                                                 else if (!strchr(outlist,modechar))
1104                                                                                                 {
1105                                                                                                         strlcat(outlist, app,MAXBUF);
1106                                                                                                 }
1107                                                                                         }
1108                                                                                         chan->SetCustomMode(modechar,mdir);
1109                                                                                         // include parameters in output if mode has them
1110                                                                                         if ((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir))
1111                                                                                         {
1112                                                                                                 chan->SetCustomModeParam(modelist[ptr],parameters[param],mdir);
1113                                                                                                 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
1114                                                                                         }
1115                                                                                 }
1116                                                                                 // break, because only one module can handle the mode.
1117                                                                                 handled = true;
1118                                                                         }
1119                                                                 }
1120                                                         }
1121                                                 }
1122                                         }
1123                                         else
1124                                         {
1125                                                 WriteServ(user->fd,"472 %s %c :is unknown mode char to me",user->nick,modechar);
1126                                         }
1127                                 break;
1128                                 
1129                         }
1130                 }
1131         }
1132
1133         /* this ensures only the *valid* modes are sent out onto the network */
1134         int xt = strlen(outlist)-1;
1135         while ((outlist[xt] == '-') || (outlist[xt] == '+'))
1136         {
1137                 outlist[xt] = '\0';
1138                 xt = strlen(outlist)-1;
1139         }
1140         if (outlist[0])
1141         {
1142                 strlcpy(outstr,outlist,MAXBUF);
1143                 for (ptr = 0; ptr < pc; ptr++)
1144                 {
1145                         strlcat(outstr," ",MAXBUF);
1146                         strlcat(outstr,outpars[ptr],MAXBUF);
1147                 }
1148                 if (local)
1149                 {
1150                         log(DEBUG,"Local mode change");
1151                         WriteChannelLocal(chan, user, "MODE %s %s",chan->name,outstr);
1152                 }
1153                 else
1154                 {
1155                         if (servermode)
1156                         {
1157                                 if (!silent)
1158                                 {
1159                                         WriteChannelWithServ(ServerName,chan,"MODE %s %s",chan->name,outstr);
1160                                         // M token for a usermode must go to all servers
1161                                         char buffer[MAXBUF];
1162                                         snprintf(buffer,MAXBUF,"M %s %s",chan->name, outstr);
1163                                         NetSendToAll(buffer);
1164                                 }
1165                                         
1166                         }
1167                         else
1168                         {
1169                                 if (!silent)
1170                                 {
1171                                         WriteChannel(chan,user,"MODE %s %s",chan->name,outstr);
1172                                         // M token for a usermode must go to all servers
1173                                         char buffer[MAXBUF];
1174                                         snprintf(buffer,MAXBUF,"m %s %s %s",user->nick,chan->name, outstr);
1175                                         NetSendToAll(buffer);
1176                                 }
1177                         }
1178                 }
1179         }
1180 }
1181
1182 // based on sourcemodes, return true or false to determine if umode is a valid mode a user may set on themselves or others.
1183
1184 bool allowed_umode(char umode, char* sourcemodes,bool adding)
1185 {
1186         log(DEBUG,"Allowed_umode: %c %s",umode,sourcemodes);
1187         // RFC1459 specified modes
1188         if ((umode == 'w') || (umode == 's') || (umode == 'i'))
1189         {
1190                 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1191                 return true;
1192         }
1193         
1194         // user may not +o themselves or others, but an oper may de-oper other opers or themselves
1195         if ((strchr(sourcemodes,'o')) && (!adding))
1196         {
1197                 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1198                 return true;
1199         }
1200         else if (umode == 'o')
1201         {
1202                 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1203                 return false;
1204         }
1205         
1206         // process any module-defined modes that need oper
1207         if ((ModeDefinedOper(umode,MT_CLIENT)) && (strchr(sourcemodes,'o')))
1208         {
1209                 log(DEBUG,"umode %c allowed by module handler (oper only mode)",umode);
1210                 return true;
1211         }
1212         else
1213         if (ModeDefined(umode,MT_CLIENT))
1214         {
1215                 // process any module-defined modes that don't need oper
1216                 log(DEBUG,"umode %c allowed by module handler (non-oper mode)",umode);
1217                 if ((ModeDefinedOper(umode,MT_CLIENT)) && (!strchr(sourcemodes,'o')))
1218                 {
1219                         // no, this mode needs oper, and this user 'aint got what it takes!
1220                         return false;
1221                 }
1222                 return true;
1223         }
1224
1225         // anything else - return false.
1226         log(DEBUG,"umode %c not known by any ruleset",umode);
1227         return false;
1228 }
1229
1230 bool process_module_umode(char umode, userrec* source, void* dest, bool adding)
1231 {
1232         userrec* s2;
1233         bool faked = false;
1234         if (!source)
1235         {
1236                 s2 = new userrec;
1237                 strlcpy(s2->nick,ServerName,NICKMAX);
1238                 strcpy(s2->modes,"o");
1239                 s2->fd = -1;
1240                 source = s2;
1241                 faked = true;
1242         }
1243         string_list p;
1244         p.clear();
1245         if (ModeDefined(umode,MT_CLIENT))
1246         {
1247                 for (int i = 0; i <= MODCOUNT; i++)
1248                 {
1249                         if (modules[i]->OnExtendedMode(source,(void*)dest,umode,MT_CLIENT,adding,p))
1250                         {
1251                                 log(DEBUG,"Module %s claims umode %c",module_names[i].c_str(),umode);
1252                                 return true;
1253                         }
1254                 }
1255                 log(DEBUG,"No module claims umode %c",umode);
1256                 if (faked)
1257                 {
1258                         delete s2;
1259                         source = NULL;
1260                 }
1261                 return false;
1262         }
1263         else
1264         {
1265                 if (faked)
1266                 {
1267                         delete s2;
1268                         source = NULL;
1269                 }
1270                 return false;
1271         }
1272 }
1273
1274 void handle_mode(char **parameters, int pcnt, userrec *user)
1275 {
1276         chanrec* Ptr;
1277         userrec* dest;
1278         int can_change;
1279         int direction = 1;
1280         char outpars[MAXBUF];
1281
1282         dest = Find(parameters[0]);
1283
1284         if (!user)
1285         {
1286                 return;
1287         }
1288
1289         if ((dest) && (pcnt == 1))
1290         {
1291                 WriteServ(user->fd,"221 %s :+%s",dest->nick,dest->modes);
1292                 return;
1293         }
1294
1295         if ((dest) && (pcnt > 1))
1296         {
1297                 std::string tidied = compress_modes(parameters[1],false);
1298                 parameters[1] = (char*)tidied.c_str();
1299
1300                 char dmodes[MAXBUF];
1301                 strlcpy(dmodes,dest->modes,MAXBUF);
1302                 log(DEBUG,"pulled up dest user modes: %s",dmodes);
1303
1304                 can_change = 0;
1305                 if (user != dest)
1306                 {
1307                         if (strchr(user->modes,'o'))
1308                         {
1309                                 can_change = 1;
1310                         }
1311                 }
1312                 else
1313                 {
1314                         can_change = 1;
1315                 }
1316                 if (!can_change)
1317                 {
1318                         WriteServ(user->fd,"482 %s :Can't change mode for other users",user->nick);
1319                         return;
1320                 }
1321                 
1322                 strcpy(outpars,"+");
1323                 direction = 1;
1324
1325                 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1326                         return;
1327
1328                 for (int i = 0; i < strlen(parameters[1]); i++)
1329                 {
1330                         if (parameters[1][i] == ' ')
1331                                 continue;
1332                         if (parameters[1][i] == '+')
1333                         {
1334                                 if (direction != 1)
1335                                 {
1336                                         int t = strlen(outpars)-1;
1337                                         if ((outpars[t] == '+') || (outpars[t] == '-'))
1338                                         {
1339                                                 outpars[t] = '+';
1340                                         }
1341                                         else
1342                                         {
1343                                                 strcat(outpars,"+");
1344                                         }
1345                                 }
1346                                 direction = 1;
1347                         }
1348                         else
1349                         if (parameters[1][i] == '-')
1350                         {
1351                                 if (direction != 0)
1352                                 {
1353                                         int t = strlen(outpars)-1;
1354                                         if ((outpars[t] == '+') || (outpars[t] == '-'))
1355                                         {
1356                                                 outpars[t] = '-';
1357                                         }
1358                                         else
1359                                         {
1360                                                 strcat(outpars,"-");
1361                                         }
1362                                 }
1363                                 direction = 0;
1364                         }
1365                         else
1366                         {
1367                                 can_change = 0;
1368                                 if (strchr(user->modes,'o'))
1369                                 {
1370                                         can_change = 1;
1371                                 }
1372                                 else
1373                                 {
1374                                         if ((parameters[1][i] == 'i') || (parameters[1][i] == 'w') || (parameters[1][i] == 's') || (allowed_umode(parameters[1][i],user->modes,direction)))
1375                                         {
1376                                                 can_change = 1;
1377                                         }
1378                                 }
1379                                 if (can_change)
1380                                 {
1381                                         if (direction == 1)
1382                                         {
1383                                                 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true)))
1384                                                 {
1385                                                         char umode = parameters[1][i];
1386                                                         if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1387                                                         {
1388                                                                 int q = strlen(dmodes);
1389                                                                 int r = strlen(outpars);
1390                                                                 dmodes[q+1]='\0';
1391                                                                 dmodes[q] = parameters[1][i];
1392                                                                 outpars[r+1]='\0';
1393                                                                 outpars[r] = parameters[1][i];
1394                                                                 if (parameters[1][i] == 'o')
1395                                                                 {
1396                                                                         FOREACH_MOD OnGlobalOper(dest);
1397                                                                 }
1398                                                         }
1399                                                 }
1400                                         }
1401                                         else
1402                                         {
1403                                                 if ((allowed_umode(parameters[1][i],user->modes,false)) && (strchr(dmodes,parameters[1][i])))
1404                                                 {
1405                                                         char umode = parameters[1][i];
1406                                                         if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1407                                                         {
1408                                                                 int q = 0;
1409                                                                 char temp[MAXBUF];      
1410                                                                 char moo[MAXBUF];       
1411
1412                                                                 int r = strlen(outpars);
1413                                                                 outpars[r+1]='\0';
1414                                                                 outpars[r] = parameters[1][i];
1415                                                         
1416                                                                 strcpy(temp,"");
1417                                                                 for (q = 0; q < strlen(dmodes); q++)
1418                                                                 {
1419                                                                         if (dmodes[q] != parameters[1][i])
1420                                                                         {
1421                                                                                 moo[0] = dmodes[q];
1422                                                                                 moo[1] = '\0';
1423                                                                                 strlcat(temp,moo,MAXBUF);
1424                                                                         }
1425                                                                 }
1426                                                                 strlcpy(dmodes,temp,MAXBUF);
1427
1428                                                                 if (umode == 'o')
1429                                                                         DeleteOper(dest);
1430                                                         }
1431                                                 }
1432                                         }
1433                                 }
1434                         }
1435                 }
1436                 if (outpars[0])
1437                 {
1438                         char b[MAXBUF];
1439                         strlcpy(b,"",MAXBUF);
1440                         int z = 0;
1441                         int i = 0;
1442                         while (i < strlen (outpars))
1443                         {
1444                                 b[z++] = outpars[i++];
1445                                 b[z] = '\0';
1446                                 if (i<strlen(outpars)-1)
1447                                 {
1448                                         if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1449                                         {
1450                                                 // someones playing silly buggers and trying
1451                                                 // to put a +- or -+ into the line...
1452                                                 i++;
1453                                         }
1454                                 }
1455                                 if (i == strlen(outpars)-1)
1456                                 {
1457                                         if ((outpars[i] == '-') || (outpars[i] == '+'))
1458                                         {
1459                                                 i++;
1460                                         }
1461                                 }
1462                         }
1463
1464                         z = strlen(b)-1;
1465                         if ((b[z] == '-') || (b[z] == '+'))
1466                                 b[z] = '\0';
1467
1468                         if ((!b[0]) || (!strcmp(b,"+")) || (!strcmp(b,"-")))
1469                                 return;
1470
1471                         if (strcmp(b,""))
1472                         {
1473                                 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1474                                 // M token for a usermode must go to all servers
1475                                 char buffer[MAXBUF];
1476                                 snprintf(buffer,MAXBUF,"m %s %s %s",user->nick, dest->nick, b);
1477                                 NetSendToAll(buffer);
1478                         }
1479
1480                         if (strlen(dmodes)>MAXMODES)
1481                         {
1482                                 dmodes[MAXMODES-1] = '\0';
1483                         }
1484                         log(DEBUG,"Stripped mode line");
1485                         log(DEBUG,"Line dest is now %s",dmodes);
1486                         strlcpy(dest->modes,dmodes,MAXMODES);
1487
1488                 }
1489
1490                 return;
1491         }
1492         
1493         Ptr = FindChan(parameters[0]);
1494         if (Ptr)
1495         {
1496                 if (pcnt == 1)
1497                 {
1498                         /* just /modes #channel */
1499                         WriteServ(user->fd,"324 %s %s +%s",user->nick, Ptr->name, chanmodes(Ptr));
1500                         WriteServ(user->fd,"329 %s %s %d", user->nick, Ptr->name, Ptr->created);
1501                         return;
1502                 }
1503                 else
1504                 if (pcnt == 2)
1505                 {
1506                         char* mode = parameters[1];
1507                         if (*mode == '+')
1508                                 mode++;
1509                         int MOD_RESULT = 0;
1510                         FOREACH_RESULT(OnRawMode(user, Ptr, *mode, "", false, 0));
1511                         if (!MOD_RESULT)
1512                         {
1513                                 if (*mode == 'b')
1514                                 {
1515
1516                                         for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
1517                                         {
1518                                                 WriteServ(user->fd,"367 %s %s %s %s %d",user->nick, Ptr->name, i->data, i->set_by, i->set_time);
1519                                         }
1520                                         WriteServ(user->fd,"368 %s %s :End of channel ban list",user->nick, Ptr->name);
1521                                         return;
1522                                 }
1523                                 if ((ModeDefined(*mode,MT_CHANNEL)) && (ModeIsListMode(*mode,MT_CHANNEL)))
1524                                 {
1525                                         // list of items for an extmode
1526                                         log(DEBUG,"Calling OnSendList for all modules, list output for mode %c",*mode);
1527                                         FOREACH_MOD OnSendList(user,Ptr,*mode);
1528                                         return;
1529                                 }
1530                         }
1531                 }
1532
1533                 if ((Ptr) && (!has_channel(user,Ptr)))
1534                 {
1535                         WriteServ(user->fd,"442 %s %s :You're not on that channel!",user->nick, Ptr->name);
1536                         return;
1537                 }
1538
1539                 if (Ptr)
1540                 {
1541                         int MOD_RESULT = 0;
1542                         FOREACH_RESULT(OnAccessCheck(user,NULL,Ptr,AC_GENERAL_MODE));
1543                         
1544                         if (MOD_RESULT == ACR_DENY)
1545                                 return;
1546                         if (MOD_RESULT == ACR_DEFAULT)
1547                         {
1548                                 if (cstatus(user,Ptr) < STATUS_HOP)
1549                                 {
1550                                         WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, Ptr->name);
1551                                         return;
1552                                 }
1553                         }
1554
1555                         process_modes(parameters,user,Ptr,cstatus(user,Ptr),pcnt,false,false,false);
1556                 }
1557         }
1558         else
1559         {
1560                 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
1561         }
1562 }
1563
1564
1565
1566
1567 void server_mode(char **parameters, int pcnt, userrec *user)
1568 {
1569         chanrec* Ptr;
1570         userrec* dest;
1571         int can_change;
1572         int direction = 1;
1573         char outpars[MAXBUF];
1574
1575         dest = Find(parameters[0]);
1576         
1577         // fix: ChroNiCk found this - we cant use this as debug if its null!
1578         if (dest)
1579         {
1580                 log(DEBUG,"server_mode on %s",dest->nick);
1581         }
1582
1583         if ((dest) && (pcnt > 1))
1584         {
1585                 std::string tidied = compress_modes(parameters[1],false);
1586                 parameters[1] = (char*)tidied.c_str();
1587
1588                 char dmodes[MAXBUF];
1589                 strlcpy(dmodes,dest->modes,MAXBUF);
1590
1591                 strcpy(outpars,"+");
1592                 direction = 1;
1593
1594                 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1595                         return;
1596
1597                 for (int i = 0; i < strlen(parameters[1]); i++)
1598                 {
1599                         if (parameters[1][i] == ' ')
1600                                 continue;
1601                         if (parameters[1][i] == '+')
1602                         {
1603                                 if (direction != 1)
1604                                 {
1605                                         int t = strlen(outpars)-1;
1606                                         if ((outpars[t] == '+') || (outpars[t] == '-'))
1607                                         {
1608                                                 outpars[t] = '+';
1609                                         }
1610                                         else
1611                                         {
1612                                                 strcat(outpars,"+");
1613                                         }
1614                                 }
1615                                 direction = 1;
1616                         }
1617                         else
1618                         if (parameters[1][i] == '-')
1619                         {
1620                                 if (direction != 0)
1621                                 {
1622                                         int t = strlen(outpars)-1;
1623                                         if ((outpars[t] == '+') || (outpars[t] == '-'))
1624                                         {
1625                                                 outpars[t] = '-';
1626                                         }
1627                                         else
1628                                         {
1629                                                 strcat(outpars,"-");
1630                                         }
1631                                 }
1632                                 direction = 0;
1633                         }
1634                         else
1635                         {
1636                                 log(DEBUG,"begin mode processing entry");
1637                                 can_change = 1;
1638                                 if (can_change)
1639                                 {
1640                                         if (direction == 1)
1641                                         {
1642                                                 log(DEBUG,"umode %c being added",parameters[1][i]);
1643                                                 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true)))
1644                                                 {
1645                                                         char umode = parameters[1][i];
1646                                                         log(DEBUG,"umode %c is an allowed umode",umode);
1647                                                         if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1648                                                         {
1649                                                                 int v1 = strlen(dmodes);
1650                                                                 int v2 = strlen(outpars);
1651                                                                 dmodes[v1+1]='\0';
1652                                                                 dmodes[v1] = parameters[1][i];
1653                                                                 outpars[v2+1]='\0';
1654                                                                 outpars[v2] = parameters[1][i];
1655                                                         }
1656                                                 }
1657                                         }
1658                                         else
1659                                         {
1660                                                 // can only remove a mode they already have
1661                                                 log(DEBUG,"umode %c being removed",parameters[1][i]);
1662                                                 if ((allowed_umode(parameters[1][i],user->modes,false)) && (strchr(dmodes,parameters[1][i])))
1663                                                 {
1664                                                         char umode = parameters[1][i];
1665                                                         log(DEBUG,"umode %c is an allowed umode",umode);
1666                                                         if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1667                                                         {
1668                                                                 int q = 0;
1669                                                                 char temp[MAXBUF];
1670                                                                 char moo[MAXBUF];       
1671
1672                                                                 int v1 = strlen(outpars);
1673                                                                 outpars[v1+1]='\0';
1674                                                                 outpars[v1] = parameters[1][i];
1675                                                         
1676                                                                 strcpy(temp,"");
1677                                                                 for (q = 0; q < strlen(dmodes); q++)
1678                                                                 {
1679                                                                         if (dmodes[q] != parameters[1][i])
1680                                                                         {
1681                                                                                 moo[0] = dmodes[q];
1682                                                                                 moo[1] = '\0';
1683                                                                                 strlcat(temp,moo,MAXBUF);
1684                                                                         }
1685                                                                 }
1686                                                                 strlcpy(dmodes,temp,MAXBUF);
1687                                                         }
1688                                                 }
1689                                         }
1690                                 }
1691                         }
1692                 }
1693                 if (outpars[0])
1694                 {
1695                         char b[MAXBUF];
1696                         strlcpy(b,"",MAXBUF);
1697                         int z = 0;
1698                         int i = 0;
1699                         while (i < strlen (outpars))
1700                         {
1701                                 b[z++] = outpars[i++];
1702                                 b[z] = '\0';
1703                                 if (i<strlen(outpars)-1)
1704                                 {
1705                                         if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1706                                         {
1707                                                 // someones playing silly buggers and trying
1708                                                 // to put a +- or -+ into the line...
1709                                                 i++;
1710                                         }
1711                                 }
1712                                 if (i == strlen(outpars)-1)
1713                                 {
1714                                         if ((outpars[i] == '-') || (outpars[i] == '+'))
1715                                         {
1716                                                 i++;
1717                                         }
1718                                 }
1719                         }
1720
1721                         z = strlen(b)-1;
1722                         if ((b[z] == '-') || (b[z] == '+'))
1723                                 b[z] = '\0';
1724
1725                         if ((!b[0]) || (!strcmp(b,"+")) || (!strcmp(b,"-")))
1726                                 return;
1727
1728                         if (strcmp(b,""))
1729                         {
1730                                 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1731                                 // M token for a usermode must go to all servers
1732                                 char buffer[MAXBUF];
1733                                 snprintf(buffer,MAXBUF,"m %s %s %s",user->nick, dest->nick, b);
1734                                 NetSendToAll(buffer);
1735                         }
1736                         
1737                         if (strlen(dmodes)>MAXMODES)
1738                         {
1739                                 dmodes[MAXMODES-1] = '\0';
1740                         }
1741                         log(DEBUG,"Stripped mode line");
1742                         log(DEBUG,"Line dest is now %s",dmodes);
1743                         strlcpy(dest->modes,dmodes,MAXMODES);
1744
1745                 }
1746
1747                 return;
1748         }
1749         
1750         Ptr = FindChan(parameters[0]);
1751         if (Ptr)
1752         {
1753                 process_modes(parameters,user,Ptr,STATUS_OP,pcnt,true,false,false);
1754         }
1755         else
1756         {
1757                 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
1758         }
1759 }
1760
1761
1762
1763 void merge_mode(char **parameters, int pcnt)
1764 {
1765         chanrec* Ptr;
1766         userrec* dest;
1767         int can_change;
1768         int direction = 1;
1769         char outpars[MAXBUF];
1770
1771         dest = Find(parameters[0]);
1772         
1773         // fix: ChroNiCk found this - we cant use this as debug if its null!
1774         if (dest)
1775         {
1776                 log(DEBUG,"merge_mode on %s",dest->nick);
1777         }
1778
1779         if ((dest) && (pcnt > 1))
1780         {
1781                 std::string tidied = compress_modes(parameters[1],false);
1782                 parameters[1] = (char*)tidied.c_str();
1783
1784                 char dmodes[MAXBUF];
1785                 strlcpy(dmodes,dest->modes,MAXBUF);
1786
1787                 strcpy(outpars,"+");
1788                 direction = 1;
1789
1790                 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1791                         return;
1792
1793                 for (int i = 0; i < strlen(parameters[1]); i++)
1794                 {
1795                         if (parameters[1][i] == ' ')
1796                                 continue;
1797                         if (parameters[1][i] == '+')
1798                         {
1799                                 if (direction != 1)
1800                                 {
1801                                         int t = strlen(outpars)-1;
1802                                         if ((outpars[t] == '+') || (outpars[t] == '-'))
1803                                         {
1804                                                 outpars[t] = '+';
1805                                         }
1806                                         else
1807                                         {
1808                                                 strcat(outpars,"+");
1809                                         }
1810                                 }
1811                                 direction = 1;
1812                         }
1813                         else
1814                         if (parameters[1][i] == '-')
1815                         {
1816                                 if (direction != 0)
1817                                 {
1818                                         int t = strlen(outpars)-1;
1819                                         if ((outpars[t] == '+') || (outpars[t] == '-'))
1820                                         {
1821                                                 outpars[t] = '-';
1822                                         }
1823                                         else
1824                                         {
1825                                                 strcat(outpars,"-");
1826                                         }
1827                                 }
1828                                 direction = 0;
1829                         }
1830                         else
1831                         {
1832                                 log(DEBUG,"begin mode processing entry");
1833                                 can_change = 1;
1834                                 if (can_change)
1835                                 {
1836                                         if (direction == 1)
1837                                         {
1838                                                 log(DEBUG,"umode %c being added",parameters[1][i]);
1839                                                 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],"o",true)))
1840                                                 {
1841                                                         char umode = parameters[1][i];
1842                                                         log(DEBUG,"umode %c is an allowed umode",umode);
1843                                                         if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1844                                                         {
1845                                                                 int v1 = strlen(dmodes);
1846                                                                 int v2 = strlen(outpars);
1847                                                                 dmodes[v1+1]='\0';
1848                                                                 dmodes[v1] = parameters[1][i];
1849                                                                 outpars[v2+1]='\0';
1850                                                                 outpars[v2] = parameters[1][i];
1851                                                         }
1852                                                 }
1853                                         }
1854                                         else
1855                                         {
1856                                                 // can only remove a mode they already have
1857                                                 log(DEBUG,"umode %c being removed",parameters[1][i]);
1858                                                 if ((allowed_umode(parameters[1][i],"o",false)) && (strchr(dmodes,parameters[1][i])))
1859                                                 {
1860                                                         char umode = parameters[1][i];
1861                                                         log(DEBUG,"umode %c is an allowed umode",umode);
1862                                                         if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1863                                                         {
1864                                                                 int q = 0;
1865                                                                 char temp[MAXBUF];
1866                                                                 char moo[MAXBUF];       
1867
1868                                                                 int v1 = strlen(outpars);
1869                                                                 outpars[v1+1]='\0';
1870                                                                 outpars[v1] = parameters[1][i];
1871                                                         
1872                                                                 strcpy(temp,"");
1873                                                                 for (q = 0; q < strlen(dmodes); q++)
1874                                                                 {
1875                                                                         if (dmodes[q] != parameters[1][i])
1876                                                                         {
1877                                                                                 moo[0] = dmodes[q];
1878                                                                                 moo[1] = '\0';
1879                                                                                 strlcat(temp,moo,MAXBUF);
1880                                                                         }
1881                                                                 }
1882                                                                 strlcpy(dmodes,temp,MAXBUF);
1883                                                         }
1884                                                 }
1885                                         }
1886                                 }
1887                         }
1888                 }
1889                 if (outpars[0])
1890                 {
1891                         char b[MAXBUF];
1892                         strcpy(b,"");
1893                         int z = 0;
1894                         int i = 0;
1895                         while (i < strlen (outpars))
1896                         {
1897                                 b[z++] = outpars[i++];
1898                                 b[z] = '\0';
1899                                 if (i<strlen(outpars)-1)
1900                                 {
1901                                         if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1902                                         {
1903                                                 // someones playing silly buggers and trying
1904                                                 // to put a +- or -+ into the line...
1905                                                 i++;
1906                                         }
1907                                 }
1908                                 if (i == strlen(outpars)-1)
1909                                 {
1910                                         if ((outpars[i] == '-') || (outpars[i] == '+'))
1911                                         {
1912                                                 i++;
1913                                         }
1914                                 }
1915                         }
1916
1917                         z = strlen(b)-1;
1918                         if ((b[z] == '-') || (b[z] == '+'))
1919                                 b[z] = '\0';
1920
1921                         if ((!strcmp(b,"+")) || (!strcmp(b,"-")))
1922                                 return;
1923
1924                         if (strlen(dmodes)>MAXMODES)
1925                         {
1926                                 dmodes[MAXMODES-1] = '\0';
1927                         }
1928                         log(DEBUG,"Stripped mode line");
1929                         log(DEBUG,"Line dest is now %s",dmodes);
1930                         strlcpy(dest->modes,dmodes,MAXMODES);
1931
1932                 }
1933
1934                 return;
1935         }
1936         
1937         Ptr = FindChan(parameters[0]);
1938         if (Ptr)
1939         {
1940                 userrec s2;
1941                 strlcpy(s2.nick,ServerName,NICKMAX);
1942                 strcpy(s2.modes,"o");
1943                 s2.fd = -1;
1944                 process_modes(parameters,&s2,Ptr,STATUS_OP,pcnt,true,true,false);
1945         }
1946 }
1947
1948
1949 void merge_mode2(char **parameters, int pcnt, userrec* user)
1950 {
1951         chanrec* Ptr;
1952         userrec* dest;
1953         int can_change;
1954         int direction = 1;
1955         char outpars[MAXBUF];
1956
1957         dest = Find(parameters[0]);
1958         
1959         // fix: ChroNiCk found this - we cant use this as debug if its null!
1960         if (dest)
1961         {
1962                 log(DEBUG,"merge_mode2 on %s",dest->nick);
1963         }
1964
1965         if ((dest) && (pcnt > 1))
1966         {
1967                 std::string tidied = compress_modes(parameters[1],false);
1968                 parameters[1] = (char*)tidied.c_str();
1969
1970                 char dmodes[MAXBUF];
1971                 strlcpy(dmodes,dest->modes,MAXBUF);
1972
1973                 strcpy(outpars,"+");
1974                 direction = 1;
1975
1976                 if ((parameters[1][0] == ':') && (strlen(parameters[1])>1))
1977                 {
1978                         // some stupid 3rd party things (such as services packages) put a colon on the mode list...
1979                         log(DEBUG,"Some muppet put a colon on the modelist! changed to '%s'",++parameters[1]);
1980                 }
1981                 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1982                 return;
1983
1984                 for (int i = 0; i < strlen(parameters[1]); i++)
1985                 {
1986                         if (parameters[1][i] == ' ')
1987                                 continue;
1988                         if (parameters[1][i] == '+')
1989                         {
1990                                 if (direction != 1)
1991                                 {
1992                                         int t = strlen(outpars)-1;
1993                                         if ((outpars[t] == '+') || (outpars[t] == '-'))
1994                                         {
1995                                                 outpars[t] = '+';
1996                                         }
1997                                         else
1998                                         {
1999                                                 strcat(outpars,"+");
2000                                         }
2001                                 }
2002                                 direction = 1;
2003                         }
2004                         else
2005                         if (parameters[1][i] == '-')
2006                         {
2007                                 if (direction != 0)
2008                                 {
2009                                         int t = strlen(outpars)-1;
2010                                         if ((outpars[t] == '+') || (outpars[t] == '-'))
2011                                         {
2012                                                 outpars[t] = '-';
2013                                         }
2014                                         else
2015                                         {
2016                                                 strcat(outpars,"-");
2017                                         }
2018                                 }
2019                                 direction = 0;
2020                         }
2021                         else
2022                         {
2023                                 log(DEBUG,"begin mode processing entry");
2024                                 can_change = 1;
2025                                 if (can_change)
2026                                 {
2027                                         if (direction == 1)
2028                                         {
2029                                                 log(DEBUG,"umode %c being added",parameters[1][i]);
2030                                                 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true)))
2031                                                 {
2032                                                         char umode = parameters[1][i];
2033                                                         log(DEBUG,"umode %c is an allowed umode",umode);
2034                                                         if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
2035                                                         {
2036                                                                 int v1 = strlen(dmodes);
2037                                                                 int v2 = strlen(outpars);
2038                                                                 dmodes[v1+1]='\0';
2039                                                                 dmodes[v1] = parameters[1][i];
2040                                                                 outpars[v2+1]='\0';
2041                                                                 outpars[v2] = parameters[1][i];
2042                                                                 log(DEBUG,"OUTPARS='%s', DMODES='%s'",outpars,dmodes);
2043                                                         }
2044                                                 }
2045                                         }
2046                                         else
2047                                         {
2048                                                 // can only remove a mode they already have
2049                                                 log(DEBUG,"umode %c being removed",parameters[1][i]);
2050                                                 if ((allowed_umode(parameters[1][i],user->modes,false)) && (strchr(dmodes,parameters[1][i])))
2051                                                 {
2052                                                         char umode = parameters[1][i];
2053                                                         log(DEBUG,"umode %c is an allowed umode",umode);
2054                                                         if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
2055                                                         {
2056                                                                 int q = 0;
2057                                                                 char temp[MAXBUF];
2058                                                                 char moo[MAXBUF];       
2059
2060                                                                 int v1 = strlen(outpars);
2061                                                                 outpars[v1+1]='\0';
2062                                                                 outpars[v1] = parameters[1][i];
2063                                                         
2064                                                                 strcpy(temp,"");
2065                                                                 for (q = 0; q < strlen(dmodes); q++)
2066                                                                 {
2067                                                                         if (dmodes[q] != parameters[1][i])
2068                                                                         {
2069                                                                                 moo[0] = dmodes[q];
2070                                                                                 moo[1] = '\0';
2071                                                                                 strlcat(temp,moo,MAXBUF);
2072                                                                         }
2073                                                                 }
2074                                                                 strlcpy(dmodes,temp,MAXBUF);
2075                                                         }
2076                                                 }
2077                                         }
2078                                 }
2079                         }
2080                 }
2081                 log(DEBUG,"DONE! OUTPARS='%s', DMODES='%s'",outpars,dmodes);
2082                 if (outpars[0])
2083                 {
2084                         char b[MAXBUF];
2085                         strcpy(b,"");
2086                         int z = 0;
2087                         int i = 0;
2088                         while (i < strlen (outpars))
2089                         {
2090                                 b[z++] = outpars[i++];
2091                                 b[z] = '\0';
2092                                 if (i<strlen(outpars)-1)
2093                                 {
2094                                         if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
2095                                         {
2096                                                 // someones playing silly buggers and trying
2097                                                 // to put a +- or -+ into the line...
2098                                                 i++;
2099                                         }
2100                                 }
2101                                 if (i == strlen(outpars)-1)
2102                                 {
2103                                         if ((outpars[i] == '-') || (outpars[i] == '+'))
2104                                         {
2105                                                 i++;
2106                                         }
2107                                 }
2108                         }
2109
2110                         z = strlen(b)-1;
2111                         if ((b[z] == '-') || (b[z] == '+'))
2112                                 b[z] = '\0';
2113
2114
2115                         if ((!b[0]) || (!strcmp(b,"+")) || (!strcmp(b,"-")))
2116                                 return;
2117
2118                         if (strcmp(b,""))
2119                         {
2120                                 WriteTo(user,dest,"MODE %s :%s",dest->nick,b);
2121                                 log(DEBUG,"Sent: :%s MODE %s",user->nick,b);
2122                         }
2123
2124                         if (strlen(dmodes)>MAXMODES)
2125                         {
2126                                 dmodes[MAXMODES-1] = '\0';
2127                         }
2128                         log(DEBUG,"Stripped mode line");
2129                         log(DEBUG,"Line dest is now %s",dmodes);
2130                         strlcpy(dest->modes,dmodes,MAXMODES);
2131                 }
2132
2133                 return;
2134         }
2135         
2136         Ptr = FindChan(parameters[0]);
2137         if (Ptr)
2138         {
2139                 log(DEBUG,"merge_mode2: found channel %s",Ptr->name);
2140                 if (Ptr)
2141                 {
2142                         if ((cstatus(user,Ptr) < STATUS_HOP) && (!is_uline(user->server)))
2143                         {
2144                                 return;
2145                         }
2146                         process_modes(parameters,user,Ptr,cstatus(user,Ptr),pcnt,false,false,true);
2147                 }
2148         }
2149 }
2150
2151