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