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 "configreader.h"
23 #include <sys/errno.h>
27 #include <ext/hash_map>
35 #include "connection.h"
45 #include "inspstring.h"
46 #include "helperfuncs.h"
49 #include "modes/cmode_s.h"
52 extern std::vector<Module*> modules;
53 extern std::vector<ircd_module*> factory;
54 extern InspIRCd* ServerInstance;
55 extern ServerConfig* Config;
59 ModeHandler::ModeHandler(char modeletter, int parameters_on, int parameters_off, bool listmode, ModeType type, bool operonly) : mode(modeletter), n_params_on(parameters_on), n_params_off(parameters_off), list(listmode), m_type(type), oper(operonly)
63 ModeHandler::~ModeHandler()
67 bool ModeHandler::IsListMode()
72 ModeType ModeHandler::GetModeType()
77 bool ModeHandler::NeedsOper()
82 int ModeHandler::GetNumParams(bool adding)
84 return adding ? n_params_on : n_params_off;
87 char ModeHandler::GetModeChar()
92 ModeAction ModeHandler::OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
94 return MODEACTION_DENY;
97 void ModeHandler::DisplayList(userrec* user, chanrec* channel)
101 bool ModeHandler::CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, chanrec* channel)
103 return (ours < theirs);
106 ModeWatcher::ModeWatcher(char modeletter, ModeType type) : mode(modeletter), m_type(type)
110 ModeWatcher::~ModeWatcher()
114 char ModeWatcher::GetModeChar()
119 ModeType ModeWatcher::GetModeType()
124 bool ModeWatcher::BeforeMode(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding, ModeType type)
129 void ModeWatcher::AfterMode(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter, bool adding, ModeType type)
133 userrec* ModeParser::SanityChecks(userrec *user,char *dest,chanrec *chan,int status)
136 if ((!user) || (!dest) || (!chan) || (!*dest))
143 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
149 char* ModeParser::Grant(userrec *d,chanrec *chan,int MASK)
154 for (std::vector<ucrec*>::const_iterator i = d->chans.begin(); i != d->chans.end(); i++)
156 if (((ucrec*)(*i))->channel == chan)
158 if (((ucrec*)(*i))->uc_modes & MASK)
162 ((ucrec*)(*i))->uc_modes = ((ucrec*)(*i))->uc_modes | MASK;
166 ((ucrec*)(*i))->channel->AddOppedUser(d);
169 ((ucrec*)(*i))->channel->AddHalfoppedUser(d);
172 ((ucrec*)(*i))->channel->AddVoicedUser(d);
175 log(DEBUG,"grant: %s %s",((ucrec*)(*i))->channel->name,d->nick);
182 char* ModeParser::Revoke(userrec *d,chanrec *chan,int MASK)
187 for (std::vector<ucrec*>::const_iterator i = d->chans.begin(); i != d->chans.end(); i++)
189 if (((ucrec*)(*i))->channel == chan)
191 if ((((ucrec*)(*i))->uc_modes & MASK) == 0)
195 ((ucrec*)(*i))->uc_modes ^= MASK;
199 ((ucrec*)(*i))->channel->DelOppedUser(d);
202 ((ucrec*)(*i))->channel->DelHalfoppedUser(d);
205 ((ucrec*)(*i))->channel->DelVoicedUser(d);
208 log(DEBUG,"revoke: %s %s",((ucrec*)(*i))->channel->name,d->nick);
215 char* ModeParser::GiveOps(userrec *user,char *dest,chanrec *chan,int status)
217 userrec *d = this->SanityChecks(user,dest,chan,status);
224 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_OP));
226 if (MOD_RESULT == ACR_DENY)
228 if (MOD_RESULT == ACR_DEFAULT)
230 if ((status < STATUS_OP) && (!is_uline(user->server)))
232 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
238 return this->Grant(d,chan,UCMODE_OP);
243 char* ModeParser::GiveHops(userrec *user,char *dest,chanrec *chan,int status)
245 userrec *d = this->SanityChecks(user,dest,chan,status);
252 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_HALFOP));
254 if (MOD_RESULT == ACR_DENY)
256 if (MOD_RESULT == ACR_DEFAULT)
258 if ((status < STATUS_OP) && (!is_uline(user->server)))
260 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
266 return this->Grant(d,chan,UCMODE_HOP);
271 char* ModeParser::GiveVoice(userrec *user,char *dest,chanrec *chan,int status)
273 userrec *d = this->SanityChecks(user,dest,chan,status);
280 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_VOICE));
282 if (MOD_RESULT == ACR_DENY)
284 if (MOD_RESULT == ACR_DEFAULT)
286 if ((status < STATUS_HOP) && (!is_uline(user->server)))
288 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
294 return this->Grant(d,chan,UCMODE_VOICE);
299 char* ModeParser::TakeOps(userrec *user,char *dest,chanrec *chan,int status)
301 userrec *d = this->SanityChecks(user,dest,chan,status);
308 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_DEOP));
310 if (MOD_RESULT == ACR_DENY)
312 if (MOD_RESULT == ACR_DEFAULT)
314 if ((status < STATUS_OP) && (!is_uline(user->server)) && (IS_LOCAL(user)))
316 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
322 return this->Revoke(d,chan,UCMODE_OP);
327 char* ModeParser::TakeHops(userrec *user,char *dest,chanrec *chan,int status)
329 userrec *d = this->SanityChecks(user,dest,chan,status);
336 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_DEHALFOP));
338 if (MOD_RESULT == ACR_DENY)
340 if (MOD_RESULT == ACR_DEFAULT)
342 /* Tweak by Brain suggested by w00t, allow a halfop to dehalfop themselves */
343 if ((user != d) && ((status < STATUS_OP) && (!is_uline(user->server))))
345 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
351 return this->Revoke(d,chan,UCMODE_HOP);
356 char* ModeParser::TakeVoice(userrec *user,char *dest,chanrec *chan,int status)
358 userrec *d = this->SanityChecks(user,dest,chan,status);
365 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_DEVOICE));
367 if (MOD_RESULT == ACR_DENY)
369 if (MOD_RESULT == ACR_DEFAULT)
371 if ((status < STATUS_HOP) && (!is_uline(user->server)))
373 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
379 return this->Revoke(d,chan,UCMODE_VOICE);
384 char* ModeParser::AddBan(userrec *user,char *dest,chanrec *chan,int status)
387 int toomanyexclamation = 0;
390 if ((!user) || (!dest) || (!chan) || (!*dest))
392 log(DEFAULT,"*** BUG *** AddBan was given an invalid parameter");
396 for (char* i = dest; *i; i++)
398 if ((*i < 32) || (*i > 126))
404 toomanyexclamation++;
412 if (toomanyexclamation != 1 || toomanyat != 1)
414 * this stops sillyness like n!u!u!u@h, though note that most
415 * ircds don't actually verify banmask validity. --w00t
419 long maxbans = GetMaxBans(chan->name);
420 if ((unsigned)chan->bans.size() > (unsigned)maxbans)
422 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);
426 log(DEBUG,"AddBan: %s %s",chan->name,user->nick);
429 FOREACH_RESULT(I_OnAddBan,OnAddBan(user,chan,dest));
434 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
436 if (!strcasecmp(i->data,dest))
438 // dont allow a user to set the same ban twice
444 strlcpy(b.data,dest,MAXBUF);
447 strlcpy(b.set_by,user->nick,NICKMAX-1);
451 strlcpy(b.set_by,Config->ServerName,NICKMAX-1);
453 chan->bans.push_back(b);
457 char* ModeParser::TakeBan(userrec *user,char *dest,chanrec *chan,int status)
459 if ((!user) || (!dest) || (!chan) || (!*dest)) {
460 log(DEFAULT,"*** BUG *** TakeBan was given an invalid parameter");
464 log(DEBUG,"del_ban: %s %s",chan->name,user->nick);
465 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
467 if (!strcasecmp(i->data,dest))
470 FOREACH_RESULT(I_OnDelBan,OnDelBan(user,chan,dest));
480 void ModeParser::Process(char **parameters, int pcnt, userrec *user, bool servermode)
482 std::string target = parameters[0];
483 ModeType type = MODETYPE_USER;
484 chanrec* targetchannel = FindChan(parameters[0]);
485 userrec* targetuser = Find(parameters[0]);
491 type = MODETYPE_CHANNEL;
495 type = MODETYPE_USER;
499 /* No such nick/channel */
502 std::string mode_sequence = parameters[1];
503 std::string parameter = "";
504 std::ostringstream parameter_list;
505 std::string output_sequence = "";
506 bool adding = true, state_change = false;
508 int parameter_counter = 2; /* Index of first parameter */
510 for (std::string::const_iterator modeletter = mode_sequence.begin(); modeletter != mode_sequence.end(); modeletter++)
515 * For + and - mode characters, we don't just stick the character into the output sequence.
516 * This is because the user may do something dumb, like: +-+ooo or +oo-+. To prevent this
517 * appearing in the output sequence, we store a flag which says there was a state change,
518 * which is set on any + or -, however, the + or - that we finish on is only appended to
519 * the output stream in the event it is followed by a non "+ or -" character, such as o or v.
522 /* The following expression prevents: +o+o nick nick, compressing it to +oo nick nick,
523 * however, will allow the + if it is the first item in the sequence, regardless.
525 if ((!adding) || (!output_sequence.length()))
531 if ((adding) || (!output_sequence.length()))
538 /* 65 is the ascii value of 'A' */
539 handler_id = *modeletter - 65;
541 if (modehandlers[handler_id])
544 for (ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++)
546 if ((*watchers)->BeforeMode(user, targetuser, targetchannel, parameter, adding, type) == MODEACTION_DENY)
549 if ((modehandlers[handler_id]->GetModeType() == type) && (!abort))
551 if (modehandlers[handler_id]->GetNumParams(adding))
553 if (pcnt < parameter_counter)
555 parameter = parameters[parameter_counter++];
562 ModeAction ma = modehandlers[handler_id]->OnModeChange(user, targetuser, targetchannel, parameter, adding);
563 if (ma == MODEACTION_ALLOW)
565 /* We're about to output a valid mode letter - was there previously a pending state-change? */
567 output_sequence.append(adding ? "+" : "-");
569 /* Add the mode letter */
570 output_sequence = output_sequence + *modeletter;
572 /* Is there a valid parameter for this mode? If so add it to the parameter list */
573 if ((modehandlers[handler_id]->GetNumParams(adding)) && (parameter != ""))
575 parameter_list << " " << parameter;
578 /* Call all the AfterMode events in the mode watchers for this mode */
579 for (ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++)
581 (*watchers)->AfterMode(user, targetuser, targetchannel, parameter, adding, type);
584 /* Reset the state change flag */
585 state_change = false;
592 /* Was there at least one valid mode in the sequence? */
593 if (output_sequence != "")
597 if (type == MODETYPE_CHANNEL)
599 WriteChannelWithServ(Config->ServerName,targetchannel,"MODE %s %s%s",targetchannel->name,output_sequence.c_str(),parameter_list.str().c_str());
604 if (type == MODETYPE_CHANNEL)
606 WriteChannel(targetchannel,user,"MODE %s %s%s",targetchannel->name,output_sequence.c_str(),parameter_list.str().c_str());
607 FOREACH_MOD(I_OnMode,OnMode(user, targetchannel, TYPE_CHANNEL, output_sequence + parameter_list.str()));
614 std::string ModeParser::CompressModes(std::string modes,bool channelmodes)
619 void ModeParser::ProcessModes(char **parameters,userrec* user,chanrec *chan,int status, int pcnt, bool servermode, bool silent, bool local)
621 if ((!parameters) || (pcnt < 2)) {
625 char outlist[MAXBUF];
633 bool k_set = false, l_set = false, previously_set_l = false, previously_unset_l = false, previously_set_k = false, previously_unset_k = false;
639 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,NULL,chan,AC_GENERAL_MODE));
640 if (MOD_RESULT == ACR_DENY)
644 std::string tidied = this->CompressModes(parameters[1],true);
645 strlcpy(mlist,tidied.c_str(),MAXBUF);
646 char* modelist = mlist;
648 *outlist = *modelist;
649 char* outl = outlist+1;
651 mdir = (*modelist == '+');
653 log(DEBUG,"process_modes: modelist: %s",modelist);
655 int len = tidied.length();
656 while (modelist[len-1] == ' ')
657 modelist[--len] = '\0';
659 for (char* modechar = (modelist + 1); *modechar; ptr++, modechar++)
663 /* If we have more than MAXMODES changes in one line,
664 * drop all after the MAXMODES
685 if ((param >= pcnt)) break;
686 log(DEBUG,"Enough parameters left");
691 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'o', parameters[param], true, 1));
694 r = GiveOps(user,parameters[param++],chan,status);
701 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'o', parameters[param], false, 1));
704 r = TakeOps(user,parameters[param++],chan,status);
716 if (((param >= pcnt)) || (!Config->AllowHalfop)) break;
721 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'h', parameters[param], true, 1));
724 r = GiveHops(user,parameters[param++],chan,status);
731 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'h', parameters[param], false, 1));
734 r = TakeHops(user,parameters[param++],chan,status);
747 if ((param >= pcnt)) break;
752 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'v', parameters[param], true, 1));
755 r = GiveVoice(user,parameters[param++],chan,status);
762 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'v', parameters[param], false, 1));
765 r = TakeVoice(user,parameters[param++],chan,status);
777 if ((param >= pcnt)) break;
782 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'b', parameters[param], true, 1));
785 r = AddBan(user,parameters[param++],chan,status);
792 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'b', parameters[param], false, 1));
795 r = TakeBan(user,parameters[param++],chan,status);
802 outpars[pc++] = parameters[param-1];
816 if (previously_unset_k)
818 previously_set_k = true;
820 if (!chan->modes[CM_KEY])
823 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'k', parameters[param], true, 1));
828 strlcpy(key,parameters[param++],32);
830 strlcpy(chan->key,key,MAXBUF);
831 chan->modes[CM_KEY] = 1;
839 /* checks on -k are case sensitive and only accurate to the
840 first 32 characters */
841 if (previously_set_k)
843 previously_unset_k = true;
847 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'k', parameters[param], false, 1));
850 strlcpy(key,parameters[param++],32);
851 /* only allow -k if correct key given */
852 if (!strcmp(chan->key,key))
856 chan->modes[CM_KEY] = 0;
867 if (previously_set_l)
869 previously_unset_l = true;
871 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'l', "", false, 0));
874 if (chan->modes[CM_LIMIT])
878 chan->modes[CM_LIMIT] = 0;
883 if ((param >= pcnt)) break;
888 if (previously_unset_l)
890 previously_set_l = true;
891 bool invalid = false;
892 for (char* f = parameters[param]; *f; f++)
894 if ((*f < '0') || (*f > '9'))
899 /* If the limit is < 1, or the new limit is the current limit, dont allow */
900 if ((atoi(parameters[param]) < 1) || ((chan->limit > 0) && (atoi(parameters[param]) == chan->limit)))
909 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'l', parameters[param], true, 1));
913 chan->limit = atoi(parameters[param]);
915 // reported by mech: large values cause underflow
917 chan->limit = 0x7FFF;
923 chan->modes[CM_LIMIT] = 1;
924 outpars[pc++] = parameters[param++];
932 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'i', "", mdir, 0));
937 if (!(chan->modes[CM_INVITEONLY])) *outl++ = 'i';
938 chan->modes[CM_INVITEONLY] = 1;
942 if (chan->modes[CM_INVITEONLY]) *outl++ = 'i';
943 chan->modes[CM_INVITEONLY] = 0;
950 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 't', "", mdir, 0));
955 if (!(chan->modes[CM_TOPICLOCK])) *outl++ = 't';
956 chan->modes[CM_TOPICLOCK] = 1;
960 if (chan->modes[CM_TOPICLOCK]) *outl++ = 't';
961 chan->modes[CM_TOPICLOCK] = 0;
968 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'n', "", mdir, 0));
973 if (!(chan->modes[CM_NOEXTERNAL])) *outl++ = 'n';
974 chan->modes[CM_NOEXTERNAL] = 1;
978 if (chan->modes[CM_NOEXTERNAL]) *outl++ = 'n';
979 chan->modes[CM_NOEXTERNAL] = 0;
986 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'm', "", mdir, 0));
991 if (!(chan->modes[CM_MODERATED])) *outl++ = 'm';
992 chan->modes[CM_MODERATED] = 1;
996 if (chan->modes[CM_MODERATED]) *outl++ = 'm';
997 chan->modes[CM_MODERATED] = 0;
1004 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 's', "", mdir, 0));
1009 if (!(chan->modes[CM_SECRET])) *outl++ = 's';
1010 chan->modes[CM_SECRET] = 1;
1011 if (chan->modes[CM_PRIVATE])
1013 chan->modes[CM_PRIVATE] = 0;
1016 *outl++ = '-'; *outl++ = 'p'; *outl++ = '+';
1022 if (chan->modes[CM_SECRET]) *outl++ = 's';
1023 chan->modes[CM_SECRET] = 0;
1030 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'p', "", mdir, 0));
1035 if(!(chan->modes[CM_PRIVATE]))
1038 chan->modes[CM_PRIVATE] = 1;
1040 if(chan->modes[CM_SECRET])
1042 chan->modes[CM_SECRET] = 0;
1051 if(chan->modes[CM_PRIVATE])
1054 chan->modes[CM_PRIVATE] = 0;
1062 bool x = chan->modes[*modechar-65];
1063 if ((!x && !mdir) || (x && mdir))
1065 if (!ModeIsListMode(*modechar,MT_CHANNEL))
1067 log(DEBUG,"Mode %c isnt set on %s but trying to remove!",*modechar,chan->name);
1071 if (ModeDefined(*modechar,MT_CHANNEL))
1073 /* A module has claimed this mode */
1076 if ((ModeDefinedOn(*modechar,MT_CHANNEL)>0) && (mdir))
1078 p.push_back(parameters[param]);
1080 if ((ModeDefinedOff(*modechar,MT_CHANNEL)>0) && (!mdir))
1082 p.push_back(parameters[param]);
1085 bool handled = false;
1088 // we're supposed to have a parameter, but none was given... so dont handle the mode.
1089 if (((ModeDefinedOn(*modechar,MT_CHANNEL)>0) && (mdir)) || ((ModeDefinedOff(*modechar,MT_CHANNEL)>0) && (!mdir)))
1091 log(DEBUG,"Not enough parameters for module-mode %c",*modechar);
1096 // BIG ASS IDIOTIC CODER WARNING!
1097 // Using OnRawMode on another modules mode's behavour
1098 // will confuse the crap out of admins! just because you CAN
1099 // do it, doesnt mean you SHOULD!
1101 std::string para = "";
1105 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, *modechar, para, mdir, pcnt));
1108 for (int i = 0; i <= MODCOUNT; i++)
1112 int t = modules[i]->OnExtendedMode(user,chan,*modechar,MT_CHANNEL,mdir,p);
1115 log(DEBUG,"OnExtendedMode returned nonzero for a module");
1116 if (ModeIsListMode(*modechar,MT_CHANNEL))
1127 *outl++ = *modechar;
1129 outpars[pc++] = parameters[param++];
1134 *outl++ = *modechar;
1135 chan->SetCustomMode(*modechar,mdir);
1136 // include parameters in output if mode has them
1137 if ((ModeDefinedOn(*modechar,MT_CHANNEL)>0) && (mdir))
1141 chan->SetCustomModeParam(*modechar,parameters[param],mdir);
1142 outpars[pc++] = parameters[param++];
1146 // break, because only one module can handle the mode.
1155 WriteServ(user->fd,"472 %s %c :is unknown mode char to me",user->nick,*modechar);
1161 /* Null terminate it now we're done */
1165 /************ Fast, but confusing string tidying ************/
1167 while (*outl && (*outl < 'A'))
1169 /* outl now points to the first mode character after +'s and -'s */
1171 /* Now points at first mode-modifier + or - symbol */
1173 /* Now we tidy off any trailing -'s etc */
1176 while ((*--trim == '+') || (*trim == '-'))
1178 /************ Done wih the string tidy functions ************/
1181 /* The mode change must be at least two characters long (+ or - and at least one mode) */
1182 if (((*outl == '+') || (*outl == '-')) && *(outl+1))
1184 for (ptr = 0; ptr < pc; ptr++)
1186 charlcat(outl,' ',MAXBUF);
1187 strlcat(outl,outpars[ptr],MAXBUF-1);
1191 log(DEBUG,"Local mode change");
1192 WriteChannelLocal(chan, user, "MODE %s %s",chan->name,outl);
1193 FOREACH_MOD(I_OnMode,OnMode(user, chan, TYPE_CHANNEL, outl));
1201 WriteChannelWithServ(Config->ServerName,chan,"MODE %s %s",chan->name,outl);
1209 WriteChannel(chan,user,"MODE %s %s",chan->name,outl);
1210 FOREACH_MOD(I_OnMode,OnMode(user, chan, TYPE_CHANNEL, outl));
1217 // based on sourcemodes, return true or false to determine if umode is a valid mode a user may set on themselves or others.
1219 bool ModeParser::AllowedUmode(char umode, char* sourcemodes,bool adding,bool serveroverride)
1221 bool sourceoper = (strchr(sourcemodes,'o') != NULL);
1222 log(DEBUG,"Allowed_umode: %c %s",umode,sourcemodes);
1223 // Servers can +o and -o arbitrarily
1224 if ((serveroverride == true) && (umode == 'o'))
1228 // RFC1459 specified modes
1229 if ((umode == 'w') || (umode == 's') || (umode == 'i'))
1231 /* umode allowed by RFC1459 scemantics */
1235 /* user may not +o themselves or others, but an oper may de-oper other opers or themselves */
1236 if (sourceoper && !adding)
1240 else if (umode == 'o')
1242 /* Bad oper, bad bad! */
1246 /* process any module-defined modes that need oper */
1247 if ((ModeDefinedOper(umode,MT_CLIENT)) && (sourceoper))
1249 log(DEBUG,"umode %c allowed by module handler (oper only mode)",umode);
1252 else if (ModeDefined(umode,MT_CLIENT))
1254 // process any module-defined modes that don't need oper
1255 log(DEBUG,"umode %c allowed by module handler (non-oper mode)",umode);
1256 if ((ModeDefinedOper(umode,MT_CLIENT)) && (!sourceoper))
1258 // no, this mode needs oper, and this user 'aint got what it takes!
1264 // anything else - return false.
1265 log(DEBUG,"umode %c not known by any ruleset",umode);
1269 bool ModeParser::ProcessModuleUmode(char umode, userrec* source, void* dest, bool adding)
1276 strlcpy(s2->nick,Config->ServerName,NICKMAX-1);
1285 if (ModeDefined(umode,MT_CLIENT))
1287 for (int i = 0; i <= MODCOUNT; i++)
1289 if (modules[i]->OnExtendedMode(source,(void*)dest,umode,MT_CLIENT,adding,p))
1291 log(DEBUG,"Module %s claims umode %c",Config->module_names[i].c_str(),umode);
1295 log(DEBUG,"No module claims umode %c",umode);
1314 void cmd_mode::Handle (char **parameters, int pcnt, userrec *user)
1317 userrec* dest = Find(parameters[0]);
1321 char outpars[MAXBUF];
1322 bool next_ok = true;
1327 if ((dest) && (pcnt == 1))
1329 WriteServ(user->fd,"221 %s :+%s",dest->nick,dest->modes);
1332 else if ((dest) && (pcnt > 1))
1334 std::string tidied = ServerInstance->ModeGrok->CompressModes(parameters[1],false);
1335 parameters[1] = (char*)tidied.c_str();
1337 char dmodes[MAXBUF];
1338 strlcpy(dmodes,dest->modes,MAXMODES);
1339 log(DEBUG,"pulled up dest user modes: %s",dmodes);
1344 if ((*user->oper) || (is_uline(user->server)))
1355 WriteServ(user->fd,"482 %s :Can't change mode for other users",user->nick);
1359 outpars[0] = *parameters[1];
1361 direction = (*parameters[1] == '+');
1363 if ((*parameters[1] != '+') && (*parameters[1] != '-'))
1366 for (char* i = parameters[1]; *i; i++)
1368 if ((i != parameters[1]) && (*i != '+') && (*i != '-'))
1377 if ((direction != 1) && (next_ok))
1379 charlcat(outpars,'+',MAXBUF);
1386 if ((direction != 0) && (next_ok))
1388 charlcat(outpars,'-',MAXBUF);
1402 if ((*i == 'i') || (*i == 'w') || (*i == 's') || (ServerInstance->ModeGrok->AllowedUmode(*i,user->modes,direction,false)))
1411 if ((!strchr(dmodes,*i)) && (ServerInstance->ModeGrok->AllowedUmode(*i,user->modes,true,false)))
1413 if ((ServerInstance->ModeGrok->ProcessModuleUmode(*i, user, dest, direction)) || (*i == 'i') || (*i == 's') || (*i == 'w') || (*i == 'o'))
1415 charlcat(dmodes,*i,53);
1416 charlcat(outpars,*i,MAXMODES);
1420 FOREACH_MOD(I_OnGlobalOper,OnGlobalOper(dest));
1423 dest->modebits |= UM_INVISIBLE;
1426 dest->modebits |= UM_SERVERNOTICE;
1429 dest->modebits |= UM_WALLOPS;
1439 if ((ServerInstance->ModeGrok->AllowedUmode(*i,user->modes,false,false)) && (strchr(dmodes,*i)))
1441 if ((ServerInstance->ModeGrok->ProcessModuleUmode(*i, user, dest, direction)) || (*i == 'i') || (*i == 's') || (*i == 'w') || (*i == 'o'))
1443 charlcat(outpars,*i,MAXMODES);
1444 charremove(dmodes,*i);
1452 dest->modebits &= ~UM_INVISIBLE;
1455 dest->modebits &= ~UM_SERVERNOTICE;
1458 dest->modebits &= ~UM_WALLOPS;
1475 for (char* i = outpars; *i;)
1478 if (((*i == '-') || (*i == '+')) && ((*(i+1) == '-') || (*(i+1) == '+')))
1480 // someones playing silly buggers and trying
1481 // to put a +- or -+ into the line...
1486 // Someone's trying to make the last character in
1487 // the line be a + or - symbol.
1488 if ((*i == '-') || (*i == '+'))
1496 if ((*b) && (!IS_SINGLE(b,'+')) && (!IS_SINGLE(b,'-')))
1498 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1499 FOREACH_MOD(I_OnMode,OnMode(user, dest, TYPE_USER, b));
1502 log(DEBUG,"Stripped mode line");
1503 log(DEBUG,"Line dest is now %s",dmodes);
1504 strlcpy(dest->modes,dmodes,MAXMODES-1);
1512 chan = FindChan(parameters[0]);
1517 /* just /modes #channel */
1518 WriteServ(user->fd,"324 %s %s +%s",user->nick, chan->name, chanmodes(chan, chan->HasUser(user)));
1519 WriteServ(user->fd,"329 %s %s %d", user->nick, chan->name, chan->created);
1524 char* mode = parameters[1];
1531 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, *mode, "", false, 0));
1536 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
1538 WriteServ(user->fd,"367 %s %s %s %s %d",user->nick, chan->name, i->data, i->set_by, i->set_time);
1540 WriteServ(user->fd,"368 %s %s :End of channel ban list",user->nick, chan->name);
1544 if ((ModeDefined(*mode,MT_CHANNEL)) && (ModeIsListMode(*mode,MT_CHANNEL)))
1546 // list of items for an extmode
1547 log(DEBUG,"Calling OnSendList for all modules, list output for mode %c",*mode);
1548 FOREACH_MOD(I_OnSendList,OnSendList(user,chan,*mode));
1554 if ((IS_LOCAL(user)) && (!is_uline(user->server)) && (!chan->HasUser(user)))
1556 WriteServ(user->fd,"442 %s %s :You're not on that channel!",user->nick, chan->name);
1561 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user, NULL, chan, AC_GENERAL_MODE));
1563 if(MOD_RESULT == ACR_DENY)
1566 if(MOD_RESULT == ACR_DEFAULT)
1568 if ((IS_LOCAL(user)) && (cstatus(user,chan) < STATUS_HOP))
1570 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
1575 ServerInstance->ModeGrok->ProcessModes(parameters,user,chan,cstatus(user,chan),pcnt,false,false,false);
1579 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
1587 void ModeParser::ServerMode(char **parameters, int pcnt, userrec *user)
1590 userrec* dest = Find(parameters[0]);
1593 char outpars[MAXBUF];
1594 bool next_ok = true;
1596 if ((dest) && (pcnt > 1))
1598 std::string tidied = ServerInstance->ModeGrok->CompressModes(parameters[1],false);
1599 parameters[1] = (char*)tidied.c_str();
1601 char dmodes[MAXBUF];
1602 strlcpy(dmodes,dest->modes,MAXBUF);
1604 outpars[0] = *parameters[1];
1606 direction = (*parameters[1] == '+');
1608 if ((*parameters[1] != '+') && (*parameters[1] != '-'))
1611 for (char* i = parameters[1]; *i; i++)
1613 if ((i != parameters[1]) && (*i != '+') && (*i != '-'))
1622 if ((direction != 1) && (next_ok))
1625 charlcat(outpars,'+',MAXBUF);
1631 if ((direction != 0) && (next_ok))
1634 charlcat(outpars,'-',MAXBUF);
1640 log(DEBUG,"begin mode processing entry");
1646 log(DEBUG,"umode %c being added",*i);
1647 if ((!strchr(dmodes,*i)) && (ServerInstance->ModeGrok->AllowedUmode(*i,user->modes,true,true)))
1649 log(DEBUG,"umode %c is an allowed umode",*i);
1650 if ((*i == 'i') || (*i == 's') || (*i == 'w') || (*i == 'o') || (ServerInstance->ModeGrok->ProcessModuleUmode(*i, user, dest, direction)))
1652 charlcat(dmodes,*i,MAXBUF);
1653 charlcat(outpars,*i,53);
1657 dest->modebits |= UM_INVISIBLE;
1660 dest->modebits |= UM_SERVERNOTICE;
1663 dest->modebits |= UM_WALLOPS;
1673 // can only remove a mode they already have
1674 log(DEBUG,"umode %c being removed",*i);
1675 if ((ServerInstance->ModeGrok->AllowedUmode(*i,user->modes,false,true)) && (strchr(dmodes,*i)))
1677 log(DEBUG,"umode %c is an allowed umode",*i);
1678 if ((*i == 'i') || (*i == 's') || (*i == 'w') || (*i == 'o') || (ServerInstance->ModeGrok->ProcessModuleUmode(*i, user, dest, direction)))
1680 charlcat(outpars,*i,MAXBUF);
1681 charremove(dmodes,*i);
1685 dest->modebits &= ~UM_INVISIBLE;
1688 dest->modebits &= ~UM_SERVERNOTICE;
1691 dest->modebits &= ~UM_WALLOPS;
1708 for (char* i = outpars; *i;)
1711 if (((*i == '-') || (*i == '+')) && ((*(i+1) == '-') || (*(i+1) == '+')))
1713 // someones playing silly buggers and trying
1714 // to put a +- or -+ into the line...
1719 // Someone's trying to make the last character in
1720 // the line be a + or - symbol.
1721 if ((*i == '-') || (*i == '+'))
1729 if ((*b) && (!IS_SINGLE(b,'+')) && (!IS_SINGLE(b,'-')))
1731 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1732 FOREACH_MOD(I_OnMode,OnMode(user, dest, TYPE_USER, b));
1735 log(DEBUG,"Stripped mode line");
1736 log(DEBUG,"Line dest is now %s",dmodes);
1737 strlcpy(dest->modes,dmodes,MAXMODES-1);
1744 Ptr = FindChan(parameters[0]);
1747 ServerInstance->ModeGrok->ProcessModes(parameters,user,Ptr,STATUS_OP,pcnt,true,false,false);
1751 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
1755 ModeParser::ModeParser()
1757 /* Dummy framework, XXX tidyme */
1758 ModeChannelSecret* cmode_s = new ModeChannelSecret();
1759 modehandlers[(unsigned char)'s'] = cmode_s;