2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2019 linuxdaemon <linuxdaemon.irc@gmail.com>
5 * Copyright (C) 2018-2021 Sadie Powell <sadie@witchery.services>
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 // Check whether a module zapped the message tags.
45 if (msgdetails.tags_out.empty())
47 source->WriteNumeric(ERR_NOTEXTTOSEND, "No tags to send");
51 // Inform modules that a TAGMSG is about to be sent.
52 FOREACH_MOD_CUSTOM(tagevprov, CTCTags::EventListener, OnUserTagMessage, (source, msgtarget, msgdetails));
56 CmdResult FirePostEvent(User* source, const MessageTarget& msgtarget, const CTCTags::TagMessageDetails& msgdetails)
58 // If the source is local then update its idle time.
59 LocalUser* lsource = IS_LOCAL(source);
60 if (lsource && msgdetails.update_idle)
61 lsource->idle_lastmsg = ServerInstance->Time();
63 // Inform modules that a TAGMSG was sent.
64 FOREACH_MOD_CUSTOM(tagevprov, CTCTags::EventListener, OnUserPostTagMessage, (source, msgtarget, msgdetails));
68 CmdResult HandleChannelTarget(User* source, const Params& parameters, const char* target, PrefixMode* pm)
70 Channel* chan = ServerInstance->FindChan(target);
73 // The target channel does not exist.
74 source->WriteNumeric(Numerics::NoSuchChannel(parameters[0]));
78 // Fire the pre-message events.
79 MessageTarget msgtarget(chan, pm ? pm->GetPrefix() : 0);
80 CTCTags::TagMessageDetails msgdetails(parameters.GetTags());
81 if (!FirePreEvents(source, msgtarget, msgdetails))
84 unsigned int minrank = pm ? pm->GetPrefixRank() : 0;
85 CTCTags::TagMessage message(source, chan, msgdetails.tags_out, msgtarget.status);
86 message.SetSideEffect(true);
87 const Channel::MemberMap& userlist = chan->GetUsers();
88 for (Channel::MemberMap::const_iterator iter = userlist.begin(); iter != userlist.end(); ++iter)
90 LocalUser* luser = IS_LOCAL(iter->first);
92 // Don't send to remote users or the user who is the source.
93 if (!luser || luser == source)
96 // Don't send to unprivileged or exempt users.
97 if (iter->second->getRank() < minrank || msgdetails.exemptions.count(luser))
100 // Send to users if they have the capability.
102 luser->Send(msgevprov, message);
104 return FirePostEvent(source, msgtarget, msgdetails);
107 CmdResult HandleServerTarget(User* source, const Params& parameters)
109 // If the source isn't allowed to mass message users then reject
110 // the attempt to mass-message users.
111 if (!source->HasPrivPermission("users/mass-message"))
113 source->WriteNumeric(ERR_NOPRIVILEGES, "Permission Denied - You do not have the required operator privileges");
117 // Extract the server glob match from the target parameter.
118 std::string servername(parameters[0], 1);
120 // Fire the pre-message events.
121 MessageTarget msgtarget(&servername);
122 CTCTags::TagMessageDetails msgdetails(parameters.GetTags());
123 if (!FirePreEvents(source, msgtarget, msgdetails))
126 // If the current server name matches the server name glob then send
127 // the message out to the local users.
128 if (InspIRCd::Match(ServerInstance->Config->ServerName, servername))
130 CTCTags::TagMessage message(source, "$*", msgdetails.tags_out);
131 message.SetSideEffect(true);
132 const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
133 for (UserManager::LocalList::const_iterator iter = list.begin(); iter != list.end(); ++iter)
135 LocalUser* luser = IS_LOCAL(*iter);
137 // Don't send to unregistered users or the user who is the source.
138 if (luser->registered != REG_ALL || luser == source)
141 // Don't send to exempt users.
142 if (msgdetails.exemptions.count(luser))
145 // Send to users if they have the capability.
147 luser->Send(msgevprov, message);
151 // Fire the post-message event.
152 return FirePostEvent(source, msgtarget, msgdetails);
155 CmdResult HandleUserTarget(User* source, const Params& parameters)
158 if (IS_LOCAL(source))
160 // Local sources can specify either a nick or a nick@server mask as the target.
161 const char* targetserver = strchr(parameters[0].c_str(), '@');
164 // The target is a user on a specific server (e.g. jto@tolsun.oulu.fi).
165 target = ServerInstance->FindNickOnly(parameters[0].substr(0, targetserver - parameters[0].c_str()));
166 if (target && strcasecmp(target->server->GetName().c_str(), targetserver + 1))
171 // If the source is a local user then we only look up the target by nick.
172 target = ServerInstance->FindNickOnly(parameters[0]);
177 // Remote users can only specify a nick or UUID as the target.
178 target = ServerInstance->FindNick(parameters[0]);
181 if (!target || target->registered != REG_ALL)
183 // The target user does not exist or is not fully registered.
184 source->WriteNumeric(Numerics::NoSuchNick(parameters[0]));
188 // Fire the pre-message events.
189 MessageTarget msgtarget(target);
190 CTCTags::TagMessageDetails msgdetails(parameters.GetTags());
191 if (!FirePreEvents(source, msgtarget, msgdetails))
194 LocalUser* const localtarget = IS_LOCAL(target);
195 if (localtarget && cap.get(localtarget))
197 // Send to the target if they have the capability and are a local user.
198 CTCTags::TagMessage message(source, localtarget, msgdetails.tags_out);
199 message.SetSideEffect(true);
200 localtarget->Send(msgevprov, message);
203 // Fire the post-message event.
204 return FirePostEvent(source, msgtarget, msgdetails);
208 CommandTagMsg(Module* Creator, Cap::Capability& Cap)
209 : Command(Creator, "TAGMSG", 1)
211 , tagevprov(Creator, "event/tagmsg")
212 , msgevprov(Creator, "TAGMSG")
214 allow_empty_last_param = false;
217 CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
219 if (CommandParser::LoopCall(user, this, parameters, 0))
222 // Check that the source has the message tags capability.
223 if (IS_LOCAL(user) && !cap.get(user))
226 // The specified message tags were empty.
227 if (parameters.GetTags().empty())
229 user->WriteNumeric(ERR_NOTEXTTOSEND, "No tags to send");
233 // The target is a server glob.
234 if (parameters[0][0] == '$')
235 return HandleServerTarget(user, parameters);
237 // If the message begins with one or more status characters then look them up.
238 const char* target = parameters[0].c_str();
239 PrefixMode* targetpfx = NULL;
240 for (PrefixMode* pfx; (pfx = ServerInstance->Modes->FindPrefix(target[0])); ++target)
242 // We want the lowest ranked prefix specified.
243 if (!targetpfx || pfx->GetPrefixRank() < targetpfx->GetPrefixRank())
249 // The target consisted solely of prefix modes.
250 user->WriteNumeric(ERR_NORECIPIENT, "No recipient given");
254 // The target is a channel name.
256 return HandleChannelTarget(user, parameters, target, targetpfx);
258 // The target is a nickname.
259 return HandleUserTarget(user, parameters);
262 RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE
265 // This is handled by the OnUserPostTagMessage hook to split the LoopCall pieces
266 return ROUTE_LOCALONLY;
268 return ROUTE_MESSAGE(parameters[0]);
272 class C2CTags : public ClientProtocol::MessageTagProvider
275 Cap::Capability& cap;
278 bool allowclientonlytags;
279 C2CTags(Module* Creator, Cap::Capability& Cap)
280 : ClientProtocol::MessageTagProvider(Creator)
285 ModResult OnProcessTag(User* user, const std::string& tagname, std::string& tagvalue) CXX11_OVERRIDE
287 // A client-only tag is prefixed with a plus sign (+) and otherwise conforms
288 // to the format specified in IRCv3.2 tags.
289 if (tagname[0] != '+' || tagname.length() < 2 || !allowclientonlytags)
290 return MOD_RES_PASSTHRU;
292 // If the user is local then we check whether they have the message-tags cap
293 // enabled. If not then we reject all client-only tags originating from them.
294 LocalUser* lu = IS_LOCAL(user);
295 if (lu && !cap.get(lu))
298 // Remote users have their client-only tags checked by their local server.
299 return MOD_RES_ALLOW;
302 bool ShouldSendTag(LocalUser* user, const ClientProtocol::MessageTagData& tagdata) CXX11_OVERRIDE
304 return cap.get(user);
308 class ModuleIRCv3CTCTags
310 , public CTCTags::EventListener
316 ChanModeReference moderatedmode;
317 ChanModeReference noextmsgmode;
319 ModResult CopyClientTags(const ClientProtocol::TagMap& tags_in, ClientProtocol::TagMap& tags_out)
321 for (ClientProtocol::TagMap::const_iterator i = tags_in.begin(); i != tags_in.end(); ++i)
323 const ClientProtocol::MessageTagData& tagdata = i->second;
324 if (tagdata.tagprov == &c2ctags)
327 return MOD_RES_PASSTHRU;
332 : CTCTags::EventListener(this)
333 , cap(this, "message-tags")
336 , moderatedmode(this, "moderated")
337 , noextmsgmode(this, "noextmsg")
341 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
343 c2ctags.allowclientonlytags = ServerInstance->Config->ConfValue("ctctags")->getBool("allowclientonlytags", true);
346 void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
348 if (!c2ctags.allowclientonlytags)
349 tokens["CLIENTTAGDENY"] = "*";
352 ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
354 return CopyClientTags(details.tags_in, details.tags_out);
357 ModResult OnUserPreTagMessage(User* user, const MessageTarget& target, CTCTags::TagMessageDetails& details) CXX11_OVERRIDE
359 if (IS_LOCAL(user) && target.type == MessageTarget::TYPE_CHANNEL)
361 Channel* chan = target.Get<Channel>();
362 if (chan->IsModeSet(noextmsgmode) && !chan->HasUser(user))
364 // The noextmsg mode is set and the user is not in the channel.
365 user->WriteNumeric(Numerics::CannotSendTo(chan, "external messages", *noextmsgmode));
369 bool no_chan_priv = chan->GetPrefixValue(user) < VOICE_VALUE;
370 if (no_chan_priv && chan->IsModeSet(moderatedmode))
372 // The moderated mode is set and the user has no status rank.
373 user->WriteNumeric(Numerics::CannotSendTo(chan, "messages", *noextmsgmode));
377 if (no_chan_priv && ServerInstance->Config->RestrictBannedUsers != ServerConfig::BUT_NORMAL && chan->IsBanned(user))
379 // The user is banned in the channel and restrictbannedusers is enabled.
380 if (ServerInstance->Config->RestrictBannedUsers == ServerConfig::BUT_RESTRICT_NOTIFY)
381 user->WriteNumeric(Numerics::CannotSendTo(chan, "You cannot send messages to this channel whilst banned."));
386 return CopyClientTags(details.tags_in, details.tags_out);
389 Version GetVersion() CXX11_OVERRIDE
391 return Version("Provides the IRCv3 message-tags client capability.", VF_VENDOR | VF_COMMON);
395 MODULE_INIT(ModuleIRCv3CTCTags)