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