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