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