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