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