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