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