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