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