1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
19 #include "inspircd_config.h"
21 #include "inspircd_io.h"
23 #include <sys/errno.h>
27 #include <ext/hash_map>
35 #include "connection.h"
45 #include "inspstring.h"
46 #include "helperfuncs.h"
50 extern std::vector<Module*> modules;
51 extern std::vector<ircd_module*> factory;
52 extern InspIRCd* ServerInstance;
53 extern ServerConfig* Config;
57 char* ModeParser::GiveOps(userrec *user,char *dest,chanrec *chan,int status)
61 if ((!user) || (!dest) || (!chan) || (!*dest))
63 log(DEFAULT,"*** BUG *** GiveOps was given an invalid parameter");
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);
75 if (user->server == d->server)
78 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_OP));
80 if (MOD_RESULT == ACR_DENY)
82 if (MOD_RESULT == ACR_DEFAULT)
84 if ((status < STATUS_OP) && (!is_uline(user->server)) && (IS_LOCAL(user)))
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);
94 for (unsigned int i = 0; i < d->chans.size(); i++)
96 if ((d->chans[i].channel != NULL) && (chan != NULL))
97 if (!strcasecmp(d->chans[i].channel->name,chan->name))
99 if (d->chans[i].uc_modes & UCMODE_OP)
101 /* mode already set on user, dont allow multiple */
104 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_OP;
105 d->chans[i].channel->AddOppedUser((char*)d);
106 log(DEBUG,"gave ops: %s %s",d->chans[i].channel->name,d->nick);
110 log(DEFAULT,"The target channel given to GiveOps was not in the users mode list");
115 char* ModeParser::GiveHops(userrec *user,char *dest,chanrec *chan,int status)
119 if ((!user) || (!dest) || (!chan) || (!*dest))
121 log(DEFAULT,"*** BUG *** GiveHops was given an invalid parameter");
128 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
133 if (user->server == d->server)
136 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_HALFOP));
138 if (MOD_RESULT == ACR_DENY)
140 if (MOD_RESULT == ACR_DEFAULT)
142 if ((status < STATUS_OP) && (!is_uline(user->server)) && (IS_LOCAL(user)))
144 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
150 for (unsigned int i = 0; i < d->chans.size(); i++)
152 if ((d->chans[i].channel != NULL) && (chan != NULL))
153 if (!strcasecmp(d->chans[i].channel->name,chan->name))
155 if (d->chans[i].uc_modes & UCMODE_HOP)
157 /* mode already set on user, dont allow multiple */
160 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_HOP;
161 d->chans[i].channel->AddHalfoppedUser((char*)d);
162 log(DEBUG,"gave h-ops: %s %s",d->chans[i].channel->name,d->nick);
170 char* ModeParser::GiveVoice(userrec *user,char *dest,chanrec *chan,int status)
174 if ((!user) || (!dest) || (!chan) || (!*dest))
176 log(DEFAULT,"*** BUG *** GiveVoice was given an invalid parameter");
183 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
188 if (user->server == d->server)
191 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_VOICE));
193 if (MOD_RESULT == ACR_DENY)
195 if (MOD_RESULT == ACR_DEFAULT)
197 if ((status < STATUS_HOP) && (!is_uline(user->server)) && (IS_LOCAL(user)))
199 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
205 for (unsigned int i = 0; i < d->chans.size(); i++)
207 if ((d->chans[i].channel != NULL) && (chan != NULL))
208 if (!strcasecmp(d->chans[i].channel->name,chan->name))
210 if (d->chans[i].uc_modes & UCMODE_VOICE)
212 /* mode already set on user, dont allow multiple */
215 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_VOICE;
216 d->chans[i].channel->AddVoicedUser((char*)d);
217 log(DEBUG,"gave voice: %s %s",d->chans[i].channel->name,d->nick);
225 char* ModeParser::TakeOps(userrec *user,char *dest,chanrec *chan,int status)
229 if ((!user) || (!dest) || (!chan) || (!*dest))
231 log(DEFAULT,"*** BUG *** TakeOps was given an invalid parameter");
238 log(DEBUG,"TakeOps couldnt resolve the target nickname: %s",dest);
239 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
244 if (user->server == d->server)
247 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_DEOP));
249 if (MOD_RESULT == ACR_DENY)
251 if (MOD_RESULT == ACR_DEFAULT)
253 if ((status < STATUS_OP) && (!is_uline(user->server)) && (IS_LOCAL(user)) && (IS_LOCAL(user)))
255 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
261 for (unsigned int i = 0; i < d->chans.size(); i++)
263 if ((d->chans[i].channel != NULL) && (chan != NULL))
264 if (!strcasecmp(d->chans[i].channel->name,chan->name))
266 if ((d->chans[i].uc_modes & UCMODE_OP) == 0)
268 /* mode already set on user, dont allow multiple */
271 d->chans[i].uc_modes ^= UCMODE_OP;
272 d->chans[i].channel->DelOppedUser((char*)d);
273 log(DEBUG,"took ops: %s %s",d->chans[i].channel->name,d->nick);
277 log(DEBUG,"TakeOps couldnt locate the target channel in the target users list");
282 char* ModeParser::TakeHops(userrec *user,char *dest,chanrec *chan,int status)
286 if ((!user) || (!dest) || (!chan) || (!*dest))
288 log(DEFAULT,"*** BUG *** TakeHops was given an invalid parameter");
295 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
300 if (user->server == d->server)
303 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_DEHALFOP));
305 if (MOD_RESULT == ACR_DENY)
307 if (MOD_RESULT == ACR_DEFAULT)
309 /* Tweak by Brain suggested by w00t, allow a halfop to dehalfop themselves */
310 if ((user != d) && ((status < STATUS_OP) && (!is_uline(user->server))) && (IS_LOCAL(user)))
312 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
318 for (unsigned int i = 0; i < d->chans.size(); i++)
320 if ((d->chans[i].channel != NULL) && (chan != NULL))
321 if (!strcasecmp(d->chans[i].channel->name,chan->name))
323 if ((d->chans[i].uc_modes & UCMODE_HOP) == 0)
325 /* mode already set on user, dont allow multiple */
328 d->chans[i].uc_modes ^= UCMODE_HOP;
329 d->chans[i].channel->DelHalfoppedUser((char*)d);
330 log(DEBUG,"took h-ops: %s %s",d->chans[i].channel->name,d->nick);
338 char* ModeParser::TakeVoice(userrec *user,char *dest,chanrec *chan,int status)
342 if ((!user) || (!dest) || (!chan) || (!*dest))
344 log(DEFAULT,"*** BUG *** TakeVoice was given an invalid parameter");
351 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
356 if (user->server == d->server)
359 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_DEVOICE));
361 if (MOD_RESULT == ACR_DENY)
363 if (MOD_RESULT == ACR_DEFAULT)
365 if ((status < STATUS_HOP) && (!is_uline(user->server)) && (IS_LOCAL(user)))
367 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
373 for (unsigned int i = 0; i < d->chans.size(); i++)
375 if ((d->chans[i].channel != NULL) && (chan != NULL))
376 if (!strcasecmp(d->chans[i].channel->name,chan->name))
378 if ((d->chans[i].uc_modes & UCMODE_VOICE) == 0)
380 /* mode already set on user, dont allow multiple */
383 d->chans[i].uc_modes ^= UCMODE_VOICE;
384 d->chans[i].channel->DelVoicedUser((char*)d);
385 log(DEBUG,"took voice: %s %s",d->chans[i].channel->name,d->nick);
393 char* ModeParser::AddBan(userrec *user,char *dest,chanrec *chan,int status)
396 int toomanyexclamation = 0;
399 if ((!user) || (!dest) || (!chan) || (!*dest))
401 log(DEFAULT,"*** BUG *** AddBan was given an invalid parameter");
405 for (char* i = dest; *i; i++)
407 if ((*i < 32) || (*i > 126))
413 toomanyexclamation++;
421 if (toomanyexclamation != 1 || toomanyat != 1)
423 * this stops sillyness like n!u!u!u@h, though note that most
424 * ircds don't actually verify banmask validity. --w00t
428 long maxbans = GetMaxBans(chan->name);
429 if ((unsigned)chan->bans.size() > (unsigned)maxbans)
431 WriteServ(user->fd,"478 %s %s :Channel ban list for %s is full (maximum entries for this channel is %d)",user->nick, chan->name,chan->name,maxbans);
435 log(DEBUG,"AddBan: %s %s",chan->name,user->nick);
438 FOREACH_RESULT(I_OnAddBan,OnAddBan(user,chan,dest));
443 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
445 if (!strcasecmp(i->data,dest))
447 // dont allow a user to set the same ban twice
453 strlcpy(b.data,dest,MAXBUF);
456 strlcpy(b.set_by,user->nick,NICKMAX-1);
460 strlcpy(b.set_by,Config->ServerName,NICKMAX-1);
462 chan->bans.push_back(b);
466 char* ModeParser::TakeBan(userrec *user,char *dest,chanrec *chan,int status)
468 if ((!user) || (!dest) || (!chan) || (!*dest)) {
469 log(DEFAULT,"*** BUG *** TakeBan was given an invalid parameter");
473 log(DEBUG,"del_ban: %s %s",chan->name,user->nick);
474 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
476 if (!strcasecmp(i->data,dest))
479 FOREACH_RESULT(I_OnDelBan,OnDelBan(user,chan,dest));
489 // tidies up redundant modes, e.g. +nt-nt+i becomes +-+i,
490 // a section further down the chain tidies up the +-+- crap.
491 std::string ModeParser::CompressModes(std::string modes,bool channelmodes)
495 memset(counts,0,sizeof(counts));
496 memset(active,0,sizeof(active));
497 for (unsigned int i = 0; i < modes.length(); i++)
499 if ((modes[i] == '+') || (modes[i] == '-'))
503 if ((strchr("itnmsp",modes[i])) || ((ModeDefined(modes[i],MT_CHANNEL)) && (ModeDefinedOn(modes[i],MT_CHANNEL)==0) && (ModeDefinedOff(modes[i],MT_CHANNEL)==0)))
505 log(DEBUG,"Tidy mode %c",modes[i]);
506 counts[(unsigned int)modes[i]]++;
507 active[(unsigned int)modes[i]] = true;
512 log(DEBUG,"Tidy mode %c",modes[i]);
513 counts[(unsigned int)modes[i]]++;
514 active[(unsigned int)modes[i]] = true;
517 for (int j = 65; j < 127; j++)
519 if ((counts[j] > 1) && (active[j] == true))
522 v[0] = (unsigned char)j;
524 std::string mode_str = v;
525 std::string::size_type pos = modes.find(mode_str);
526 if (pos != std::string::npos)
528 log(DEBUG,"all occurances of mode %c to be deleted...",(unsigned char)j);
529 while (modes.find(mode_str) != std::string::npos)
530 modes.erase(modes.find(mode_str),1);
531 log(DEBUG,"New mode line: %s",modes.c_str());
538 void ModeParser::ProcessModes(char **parameters,userrec* user,chanrec *chan,int status, int pcnt, bool servermode, bool silent, bool local)
541 log(DEFAULT,"*** BUG *** process_modes was given an invalid parameter");
545 char modelist[MAXBUF];
546 char outlist[MAXBUF];
548 char outpars[32][MAXBUF];
554 bool k_set = false, l_set = false, previously_set_l = false, previously_unset_l = false, previously_set_k = false, previously_unset_k = false;
565 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,NULL,chan,AC_GENERAL_MODE));
566 if (MOD_RESULT == ACR_DENY)
570 log(DEBUG,"process_modes: start: parameters=%d",pcnt);
572 strlcpy(modelist,parameters[1],MAXBUF); /* mode list, e.g. +oo-o *
573 * parameters[2] onwards are parameters for
574 * modes that require them :) */
579 log(DEBUG,"process_modes: modelist: %s",modelist);
581 std::string tidied = this->CompressModes(modelist,true);
582 strlcpy(modelist,tidied.c_str(),MAXBUF);
584 int len = tidied.length();
585 while (modelist[len-1] == ' ')
586 modelist[--len] = '\0';
588 bool next_cant_be_modifier = false;
591 for (modechar = modelist; *modechar; ptr++, modechar++)
595 /* If we have more than MAXMODES changes in one line,
596 * drop all after the MAXMODES
601 if ((*modechar != '+') && (*modechar != '-'))
602 next_cant_be_modifier = false;
604 if (((*modechar == '+') || (*modechar == '-')) && ((*(modechar+1) == 0) || (*(modechar+1) == '+') || (*(modechar+1) == '-')))
605 next_cant_be_modifier = true;
611 if (!next_cant_be_modifier)
615 int t = strlen(outlist)-1;
616 if ((outlist[t] == '+') || (outlist[t] == '-'))
622 charlcat(outlist,'-',MAXBUF);
626 next_cant_be_modifier = true;
631 if (!next_cant_be_modifier)
635 int t = strlen(outlist)-1;
636 if ((outlist[t] == '+') || (outlist[t] == '-'))
642 charlcat(outlist,'+',MAXBUF);
646 next_cant_be_modifier = true;
652 if ((param >= pcnt)) break;
653 log(DEBUG,"Enough parameters left");
658 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'o', parameters[param], true, 1));
661 log(DEBUG,"calling GiveOps");
662 r = GiveOps(user,parameters[param++],chan,status);
669 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'o', parameters[param], false, 1));
672 log(DEBUG,"calling TakeOps");
673 r = TakeOps(user,parameters[param++],chan,status);
679 charlcat(outlist,'o',MAXBUF);
680 strlcpy(outpars[pc++],r,MAXBUF);
685 if (((param >= pcnt)) || (!Config->AllowHalfop)) break;
690 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'h', parameters[param], true, 1));
693 r = GiveHops(user,parameters[param++],chan,status);
700 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'h', parameters[param], false, 1));
703 r = TakeHops(user,parameters[param++],chan,status);
709 charlcat(outlist,'h',MAXBUF);
710 strlcpy(outpars[pc++],r,MAXBUF);
716 if ((param >= pcnt)) break;
721 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'v', parameters[param], true, 1));
724 r = GiveVoice(user,parameters[param++],chan,status);
731 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'v', parameters[param], false, 1));
734 r = TakeVoice(user,parameters[param++],chan,status);
740 charlcat(outlist,'v',MAXBUF);
741 strlcpy(outpars[pc++],r,MAXBUF);
746 if ((param >= pcnt)) break;
751 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'b', parameters[param], true, 1));
754 r = AddBan(user,parameters[param++],chan,status);
761 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'b', parameters[param], false, 1));
764 r = TakeBan(user,parameters[param++],chan,status);
770 charlcat(outlist,'b',MAXBUF);
771 strlcpy(outpars[pc++],parameters[param-1],MAXBUF);
785 if (previously_unset_k)
787 previously_set_k = true;
792 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'k', parameters[param], true, 1));
795 charlcat(outlist,'k',MAXBUF);
797 strlcpy(key,parameters[param++],32);
798 strlcpy(outpars[pc++],key,MAXBUF);
799 strlcpy(chan->key,key,MAXBUF);
807 /* checks on -k are case sensitive and only accurate to the
808 first 32 characters */
809 if (previously_set_k)
811 previously_unset_k = true;
815 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'k', parameters[param], false, 1));
818 strlcpy(key,parameters[param++],32);
819 /* only allow -k if correct key given */
820 if (!strcmp(chan->key,key))
822 charlcat(outlist,'k',MAXBUF);
824 strlcpy(outpars[pc++],key,MAXBUF);
834 if (previously_set_l)
836 previously_unset_l = true;
838 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'l', "", false, 0));
843 charlcat(outlist,'l',MAXBUF);
849 if ((param >= pcnt)) break;
854 if (previously_unset_l)
856 previously_set_l = true;
857 bool invalid = false;
858 for (char* f = parameters[param]; *f; f++)
860 if ((*f < '0') || (*f > '9'))
865 /* If the limit is < 1, or the new limit is the current limit, dont allow */
866 if ((atoi(parameters[param]) < 1) || ((chan->limit > 0) && (atoi(parameters[param]) == chan->limit)))
875 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'l', parameters[param], true, 1));
879 chan->limit = atoi(parameters[param]);
881 // reported by mech: large values cause underflow
883 chan->limit = 0x7FFF;
888 charlcat(outlist,'l',MAXBUF);
889 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
897 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'i', "", mdir, 0));
902 if (!(chan->binarymodes & CM_INVITEONLY)) charlcat(outlist,'i',MAXBUF);
903 chan->binarymodes |= CM_INVITEONLY;
907 if (chan->binarymodes & CM_INVITEONLY) charlcat(outlist,'i',MAXBUF);
908 chan->binarymodes &= ~CM_INVITEONLY;
915 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 't', "", mdir, 0));
920 if (!(chan->binarymodes & CM_TOPICLOCK)) charlcat(outlist,'t',MAXBUF);
921 chan->binarymodes |= CM_TOPICLOCK;
925 if (chan->binarymodes & CM_TOPICLOCK) charlcat(outlist,'t',MAXBUF);
926 chan->binarymodes &= ~CM_TOPICLOCK;
933 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'n', "", mdir, 0));
938 if (!(chan->binarymodes & CM_NOEXTERNAL)) charlcat(outlist,'n',MAXBUF);
939 chan->binarymodes |= CM_NOEXTERNAL;
943 if (chan->binarymodes & CM_NOEXTERNAL) charlcat(outlist,'n',MAXBUF);
944 chan->binarymodes &= ~CM_NOEXTERNAL;
951 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'm', "", mdir, 0));
956 if (!(chan->binarymodes & CM_MODERATED)) charlcat(outlist,'m',MAXBUF);
957 chan->binarymodes |= CM_MODERATED;
961 if (chan->binarymodes & CM_MODERATED) charlcat(outlist,'m',MAXBUF);
962 chan->binarymodes &= ~CM_MODERATED;
969 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 's', "", mdir, 0));
974 if (!(chan->binarymodes & CM_SECRET)) charlcat(outlist,'s',MAXBUF);
975 chan->binarymodes |= CM_SECRET;
976 if (chan->binarymodes & CM_PRIVATE)
978 chan->binarymodes &= ~CM_PRIVATE;
981 strlcat(outlist,"-p+",MAXBUF);
987 if (chan->binarymodes & CM_SECRET) charlcat(outlist,'s',MAXBUF);
988 chan->binarymodes &= ~CM_SECRET;
995 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'p', "", mdir, 0));
1000 if (!(chan->binarymodes & CM_PRIVATE)) charlcat(outlist,'p',MAXBUF);
1001 chan->binarymodes |= CM_PRIVATE;
1002 if (chan->binarymodes & CM_SECRET)
1004 chan->binarymodes &= ~CM_SECRET;
1007 strlcat(outlist,"-s+",MAXBUF);
1013 if (chan->binarymodes & CM_PRIVATE) charlcat(outlist,'p',MAXBUF);
1014 chan->binarymodes &= ~CM_PRIVATE;
1020 log(DEBUG,"Preprocessing custom mode %c: modelist: %s",*modechar,chan->custom_modes);
1023 if (((!strchr(chan->custom_modes,*modechar)) && (!mdir)) || ((strchr(chan->custom_modes,*modechar)) && (mdir)))
1025 if (!ModeIsListMode(*modechar,MT_CHANNEL))
1027 log(DEBUG,"Mode %c isnt set on %s but trying to remove!",*modechar,chan->name);
1031 if (ModeDefined(*modechar,MT_CHANNEL))
1033 log(DEBUG,"A module has claimed this mode");
1036 if ((ModeDefinedOn(*modechar,MT_CHANNEL)>0) && (mdir))
1038 p.push_back(parameters[param]);
1040 if ((ModeDefinedOff(*modechar,MT_CHANNEL)>0) && (!mdir))
1042 p.push_back(parameters[param]);
1045 bool handled = false;
1048 // we're supposed to have a parameter, but none was given... so dont handle the mode.
1049 if (((ModeDefinedOn(*modechar,MT_CHANNEL)>0) && (mdir)) || ((ModeDefinedOff(*modechar,MT_CHANNEL)>0) && (!mdir)))
1051 log(DEBUG,"Not enough parameters for module-mode %c",*modechar);
1057 // BIG ASS IDIOTIC CODER WARNING!
1058 // Using OnRawMode on another modules mode's behavour
1059 // will confuse the crap out of admins! just because you CAN
1060 // do it, doesnt mean you SHOULD!
1062 std::string para = "";
1065 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, *modechar, para, mdir, pcnt));
1068 for (int i = 0; i <= MODCOUNT; i++)
1072 int t = modules[i]->OnExtendedMode(user,chan,*modechar,MT_CHANNEL,mdir,p);
1075 log(DEBUG,"OnExtendedMode returned nonzero for a module");
1076 if (ModeIsListMode(*modechar,MT_CHANNEL))
1087 charlcat(outlist,*modechar,MAXBUF);
1089 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
1096 if ((modelist[ptr-1] == '+') || (modelist[ptr-1] == '-'))
1098 charlcat(outlist,*modechar,MAXBUF);
1100 else if (!strchr(outlist,*modechar))
1102 charlcat(outlist,*modechar,MAXBUF);
1105 chan->SetCustomMode(*modechar,mdir);
1106 // include parameters in output if mode has them
1107 if ((ModeDefinedOn(*modechar,MT_CHANNEL)>0) && (mdir))
1109 chan->SetCustomModeParam(modelist[ptr],parameters[param],mdir);
1110 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
1113 // break, because only one module can handle the mode.
1122 WriteServ(user->fd,"472 %s %c :is unknown mode char to me",user->nick,*modechar);
1130 /* This means the mode line is something like: "+o-", we have to take the last char off. */
1131 char* x = outlist + strlen(outlist) - 1;
1132 while (((*x == '-') || (*x == '+')) && (x != outlist))
1134 log(DEBUG,"Cut off trailing modifier");
1137 /* The mode change must be at least two characters long (+ or - and at least one mode) */
1138 if (((*outlist == '+') || (*outlist == '-')) && *(outlist+1))
1140 strlcpy(outstr,outlist,MAXBUF);
1141 for (ptr = 0; ptr < pc; ptr++)
1143 charlcat(outstr,' ',MAXBUF);
1144 strlcat(outstr,outpars[ptr],MAXBUF);
1148 log(DEBUG,"Local mode change");
1149 WriteChannelLocal(chan, user, "MODE %s %s",chan->name,outstr);
1150 FOREACH_MOD(I_OnMode,OnMode(user, chan, TYPE_CHANNEL, outstr));
1158 WriteChannelWithServ(Config->ServerName,chan,"MODE %s %s",chan->name,outstr);
1166 WriteChannel(chan,user,"MODE %s %s",chan->name,outstr);
1167 FOREACH_MOD(I_OnMode,OnMode(user, chan, TYPE_CHANNEL, outstr));
1174 // based on sourcemodes, return true or false to determine if umode is a valid mode a user may set on themselves or others.
1176 bool ModeParser::AllowedUmode(char umode, char* sourcemodes,bool adding,bool serveroverride)
1178 log(DEBUG,"Allowed_umode: %c %s",umode,sourcemodes);
1179 // Servers can +o and -o arbitrarily
1180 if ((serveroverride == true) && (umode == 'o'))
1184 // RFC1459 specified modes
1185 if ((umode == 'w') || (umode == 's') || (umode == 'i'))
1187 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1191 // user may not +o themselves or others, but an oper may de-oper other opers or themselves
1192 if ((strchr(sourcemodes,'o')) && (!adding))
1194 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1197 else if (umode == 'o')
1199 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1203 // process any module-defined modes that need oper
1204 if ((ModeDefinedOper(umode,MT_CLIENT)) && (strchr(sourcemodes,'o')))
1206 log(DEBUG,"umode %c allowed by module handler (oper only mode)",umode);
1210 if (ModeDefined(umode,MT_CLIENT))
1212 // process any module-defined modes that don't need oper
1213 log(DEBUG,"umode %c allowed by module handler (non-oper mode)",umode);
1214 if ((ModeDefinedOper(umode,MT_CLIENT)) && (!strchr(sourcemodes,'o')))
1216 // no, this mode needs oper, and this user 'aint got what it takes!
1222 // anything else - return false.
1223 log(DEBUG,"umode %c not known by any ruleset",umode);
1227 bool ModeParser::ProcessModuleUmode(char umode, userrec* source, void* dest, bool adding)
1234 strlcpy(s2->nick,Config->ServerName,NICKMAX-1);
1243 if (ModeDefined(umode,MT_CLIENT))
1245 for (int i = 0; i <= MODCOUNT; i++)
1247 if (modules[i]->OnExtendedMode(source,(void*)dest,umode,MT_CLIENT,adding,p))
1249 log(DEBUG,"Module %s claims umode %c",Config->module_names[i].c_str(),umode);
1253 log(DEBUG,"No module claims umode %c",umode);
1272 void cmd_mode::Handle (char **parameters, int pcnt, userrec *user)
1278 char outpars[MAXBUF];
1280 dest = Find(parameters[0]);
1287 if ((dest) && (pcnt == 1))
1289 WriteServ(user->fd,"221 %s :+%s",dest->nick,dest->modes);
1293 if ((dest) && (pcnt > 1))
1295 std::string tidied = ServerInstance->ModeGrok->CompressModes(parameters[1],false);
1296 parameters[1] = (char*)tidied.c_str();
1298 char dmodes[MAXBUF];
1299 strlcpy(dmodes,dest->modes,52);
1300 log(DEBUG,"pulled up dest user modes: %s",dmodes);
1305 if ((strchr(user->modes,'o')) || (is_uline(user->server)))
1316 WriteServ(user->fd,"482 %s :Can't change mode for other users",user->nick);
1324 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1327 for (unsigned int i = 0; i < strlen(parameters[1]); i++)
1329 if (parameters[1][i] == ' ')
1331 if (parameters[1][i] == '+')
1335 int t = strlen(outpars)-1;
1336 if ((outpars[t] == '+') || (outpars[t] == '-'))
1342 charlcat(outpars,'+',MAXBUF);
1348 if (parameters[1][i] == '-')
1352 int t = strlen(outpars)-1;
1353 if ((outpars[t] == '+') || (outpars[t] == '-'))
1359 charlcat(outpars,'-',MAXBUF);
1367 if (strchr(user->modes,'o'))
1373 if ((parameters[1][i] == 'i') || (parameters[1][i] == 'w') || (parameters[1][i] == 's') || (ServerInstance->ModeGrok->AllowedUmode(parameters[1][i],user->modes,direction,false)))
1382 if ((!strchr(dmodes,parameters[1][i])) && (ServerInstance->ModeGrok->AllowedUmode(parameters[1][i],user->modes,true,false)))
1384 char umode = parameters[1][i];
1385 if ((ServerInstance->ModeGrok->ProcessModuleUmode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1387 int q = strlen(dmodes);
1388 int r = strlen(outpars);
1390 dmodes[q] = parameters[1][i];
1392 outpars[r] = parameters[1][i];
1393 if (parameters[1][i] == 'o')
1395 FOREACH_MOD(I_OnGlobalOper,OnGlobalOper(dest));
1402 if ((ServerInstance->ModeGrok->AllowedUmode(parameters[1][i],user->modes,false,false)) && (strchr(dmodes,parameters[1][i])))
1404 char umode = parameters[1][i];
1405 if ((ServerInstance->ModeGrok->ProcessModuleUmode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1410 unsigned int r = strlen(outpars);
1412 outpars[r] = parameters[1][i];
1415 for (q = 0; q < strlen(dmodes); q++)
1417 if (dmodes[q] != parameters[1][i])
1419 charlcat(temp,dmodes[q],MAXBUF);
1422 strlcpy(dmodes,temp,52);
1441 while (i < strlen (outpars))
1443 b[z++] = outpars[i++];
1445 if (i<strlen(outpars)-1)
1447 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1449 // someones playing silly buggers and trying
1450 // to put a +- or -+ into the line...
1454 if (i == strlen(outpars)-1)
1456 if ((outpars[i] == '-') || (outpars[i] == '+'))
1464 if ((b[z] == '-') || (b[z] == '+'))
1467 if ((!*b) || (IS_SINGLE(b,'+')) || (IS_SINGLE(b,'-')))
1472 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1473 FOREACH_MOD(I_OnMode,OnMode(user, dest, TYPE_USER, b));
1476 if (strlen(dmodes)>MAXMODES)
1478 dmodes[MAXMODES-1] = '\0';
1480 log(DEBUG,"Stripped mode line");
1481 log(DEBUG,"Line dest is now %s",dmodes);
1482 strlcpy(dest->modes,dmodes,52);
1489 Ptr = FindChan(parameters[0]);
1494 /* just /modes #channel */
1495 WriteServ(user->fd,"324 %s %s +%s",user->nick, Ptr->name, chanmodes(Ptr,has_channel(user,Ptr)));
1496 WriteServ(user->fd,"329 %s %s %d", user->nick, Ptr->name, Ptr->created);
1502 char* mode = parameters[1];
1506 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, Ptr, *mode, "", false, 0));
1512 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
1514 WriteServ(user->fd,"367 %s %s %s %s %d",user->nick, Ptr->name, i->data, i->set_by, i->set_time);
1516 WriteServ(user->fd,"368 %s %s :End of channel ban list",user->nick, Ptr->name);
1519 if ((ModeDefined(*mode,MT_CHANNEL)) && (ModeIsListMode(*mode,MT_CHANNEL)))
1521 // list of items for an extmode
1522 log(DEBUG,"Calling OnSendList for all modules, list output for mode %c",*mode);
1523 FOREACH_MOD(I_OnSendList,OnSendList(user,Ptr,*mode));
1529 if (((Ptr) && (!has_channel(user,Ptr))) && (!is_uline(user->server)) && (IS_LOCAL(user)))
1531 WriteServ(user->fd,"442 %s %s :You're not on that channel!",user->nick, Ptr->name);
1538 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,NULL,Ptr,AC_GENERAL_MODE));
1540 if (MOD_RESULT == ACR_DENY)
1542 if (MOD_RESULT == ACR_DEFAULT)
1544 if ((cstatus(user,Ptr) < STATUS_HOP) && (IS_LOCAL(user)))
1546 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, Ptr->name);
1551 ServerInstance->ModeGrok->ProcessModes(parameters,user,Ptr,cstatus(user,Ptr),pcnt,false,false,false);
1556 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
1563 void ModeParser::ServerMode(char **parameters, int pcnt, userrec *user)
1569 char outpars[MAXBUF];
1571 dest = Find(parameters[0]);
1573 // fix: ChroNiCk found this - we cant use this as debug if its null!
1576 log(DEBUG,"server_mode on %s",dest->nick);
1579 if ((dest) && (pcnt > 1))
1581 std::string tidied = ServerInstance->ModeGrok->CompressModes(parameters[1],false);
1582 parameters[1] = (char*)tidied.c_str();
1584 char dmodes[MAXBUF];
1585 strlcpy(dmodes,dest->modes,52);
1591 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1594 for (unsigned int i = 0; i < strlen(parameters[1]); i++)
1596 if (parameters[1][i] == ' ')
1598 if (parameters[1][i] == '+')
1602 int t = strlen(outpars)-1;
1603 if ((outpars[t] == '+') || (outpars[t] == '-'))
1609 charlcat(outpars,'+',MAXBUF);
1615 if (parameters[1][i] == '-')
1619 int t = strlen(outpars)-1;
1620 if ((outpars[t] == '+') || (outpars[t] == '-'))
1626 charlcat(outpars,'-',MAXBUF);
1633 log(DEBUG,"begin mode processing entry");
1639 log(DEBUG,"umode %c being added",parameters[1][i]);
1640 if ((!strchr(dmodes,parameters[1][i])) && (ServerInstance->ModeGrok->AllowedUmode(parameters[1][i],user->modes,true,true)))
1642 char umode = parameters[1][i];
1643 log(DEBUG,"umode %c is an allowed umode",umode);
1644 if ((ServerInstance->ModeGrok->ProcessModuleUmode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1646 int v1 = strlen(dmodes);
1647 int v2 = strlen(outpars);
1649 dmodes[v1] = parameters[1][i];
1651 outpars[v2] = parameters[1][i];
1657 // can only remove a mode they already have
1658 log(DEBUG,"umode %c being removed",parameters[1][i]);
1659 if ((ServerInstance->ModeGrok->AllowedUmode(parameters[1][i],user->modes,false,true)) && (strchr(dmodes,parameters[1][i])))
1661 char umode = parameters[1][i];
1662 log(DEBUG,"umode %c is an allowed umode",umode);
1663 if ((ServerInstance->ModeGrok->ProcessModuleUmode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1668 unsigned int v1 = strlen(outpars);
1670 outpars[v1] = parameters[1][i];
1672 for (q = 0; q < strlen(dmodes); q++)
1674 if (dmodes[q] != parameters[1][i])
1676 charlcat(temp,dmodes[q],MAXBUF);
1679 strlcpy(dmodes,temp,52);
1692 while (i < strlen (outpars))
1694 b[z++] = outpars[i++];
1696 if (i<strlen(outpars)-1)
1698 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1700 // someones playing silly buggers and trying
1701 // to put a +- or -+ into the line...
1705 if (i == strlen(outpars)-1)
1707 if ((outpars[i] == '-') || (outpars[i] == '+'))
1715 if ((b[z] == '-') || (b[z] == '+'))
1718 if ((!*b) || (IS_SINGLE(b,'+')) || (IS_SINGLE(b,'-')))
1723 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1724 FOREACH_MOD(I_OnMode,OnMode(user, dest, TYPE_USER, b));
1727 if (strlen(dmodes)>MAXMODES)
1729 dmodes[MAXMODES-1] = '\0';
1731 log(DEBUG,"Stripped mode line");
1732 log(DEBUG,"Line dest is now %s",dmodes);
1733 strlcpy(dest->modes,dmodes,MAXMODES);
1740 Ptr = FindChan(parameters[0]);
1743 ServerInstance->ModeGrok->ProcessModes(parameters,user,Ptr,STATUS_OP,pcnt,true,false,false);
1747 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);