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