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