Replace large if/else blocks for target.type with switches (#1668).

This commit is contained in:
linuxdaemon 2019-06-24 11:10:17 -05:00 committed by Peter Powell
parent 2ab383f707
commit 7ad534c1af
8 changed files with 245 additions and 185 deletions

View File

@ -89,50 +89,58 @@ class ModuleDeaf : public Module
ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
{
if (target.type == MessageTarget::TYPE_CHANNEL)
switch (target.type)
{
Channel* chan = target.Get<Channel>();
bool is_bypasschar = (deaf_bypasschars.find(details.text[0]) != std::string::npos);
bool is_bypasschar_uline = (deaf_bypasschars_uline.find(details.text[0]) != std::string::npos);
// If we have no bypasschars_uline in config, and this is a bypasschar (regular)
// Then it is obviously going to get through +d, no exemption list required
if (deaf_bypasschars_uline.empty() && is_bypasschar)
return MOD_RES_PASSTHRU;
// If it matches both bypasschar and bypasschar_uline, it will get through.
if (is_bypasschar && is_bypasschar_uline)
return MOD_RES_PASSTHRU;
const Channel::MemberMap& ulist = chan->GetUsers();
for (Channel::MemberMap::const_iterator i = ulist.begin(); i != ulist.end(); ++i)
case MessageTarget::TYPE_CHANNEL:
{
// not +d
if (!i->first->IsModeSet(deafmode))
continue;
Channel* chan = target.Get<Channel>();
bool is_bypasschar = (deaf_bypasschars.find(details.text[0]) != std::string::npos);
bool is_bypasschar_uline = (deaf_bypasschars_uline.find(details.text[0]) != std::string::npos);
bool is_a_uline = i->first->server->IsULine();
// matched a U-line only bypass
if (is_bypasschar_uline && is_a_uline)
continue;
// matched a regular bypass
if (is_bypasschar && !is_a_uline)
continue;
// If we have no bypasschars_uline in config, and this is a bypasschar (regular)
// Then it is obviously going to get through +d, no exemption list required
if (deaf_bypasschars_uline.empty() && is_bypasschar)
return MOD_RES_PASSTHRU;
// If it matches both bypasschar and bypasschar_uline, it will get through.
if (is_bypasschar && is_bypasschar_uline)
return MOD_RES_PASSTHRU;
// don't deliver message!
details.exemptions.insert(i->first);
const Channel::MemberMap& ulist = chan->GetUsers();
for (Channel::MemberMap::const_iterator i = ulist.begin(); i != ulist.end(); ++i)
{
// not +d
if (!i->first->IsModeSet(deafmode))
continue;
bool is_a_uline = i->first->server->IsULine();
// matched a U-line only bypass
if (is_bypasschar_uline && is_a_uline)
continue;
// matched a regular bypass
if (is_bypasschar && !is_a_uline)
continue;
// don't deliver message!
details.exemptions.insert(i->first);
}
break;
}
}
else if (target.type == MessageTarget::TYPE_USER)
{
User* targ = target.Get<User>();
if (!targ->IsModeSet(privdeafmode))
return MOD_RES_PASSTHRU;
case MessageTarget::TYPE_USER:
{
User* targ = target.Get<User>();
if (!targ->IsModeSet(privdeafmode))
return MOD_RES_PASSTHRU;
if (!privdeafuline && user->server->IsULine())
return MOD_RES_DENY;
if (!privdeafuline && user->server->IsULine())
return MOD_RES_DENY;
if (!user->HasPrivPermission("users/ignore-privdeaf"))
return MOD_RES_DENY;
if (!user->HasPrivPermission("users/ignore-privdeaf"))
return MOD_RES_DENY;
break;
}
case MessageTarget::TYPE_SERVER:
break;
}
return MOD_RES_PASSTHRU;

View File

@ -372,25 +372,32 @@ ModResult ModuleFilter::OnUserPreMessage(User* user, const MessageTarget& msgtar
{
bool is_selfmsg = false;
std::string target;
if (msgtarget.type == MessageTarget::TYPE_USER)
switch (msgtarget.type)
{
User* t = msgtarget.Get<User>();
// Check if the target nick is exempted, if yes, ignore this message
if (exemptednicks.count(t->nick))
return MOD_RES_PASSTHRU;
case MessageTarget::TYPE_USER:
{
User* t = msgtarget.Get<User>();
// Check if the target nick is exempted, if yes, ignore this message
if (exemptednicks.count(t->nick))
return MOD_RES_PASSTHRU;
if (user == t)
is_selfmsg = true;
if (user == t)
is_selfmsg = true;
target = t->nick;
}
else if (msgtarget.type == MessageTarget::TYPE_CHANNEL)
{
Channel* t = msgtarget.Get<Channel>();
if (exemptedchans.count(t->name))
return MOD_RES_PASSTHRU;
target = t->nick;
break;
}
case MessageTarget::TYPE_CHANNEL:
{
Channel* t = msgtarget.Get<Channel>();
if (exemptedchans.count(t->name))
return MOD_RES_PASSTHRU;
target = t->name;
target = t->name;
break;
}
case MessageTarget::TYPE_SERVER:
break;
}
if (is_selfmsg && warnonselfmsg)

View File

@ -48,26 +48,32 @@ class ModuleIRCv3EchoMessage
const std::string& text = details.echo_original ? details.original_text : details.text;
const ClientProtocol::TagMap& tags = details.echo_original ? details.tags_in : details.tags_out;
if (target.type == MessageTarget::TYPE_USER)
switch (target.type)
{
User* destuser = target.Get<User>();
ClientProtocol::Messages::Privmsg privmsg(ClientProtocol::Messages::Privmsg::nocopy, user, destuser, text, details.type);
privmsg.AddTags(tags);
localuser->Send(ServerInstance->GetRFCEvents().privmsg, privmsg);
}
else if (target.type == MessageTarget::TYPE_CHANNEL)
{
Channel* chan = target.Get<Channel>();
ClientProtocol::Messages::Privmsg privmsg(ClientProtocol::Messages::Privmsg::nocopy, user, chan, text, details.type, target.status);
privmsg.AddTags(tags);
localuser->Send(ServerInstance->GetRFCEvents().privmsg, privmsg);
}
else
{
const std::string* servername = target.Get<std::string>();
ClientProtocol::Messages::Privmsg privmsg(ClientProtocol::Messages::Privmsg::nocopy, user, *servername, text, details.type);
privmsg.AddTags(tags);
localuser->Send(ServerInstance->GetRFCEvents().privmsg, privmsg);
case MessageTarget::TYPE_USER:
{
User* destuser = target.Get<User>();
ClientProtocol::Messages::Privmsg privmsg(ClientProtocol::Messages::Privmsg::nocopy, user, destuser, text, details.type);
privmsg.AddTags(tags);
localuser->Send(ServerInstance->GetRFCEvents().privmsg, privmsg);
break;
}
case MessageTarget::TYPE_CHANNEL:
{
Channel* chan = target.Get<Channel>();
ClientProtocol::Messages::Privmsg privmsg(ClientProtocol::Messages::Privmsg::nocopy, user, chan, text, details.type, target.status);
privmsg.AddTags(tags);
localuser->Send(ServerInstance->GetRFCEvents().privmsg, privmsg);
break;
}
case MessageTarget::TYPE_SERVER:
{
const std::string* servername = target.Get<std::string>();
ClientProtocol::Messages::Privmsg privmsg(ClientProtocol::Messages::Privmsg::nocopy, user, *servername, text, details.type);
privmsg.AddTags(tags);
localuser->Send(ServerInstance->GetRFCEvents().privmsg, privmsg);
break;
}
}
}
@ -80,23 +86,29 @@ class ModuleIRCv3EchoMessage
LocalUser* const localuser = static_cast<LocalUser*>(user);
const ClientProtocol::TagMap& tags = details.echo_original ? details.tags_in : details.tags_out;
if (target.type == MessageTarget::TYPE_USER)
switch (target.type)
{
User* destuser = target.Get<User>();
CTCTags::TagMessage message(user, destuser, tags);
localuser->Send(tagmsgprov, message);
}
else if (target.type == MessageTarget::TYPE_CHANNEL)
{
Channel* chan = target.Get<Channel>();
CTCTags::TagMessage message(user, chan, tags);
localuser->Send(tagmsgprov, message);
}
else
{
const std::string* servername = target.Get<std::string>();
CTCTags::TagMessage message(user, servername->c_str(), tags);
localuser->Send(tagmsgprov, message);
case MessageTarget::TYPE_USER:
{
User* destuser = target.Get<User>();
CTCTags::TagMessage message(user, destuser, tags);
localuser->Send(tagmsgprov, message);
break;
}
case MessageTarget::TYPE_CHANNEL:
{
Channel* chan = target.Get<Channel>();
CTCTags::TagMessage message(user, chan, tags);
localuser->Send(tagmsgprov, message);
break;
}
case MessageTarget::TYPE_SERVER:
{
const std::string* servername = target.Get<std::string>();
CTCTags::TagMessage message(user, servername->c_str(), tags);
localuser->Send(tagmsgprov, message);
break;
}
}
}

View File

@ -61,33 +61,40 @@ class ModuleNoCTCP : public Module
if (!details.IsCTCP(ctcpname) || irc::equals(ctcpname, "ACTION"))
return MOD_RES_PASSTHRU;
if (target.type == MessageTarget::TYPE_CHANNEL)
switch (target.type)
{
if (user->HasPrivPermission("channels/ignore-noctcp"))
return MOD_RES_PASSTHRU;
Channel* c = target.Get<Channel>();
ModResult res = CheckExemption::Call(exemptionprov, user, c, "noctcp");
if (res == MOD_RES_ALLOW)
return MOD_RES_PASSTHRU;
if (!c->GetExtBanStatus(user, 'C').check(!c->IsModeSet(nc)))
case MessageTarget::TYPE_CHANNEL:
{
user->WriteNumeric(ERR_CANNOTSENDTOCHAN, c->name, "Can't send CTCP to channel (+C is set)");
return MOD_RES_DENY;
}
}
else if (target.type == MessageTarget::TYPE_USER)
{
if (user->HasPrivPermission("users/ignore-noctcp"))
return MOD_RES_PASSTHRU;
if (user->HasPrivPermission("channels/ignore-noctcp"))
return MOD_RES_PASSTHRU;
User* u = target.Get<User>();
if (u->IsModeSet(ncu))
{
user->WriteNumeric(ERR_CANTSENDTOUSER, u->nick, "Can't send CTCP to user (+T is set)");
return MOD_RES_DENY;
Channel* c = target.Get<Channel>();
ModResult res = CheckExemption::Call(exemptionprov, user, c, "noctcp");
if (res == MOD_RES_ALLOW)
return MOD_RES_PASSTHRU;
if (!c->GetExtBanStatus(user, 'C').check(!c->IsModeSet(nc)))
{
user->WriteNumeric(ERR_CANNOTSENDTOCHAN, c->name, "Can't send CTCP to channel (+C is set)");
return MOD_RES_DENY;
}
break;
}
case MessageTarget::TYPE_USER:
{
if (user->HasPrivPermission("users/ignore-noctcp"))
return MOD_RES_PASSTHRU;
User* u = target.Get<User>();
if (u->IsModeSet(ncu))
{
user->WriteNumeric(ERR_CANTSENDTOUSER, u->nick, "Can't send CTCP to user (+T is set)");
return MOD_RES_DENY;
}
break;
}
case MessageTarget::TYPE_SERVER:
break;
}
return MOD_RES_PASSTHRU;
}

View File

@ -207,32 +207,39 @@ class ModuleServicesAccount
std::string *account = accountname.get(user);
bool is_registered = account && !account->empty();
if (target.type == MessageTarget::TYPE_CHANNEL)
switch (target.type)
{
Channel* targchan = target.Get<Channel>();
case MessageTarget::TYPE_CHANNEL:
{
Channel* targchan = target.Get<Channel>();
if (!targchan->IsModeSet(m2) || is_registered)
return MOD_RES_PASSTHRU;
if (!targchan->IsModeSet(m2) || is_registered)
return MOD_RES_PASSTHRU;
if (CheckExemption::Call(exemptionprov, user, targchan, "regmoderated") == MOD_RES_ALLOW)
return MOD_RES_PASSTHRU;
if (CheckExemption::Call(exemptionprov, user, targchan, "regmoderated") == MOD_RES_ALLOW)
return MOD_RES_PASSTHRU;
// User is messaging a +M channel and is not registered or exempt.
user->WriteNumeric(ERR_NEEDREGGEDNICK, targchan->name, "You need to be identified to a registered account to message this channel");
return MOD_RES_DENY;
}
else if (target.type == MessageTarget::TYPE_USER)
{
User* targuser = target.Get<User>();
if (!targuser->IsModeSet(m3) || is_registered)
return MOD_RES_PASSTHRU;
// User is messaging a +M channel and is not registered or exempt.
user->WriteNumeric(ERR_NEEDREGGEDNICK, targchan->name, "You need to be identified to a registered account to message this channel");
return MOD_RES_DENY;
break;
}
case MessageTarget::TYPE_USER:
{
User* targuser = target.Get<User>();
if (!targuser->IsModeSet(m3) || is_registered)
return MOD_RES_PASSTHRU;
if (calleridapi && calleridapi->IsOnAcceptList(user, targuser))
return MOD_RES_PASSTHRU;
if (calleridapi && calleridapi->IsOnAcceptList(user, targuser))
return MOD_RES_PASSTHRU;
// User is messaging a +R user and is not registered or on an accept list.
user->WriteNumeric(ERR_NEEDREGGEDNICK, targuser->nick, "You need to be identified to a registered account to message this user");
return MOD_RES_DENY;
// User is messaging a +R user and is not registered or on an accept list.
user->WriteNumeric(ERR_NEEDREGGEDNICK, targuser->nick, "You need to be identified to a registered account to message this user");
return MOD_RES_DENY;
break;
}
case MessageTarget::TYPE_SERVER:
break;
}
return MOD_RES_PASSTHRU;
}

View File

@ -384,32 +384,38 @@ class ModuleSilence
bool is_ctcp = details.IsCTCP(ctcpname) && !irc::equals(ctcpname, "ACTION");
SilenceEntry::SilenceFlags flag = SilenceEntry::SF_NONE;
if (target.type == MessageTarget::TYPE_CHANNEL)
switch (target.type)
{
if (is_ctcp)
flag = SilenceEntry::SF_CTCP_CHANNEL;
else if (details.type == MSG_NOTICE)
flag = SilenceEntry::SF_NOTICE_CHANNEL;
else if (details.type == MSG_PRIVMSG)
flag = SilenceEntry::SF_PRIVMSG_CHANNEL;
return BuildChannelExempts(user, target.Get<Channel>(), flag, details.exemptions);
}
if (target.type == MessageTarget::TYPE_USER)
{
if (is_ctcp)
flag = SilenceEntry::SF_CTCP_USER;
else if (details.type == MSG_NOTICE)
flag = SilenceEntry::SF_NOTICE_USER;
else if (details.type == MSG_PRIVMSG)
flag = SilenceEntry::SF_PRIVMSG_USER;
if (!CanReceiveMessage(user, target.Get<User>(), flag))
case MessageTarget::TYPE_CHANNEL:
{
details.echo_original = true;
return MOD_RES_DENY;
if (is_ctcp)
flag = SilenceEntry::SF_CTCP_CHANNEL;
else if (details.type == MSG_NOTICE)
flag = SilenceEntry::SF_NOTICE_CHANNEL;
else if (details.type == MSG_PRIVMSG)
flag = SilenceEntry::SF_PRIVMSG_CHANNEL;
return BuildChannelExempts(user, target.Get<Channel>(), flag, details.exemptions);
break;
}
case MessageTarget::TYPE_USER:
{
if (is_ctcp)
flag = SilenceEntry::SF_CTCP_USER;
else if (details.type == MSG_NOTICE)
flag = SilenceEntry::SF_NOTICE_USER;
else if (details.type == MSG_PRIVMSG)
flag = SilenceEntry::SF_PRIVMSG_USER;
if (!CanReceiveMessage(user, target.Get<User>(), flag))
{
details.echo_original = true;
return MOD_RES_DENY;
}
break;
}
case MessageTarget::TYPE_SERVER:
break;
}
return MOD_RES_PASSTHRU;

View File

@ -392,30 +392,36 @@ void ModuleSpanningTree::OnUserPostMessage(User* user, const MessageTarget& targ
return;
const char* message_type = (details.type == MSG_PRIVMSG ? "PRIVMSG" : "NOTICE");
if (target.type == MessageTarget::TYPE_USER)
switch (target.type)
{
User* d = target.Get<User>();
if (!IS_LOCAL(d))
case MessageTarget::TYPE_USER:
{
CmdBuilder params(user, message_type);
params.push_tags(details.tags_out);
params.push_back(d->uuid);
params.push_last(details.text);
params.Unicast(d);
User* d = target.Get<User>();
if (!IS_LOCAL(d))
{
CmdBuilder params(user, message_type);
params.push_tags(details.tags_out);
params.push_back(d->uuid);
params.push_last(details.text);
params.Unicast(d);
}
break;
}
case MessageTarget::TYPE_CHANNEL:
{
Utils->SendChannelMessage(user->uuid, target.Get<Channel>(), details.text, target.status, details.tags_out, details.exemptions, message_type);
break;
}
case MessageTarget::TYPE_SERVER:
{
const std::string* serverglob = target.Get<std::string>();
CmdBuilder par(user, message_type);
par.push_tags(details.tags_out);
par.push_back(*serverglob);
par.push_last(details.text);
par.Broadcast();
break;
}
}
else if (target.type == MessageTarget::TYPE_CHANNEL)
{
Utils->SendChannelMessage(user->uuid, target.Get<Channel>(), details.text, target.status, details.tags_out, details.exemptions, message_type);
}
else if (target.type == MessageTarget::TYPE_SERVER)
{
const std::string* serverglob = target.Get<std::string>();
CmdBuilder par(user, message_type);
par.push_tags(details.tags_out);
par.push_back(*serverglob);
par.push_last(details.text);
par.Broadcast();
}
}

View File

@ -47,20 +47,27 @@ class ModuleStripColor : public Module
return MOD_RES_PASSTHRU;
bool active = false;
if (target.type == MessageTarget::TYPE_USER)
switch (target.type)
{
User* t = target.Get<User>();
active = t->IsModeSet(usc);
}
else if (target.type == MessageTarget::TYPE_CHANNEL)
{
Channel* t = target.Get<Channel>();
ModResult res = CheckExemption::Call(exemptionprov, user, t, "stripcolor");
case MessageTarget::TYPE_USER:
{
User* t = target.Get<User>();
active = t->IsModeSet(usc);
break;
}
case MessageTarget::TYPE_CHANNEL:
{
Channel* t = target.Get<Channel>();
ModResult res = CheckExemption::Call(exemptionprov, user, t, "stripcolor");
if (res == MOD_RES_ALLOW)
return MOD_RES_PASSTHRU;
if (res == MOD_RES_ALLOW)
return MOD_RES_PASSTHRU;
active = !t->GetExtBanStatus(user, 'S').check(!t->IsModeSet(csc));
active = !t->GetExtBanStatus(user, 'S').check(!t->IsModeSet(csc));
break;
}
case MessageTarget::TYPE_SERVER:
break;
}
if (active)