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