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);
454 strlcpy(b.set_by,user->nick,NICKMAX);
455 chan->bans.push_back(b);
459 char* ModeParser::TakeBan(userrec *user,char *dest,chanrec *chan,int status)
461 if ((!user) || (!dest) || (!chan) || (!*dest)) {
462 log(DEFAULT,"*** BUG *** TakeBan was given an invalid parameter");
466 log(DEBUG,"del_ban: %s %s",chan->name,user->nick);
467 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
469 if (!strcasecmp(i->data,dest))
472 FOREACH_RESULT(I_OnDelBan,OnDelBan(user,chan,dest));
482 // tidies up redundant modes, e.g. +nt-nt+i becomes +-+i,
483 // a section further down the chain tidies up the +-+- crap.
484 std::string ModeParser::CompressModes(std::string modes,bool channelmodes)
488 memset(counts,0,sizeof(counts));
489 memset(active,0,sizeof(active));
490 for (unsigned int i = 0; i < modes.length(); i++)
492 if ((modes[i] == '+') || (modes[i] == '-'))
496 if ((strchr("itnmsp",modes[i])) || ((ModeDefined(modes[i],MT_CHANNEL)) && (ModeDefinedOn(modes[i],MT_CHANNEL)==0) && (ModeDefinedOff(modes[i],MT_CHANNEL)==0)))
498 log(DEBUG,"Tidy mode %c",modes[i]);
499 counts[(unsigned int)modes[i]]++;
500 active[(unsigned int)modes[i]] = true;
505 log(DEBUG,"Tidy mode %c",modes[i]);
506 counts[(unsigned int)modes[i]]++;
507 active[(unsigned int)modes[i]] = true;
510 for (int j = 65; j < 127; j++)
512 if ((counts[j] > 1) && (active[j] == true))
515 v[0] = (unsigned char)j;
517 std::string mode_str = v;
518 std::string::size_type pos = modes.find(mode_str);
519 if (pos != std::string::npos)
521 log(DEBUG,"all occurances of mode %c to be deleted...",(unsigned char)j);
522 while (modes.find(mode_str) != std::string::npos)
523 modes.erase(modes.find(mode_str),1);
524 log(DEBUG,"New mode line: %s",modes.c_str());
531 void ModeParser::ProcessModes(char **parameters,userrec* user,chanrec *chan,int status, int pcnt, bool servermode, bool silent, bool local)
534 log(DEFAULT,"*** BUG *** process_modes was given an invalid parameter");
538 char modelist[MAXBUF];
539 char outlist[MAXBUF];
541 char outpars[32][MAXBUF];
547 bool k_set = false, l_set = false, previously_set_l = false, previously_unset_l = false, previously_set_k = false, previously_unset_k = false;
558 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,NULL,chan,AC_GENERAL_MODE));
559 if (MOD_RESULT == ACR_DENY)
563 log(DEBUG,"process_modes: start: parameters=%d",pcnt);
565 strlcpy(modelist,parameters[1],MAXBUF); /* mode list, e.g. +oo-o *
566 * parameters[2] onwards are parameters for
567 * modes that require them :) */
572 log(DEBUG,"process_modes: modelist: %s",modelist);
574 std::string tidied = this->CompressModes(modelist,true);
575 strlcpy(modelist,tidied.c_str(),MAXBUF);
577 int len = tidied.length();
578 while (modelist[len-1] == ' ')
579 modelist[--len] = '\0';
581 for (char* modechar = modelist; *modechar; ptr++, modechar++)
591 int t = strlen(outlist)-1;
592 if ((outlist[t] == '+') || (outlist[t] == '-'))
608 int t = strlen(outlist)-1;
609 if ((outlist[t] == '+') || (outlist[t] == '-'))
623 if ((param >= pcnt)) break;
624 log(DEBUG,"Enough parameters left");
628 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'o', parameters[param], true, 1));
631 log(DEBUG,"calling GiveOps");
632 r = GiveOps(user,parameters[param++],chan,status);
639 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'o', parameters[param], false, 1));
642 log(DEBUG,"calling TakeOps");
643 r = TakeOps(user,parameters[param++],chan,status);
649 strlcat(outlist,"o",MAXBUF);
650 strlcpy(outpars[pc++],r,MAXBUF);
655 if (((param >= pcnt)) || (!Config->AllowHalfop)) break;
659 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'h', parameters[param], true, 1));
662 r = GiveHops(user,parameters[param++],chan,status);
669 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'h', parameters[param], false, 1));
672 r = TakeHops(user,parameters[param++],chan,status);
678 strlcat(outlist,"h",MAXBUF);
679 strlcpy(outpars[pc++],r,MAXBUF);
685 if ((param >= pcnt)) break;
689 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'v', parameters[param], true, 1));
692 r = GiveVoice(user,parameters[param++],chan,status);
699 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'v', parameters[param], false, 1));
702 r = TakeVoice(user,parameters[param++],chan,status);
708 strlcat(outlist,"v",MAXBUF);
709 strlcpy(outpars[pc++],r,MAXBUF);
714 if ((param >= pcnt)) break;
718 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'b', parameters[param], true, 1));
721 r = AddBan(user,parameters[param++],chan,status);
728 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'b', parameters[param], false, 1));
731 r = TakeBan(user,parameters[param++],chan,status);
737 strlcat(outlist,"b",MAXBUF);
738 strlcpy(outpars[pc++],parameters[param-1],MAXBUF);
752 if (previously_unset_k)
754 previously_set_k = true;
759 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'k', parameters[param], true, 1));
764 strlcpy(key,parameters[param++],32);
765 strlcpy(outpars[pc++],key,MAXBUF);
766 strlcpy(chan->key,key,MAXBUF);
774 /* checks on -k are case sensitive and only accurate to the
775 first 32 characters */
776 if (previously_set_k)
778 previously_unset_k = true;
782 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'k', parameters[param], false, 1));
785 strlcpy(key,parameters[param++],32);
786 /* only allow -k if correct key given */
787 if (!strcmp(chan->key,key))
789 strlcat(outlist,"k",MAXBUF);
790 strlcpy(chan->key,"",MAXBUF);
791 strlcpy(outpars[pc++],key,MAXBUF);
801 if (previously_set_l)
803 previously_unset_l = true;
805 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'l', "", false, 0));
816 if ((param >= pcnt)) break;
821 if (previously_unset_l)
823 previously_set_l = true;
824 bool invalid = false;
825 for (char* f = parameters[param]; *f; f++)
827 if ((*f < '0') || (*f > '9'))
832 /* If the limit is < 1, or the new limit is the current limit, dont allow */
833 if ((atoi(parameters[param]) < 1) || ((chan->limit > 0) && (atoi(parameters[param]) == chan->limit)))
842 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'l', parameters[param], true, 1));
846 chan->limit = atoi(parameters[param]);
848 // reported by mech: large values cause underflow
850 chan->limit = 0x7FFF;
855 strlcat(outlist,"l",MAXBUF);
856 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
864 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'i', "", mdir, 0));
869 if (!(chan->binarymodes & CM_INVITEONLY)) strlcat(outlist,"i",MAXBUF);
870 chan->binarymodes |= CM_INVITEONLY;
874 if (chan->binarymodes & CM_INVITEONLY) strlcat(outlist,"i",MAXBUF);
875 chan->binarymodes &= ~CM_INVITEONLY;
882 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 't', "", mdir, 0));
887 if (!(chan->binarymodes & CM_TOPICLOCK)) strlcat(outlist,"t",MAXBUF);
888 chan->binarymodes |= CM_TOPICLOCK;
892 if (chan->binarymodes & CM_TOPICLOCK) strlcat(outlist,"t",MAXBUF);
893 chan->binarymodes &= ~CM_TOPICLOCK;
900 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'n', "", mdir, 0));
905 if (!(chan->binarymodes & CM_NOEXTERNAL)) strlcat(outlist,"n",MAXBUF);
906 chan->binarymodes |= CM_NOEXTERNAL;
910 if (chan->binarymodes & CM_NOEXTERNAL) strlcat(outlist,"n",MAXBUF);
911 chan->binarymodes &= ~CM_NOEXTERNAL;
918 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'm', "", mdir, 0));
923 if (!(chan->binarymodes & CM_MODERATED)) strlcat(outlist,"m",MAXBUF);
924 chan->binarymodes |= CM_MODERATED;
928 if (chan->binarymodes & CM_MODERATED) strlcat(outlist,"m",MAXBUF);
929 chan->binarymodes &= ~CM_MODERATED;
936 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 's', "", mdir, 0));
941 if (!(chan->binarymodes & CM_SECRET)) strlcat(outlist,"s",MAXBUF);
942 chan->binarymodes |= CM_SECRET;
943 if (chan->binarymodes & CM_PRIVATE)
945 chan->binarymodes &= ~CM_PRIVATE;
948 strlcat(outlist,"-p+",MAXBUF);
954 if (chan->binarymodes & CM_SECRET) strlcat(outlist,"s",MAXBUF);
955 chan->binarymodes &= ~CM_SECRET;
962 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'p', "", mdir, 0));
967 if (!(chan->binarymodes & CM_PRIVATE)) strlcat(outlist,"p",MAXBUF);
968 chan->binarymodes |= CM_PRIVATE;
969 if (chan->binarymodes & CM_SECRET)
971 chan->binarymodes &= ~CM_SECRET;
974 strlcat(outlist,"-s+",MAXBUF);
980 if (chan->binarymodes & CM_PRIVATE) strlcat(outlist,"p",MAXBUF);
981 chan->binarymodes &= ~CM_PRIVATE;
987 log(DEBUG,"Preprocessing custom mode %c: modelist: %s",*modechar,chan->custom_modes);
990 if (((!strchr(chan->custom_modes,*modechar)) && (!mdir)) || ((strchr(chan->custom_modes,*modechar)) && (mdir)))
992 if (!ModeIsListMode(*modechar,MT_CHANNEL))
994 log(DEBUG,"Mode %c isnt set on %s but trying to remove!",*modechar,chan->name);
998 if (ModeDefined(*modechar,MT_CHANNEL))
1000 log(DEBUG,"A module has claimed this mode");
1003 if ((ModeDefinedOn(*modechar,MT_CHANNEL)>0) && (mdir))
1005 p.push_back(parameters[param]);
1007 if ((ModeDefinedOff(*modechar,MT_CHANNEL)>0) && (!mdir))
1009 p.push_back(parameters[param]);
1012 bool handled = false;
1015 // we're supposed to have a parameter, but none was given... so dont handle the mode.
1016 if (((ModeDefinedOn(*modechar,MT_CHANNEL)>0) && (mdir)) || ((ModeDefinedOff(*modechar,MT_CHANNEL)>0) && (!mdir)))
1018 log(DEBUG,"Not enough parameters for module-mode %c",*modechar);
1024 // BIG ASS IDIOTIC CODER WARNING!
1025 // Using OnRawMode on another modules mode's behavour
1026 // will confuse the crap out of admins! just because you CAN
1027 // do it, doesnt mean you SHOULD!
1029 std::string para = "";
1032 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, *modechar, para, mdir, pcnt));
1035 for (int i = 0; i <= MODCOUNT; i++)
1039 int t = modules[i]->OnExtendedMode(user,chan,*modechar,MT_CHANNEL,mdir,p);
1042 log(DEBUG,"OnExtendedMode returned nonzero for a module");
1043 char app[] = {*modechar, 0};
1044 if (ModeIsListMode(*modechar,MT_CHANNEL))
1055 strlcat(outlist, app,MAXBUF);
1057 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
1064 if ((modelist[ptr-1] == '+') || (modelist[ptr-1] == '-'))
1066 strlcat(outlist, app,MAXBUF);
1068 else if (!strchr(outlist,*modechar))
1070 strlcat(outlist, app,MAXBUF);
1073 chan->SetCustomMode(*modechar,mdir);
1074 // include parameters in output if mode has them
1075 if ((ModeDefinedOn(*modechar,MT_CHANNEL)>0) && (mdir))
1077 chan->SetCustomModeParam(modelist[ptr],parameters[param],mdir);
1078 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
1081 // break, because only one module can handle the mode.
1090 WriteServ(user->fd,"472 %s %c :is unknown mode char to me",user->nick,*modechar);
1098 /* this ensures only the *valid* modes are sent out onto the network */
1099 int xt = strlen(outlist)-1;
1100 while ((outlist[xt] == '-') || (outlist[xt] == '+'))
1103 xt = strlen(outlist)-1;
1107 strlcpy(outstr,outlist,MAXBUF);
1108 for (ptr = 0; ptr < pc; ptr++)
1110 strlcat(outstr," ",MAXBUF);
1111 strlcat(outstr,outpars[ptr],MAXBUF);
1115 log(DEBUG,"Local mode change");
1116 WriteChannelLocal(chan, user, "MODE %s %s",chan->name,outstr);
1117 FOREACH_MOD(I_OnMode,OnMode(user, chan, TYPE_CHANNEL, outstr));
1125 WriteChannelWithServ(Config->ServerName,chan,"MODE %s %s",chan->name,outstr);
1133 WriteChannel(chan,user,"MODE %s %s",chan->name,outstr);
1134 FOREACH_MOD(I_OnMode,OnMode(user, chan, TYPE_CHANNEL, outstr));
1141 // based on sourcemodes, return true or false to determine if umode is a valid mode a user may set on themselves or others.
1143 bool ModeParser::AllowedUmode(char umode, char* sourcemodes,bool adding,bool serveroverride)
1145 log(DEBUG,"Allowed_umode: %c %s",umode,sourcemodes);
1146 // Servers can +o and -o arbitrarily
1147 if ((serveroverride == true) && (umode == 'o'))
1151 // RFC1459 specified modes
1152 if ((umode == 'w') || (umode == 's') || (umode == 'i'))
1154 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1158 // user may not +o themselves or others, but an oper may de-oper other opers or themselves
1159 if ((strchr(sourcemodes,'o')) && (!adding))
1161 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1164 else if (umode == 'o')
1166 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1170 // process any module-defined modes that need oper
1171 if ((ModeDefinedOper(umode,MT_CLIENT)) && (strchr(sourcemodes,'o')))
1173 log(DEBUG,"umode %c allowed by module handler (oper only mode)",umode);
1177 if (ModeDefined(umode,MT_CLIENT))
1179 // process any module-defined modes that don't need oper
1180 log(DEBUG,"umode %c allowed by module handler (non-oper mode)",umode);
1181 if ((ModeDefinedOper(umode,MT_CLIENT)) && (!strchr(sourcemodes,'o')))
1183 // no, this mode needs oper, and this user 'aint got what it takes!
1189 // anything else - return false.
1190 log(DEBUG,"umode %c not known by any ruleset",umode);
1194 bool ModeParser::ProcessModuleUmode(char umode, userrec* source, void* dest, bool adding)
1201 strlcpy(s2->nick,Config->ServerName,NICKMAX);
1202 strlcpy(s2->modes,"o",52);
1209 if (ModeDefined(umode,MT_CLIENT))
1211 for (int i = 0; i <= MODCOUNT; i++)
1213 if (modules[i]->OnExtendedMode(source,(void*)dest,umode,MT_CLIENT,adding,p))
1215 log(DEBUG,"Module %s claims umode %c",Config->module_names[i].c_str(),umode);
1219 log(DEBUG,"No module claims umode %c",umode);
1238 void cmd_mode::Handle (char **parameters, int pcnt, userrec *user)
1244 char outpars[MAXBUF];
1246 dest = Find(parameters[0]);
1253 if ((dest) && (pcnt == 1))
1255 WriteServ(user->fd,"221 %s :+%s",dest->nick,dest->modes);
1259 if ((dest) && (pcnt > 1))
1261 std::string tidied = ServerInstance->ModeGrok->CompressModes(parameters[1],false);
1262 parameters[1] = (char*)tidied.c_str();
1264 char dmodes[MAXBUF];
1265 strlcpy(dmodes,dest->modes,52);
1266 log(DEBUG,"pulled up dest user modes: %s",dmodes);
1271 if ((strchr(user->modes,'o')) || (is_uline(user->server)))
1282 WriteServ(user->fd,"482 %s :Can't change mode for other users",user->nick);
1290 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1293 for (unsigned int i = 0; i < strlen(parameters[1]); i++)
1295 if (parameters[1][i] == ' ')
1297 if (parameters[1][i] == '+')
1301 int t = strlen(outpars)-1;
1302 if ((outpars[t] == '+') || (outpars[t] == '-'))
1308 strcat(outpars,"+");
1314 if (parameters[1][i] == '-')
1318 int t = strlen(outpars)-1;
1319 if ((outpars[t] == '+') || (outpars[t] == '-'))
1325 strcat(outpars,"-");
1333 if (strchr(user->modes,'o'))
1339 if ((parameters[1][i] == 'i') || (parameters[1][i] == 'w') || (parameters[1][i] == 's') || (ServerInstance->ModeGrok->AllowedUmode(parameters[1][i],user->modes,direction,false)))
1348 if ((!strchr(dmodes,parameters[1][i])) && (ServerInstance->ModeGrok->AllowedUmode(parameters[1][i],user->modes,true,false)))
1350 char umode = parameters[1][i];
1351 if ((ServerInstance->ModeGrok->ProcessModuleUmode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1353 int q = strlen(dmodes);
1354 int r = strlen(outpars);
1356 dmodes[q] = parameters[1][i];
1358 outpars[r] = parameters[1][i];
1359 if (parameters[1][i] == 'o')
1361 FOREACH_MOD(I_OnGlobalOper,OnGlobalOper(dest));
1368 if ((ServerInstance->ModeGrok->AllowedUmode(parameters[1][i],user->modes,false,false)) && (strchr(dmodes,parameters[1][i])))
1370 char umode = parameters[1][i];
1371 if ((ServerInstance->ModeGrok->ProcessModuleUmode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1377 unsigned int r = strlen(outpars);
1379 outpars[r] = parameters[1][i];
1382 for (q = 0; q < strlen(dmodes); q++)
1384 if (dmodes[q] != parameters[1][i])
1388 strlcat(temp,moo,MAXBUF);
1391 strlcpy(dmodes,temp,52);
1407 strlcpy(b,"",MAXBUF);
1410 while (i < strlen (outpars))
1412 b[z++] = outpars[i++];
1414 if (i<strlen(outpars)-1)
1416 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1418 // someones playing silly buggers and trying
1419 // to put a +- or -+ into the line...
1423 if (i == strlen(outpars)-1)
1425 if ((outpars[i] == '-') || (outpars[i] == '+'))
1433 if ((b[z] == '-') || (b[z] == '+'))
1436 if ((!b[0]) || (!strcmp(b,"+")) || (!strcmp(b,"-")))
1441 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1442 FOREACH_MOD(I_OnMode,OnMode(user, dest, TYPE_USER, b));
1445 if (strlen(dmodes)>MAXMODES)
1447 dmodes[MAXMODES-1] = '\0';
1449 log(DEBUG,"Stripped mode line");
1450 log(DEBUG,"Line dest is now %s",dmodes);
1451 strlcpy(dest->modes,dmodes,52);
1458 Ptr = FindChan(parameters[0]);
1463 /* just /modes #channel */
1464 WriteServ(user->fd,"324 %s %s +%s",user->nick, Ptr->name, chanmodes(Ptr,has_channel(user,Ptr)));
1465 WriteServ(user->fd,"329 %s %s %d", user->nick, Ptr->name, Ptr->created);
1471 char* mode = parameters[1];
1475 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, Ptr, *mode, "", false, 0));
1481 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
1483 WriteServ(user->fd,"367 %s %s %s %s %d",user->nick, Ptr->name, i->data, i->set_by, i->set_time);
1485 WriteServ(user->fd,"368 %s %s :End of channel ban list",user->nick, Ptr->name);
1488 if ((ModeDefined(*mode,MT_CHANNEL)) && (ModeIsListMode(*mode,MT_CHANNEL)))
1490 // list of items for an extmode
1491 log(DEBUG,"Calling OnSendList for all modules, list output for mode %c",*mode);
1492 FOREACH_MOD(I_OnSendList,OnSendList(user,Ptr,*mode));
1498 if (((Ptr) && (!has_channel(user,Ptr))) && (!is_uline(user->server)) && (IS_LOCAL(user)))
1500 WriteServ(user->fd,"442 %s %s :You're not on that channel!",user->nick, Ptr->name);
1507 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,NULL,Ptr,AC_GENERAL_MODE));
1509 if (MOD_RESULT == ACR_DENY)
1511 if (MOD_RESULT == ACR_DEFAULT)
1513 if ((cstatus(user,Ptr) < STATUS_HOP) && (IS_LOCAL(user)))
1515 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, Ptr->name);
1520 ServerInstance->ModeGrok->ProcessModes(parameters,user,Ptr,cstatus(user,Ptr),pcnt,false,false,false);
1525 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
1532 void ModeParser::ServerMode(char **parameters, int pcnt, userrec *user)
1538 char outpars[MAXBUF];
1540 dest = Find(parameters[0]);
1542 // fix: ChroNiCk found this - we cant use this as debug if its null!
1545 log(DEBUG,"server_mode on %s",dest->nick);
1548 if ((dest) && (pcnt > 1))
1550 std::string tidied = ServerInstance->ModeGrok->CompressModes(parameters[1],false);
1551 parameters[1] = (char*)tidied.c_str();
1553 char dmodes[MAXBUF];
1554 strlcpy(dmodes,dest->modes,52);
1560 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1563 for (unsigned int i = 0; i < strlen(parameters[1]); i++)
1565 if (parameters[1][i] == ' ')
1567 if (parameters[1][i] == '+')
1571 int t = strlen(outpars)-1;
1572 if ((outpars[t] == '+') || (outpars[t] == '-'))
1578 strcat(outpars,"+");
1584 if (parameters[1][i] == '-')
1588 int t = strlen(outpars)-1;
1589 if ((outpars[t] == '+') || (outpars[t] == '-'))
1595 strcat(outpars,"-");
1602 log(DEBUG,"begin mode processing entry");
1608 log(DEBUG,"umode %c being added",parameters[1][i]);
1609 if ((!strchr(dmodes,parameters[1][i])) && (ServerInstance->ModeGrok->AllowedUmode(parameters[1][i],user->modes,true,true)))
1611 char umode = parameters[1][i];
1612 log(DEBUG,"umode %c is an allowed umode",umode);
1613 if ((ServerInstance->ModeGrok->ProcessModuleUmode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1615 int v1 = strlen(dmodes);
1616 int v2 = strlen(outpars);
1618 dmodes[v1] = parameters[1][i];
1620 outpars[v2] = parameters[1][i];
1626 // can only remove a mode they already have
1627 log(DEBUG,"umode %c being removed",parameters[1][i]);
1628 if ((ServerInstance->ModeGrok->AllowedUmode(parameters[1][i],user->modes,false,true)) && (strchr(dmodes,parameters[1][i])))
1630 char umode = parameters[1][i];
1631 log(DEBUG,"umode %c is an allowed umode",umode);
1632 if ((ServerInstance->ModeGrok->ProcessModuleUmode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1638 unsigned int v1 = strlen(outpars);
1640 outpars[v1] = parameters[1][i];
1642 for (q = 0; q < strlen(dmodes); q++)
1644 if (dmodes[q] != parameters[1][i])
1648 strlcat(temp,moo,MAXBUF);
1651 strlcpy(dmodes,temp,52);
1661 strlcpy(b,"",MAXBUF);
1664 while (i < strlen (outpars))
1666 b[z++] = outpars[i++];
1668 if (i<strlen(outpars)-1)
1670 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1672 // someones playing silly buggers and trying
1673 // to put a +- or -+ into the line...
1677 if (i == strlen(outpars)-1)
1679 if ((outpars[i] == '-') || (outpars[i] == '+'))
1687 if ((b[z] == '-') || (b[z] == '+'))
1690 if ((!b[0]) || (!strcmp(b,"+")) || (!strcmp(b,"-")))
1695 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1696 FOREACH_MOD(I_OnMode,OnMode(user, dest, TYPE_USER, b));
1699 if (strlen(dmodes)>MAXMODES)
1701 dmodes[MAXMODES-1] = '\0';
1703 log(DEBUG,"Stripped mode line");
1704 log(DEBUG,"Line dest is now %s",dmodes);
1705 strlcpy(dest->modes,dmodes,MAXMODES);
1712 Ptr = FindChan(parameters[0]);
1715 ServerInstance->ModeGrok->ProcessModes(parameters,user,Ptr,STATUS_OP,pcnt,true,false,false);
1719 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);