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

View File

@ -372,25 +372,32 @@ ModResult ModuleFilter::OnUserPreMessage(User* user, const MessageTarget& msgtar
{ {
bool is_selfmsg = false; bool is_selfmsg = false;
std::string target; std::string target;
if (msgtarget.type == MessageTarget::TYPE_USER) switch (msgtarget.type)
{ {
User* t = msgtarget.Get<User>(); case MessageTarget::TYPE_USER:
// Check if the target nick is exempted, if yes, ignore this message {
if (exemptednicks.count(t->nick)) User* t = msgtarget.Get<User>();
return MOD_RES_PASSTHRU; // Check if the target nick is exempted, if yes, ignore this message
if (exemptednicks.count(t->nick))
return MOD_RES_PASSTHRU;
if (user == t) if (user == t)
is_selfmsg = true; is_selfmsg = true;
target = t->nick; target = t->nick;
} break;
else if (msgtarget.type == MessageTarget::TYPE_CHANNEL) }
{ case MessageTarget::TYPE_CHANNEL:
Channel* t = msgtarget.Get<Channel>(); {
if (exemptedchans.count(t->name)) Channel* t = msgtarget.Get<Channel>();
return MOD_RES_PASSTHRU; 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) 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 std::string& text = details.echo_original ? details.original_text : details.text;
const ClientProtocol::TagMap& tags = details.echo_original ? details.tags_in : details.tags_out; 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>(); case MessageTarget::TYPE_USER:
ClientProtocol::Messages::Privmsg privmsg(ClientProtocol::Messages::Privmsg::nocopy, user, destuser, text, details.type); {
privmsg.AddTags(tags); User* destuser = target.Get<User>();
localuser->Send(ServerInstance->GetRFCEvents().privmsg, privmsg); ClientProtocol::Messages::Privmsg privmsg(ClientProtocol::Messages::Privmsg::nocopy, user, destuser, text, details.type);
} privmsg.AddTags(tags);
else if (target.type == MessageTarget::TYPE_CHANNEL) localuser->Send(ServerInstance->GetRFCEvents().privmsg, privmsg);
{ break;
Channel* chan = target.Get<Channel>(); }
ClientProtocol::Messages::Privmsg privmsg(ClientProtocol::Messages::Privmsg::nocopy, user, chan, text, details.type, target.status); case MessageTarget::TYPE_CHANNEL:
privmsg.AddTags(tags); {
localuser->Send(ServerInstance->GetRFCEvents().privmsg, privmsg); Channel* chan = target.Get<Channel>();
} ClientProtocol::Messages::Privmsg privmsg(ClientProtocol::Messages::Privmsg::nocopy, user, chan, text, details.type, target.status);
else privmsg.AddTags(tags);
{ localuser->Send(ServerInstance->GetRFCEvents().privmsg, privmsg);
const std::string* servername = target.Get<std::string>(); break;
ClientProtocol::Messages::Privmsg privmsg(ClientProtocol::Messages::Privmsg::nocopy, user, *servername, text, details.type); }
privmsg.AddTags(tags); case MessageTarget::TYPE_SERVER:
localuser->Send(ServerInstance->GetRFCEvents().privmsg, privmsg); {
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); LocalUser* const localuser = static_cast<LocalUser*>(user);
const ClientProtocol::TagMap& tags = details.echo_original ? details.tags_in : details.tags_out; 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>(); case MessageTarget::TYPE_USER:
CTCTags::TagMessage message(user, destuser, tags); {
localuser->Send(tagmsgprov, message); User* destuser = target.Get<User>();
} CTCTags::TagMessage message(user, destuser, tags);
else if (target.type == MessageTarget::TYPE_CHANNEL) localuser->Send(tagmsgprov, message);
{ break;
Channel* chan = target.Get<Channel>(); }
CTCTags::TagMessage message(user, chan, tags); case MessageTarget::TYPE_CHANNEL:
localuser->Send(tagmsgprov, message); {
} Channel* chan = target.Get<Channel>();
else CTCTags::TagMessage message(user, chan, tags);
{ localuser->Send(tagmsgprov, message);
const std::string* servername = target.Get<std::string>(); break;
CTCTags::TagMessage message(user, servername->c_str(), tags); }
localuser->Send(tagmsgprov, message); 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")) if (!details.IsCTCP(ctcpname) || irc::equals(ctcpname, "ACTION"))
return MOD_RES_PASSTHRU; return MOD_RES_PASSTHRU;
if (target.type == MessageTarget::TYPE_CHANNEL) switch (target.type)
{ {
if (user->HasPrivPermission("channels/ignore-noctcp")) case MessageTarget::TYPE_CHANNEL:
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)))
{ {
user->WriteNumeric(ERR_CANNOTSENDTOCHAN, c->name, "Can't send CTCP to channel (+C is set)"); if (user->HasPrivPermission("channels/ignore-noctcp"))
return MOD_RES_DENY; return MOD_RES_PASSTHRU;
}
}
else if (target.type == MessageTarget::TYPE_USER)
{
if (user->HasPrivPermission("users/ignore-noctcp"))
return MOD_RES_PASSTHRU;
User* u = target.Get<User>(); Channel* c = target.Get<Channel>();
if (u->IsModeSet(ncu)) ModResult res = CheckExemption::Call(exemptionprov, user, c, "noctcp");
{ if (res == MOD_RES_ALLOW)
user->WriteNumeric(ERR_CANTSENDTOUSER, u->nick, "Can't send CTCP to user (+T is set)"); return MOD_RES_PASSTHRU;
return MOD_RES_DENY;
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; return MOD_RES_PASSTHRU;
} }

View File

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

View File

@ -384,32 +384,38 @@ class ModuleSilence
bool is_ctcp = details.IsCTCP(ctcpname) && !irc::equals(ctcpname, "ACTION"); bool is_ctcp = details.IsCTCP(ctcpname) && !irc::equals(ctcpname, "ACTION");
SilenceEntry::SilenceFlags flag = SilenceEntry::SF_NONE; SilenceEntry::SilenceFlags flag = SilenceEntry::SF_NONE;
if (target.type == MessageTarget::TYPE_CHANNEL) switch (target.type)
{ {
if (is_ctcp) case MessageTarget::TYPE_CHANNEL:
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))
{ {
details.echo_original = true; if (is_ctcp)
return MOD_RES_DENY; 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; return MOD_RES_PASSTHRU;

View File

@ -392,30 +392,36 @@ void ModuleSpanningTree::OnUserPostMessage(User* user, const MessageTarget& targ
return; return;
const char* message_type = (details.type == MSG_PRIVMSG ? "PRIVMSG" : "NOTICE"); const char* message_type = (details.type == MSG_PRIVMSG ? "PRIVMSG" : "NOTICE");
if (target.type == MessageTarget::TYPE_USER) switch (target.type)
{ {
User* d = target.Get<User>(); case MessageTarget::TYPE_USER:
if (!IS_LOCAL(d))
{ {
CmdBuilder params(user, message_type); User* d = target.Get<User>();
params.push_tags(details.tags_out); if (!IS_LOCAL(d))
params.push_back(d->uuid); {
params.push_last(details.text); CmdBuilder params(user, message_type);
params.Unicast(d); 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; return MOD_RES_PASSTHRU;
bool active = false; bool active = false;
if (target.type == MessageTarget::TYPE_USER) switch (target.type)
{ {
User* t = target.Get<User>(); case MessageTarget::TYPE_USER:
active = t->IsModeSet(usc); {
} User* t = target.Get<User>();
else if (target.type == MessageTarget::TYPE_CHANNEL) active = t->IsModeSet(usc);
{ break;
Channel* t = target.Get<Channel>(); }
ModResult res = CheckExemption::Call(exemptionprov, user, t, "stripcolor"); case MessageTarget::TYPE_CHANNEL:
{
Channel* t = target.Get<Channel>();
ModResult res = CheckExemption::Call(exemptionprov, user, t, "stripcolor");
if (res == MOD_RES_ALLOW) if (res == MOD_RES_ALLOW)
return MOD_RES_PASSTHRU; 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) if (active)