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