2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2019 Peter Powell <petpow@saberuk.com>
5 * Copyright (C) 2016 Attila Molnar <attilamolnar@hush.com>
7 * This file is part of InspIRCd. InspIRCd is free software: you can
8 * redistribute it and/or modify it under the terms of the GNU General Public
9 * License as published by the Free Software Foundation, version 2.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "modules/cap.h"
23 #include "modules/ctctags.h"
25 class CommandTagMsg : public Command
29 Events::ModuleEventProvider tagevprov;
30 ClientProtocol::EventProvider msgevprov;
32 bool FirePreEvents(User* source, MessageTarget& msgtarget, CTCTags::TagMessageDetails& msgdetails)
34 // Inform modules that a TAGMSG wants to be sent.
36 FIRST_MOD_RESULT_CUSTOM(tagevprov, CTCTags::EventListener, OnUserPreTagMessage, modres, (source, msgtarget, msgdetails));
37 if (modres == MOD_RES_DENY)
39 // Inform modules that a module blocked the TAGMSG.
40 FOREACH_MOD_CUSTOM(tagevprov, CTCTags::EventListener, OnUserTagMessageBlocked, (source, msgtarget, msgdetails));
44 // Inform modules that a TAGMSG is about to be sent.
45 FOREACH_MOD_CUSTOM(tagevprov, CTCTags::EventListener, OnUserTagMessage, (source, msgtarget, msgdetails));
49 CmdResult FirePostEvent(User* source, const MessageTarget& msgtarget, const CTCTags::TagMessageDetails& msgdetails)
51 // If the source is local then update its idle time.
52 LocalUser* lsource = IS_LOCAL(source);
54 lsource->idle_lastmsg = ServerInstance->Time();
56 // Inform modules that a TAGMSG was sent.
57 FOREACH_MOD_CUSTOM(tagevprov, CTCTags::EventListener, OnUserPostTagMessage, (source, msgtarget, msgdetails));
61 CmdResult HandleChannelTarget(User* source, const Params& parameters, const char* target, PrefixMode* pm)
63 Channel* chan = ServerInstance->FindChan(target);
66 // The target channel does not exist.
67 source->WriteNumeric(Numerics::NoSuchChannel(parameters[0]));
71 // Fire the pre-message events.
72 MessageTarget msgtarget(chan, pm ? pm->GetPrefix() : 0);
73 CTCTags::TagMessageDetails msgdetails(parameters.GetTags());
74 if (!FirePreEvents(source, msgtarget, msgdetails))
77 unsigned int minrank = pm ? pm->GetPrefixRank() : 0;
78 CTCTags::TagMessage message(source, chan, parameters.GetTags());
79 const Channel::MemberMap& userlist = chan->GetUsers();
80 for (Channel::MemberMap::const_iterator iter = userlist.begin(); iter != userlist.end(); ++iter)
82 LocalUser* luser = IS_LOCAL(iter->first);
84 // Don't send to remote users or the user who is the source.
85 if (!luser || luser == source)
88 // Don't send to unprivileged or exempt users.
89 if (iter->second->getRank() < minrank || msgdetails.exemptions.count(luser))
92 // Send to users if they have the capability.
94 luser->Send(msgevprov, message);
96 return FirePostEvent(source, msgtarget, msgdetails);
99 CmdResult HandleServerTarget(User* source, const Params& parameters)
101 // If the source isn't allowed to mass message users then reject
102 // the attempt to mass-message users.
103 if (!source->HasPrivPermission("users/mass-message"))
106 // Extract the server glob match from the target parameter.
107 std::string servername(parameters[0], 1);
109 // Fire the pre-message events.
110 MessageTarget msgtarget(&servername);
111 CTCTags::TagMessageDetails msgdetails(parameters.GetTags());
112 if (!FirePreEvents(source, msgtarget, msgdetails))
115 // If the current server name matches the server name glob then send
116 // the message out to the local users.
117 if (InspIRCd::Match(ServerInstance->Config->ServerName, servername))
119 CTCTags::TagMessage message(source, "$*", parameters.GetTags());
120 const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
121 for (UserManager::LocalList::const_iterator iter = list.begin(); iter != list.end(); ++iter)
123 LocalUser* luser = IS_LOCAL(*iter);
125 // Don't send to unregistered users or the user who is the source.
126 if (luser->registered != REG_ALL || luser == source)
129 // Don't send to exempt users.
130 if (msgdetails.exemptions.count(luser))
133 // Send to users if they have the capability.
135 luser->Send(msgevprov, message);
139 // Fire the post-message event.
140 return FirePostEvent(source, msgtarget, msgdetails);
143 CmdResult HandleUserTarget(User* source, const Params& parameters)
146 if (IS_LOCAL(source))
148 // Local sources can specify either a nick or a nick@server mask as the target.
149 const char* targetserver = strchr(parameters[0].c_str(), '@');
152 // The target is a user on a specific server (e.g. jto@tolsun.oulu.fi).
153 target = ServerInstance->FindNickOnly(parameters[0].substr(0, targetserver - parameters[0].c_str()));
154 if (target && strcasecmp(target->server->GetName().c_str(), targetserver + 1))
159 // If the source is a local user then we only look up the target by nick.
160 target = ServerInstance->FindNickOnly(parameters[0]);
165 // Remote users can only specify a nick or UUID as the target.
166 target = ServerInstance->FindNick(parameters[0]);
169 if (!target || target->registered != REG_ALL)
171 // The target user does not exist or is not fully registered.
172 source->WriteNumeric(Numerics::NoSuchNick(parameters[0]));
176 // Fire the pre-message events.
177 MessageTarget msgtarget(target);
178 CTCTags::TagMessageDetails msgdetails(parameters.GetTags());
179 if (!FirePreEvents(source, msgtarget, msgdetails))
182 LocalUser* const localtarget = IS_LOCAL(target);
183 if (localtarget && cap.get(localtarget))
185 // Send to the target if they have the capability and are a local user.
186 CTCTags::TagMessage message(source, localtarget, parameters.GetTags());
187 localtarget->Send(msgevprov, message);
190 // Fire the post-message event.
191 return FirePostEvent(source, msgtarget, msgdetails);
195 CommandTagMsg(Module* Creator, Cap::Capability& Cap)
196 : Command(Creator, "TAGMSG", 1)
198 , tagevprov(Creator, "event/tagmsg")
199 , msgevprov(Creator, "TAGMSG")
201 allow_empty_last_param = false;
204 CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
206 if (CommandParser::LoopCall(user, this, parameters, 0))
209 // Check that the source has the message tags capability.
210 if (IS_LOCAL(user) && !cap.get(user))
213 // The target is a server glob.
214 if (parameters[0][0] == '$')
215 return HandleServerTarget(user, parameters);
217 // If the message begins with a status character then look it up.
218 const char* target = parameters[0].c_str();
219 PrefixMode* pmh = ServerInstance->Modes->FindPrefix(target[0]);
223 // The target is a channel name.
225 return HandleChannelTarget(user, parameters, target, pmh);
227 // The target is a nickname.
228 return HandleUserTarget(user, parameters);
231 RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE
233 return ROUTE_MESSAGE(parameters[0]);
237 class C2CTags : public ClientProtocol::MessageTagProvider
240 Cap::Capability& cap;
243 C2CTags(Module* Creator, Cap::Capability& Cap)
244 : ClientProtocol::MessageTagProvider(Creator)
249 ModResult OnProcessTag(User* user, const std::string& tagname, std::string& tagvalue) CXX11_OVERRIDE
251 // A client-only tag is prefixed with a plus sign (+) and otherwise conforms
252 // to the format specified in IRCv3.2 tags.
253 if (tagname[0] != '+' || tagname.length() < 2)
254 return MOD_RES_PASSTHRU;
256 // If the user is local then we check whether they have the message-tags cap
257 // enabled. If not then we reject all client-only tags originating from them.
258 LocalUser* lu = IS_LOCAL(user);
259 if (lu && !cap.get(lu))
262 // Remote users have their client-only tags checked by their local server.
263 return MOD_RES_ALLOW;
266 bool ShouldSendTag(LocalUser* user, const ClientProtocol::MessageTagData& tagdata) CXX11_OVERRIDE
268 return cap.get(user);
272 class ModuleIRCv3CTCTags
274 , public CTCTags::EventListener
280 ChanModeReference moderatedmode;
281 ChanModeReference noextmsgmode;
283 ModResult CopyClientTags(const ClientProtocol::TagMap& tags_in, ClientProtocol::TagMap& tags_out)
285 for (ClientProtocol::TagMap::const_iterator i = tags_in.begin(); i != tags_in.end(); ++i)
287 const ClientProtocol::MessageTagData& tagdata = i->second;
288 if (tagdata.tagprov == &c2ctags)
291 return MOD_RES_PASSTHRU;
296 : CTCTags::EventListener(this)
297 , cap(this, "message-tags")
300 , moderatedmode(this, "moderated")
301 , noextmsgmode(this, "noextmsg")
305 ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
307 return CopyClientTags(details.tags_in, details.tags_out);
310 ModResult OnUserPreTagMessage(User* user, const MessageTarget& target, CTCTags::TagMessageDetails& details) CXX11_OVERRIDE
312 if (IS_LOCAL(user) && target.type == MessageTarget::TYPE_CHANNEL)
314 Channel* chan = target.Get<Channel>();
315 if (chan->IsModeSet(noextmsgmode) && !chan->HasUser(user))
317 // The noextmsg mode is set and the user is not in the channel.
318 user->WriteNumeric(ERR_CANNOTSENDTOCHAN, chan->name, "Cannot send to channel (no external messages)");
322 bool no_chan_priv = chan->GetPrefixValue(user) < VOICE_VALUE;
323 if (no_chan_priv && chan->IsModeSet(moderatedmode))
325 // The moderated mode is set and the user has no status rank.
326 user->WriteNumeric(ERR_CANNOTSENDTOCHAN, chan->name, "Cannot send to channel (+m is set)");
330 if (no_chan_priv && ServerInstance->Config->RestrictBannedUsers != ServerConfig::BUT_NORMAL && chan->IsBanned(user))
332 // The user is banned in the channel and restrictbannedusers is enabled.
333 if (ServerInstance->Config->RestrictBannedUsers == ServerConfig::BUT_RESTRICT_NOTIFY)
334 user->WriteNumeric(ERR_CANNOTSENDTOCHAN, chan->name, "Cannot send to channel (you're banned)");
339 return CopyClientTags(details.tags_in, details.tags_out);
342 Version GetVersion() CXX11_OVERRIDE
344 return Version("Provides the message-tags IRCv3 extension", VF_VENDOR | VF_COMMON);
348 MODULE_INIT(ModuleIRCv3CTCTags)