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