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