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