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