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, msgdetails.tags_out);
79 message.SetSideEffect(true);
80 const Channel::MemberMap& userlist = chan->GetUsers();
81 for (Channel::MemberMap::const_iterator iter = userlist.begin(); iter != userlist.end(); ++iter)
83 LocalUser* luser = IS_LOCAL(iter->first);
85 // Don't send to remote users or the user who is the source.
86 if (!luser || luser == source)
89 // Don't send to unprivileged or exempt users.
90 if (iter->second->getRank() < minrank || msgdetails.exemptions.count(luser))
93 // Send to users if they have the capability.
95 luser->Send(msgevprov, message);
97 return FirePostEvent(source, msgtarget, msgdetails);
100 CmdResult HandleServerTarget(User* source, const Params& parameters)
102 // If the source isn't allowed to mass message users then reject
103 // the attempt to mass-message users.
104 if (!source->HasPrivPermission("users/mass-message"))
107 // Extract the server glob match from the target parameter.
108 std::string servername(parameters[0], 1);
110 // Fire the pre-message events.
111 MessageTarget msgtarget(&servername);
112 CTCTags::TagMessageDetails msgdetails(parameters.GetTags());
113 if (!FirePreEvents(source, msgtarget, msgdetails))
116 // If the current server name matches the server name glob then send
117 // the message out to the local users.
118 if (InspIRCd::Match(ServerInstance->Config->ServerName, servername))
120 CTCTags::TagMessage message(source, "$*", msgdetails.tags_out);
121 message.SetSideEffect(true);
122 const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
123 for (UserManager::LocalList::const_iterator iter = list.begin(); iter != list.end(); ++iter)
125 LocalUser* luser = IS_LOCAL(*iter);
127 // Don't send to unregistered users or the user who is the source.
128 if (luser->registered != REG_ALL || luser == source)
131 // Don't send to exempt users.
132 if (msgdetails.exemptions.count(luser))
135 // Send to users if they have the capability.
137 luser->Send(msgevprov, message);
141 // Fire the post-message event.
142 return FirePostEvent(source, msgtarget, msgdetails);
145 CmdResult HandleUserTarget(User* source, const Params& parameters)
148 if (IS_LOCAL(source))
150 // Local sources can specify either a nick or a nick@server mask as the target.
151 const char* targetserver = strchr(parameters[0].c_str(), '@');
154 // The target is a user on a specific server (e.g. jto@tolsun.oulu.fi).
155 target = ServerInstance->FindNickOnly(parameters[0].substr(0, targetserver - parameters[0].c_str()));
156 if (target && strcasecmp(target->server->GetName().c_str(), targetserver + 1))
161 // If the source is a local user then we only look up the target by nick.
162 target = ServerInstance->FindNickOnly(parameters[0]);
167 // Remote users can only specify a nick or UUID as the target.
168 target = ServerInstance->FindNick(parameters[0]);
171 if (!target || target->registered != REG_ALL)
173 // The target user does not exist or is not fully registered.
174 source->WriteNumeric(Numerics::NoSuchNick(parameters[0]));
178 // Fire the pre-message events.
179 MessageTarget msgtarget(target);
180 CTCTags::TagMessageDetails msgdetails(parameters.GetTags());
181 if (!FirePreEvents(source, msgtarget, msgdetails))
184 LocalUser* const localtarget = IS_LOCAL(target);
185 if (localtarget && cap.get(localtarget))
187 // Send to the target if they have the capability and are a local user.
188 CTCTags::TagMessage message(source, localtarget, msgdetails.tags_out);
189 message.SetSideEffect(true);
190 localtarget->Send(msgevprov, message);
193 // Fire the post-message event.
194 return FirePostEvent(source, msgtarget, msgdetails);
198 CommandTagMsg(Module* Creator, Cap::Capability& Cap)
199 : Command(Creator, "TAGMSG", 1)
201 , tagevprov(Creator, "event/tagmsg")
202 , msgevprov(Creator, "TAGMSG")
204 allow_empty_last_param = false;
207 CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
209 if (CommandParser::LoopCall(user, this, parameters, 0))
212 // Check that the source has the message tags capability.
213 if (IS_LOCAL(user) && !cap.get(user))
216 // The target is a server glob.
217 if (parameters[0][0] == '$')
218 return HandleServerTarget(user, parameters);
220 // If the message begins with a status character then look it up.
221 const char* target = parameters[0].c_str();
222 PrefixMode* pmh = ServerInstance->Modes->FindPrefix(target[0]);
226 // The target is a channel name.
228 return HandleChannelTarget(user, parameters, target, pmh);
230 // The target is a nickname.
231 return HandleUserTarget(user, parameters);
234 RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE
237 // This is handled by the OnUserPostTagMessage hook to split the LoopCall pieces
238 return ROUTE_LOCALONLY;
240 return ROUTE_MESSAGE(parameters[0]);
244 class C2CTags : public ClientProtocol::MessageTagProvider
247 Cap::Capability& cap;
250 C2CTags(Module* Creator, Cap::Capability& Cap)
251 : ClientProtocol::MessageTagProvider(Creator)
256 ModResult OnProcessTag(User* user, const std::string& tagname, std::string& tagvalue) CXX11_OVERRIDE
258 // A client-only tag is prefixed with a plus sign (+) and otherwise conforms
259 // to the format specified in IRCv3.2 tags.
260 if (tagname[0] != '+' || tagname.length() < 2)
261 return MOD_RES_PASSTHRU;
263 // If the user is local then we check whether they have the message-tags cap
264 // enabled. If not then we reject all client-only tags originating from them.
265 LocalUser* lu = IS_LOCAL(user);
266 if (lu && !cap.get(lu))
269 // Remote users have their client-only tags checked by their local server.
270 return MOD_RES_ALLOW;
273 bool ShouldSendTag(LocalUser* user, const ClientProtocol::MessageTagData& tagdata) CXX11_OVERRIDE
275 return cap.get(user);
279 class ModuleIRCv3CTCTags
281 , public CTCTags::EventListener
287 ChanModeReference moderatedmode;
288 ChanModeReference noextmsgmode;
290 ModResult CopyClientTags(const ClientProtocol::TagMap& tags_in, ClientProtocol::TagMap& tags_out)
292 for (ClientProtocol::TagMap::const_iterator i = tags_in.begin(); i != tags_in.end(); ++i)
294 const ClientProtocol::MessageTagData& tagdata = i->second;
295 if (tagdata.tagprov == &c2ctags)
298 return MOD_RES_PASSTHRU;
303 : CTCTags::EventListener(this)
304 , cap(this, "message-tags")
307 , moderatedmode(this, "moderated")
308 , noextmsgmode(this, "noextmsg")
312 ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
314 return CopyClientTags(details.tags_in, details.tags_out);
317 ModResult OnUserPreTagMessage(User* user, const MessageTarget& target, CTCTags::TagMessageDetails& details) CXX11_OVERRIDE
319 if (IS_LOCAL(user) && target.type == MessageTarget::TYPE_CHANNEL)
321 Channel* chan = target.Get<Channel>();
322 if (chan->IsModeSet(noextmsgmode) && !chan->HasUser(user))
324 // The noextmsg mode is set and the user is not in the channel.
325 user->WriteNumeric(ERR_CANNOTSENDTOCHAN, chan->name, "Cannot send to channel (no external messages)");
329 bool no_chan_priv = chan->GetPrefixValue(user) < VOICE_VALUE;
330 if (no_chan_priv && chan->IsModeSet(moderatedmode))
332 // The moderated mode is set and the user has no status rank.
333 user->WriteNumeric(ERR_CANNOTSENDTOCHAN, chan->name, "Cannot send to channel (+m is set)");
337 if (no_chan_priv && ServerInstance->Config->RestrictBannedUsers != ServerConfig::BUT_NORMAL && chan->IsBanned(user))
339 // The user is banned in the channel and restrictbannedusers is enabled.
340 if (ServerInstance->Config->RestrictBannedUsers == ServerConfig::BUT_RESTRICT_NOTIFY)
341 user->WriteNumeric(ERR_CANNOTSENDTOCHAN, chan->name, "Cannot send to channel (you're banned)");
346 return CopyClientTags(details.tags_in, details.tags_out);
349 Version GetVersion() CXX11_OVERRIDE
351 return Version("Provides the message-tags IRCv3 extension", VF_VENDOR | VF_COMMON);
355 MODULE_INIT(ModuleIRCv3CTCTags)