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