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