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