]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/mode.cpp
5689f30db53ab895fbf5ac5b7fe1e2d00c9e032d
[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 (!strcasecmp(d->chans[i].channel->name,chan->name))
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 (!strcasecmp(d->chans[i].channel->name,chan->name))
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 (!strcasecmp(d->chans[i].channel->name,chan->name))
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 (!strcasecmp(d->chans[i].channel->name,chan->name))
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 (!strcasecmp(d->chans[i].channel->name,chan->name))
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 (!strcasecmp(d->chans[i].channel->name,chan->name))
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 modelist[MAXBUF];
546         char outlist[MAXBUF];
547         char outstr[MAXBUF];
548         char outpars[32][MAXBUF];
549         int param = 2;
550         int pc = 0;
551         int ptr = 0;
552         int mdir = 1;
553         char* r = NULL;
554         bool k_set = false, l_set = false, previously_set_l = false, previously_unset_l = false, previously_set_k = false, previously_unset_k = false;
555
556         if (pcnt < 2)
557         {
558                 return;
559         }
560
561         int MOD_RESULT = 0;
562         
563         if (IS_LOCAL(user))
564         {
565                 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,NULL,chan,AC_GENERAL_MODE));  
566                 if (MOD_RESULT == ACR_DENY)
567                         return;
568         }
569
570         log(DEBUG,"process_modes: start: parameters=%d",pcnt);
571
572         strlcpy(modelist,parameters[1],MAXBUF); /* mode list, e.g. +oo-o *
573                                                  * parameters[2] onwards are parameters for
574                                                  * modes that require them :) */
575         *outlist = *modelist;
576         char* outl = outlist+1;
577
578         mdir = (*modelist == '+');
579
580         log(DEBUG,"process_modes: modelist: %s",modelist);
581
582         std::string tidied = this->CompressModes(modelist,true);
583         strlcpy(modelist,tidied.c_str(),MAXBUF);
584
585         int len = tidied.length();
586         while (modelist[len-1] == ' ')
587                 modelist[--len] = '\0';
588
589         bool next_cant_be_modifier = false;
590         char* modechar;
591
592         for (modechar = (modelist + 1); *modechar; ptr++, modechar++)
593         {
594                 r = NULL;
595
596                 /* If we have more than MAXMODES changes in one line,
597                  * drop all after the MAXMODES
598                  */
599                 if (pc > MAXMODES-1)
600                         break;
601
602                 log(DEBUG,"Mode %c",*modechar);
603
604                 {
605                         switch (*modechar)
606                         {
607                                 case '-':
608                                         *outl++ = '-';
609                                         mdir = 0;
610                                         next_cant_be_modifier = true;
611                                         
612                                 break;                  
613
614                                 case '+':
615                                         *outl++ = '+';
616                                         mdir = 1;
617                                         next_cant_be_modifier = true;
618                                 break;
619
620                                 case 'o':
621                                         log(DEBUG,"Ops");
622                                         if ((param >= pcnt)) break;
623                                         log(DEBUG,"Enough parameters left");
624                                         r = NULL;
625                                         if (mdir == 1)
626                                         {
627                                                 MOD_RESULT = 0;
628                                                 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'o', parameters[param], true, 1));
629                                                 if (!MOD_RESULT)
630                                                 {
631                                                         log(DEBUG,"calling GiveOps");
632                                                         r = GiveOps(user,parameters[param++],chan,status);
633                                                 }
634                                                 else param++;
635                                         }
636                                         else
637                                         {
638                                                 MOD_RESULT = 0;
639                                                 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'o', parameters[param], false, 1));
640                                                 if (!MOD_RESULT)
641                                                 {
642                                                         log(DEBUG,"calling TakeOps");
643                                                         r = TakeOps(user,parameters[param++],chan,status);
644                                                 }
645                                                 else param++;
646                                         }
647                                         if (r)
648                                         {
649                                                 *outl++ = 'o';
650                                                 strlcpy(outpars[pc++],r,MAXBUF);
651                                         }
652                                 break;
653                         
654                                 case 'h':
655                                         if (((param >= pcnt)) || (!Config->AllowHalfop)) break;
656                                         r = NULL;
657                                         if (mdir == 1)
658                                         {
659                                                 MOD_RESULT = 0;
660                                                 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'h', parameters[param], true, 1));
661                                                 if (!MOD_RESULT)
662                                                 {
663                                                         r = GiveHops(user,parameters[param++],chan,status);
664                                                 }
665                                                 else param++;
666                                         }
667                                         else
668                                         {
669                                                 MOD_RESULT = 0;
670                                                 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'h', parameters[param], false, 1));
671                                                 if (!MOD_RESULT)
672                                                 {
673                                                         r = TakeHops(user,parameters[param++],chan,status);
674                                                 }
675                                                 else param++;
676                                         }
677                                         if (r)
678                                         {
679                                                 *outl++ = 'h';
680                                                 strlcpy(outpars[pc++],r,MAXBUF);
681                                         }
682                                 break;
683                         
684                                 
685                                 case 'v':
686                                         if ((param >= pcnt)) break;
687                                         r = NULL;
688                                         if (mdir == 1)
689                                         {
690                                                 MOD_RESULT = 0;
691                                                 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'v', parameters[param], true, 1));
692                                                 if (!MOD_RESULT)
693                                                 {
694                                                         r = GiveVoice(user,parameters[param++],chan,status);
695                                                 }
696                                                 else param++;
697                                         }
698                                         else
699                                         {
700                                                 MOD_RESULT = 0;
701                                                 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'v', parameters[param], false, 1));
702                                                 if (!MOD_RESULT)
703                                                 {
704                                                         r = TakeVoice(user,parameters[param++],chan,status);
705                                                 }
706                                                 else param++;
707                                         }
708                                         if (r)
709                                         {
710                                                 *outl++ = 'v';
711                                                 strlcpy(outpars[pc++],r,MAXBUF);
712                                         }
713                                 break;
714                                 
715                                 case 'b':
716                                         if ((param >= pcnt)) break;
717                                         r = NULL;
718                                         if (mdir == 1)
719                                         {
720                                                 MOD_RESULT = 0;
721                                                 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'b', parameters[param], true, 1));
722                                                 if (!MOD_RESULT)
723                                                 {
724                                                         r = AddBan(user,parameters[param++],chan,status);
725                                                 }
726                                                 else param++;
727                                         }
728                                         else
729                                         {
730                                                 MOD_RESULT = 0;
731                                                 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'b', parameters[param], false, 1));
732                                                 if (!MOD_RESULT)
733                                                 {
734                                                         r = TakeBan(user,parameters[param++],chan,status);
735                                                 }
736                                                 else param++;
737                                         }
738                                         if (r)
739                                         {
740                                                 *outl++ = 'b';
741                                                 strlcpy(outpars[pc++],parameters[param-1],MAXBUF);
742                                         }
743                                 break;
744
745
746                                 case 'k':
747                                         if ((param >= pcnt))
748                                                 break;
749
750                                         if (mdir == 1)
751                                         {
752                                                 if (k_set)
753                                                         break;
754
755                                                 if (previously_unset_k)
756                                                         break;
757                                                 previously_set_k = true;
758                                                 
759                                                 if (!*chan->key)
760                                                 {
761                                                         MOD_RESULT = 0;
762                                                         FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'k', parameters[param], true, 1));
763                                                         if (!MOD_RESULT)
764                                                         {
765                                                                 *outl++ = 'k';
766                                                                 char key[MAXBUF];
767                                                                 strlcpy(key,parameters[param++],32);
768                                                                 strlcpy(outpars[pc++],key,MAXBUF);
769                                                                 strlcpy(chan->key,key,MAXBUF);
770                                                                 k_set = true;
771                                                         }
772                                                         else param++;
773                                                 }
774                                         }
775                                         else
776                                         {
777                                                 /* checks on -k are case sensitive and only accurate to the
778                                                    first 32 characters */
779                                                 if (previously_set_k)
780                                                         break;
781                                                 previously_unset_k = true;
782
783                                                 char key[MAXBUF];
784                                                 MOD_RESULT = 0;
785                                                 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'k', parameters[param], false, 1));
786                                                 if (!MOD_RESULT)
787                                                 {
788                                                         strlcpy(key,parameters[param++],32);
789                                                         /* only allow -k if correct key given */
790                                                         if (!strcmp(chan->key,key))
791                                                         {
792                                                                 *outl++ = 'k';
793                                                                 *chan->key = 0;
794                                                                 strlcpy(outpars[pc++],key,MAXBUF);
795                                                         }
796                                                 }
797                                                 else param++;
798                                         }
799                                 break;
800                                 
801                                 case 'l':
802                                         if (mdir == 0)
803                                         {
804                                                 if (previously_set_l)
805                                                         break;
806                                                 previously_unset_l = true;
807                                                 MOD_RESULT = 0;
808                                                 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'l', "", false, 0));
809                                                 if (!MOD_RESULT)
810                                                 {
811                                                         if (chan->limit)
812                                                         {
813                                                                 *outl++ = 'l';
814                                                                 chan->limit = 0;
815                                                         }
816                                                 }
817                                         }
818                                         
819                                         if ((param >= pcnt)) break;
820                                         if (mdir == 1)
821                                         {
822                                                 if (l_set)
823                                                         break;
824                                                 if (previously_unset_l)
825                                                         break;
826                                                 previously_set_l = true;
827                                                 bool invalid = false;
828                                                 for (char* f = parameters[param]; *f; f++)
829                                                 {
830                                                         if ((*f < '0') || (*f > '9'))
831                                                         {
832                                                                 invalid = true;
833                                                         }
834                                                 }
835                                                 /* If the limit is < 1, or the new limit is the current limit, dont allow */
836                                                 if ((atoi(parameters[param]) < 1) || ((chan->limit > 0) && (atoi(parameters[param]) == chan->limit)))
837                                                 {
838                                                         invalid = true;
839                                                 }
840
841                                                 if (invalid)
842                                                         break;
843
844                                                 MOD_RESULT = 0;
845                                                 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'l', parameters[param], true, 1));
846                                                 if (!MOD_RESULT)
847                                                 {
848         
849                                                         chan->limit = atoi(parameters[param]);
850                                                         
851                                                         // reported by mech: large values cause underflow
852                                                         if (chan->limit < 0)
853                                                                 chan->limit = 0x7FFF;
854                                                 }
855                                                         
856                                                 if (chan->limit)
857                                                 {
858                                                         *outl++ = 'l';
859                                                         strlcpy(outpars[pc++],parameters[param++],MAXBUF);
860                                                         l_set = true;
861                                                 }
862                                         }
863                                 break;
864                                 
865                                 case 'i':
866                                         MOD_RESULT = 0;
867                                         FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'i', "", mdir, 0));
868                                         if (!MOD_RESULT)
869                                         {
870                                                 if (mdir)
871                                                 {
872                                                         if (!(chan->binarymodes & CM_INVITEONLY)) *outl++ = 'i';
873                                                         chan->binarymodes |= CM_INVITEONLY;
874                                                 }
875                                                 else
876                                                 {
877                                                         if (chan->binarymodes & CM_INVITEONLY) *outl++ = 'i';
878                                                         chan->binarymodes &= ~CM_INVITEONLY;
879                                                 }
880                                         }
881                                 break;
882                                 
883                                 case 't':
884                                         MOD_RESULT = 0;
885                                         FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 't', "", mdir, 0));
886                                         if (!MOD_RESULT)
887                                         {
888                                                 if (mdir)
889                                                 {
890                                                         if (!(chan->binarymodes & CM_TOPICLOCK)) *outl++ = 't';
891                                                         chan->binarymodes |= CM_TOPICLOCK;
892                                                 }
893                                                 else
894                                                 {
895                                                         if (chan->binarymodes & CM_TOPICLOCK) *outl++ = 't';
896                                                         chan->binarymodes &= ~CM_TOPICLOCK;
897                                                 }
898                                         }
899                                 break;
900                                 
901                                 case 'n':
902                                         MOD_RESULT = 0;
903                                         FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'n', "", mdir, 0));
904                                         if (!MOD_RESULT)
905                                         {
906                                                 if (mdir)
907                                                 {
908                                                         if (!(chan->binarymodes & CM_NOEXTERNAL)) *outl++ = 'n';
909                                                         chan->binarymodes |= CM_NOEXTERNAL;
910                                                 }
911                                                 else
912                                                 {
913                                                         if (chan->binarymodes & CM_NOEXTERNAL) *outl++ = 'n';
914                                                         chan->binarymodes &= ~CM_NOEXTERNAL;
915                                                 }
916                                         }
917                                 break;
918                                 
919                                 case 'm':
920                                         MOD_RESULT = 0;
921                                         FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'm', "", mdir, 0));
922                                         if (!MOD_RESULT)
923                                         {
924                                                 if (mdir)
925                                                 {
926                                                         if (!(chan->binarymodes & CM_MODERATED)) *outl++ = 'm';
927                                                         chan->binarymodes |= CM_MODERATED;
928                                                 }
929                                                 else
930                                                 {
931                                                         if (chan->binarymodes & CM_MODERATED) *outl++ = 'm';
932                                                         chan->binarymodes &= ~CM_MODERATED;
933                                                 }
934                                         }
935                                 break;
936                                 
937                                 case 's':
938                                         MOD_RESULT = 0;
939                                         FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 's', "", mdir, 0));
940                                         if (!MOD_RESULT)
941                                         {
942                                                 if (mdir)
943                                                 {
944                                                         if (!(chan->binarymodes & CM_SECRET)) *outl++ = 's';
945                                                         chan->binarymodes |= CM_SECRET;
946                                                         if (chan->binarymodes & CM_PRIVATE)
947                                                         {
948                                                                 chan->binarymodes &= ~CM_PRIVATE;
949                                                                 if (mdir)
950                                                                 {
951                                                                         *outl++ = '-'; *outl++ = 'p'; *outl++ = '+';
952                                                                 }
953                                                         }
954                                                 }
955                                                 else
956                                                 {
957                                                         if (chan->binarymodes & CM_SECRET) *outl++ = 's';
958                                                         chan->binarymodes &= ~CM_SECRET;
959                                                 }
960                                         }
961                                 break;
962                                 
963                                 case 'p':
964                                         MOD_RESULT = 0;
965                                         FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'p', "", mdir, 0));
966                                         if (!MOD_RESULT)
967                                         {
968                                                 if (mdir)
969                                                 {
970                                                         if (!(chan->binarymodes & CM_PRIVATE)) *outl++ = 'p';
971                                                         chan->binarymodes |= CM_PRIVATE;
972                                                         if (chan->binarymodes & CM_SECRET)
973                                                         {
974                                                                 chan->binarymodes &= ~CM_SECRET;
975                                                                 if (mdir)
976                                                                 {
977                                                                         *outl++ = '-'; *outl++ = 's'; *outl++ = '+';
978                                                                 }
979                                                         }
980                                                 }
981                                                 else
982                                                 {
983                                                         if (chan->binarymodes & CM_PRIVATE) *outl++ = 'p';
984                                                         chan->binarymodes &= ~CM_PRIVATE;
985                                                 }
986                                         }
987                                 break;
988                                 
989                                 default:
990                                         log(DEBUG,"Preprocessing custom mode %c: modelist: %s",*modechar,chan->custom_modes);
991                                         string_list p;
992                                         p.clear();
993                                         if (((!strchr(chan->custom_modes,*modechar)) && (!mdir)) || ((strchr(chan->custom_modes,*modechar)) && (mdir)))
994                                         {
995                                                 if (!ModeIsListMode(*modechar,MT_CHANNEL))
996                                                 {
997                                                         log(DEBUG,"Mode %c isnt set on %s but trying to remove!",*modechar,chan->name);
998                                                         break;
999                                                 }
1000                                         }
1001                                         if (ModeDefined(*modechar,MT_CHANNEL))
1002                                         {
1003                                                 log(DEBUG,"A module has claimed this mode");
1004                                                 if (param<pcnt)
1005                                                 {
1006                                                         if ((ModeDefinedOn(*modechar,MT_CHANNEL)>0) && (mdir))
1007                                                         {
1008                                                                 p.push_back(parameters[param]);
1009                                                         }
1010                                                         if ((ModeDefinedOff(*modechar,MT_CHANNEL)>0) && (!mdir))
1011                                                         {
1012                                                                 p.push_back(parameters[param]);
1013                                                         }
1014                                                 }
1015                                                 bool handled = false;
1016                                                 if (param>=pcnt)
1017                                                 {
1018                                                         // we're supposed to have a parameter, but none was given... so dont handle the mode.
1019                                                         if (((ModeDefinedOn(*modechar,MT_CHANNEL)>0) && (mdir)) || ((ModeDefinedOff(*modechar,MT_CHANNEL)>0) && (!mdir)))       
1020                                                         {
1021                                                                 log(DEBUG,"Not enough parameters for module-mode %c",*modechar);
1022                                                                 handled = true;
1023                                                                 param++;
1024                                                         }
1025                                                 }
1026
1027                                                 // BIG ASS IDIOTIC CODER WARNING!
1028                                                 // Using OnRawMode on another modules mode's behavour 
1029                                                 // will confuse the crap out of admins! just because you CAN
1030                                                 // do it, doesnt mean you SHOULD!
1031                                                 MOD_RESULT = 0;
1032                                                 std::string para = "";
1033                                                 if (p.size())
1034                                                         para = p[0];
1035                                                 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, *modechar, para, mdir, pcnt));
1036                                                 if (!MOD_RESULT)
1037                                                 {
1038                                                         for (int i = 0; i <= MODCOUNT; i++)
1039                                                         {
1040                                                                 if (!handled)
1041                                                                 {
1042                                                                         int t = modules[i]->OnExtendedMode(user,chan,*modechar,MT_CHANNEL,mdir,p);
1043                                                                         if (t != 0)
1044                                                                         {
1045                                                                                 log(DEBUG,"OnExtendedMode returned nonzero for a module");
1046                                                                                 if (ModeIsListMode(*modechar,MT_CHANNEL))
1047                                                                                 {
1048                                                                                         if (t == -1)
1049                                                                                         {
1050                                                                                                 //pc++;
1051                                                                                                 param++;
1052                                                                                         }
1053                                                                                         else
1054                                                                                         {
1055                                                                                                 if (param < pcnt)
1056                                                                                                 {
1057                                                                                                         *outl++ = *modechar;
1058                                                                                                 }
1059                                                                                                 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
1060                                                                                         }
1061                                                                                 }
1062                                                                                 else
1063                                                                                 {
1064                                                                                         if (param < pcnt)
1065                                                                                         {
1066                                                                                                 /* Null terminate it early for the 'else' below
1067                                                                                                  * so it can use strchr (ugh)
1068                                                                                                  */
1069                                                                                                 *outl++ = *modechar;
1070                                                                                                 chan->SetCustomMode(*modechar,mdir);
1071                                                                                                 // include parameters in output if mode has them
1072                                                                                                 if ((ModeDefinedOn(*modechar,MT_CHANNEL)>0) && (mdir))
1073                                                                                                 {
1074                                                                                                         chan->SetCustomModeParam(modelist[ptr],parameters[param],mdir);
1075                                                                                                         strlcpy(outpars[pc++],parameters[param++],MAXBUF);
1076                                                                                                 }
1077                                                                                         }
1078                                                                                 }
1079                                                                                 // break, because only one module can handle the mode.
1080                                                                                 handled = true;
1081                                                                         }
1082                                                                 }
1083                                                         }
1084                                                 }
1085                                         }
1086                                         else
1087                                         {
1088                                                 WriteServ(user->fd,"472 %s %c :is unknown mode char to me",user->nick,*modechar);
1089                                         }
1090                                 break;
1091                                 
1092                         }
1093                 }
1094         }
1095
1096         /* Null terminate it now we're done */
1097         *outl = 0;
1098
1099         outl = outlist;
1100         while (*outl && (*outl < 'A'))
1101                 outl++;
1102         /* outl now points to the first mode character after +'s and -'s */
1103         outl--;
1104         /* Now points at first mode-modifier + or - symbol */
1105
1106         char* trim = outl;
1107         /* Now we tidy off any trailing -'s etc */
1108         while (*trim++);
1109         trim--;
1110         while ((*--trim == '+') || (*trim == '-'))
1111         {
1112                 log(DEBUG,"Removed trailing modifier");
1113                 *trim = 0;
1114         }
1115         
1116         /* The mode change must be at least two characters long (+ or - and at least one mode) */
1117         if (((*outl == '+') || (*outl == '-')) && *(outl+1))
1118         {
1119                 strlcpy(outstr,outl,MAXBUF);
1120                 for (ptr = 0; ptr < pc; ptr++)
1121                 {
1122                         charlcat(outstr,' ',MAXBUF);
1123                         strlcat(outstr,outpars[ptr],MAXBUF);
1124                 }
1125                 if (local)
1126                 {
1127                         log(DEBUG,"Local mode change");
1128                         WriteChannelLocal(chan, user, "MODE %s %s",chan->name,outstr);
1129                         FOREACH_MOD(I_OnMode,OnMode(user, chan, TYPE_CHANNEL, outstr));
1130                 }
1131                 else
1132                 {
1133                         if (servermode)
1134                         {
1135                                 if (!silent)
1136                                 {
1137                                         WriteChannelWithServ(Config->ServerName,chan,"MODE %s %s",chan->name,outstr);
1138                                 }
1139                                         
1140                         }
1141                         else
1142                         {
1143                                 if (!silent)
1144                                 {
1145                                         WriteChannel(chan,user,"MODE %s %s",chan->name,outstr);
1146                                         FOREACH_MOD(I_OnMode,OnMode(user, chan, TYPE_CHANNEL, outstr));
1147                                 }
1148                         }
1149                 }
1150         }
1151 }
1152
1153 // based on sourcemodes, return true or false to determine if umode is a valid mode a user may set on themselves or others.
1154
1155 bool ModeParser::AllowedUmode(char umode, char* sourcemodes,bool adding,bool serveroverride)
1156 {
1157         log(DEBUG,"Allowed_umode: %c %s",umode,sourcemodes);
1158         // Servers can +o and -o arbitrarily
1159         if ((serveroverride == true) && (umode == 'o'))
1160         {
1161                 return true;
1162         }
1163         // RFC1459 specified modes
1164         if ((umode == 'w') || (umode == 's') || (umode == 'i'))
1165         {
1166                 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1167                 return true;
1168         }
1169         
1170         // user may not +o themselves or others, but an oper may de-oper other opers or themselves
1171         if ((strchr(sourcemodes,'o')) && (!adding))
1172         {
1173                 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1174                 return true;
1175         }
1176         else if (umode == 'o')
1177         {
1178                 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1179                 return false;
1180         }
1181         
1182         // process any module-defined modes that need oper
1183         if ((ModeDefinedOper(umode,MT_CLIENT)) && (strchr(sourcemodes,'o')))
1184         {
1185                 log(DEBUG,"umode %c allowed by module handler (oper only mode)",umode);
1186                 return true;
1187         }
1188         else
1189         if (ModeDefined(umode,MT_CLIENT))
1190         {
1191                 // process any module-defined modes that don't need oper
1192                 log(DEBUG,"umode %c allowed by module handler (non-oper mode)",umode);
1193                 if ((ModeDefinedOper(umode,MT_CLIENT)) && (!strchr(sourcemodes,'o')))
1194                 {
1195                         // no, this mode needs oper, and this user 'aint got what it takes!
1196                         return false;
1197                 }
1198                 return true;
1199         }
1200
1201         // anything else - return false.
1202         log(DEBUG,"umode %c not known by any ruleset",umode);
1203         return false;
1204 }
1205
1206 bool ModeParser::ProcessModuleUmode(char umode, userrec* source, void* dest, bool adding)
1207 {
1208         userrec* s2;
1209         bool faked = false;
1210         if (!source)
1211         {
1212                 s2 = new userrec;
1213                 strlcpy(s2->nick,Config->ServerName,NICKMAX-1);
1214                 *s2->modes = 'o';
1215                 *(s2->modes+1) = 0;
1216                 s2->fd = -1;
1217                 source = s2;
1218                 faked = true;
1219         }
1220         string_list p;
1221         p.clear();
1222         if (ModeDefined(umode,MT_CLIENT))
1223         {
1224                 for (int i = 0; i <= MODCOUNT; i++)
1225                 {
1226                         if (modules[i]->OnExtendedMode(source,(void*)dest,umode,MT_CLIENT,adding,p))
1227                         {
1228                                 log(DEBUG,"Module %s claims umode %c",Config->module_names[i].c_str(),umode);
1229                                 return true;
1230                         }
1231                 }
1232                 log(DEBUG,"No module claims umode %c",umode);
1233                 if (faked)
1234                 {
1235                         delete s2;
1236                         source = NULL;
1237                 }
1238                 return false;
1239         }
1240         else
1241         {
1242                 if (faked)
1243                 {
1244                         delete s2;
1245                         source = NULL;
1246                 }
1247                 return false;
1248         }
1249 }
1250
1251 void cmd_mode::Handle (char **parameters, int pcnt, userrec *user)
1252 {
1253         chanrec* Ptr;
1254         userrec* dest;
1255         int can_change;
1256         int direction = 1;
1257         char outpars[MAXBUF];
1258         bool next_ok = true;
1259
1260         dest = Find(parameters[0]);
1261
1262         if (!user)
1263         {
1264                 return;
1265         }
1266
1267         if ((dest) && (pcnt == 1))
1268         {
1269                 WriteServ(user->fd,"221 %s :+%s",dest->nick,dest->modes);
1270                 return;
1271         }
1272
1273         if ((dest) && (pcnt > 1))
1274         {
1275                 std::string tidied = ServerInstance->ModeGrok->CompressModes(parameters[1],false);
1276                 parameters[1] = (char*)tidied.c_str();
1277
1278                 char dmodes[MAXBUF];
1279                 strlcpy(dmodes,dest->modes,MAXMODES);
1280                 log(DEBUG,"pulled up dest user modes: %s",dmodes);
1281
1282                 can_change = 0;
1283                 if (user != dest)
1284                 {
1285                         if ((strchr(user->modes,'o')) || (is_uline(user->server)))
1286                         {
1287                                 can_change = 1;
1288                         }
1289                 }
1290                 else
1291                 {
1292                         can_change = 1;
1293                 }
1294                 if (!can_change)
1295                 {
1296                         WriteServ(user->fd,"482 %s :Can't change mode for other users",user->nick);
1297                         return;
1298                 }
1299                 
1300                 outpars[0] = *parameters[1];
1301                 outpars[1] = 0;
1302                 direction = (*parameters[1] == '+');
1303
1304                 if ((*parameters[1] != '+') && (*parameters[1] != '-'))
1305                         return;
1306
1307                 for (char* i = parameters[1]; *i; i++)
1308                 {
1309                         if (*i == ' ')
1310                                 continue;
1311
1312                         if ((i != parameters[1]) && (*i != '+') && (*i != '-'))
1313                                 next_ok = true;
1314
1315                         if (*i == '+')
1316                         {
1317                                 if ((direction != 1) && (next_ok))
1318                                 {
1319                                         charlcat(outpars,'+',MAXBUF);
1320                                         next_ok = false;
1321                                 }       
1322                                 direction = 1;
1323                         }
1324                         else
1325                         if (*i == '-')
1326                         {
1327                                 if ((direction != 0) && (next_ok))
1328                                 {
1329                                         charlcat(outpars,'-',MAXBUF);
1330                                         next_ok = false;
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 ((*i == 'i') || (*i == 'w') || (*i == 's') || (ServerInstance->ModeGrok->AllowedUmode(*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,*i)) && (ServerInstance->ModeGrok->AllowedUmode(*i,user->modes,true,false)))
1353                                                 {
1354                                                         if ((ServerInstance->ModeGrok->ProcessModuleUmode(*i, user, dest, direction)) || (*i == 'i') || (*i == 's') || (*i == 'w') || (*i == 'o'))
1355                                                         {
1356                                                                 charlcat(dmodes,*i,MAXMODES);
1357                                                                 charlcat(outpars,*i,MAXMODES);
1358                                                                 if (*i == 'o')
1359                                                                 {
1360                                                                         FOREACH_MOD(I_OnGlobalOper,OnGlobalOper(dest));
1361                                                                 }
1362                                                         }
1363                                                 }
1364                                         }
1365                                         else
1366                                         {
1367                                                 if ((ServerInstance->ModeGrok->AllowedUmode(*i,user->modes,false,false)) && (strchr(dmodes,*i)))
1368                                                 {
1369                                                         if ((ServerInstance->ModeGrok->ProcessModuleUmode(*i, user, dest, direction)) || (*i == 'i') || (*i == 's') || (*i == 'w') || (*i == 'o'))
1370                                                         {
1371                                                                 charlcat(outpars,*i,MAXMODES);
1372                                                                 charremove(dmodes,*i);
1373                                                                 if (*i == 'o')
1374                                                                 {
1375                                                                         *dest->oper = 0;
1376                                                                         DeleteOper(dest);
1377                                                                 }
1378                                                         }
1379                                                 }
1380                                         }
1381                                 }
1382                         }
1383                 }
1384                 if (*outpars)
1385                 {
1386                         char b[MAXBUF];
1387                         char* z = b;
1388
1389                         for (char* i = outpars; *i;)
1390                         {
1391                                 *z++ = *i++;
1392                                 if (((*i == '-') || (*i == '+')) && ((*(i+1) == '-') || (*(i+1) == '+')))
1393                                 {
1394                                         // someones playing silly buggers and trying
1395                                         // to put a +- or -+ into the line...
1396                                         i++;
1397                                 }
1398                                 if (!*(i+1))
1399                                 {
1400                                         // Someone's trying to make the last character in
1401                                         // the line be a + or - symbol.
1402                                         if ((*i == '-') || (*i == '+'))
1403                                         {
1404                                                 i++;
1405                                         }
1406                                 }
1407                         }
1408                         *z = 0;
1409
1410                         if ((*b) && (!IS_SINGLE(b,'+')) && (!IS_SINGLE(b,'-')))
1411                         {
1412                                 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1413                                 FOREACH_MOD(I_OnMode,OnMode(user, dest, TYPE_USER, b));
1414                         }
1415
1416                         log(DEBUG,"Stripped mode line");
1417                         log(DEBUG,"Line dest is now %s",dmodes);
1418                         strlcpy(dest->modes,dmodes,MAXMODES-1);
1419
1420                 }
1421
1422                 return;
1423         }
1424         
1425         Ptr = FindChan(parameters[0]);
1426         if (Ptr)
1427         {
1428                 if (pcnt == 1)
1429                 {
1430                         /* just /modes #channel */
1431                         WriteServ(user->fd,"324 %s %s +%s",user->nick, Ptr->name, chanmodes(Ptr,has_channel(user,Ptr)));
1432                         WriteServ(user->fd,"329 %s %s %d", user->nick, Ptr->name, Ptr->created);
1433                         return;
1434                 }
1435                 else
1436                 if (pcnt == 2)
1437                 {
1438                         char* mode = parameters[1];
1439                         if (*mode == '+')
1440                                 mode++;
1441                         int MOD_RESULT = 0;
1442                         FOREACH_RESULT(I_OnRawMode,OnRawMode(user, Ptr, *mode, "", false, 0));
1443                         if (!MOD_RESULT)
1444                         {
1445                                 if (*mode == 'b')
1446                                 {
1447
1448                                         for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
1449                                         {
1450                                                 WriteServ(user->fd,"367 %s %s %s %s %d",user->nick, Ptr->name, i->data, i->set_by, i->set_time);
1451                                         }
1452                                         WriteServ(user->fd,"368 %s %s :End of channel ban list",user->nick, Ptr->name);
1453                                         return;
1454                                 }
1455                                 if ((ModeDefined(*mode,MT_CHANNEL)) && (ModeIsListMode(*mode,MT_CHANNEL)))
1456                                 {
1457                                         // list of items for an extmode
1458                                         log(DEBUG,"Calling OnSendList for all modules, list output for mode %c",*mode);
1459                                         FOREACH_MOD(I_OnSendList,OnSendList(user,Ptr,*mode));
1460                                         return;
1461                                 }
1462                         }
1463                 }
1464
1465                 if (((Ptr) && (!has_channel(user,Ptr))) && (!is_uline(user->server)) && (IS_LOCAL(user)))
1466                 {
1467                         WriteServ(user->fd,"442 %s %s :You're not on that channel!",user->nick, Ptr->name);
1468                         return;
1469                 }
1470
1471                 if (Ptr)
1472                 {
1473                         int MOD_RESULT = 0;
1474                         FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,NULL,Ptr,AC_GENERAL_MODE));
1475                         
1476                         if (MOD_RESULT == ACR_DENY)
1477                                 return;
1478                         if (MOD_RESULT == ACR_DEFAULT)
1479                         {
1480                                 if ((cstatus(user,Ptr) < STATUS_HOP) && (IS_LOCAL(user)))
1481                                 {
1482                                         WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, Ptr->name);
1483                                         return;
1484                                 }
1485                         }
1486
1487                         ServerInstance->ModeGrok->ProcessModes(parameters,user,Ptr,cstatus(user,Ptr),pcnt,false,false,false);
1488                 }
1489         }
1490         else
1491         {
1492                 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
1493         }
1494 }
1495
1496
1497
1498
1499 void ModeParser::ServerMode(char **parameters, int pcnt, userrec *user)
1500 {
1501         chanrec* Ptr;
1502         userrec* dest;
1503         int can_change;
1504         int direction = 1;
1505         char outpars[MAXBUF];
1506         bool next_ok = true;
1507
1508         dest = Find(parameters[0]);
1509         
1510         // fix: ChroNiCk found this - we cant use this as debug if its null!
1511         if (dest)
1512         {
1513                 log(DEBUG,"server_mode on %s",dest->nick);
1514         }
1515
1516         if ((dest) && (pcnt > 1))
1517         {
1518                 std::string tidied = ServerInstance->ModeGrok->CompressModes(parameters[1],false);
1519                 parameters[1] = (char*)tidied.c_str();
1520
1521                 char dmodes[MAXBUF];
1522                 strlcpy(dmodes,dest->modes,MAXBUF);
1523
1524                 outpars[0] = *parameters[1];
1525                 outpars[1] = 0;
1526                 direction = (*parameters[1] == '+');
1527
1528                 if ((*parameters[1] != '+') && (*parameters[1] != '-'))
1529                         return;
1530
1531                 for (char* i = parameters[1]; *i; i++)
1532                 {
1533                         if (*i == ' ')
1534                                 continue;
1535
1536                         if ((i != parameters[1]) && (*i != '+') && (*i != '-'))
1537                                 next_ok = true;
1538
1539                         if (*i == '+')
1540                         {
1541                                 if ((direction != 1) && (next_ok))
1542                                 {
1543                                         next_ok = false;
1544                                         charlcat(outpars,'+',MAXBUF);
1545                                 }
1546                                 direction = 1;
1547                         }
1548                         else
1549                         if (*i == '-')
1550                         {
1551                                 if ((direction != 0) && (next_ok))
1552                                 {
1553                                         next_ok = false;
1554                                         charlcat(outpars,'-',MAXBUF);
1555                                 }
1556                                 direction = 0;
1557                         }
1558                         else
1559                         {
1560                                 log(DEBUG,"begin mode processing entry");
1561                                 can_change = 1;
1562                                 if (can_change)
1563                                 {
1564                                         if (direction == 1)
1565                                         {
1566                                                 log(DEBUG,"umode %c being added",*i);
1567                                                 if ((!strchr(dmodes,*i)) && (ServerInstance->ModeGrok->AllowedUmode(*i,user->modes,true,true)))
1568                                                 {
1569                                                         log(DEBUG,"umode %c is an allowed umode",*i);
1570                                                         if ((ServerInstance->ModeGrok->ProcessModuleUmode(*i, user, dest, direction)) || (*i == 'i') || (*i == 's') || (*i == 'w') || (*i == 'o'))
1571                                                         {
1572                                                                 charlcat(dmodes,*i,MAXMODES);
1573                                                                 charlcat(outpars,*i,MAXMODES);
1574                                                         }
1575                                                 }
1576                                         }
1577                                         else
1578                                         {
1579                                                 // can only remove a mode they already have
1580                                                 log(DEBUG,"umode %c being removed",*i);
1581                                                 if ((ServerInstance->ModeGrok->AllowedUmode(*i,user->modes,false,true)) && (strchr(dmodes,*i)))
1582                                                 {
1583                                                         log(DEBUG,"umode %c is an allowed umode",*i);
1584                                                         if ((ServerInstance->ModeGrok->ProcessModuleUmode(*i, user, dest, direction)) || (*i == 'i') || (*i == 's') || (*i == 'w') || (*i == 'o'))
1585                                                         {
1586                                                                 charlcat(outpars,*i,MAXMODES);
1587                                                                 charremove(dmodes,*i);
1588                                                         }
1589                                                 }
1590                                         }
1591                                 }
1592                         }
1593                 }
1594                 if (*outpars)
1595                 {
1596                         char b[MAXBUF];
1597                         char* z = b;
1598
1599                         for (char* i = outpars; *i;)
1600                         {
1601                                 *z++ = *i++;
1602                                 if (((*i == '-') || (*i == '+')) && ((*(i+1) == '-') || (*(i+1) == '+')))
1603                                 {
1604                                         // someones playing silly buggers and trying
1605                                         // to put a +- or -+ into the line...
1606                                         i++;
1607                                 }
1608                                 if (!*(i+1))
1609                                 {
1610                                         // Someone's trying to make the last character in
1611                                         // the line be a + or - symbol.
1612                                         if ((*i == '-') || (*i == '+'))
1613                                         {
1614                                                 i++;
1615                                         }
1616                                 }
1617                         }
1618                         *z = 0;
1619
1620                         if ((*b) && (!IS_SINGLE(b,'+')) && (!IS_SINGLE(b,'-')))
1621                         {
1622                                 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1623                                 FOREACH_MOD(I_OnMode,OnMode(user, dest, TYPE_USER, b));
1624                         }
1625
1626                         log(DEBUG,"Stripped mode line");
1627                         log(DEBUG,"Line dest is now %s",dmodes);
1628                         strlcpy(dest->modes,dmodes,MAXMODES-1);
1629                                          
1630                 }
1631
1632                 return;
1633         }
1634         
1635         Ptr = FindChan(parameters[0]);
1636         if (Ptr)
1637         {
1638                 ServerInstance->ModeGrok->ProcessModes(parameters,user,Ptr,STATUS_OP,pcnt,true,false,false);
1639         }
1640         else
1641         {
1642                 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
1643         }
1644 }