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