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