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