#include "builtinmodes.h"
ModeHandler::ModeHandler(Module* Creator, const std::string& Name, char modeletter, ParamSpec Params, ModeType type, Class mclass)
- : ServiceProvider(Creator, Name, SERVICE_MODE), m_paramtype(TR_TEXT),
+ : ServiceProvider(Creator, Name, SERVICE_MODE), modeid(ModeParser::MODEID_MAX), m_paramtype(TR_TEXT),
parameters_taken(Params), mode(modeletter), oper(false),
list(false), m_type(type), type_id(mclass), levelrequired(HALFOP_VALUE)
{
CullResult ModeHandler::cull()
{
- if (ServerInstance->Modes)
+ if (ServerInstance)
ServerInstance->Modes->DelMode(this);
return classbase::cull();
}
parameter = parameter.substr(0, 250);
ModResult MOD_RESULT;
- FIRST_MOD_RESULT(OnRawMode, MOD_RESULT, (user, chan, modechar, parameter, adding, pcnt));
+ FIRST_MOD_RESULT(OnRawMode, MOD_RESULT, (user, chan, mh, parameter, adding));
if (IS_LOCAL(user) && (MOD_RESULT == MOD_RES_DENY))
return MODEACTION_DENY;
return;
ModResult MOD_RESULT;
- FIRST_MOD_RESULT(OnRawMode, MOD_RESULT, (user, chan, mletter, "", true, 0));
+ FIRST_MOD_RESULT(OnRawMode, MOD_RESULT, (user, chan, mh, "", true));
if (MOD_RESULT == MOD_RES_DENY)
continue;
}
}
-bool ModeParser::AddMode(ModeHandler* mh)
+ModeHandler::Id ModeParser::AllocateModeId(ModeType mt)
+{
+ for (ModeHandler::Id i = 0; i != MODEID_MAX; ++i)
+ {
+ if (!modehandlersbyid[mt][i])
+ return i;
+ }
+
+ throw ModuleException("Out of ModeIds");
+}
+
+void ModeParser::AddMode(ModeHandler* mh)
{
/* Yes, i know, this might let people declare modes like '_' or '^'.
* If they do that, thats their problem, and if i ever EVER see an
* official InspIRCd developer do that, i'll beat them with a paddle!
*/
if ((mh->GetModeChar() < 'A') || (mh->GetModeChar() > 'z'))
- return false;
+ throw ModuleException("Invalid letter for mode " + mh->name);
/* A mode prefix of ',' is not acceptable, it would fuck up server to server.
* A mode prefix of ':' will fuck up both server to server, and client to server.
if (pm)
{
if ((pm->GetPrefix() > 126) || (pm->GetPrefix() == ',') || (pm->GetPrefix() == ':') || (pm->GetPrefix() == '#'))
- return false;
+ throw ModuleException("Invalid prefix for mode " + mh->name);
if (FindPrefix(pm->GetPrefix()))
- return false;
+ throw ModuleException("Prefix already exists for mode " + mh->name);
}
ModeHandler*& slot = modehandlers[mh->GetModeType()][mh->GetModeChar()-65];
if (slot)
- return false;
+ throw ModuleException("Letter is already in use for mode " + mh->name);
+
+ // The mode needs an id if it is either a user mode, a simple mode (flag) or a parameter mode.
+ // Otherwise (for listmodes and prefix modes) the id remains MODEID_MAX, which is invalid.
+ ModeHandler::Id modeid = MODEID_MAX;
+ if ((mh->GetModeType() == MODETYPE_USER) || (mh->IsParameterMode()) || (!mh->IsListMode()))
+ modeid = AllocateModeId(mh->GetModeType());
if (!modehandlersbyname[mh->GetModeType()].insert(std::make_pair(mh->name, mh)).second)
- return false;
+ throw ModuleException("Mode name already in use: " + mh->name);
// Everything is fine, add the mode
+
+ // If we allocated an id for this mode then save it and put the mode handler into the slot
+ if (modeid != MODEID_MAX)
+ {
+ mh->modeid = modeid;
+ modehandlersbyid[mh->GetModeType()][modeid] = mh;
+ }
+
slot = mh;
if (pm)
mhlist.prefix.push_back(pm);
mhlist.list.push_back(mh->IsListModeBase());
RecreateModeListFor004Numeric();
- return true;
}
bool ModeParser::DelMode(ModeHandler* mh)
switch (mh->GetModeType())
{
case MODETYPE_USER:
- for (user_hash::iterator i = ServerInstance->Users->clientlist->begin(); i != ServerInstance->Users->clientlist->end(); )
+ {
+ const user_hash& users = ServerInstance->Users->GetUsers();
+ for (user_hash::const_iterator i = users.begin(); i != users.end(); )
{
User* user = i->second;
++i;
mh->RemoveMode(user);
}
+ }
break;
case MODETYPE_CHANNEL:
- for (chan_hash::iterator i = ServerInstance->chanlist->begin(); i != ServerInstance->chanlist->end(); )
+ {
+ const chan_hash& chans = ServerInstance->GetChans();
+ for (chan_hash::const_iterator i = chans.begin(); i != chans.end(); )
{
// The channel may not be in the hash after RemoveMode(), see m_permchannels
Channel* chan = i->second;
stackresult.erase(stackresult.begin() + 1, stackresult.end());
}
}
+ }
break;
}
mhmap.erase(mhmapit);
+ if (mh->GetId() != MODEID_MAX)
+ modehandlersbyid[mh->GetModeType()][mh->GetId()] = NULL;
slot = NULL;
if (mh->IsPrefixMode())
mhlist.prefix.erase(std::find(mhlist.prefix.begin(), mhlist.prefix.end(), mh->IsPrefixMode()));
void PrefixMode::RemoveMode(Channel* chan, irc::modestacker& stack)
{
- const UserMembList* userlist = chan->GetUsers();
- for (UserMembCIter i = userlist->begin(); i != userlist->end(); ++i)
+ const Channel::MemberMap& userlist = chan->GetUsers();
+ for (Channel::MemberMap::const_iterator i = userlist.begin(); i != userlist.end(); ++i)
{
if (i->second->hasMode(this->GetModeChar()))
stack.Push(this->GetModeChar(), i->first->nick);
ModeChannelOp o;
ModeChannelVoice v;
- ModeUserWallops uw;
ModeUserInvisible ui;
ModeUserOperator uo;
ModeUserServerNoticeMask us;
void init()
{
ServiceProvider* modes[] = { &s, &p, &m, &t, &n, &i, &k, &l, &b, &o, &v,
- &uw, &ui, &uo, &us };
+ &ui, &uo, &us };
ServerInstance->Modules->AddServices(modes, sizeof(modes)/sizeof(ServiceProvider*));
}
};
{
/* Clear mode handler list */
memset(modehandlers, 0, sizeof(modehandlers));
+ memset(modehandlersbyid, 0, sizeof(modehandlersbyid));
seq = 0;
memset(&sent, 0, sizeof(sent));