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