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