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