]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_ircv3_ctctags.cpp
2856579870839a2441b0f2a0b9e9b466ce932ad3
[user/henk/code/inspircd.git] / src / modules / m_ircv3_ctctags.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2019 linuxdaemon <linuxdaemon.irc@gmail.com>
5  *   Copyright (C) 2018-2020 Sadie Powell <sadie@witchery.services>
6  *
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.
10  *
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
14  * details.
15  *
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/>.
18  */
19
20
21 #include "inspircd.h"
22 #include "modules/cap.h"
23 #include "modules/ctctags.h"
24
25 class CommandTagMsg : public Command
26 {
27  private:
28         Cap::Capability& cap;
29         Events::ModuleEventProvider tagevprov;
30         ClientProtocol::EventProvider msgevprov;
31
32         bool FirePreEvents(User* source, MessageTarget& msgtarget, CTCTags::TagMessageDetails& msgdetails)
33         {
34                 // Inform modules that a TAGMSG wants to be sent.
35                 ModResult modres;
36                 FIRST_MOD_RESULT_CUSTOM(tagevprov, CTCTags::EventListener, OnUserPreTagMessage, modres, (source, msgtarget, msgdetails));
37                 if (modres == MOD_RES_DENY)
38                 {
39                         // Inform modules that a module blocked the TAGMSG.
40                         FOREACH_MOD_CUSTOM(tagevprov, CTCTags::EventListener, OnUserTagMessageBlocked, (source, msgtarget, msgdetails));
41                         return false;
42                 }
43
44                 // Check whether a module zapped the message tags.
45                 if (msgdetails.tags_out.empty())
46                 {
47                         source->WriteNumeric(ERR_NOTEXTTOSEND, "No tags to send");
48                         return false;
49                 }
50
51                 // Inform modules that a TAGMSG is about to be sent.
52                 FOREACH_MOD_CUSTOM(tagevprov, CTCTags::EventListener, OnUserTagMessage, (source, msgtarget, msgdetails));
53                 return true;
54         }
55
56         CmdResult FirePostEvent(User* source, const MessageTarget& msgtarget, const CTCTags::TagMessageDetails& msgdetails)
57         {
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();
62
63                 // Inform modules that a TAGMSG was sent.
64                 FOREACH_MOD_CUSTOM(tagevprov, CTCTags::EventListener, OnUserPostTagMessage, (source, msgtarget, msgdetails));
65                 return CMD_SUCCESS;
66         }
67
68         CmdResult HandleChannelTarget(User* source, const Params& parameters, const char* target, PrefixMode* pm)
69         {
70                 Channel* chan = ServerInstance->FindChan(target);
71                 if (!chan)
72                 {
73                         // The target channel does not exist.
74                         source->WriteNumeric(Numerics::NoSuchChannel(parameters[0]));
75                         return CMD_FAILURE;
76                 }
77
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))
82                         return CMD_FAILURE;
83
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)
89                 {
90                         LocalUser* luser = IS_LOCAL(iter->first);
91
92                         // Don't send to remote users or the user who is the source.
93                         if (!luser || luser == source)
94                                 continue;
95
96                         // Don't send to unprivileged or exempt users.
97                         if (iter->second->getRank() < minrank || msgdetails.exemptions.count(luser))
98                                 continue;
99
100                         // Send to users if they have the capability.
101                         if (cap.get(luser))
102                                 luser->Send(msgevprov, message);
103                 }
104                 return FirePostEvent(source, msgtarget, msgdetails);
105         }
106
107         CmdResult HandleServerTarget(User* source, const Params& parameters)
108         {
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"))
112                 {
113                         source->WriteNumeric(ERR_NOPRIVILEGES, "Permission Denied - You do not have the required operator privileges");
114                         return CMD_FAILURE;
115                 }
116
117                 // Extract the server glob match from the target parameter.
118                 std::string servername(parameters[0], 1);
119
120                 // Fire the pre-message events.
121                 MessageTarget msgtarget(&servername);
122                 CTCTags::TagMessageDetails msgdetails(parameters.GetTags());
123                 if (!FirePreEvents(source, msgtarget, msgdetails))
124                         return CMD_FAILURE;
125
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))
129                 {
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)
134                         {
135                                 LocalUser* luser = IS_LOCAL(*iter);
136
137                                 // Don't send to unregistered users or the user who is the source.
138                                 if (luser->registered != REG_ALL || luser == source)
139                                         continue;
140
141                                 // Don't send to exempt users.
142                                 if (msgdetails.exemptions.count(luser))
143                                         continue;
144
145                                 // Send to users if they have the capability.
146                                 if (cap.get(luser))
147                                         luser->Send(msgevprov, message);
148                         }
149                 }
150
151                 // Fire the post-message event.
152                 return FirePostEvent(source, msgtarget, msgdetails);
153         }
154
155         CmdResult HandleUserTarget(User* source, const Params& parameters)
156         {
157                 User* target;
158                 if (IS_LOCAL(source))
159                 {
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(), '@');
162                         if (targetserver)
163                         {
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))
167                                         target = NULL;
168                         }
169                         else
170                         {
171                                 // If the source is a local user then we only look up the target by nick.
172                                 target = ServerInstance->FindNickOnly(parameters[0]);
173                         }
174                 }
175                 else
176                 {
177                         // Remote users can only specify a nick or UUID as the target.
178                         target = ServerInstance->FindNick(parameters[0]);
179                 }
180
181                 if (!target || target->registered != REG_ALL)
182                 {
183                         // The target user does not exist or is not fully registered.
184                         source->WriteNumeric(Numerics::NoSuchNick(parameters[0]));
185                         return CMD_FAILURE;
186                 }
187
188                 // Fire the pre-message events.
189                 MessageTarget msgtarget(target);
190                 CTCTags::TagMessageDetails msgdetails(parameters.GetTags());
191                 if (!FirePreEvents(source, msgtarget, msgdetails))
192                         return CMD_FAILURE;
193
194                 LocalUser* const localtarget = IS_LOCAL(target);
195                 if (localtarget && cap.get(localtarget))
196                 {
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);
201                 }
202
203                 // Fire the post-message event.
204                 return FirePostEvent(source, msgtarget, msgdetails);
205         }
206
207  public:
208         CommandTagMsg(Module* Creator, Cap::Capability& Cap)
209                 : Command(Creator, "TAGMSG", 1)
210                 , cap(Cap)
211                 , tagevprov(Creator, "event/tagmsg")
212                 , msgevprov(Creator, "TAGMSG")
213         {
214                 allow_empty_last_param = false;
215         }
216
217         CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
218         {
219                 if (CommandParser::LoopCall(user, this, parameters, 0))
220                         return CMD_SUCCESS;
221
222                 // Check that the source has the message tags capability.
223                 if (IS_LOCAL(user) && !cap.get(user))
224                         return CMD_FAILURE;
225
226                 // The specified message tags were empty.
227                 if (parameters.GetTags().empty())
228                 {
229                         user->WriteNumeric(ERR_NOTEXTTOSEND, "No tags to send");
230                         return CMD_FAILURE;
231                 }
232
233                 // The target is a server glob.
234                 if (parameters[0][0] == '$')
235                         return HandleServerTarget(user, parameters);
236
237                 // If the message begins with a status character then look it up.
238                 const char* target = parameters[0].c_str();
239                 PrefixMode* pmh = ServerInstance->Modes->FindPrefix(target[0]);
240                 if (pmh)
241                         target++;
242
243                 // The target is a channel name.
244                 if (*target == '#')
245                         return HandleChannelTarget(user, parameters, target, pmh);
246
247                 // The target is a nickname.
248                 return HandleUserTarget(user, parameters);
249         }
250
251         RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE
252         {
253                 if (IS_LOCAL(user))
254                         // This is handled by the OnUserPostTagMessage hook to split the LoopCall pieces
255                         return ROUTE_LOCALONLY;
256                 else
257                         return ROUTE_MESSAGE(parameters[0]);
258         }
259 };
260
261 class C2CTags : public ClientProtocol::MessageTagProvider
262 {
263  private:
264         Cap::Capability& cap;
265
266  public:
267         bool allowclientonlytags;
268         C2CTags(Module* Creator, Cap::Capability& Cap)
269                 : ClientProtocol::MessageTagProvider(Creator)
270                 , cap(Cap)
271         {
272         }
273
274         ModResult OnProcessTag(User* user, const std::string& tagname, std::string& tagvalue) CXX11_OVERRIDE
275         {
276                 // A client-only tag is prefixed with a plus sign (+) and otherwise conforms
277                 // to the format specified in IRCv3.2 tags.
278                 if (tagname[0] != '+' || tagname.length() < 2 || !allowclientonlytags)
279                         return MOD_RES_PASSTHRU;
280
281                 // If the user is local then we check whether they have the message-tags cap
282                 // enabled. If not then we reject all client-only tags originating from them.
283                 LocalUser* lu = IS_LOCAL(user);
284                 if (lu && !cap.get(lu))
285                         return MOD_RES_DENY;
286
287                 // Remote users have their client-only tags checked by their local server.
288                 return MOD_RES_ALLOW;
289         }
290
291         bool ShouldSendTag(LocalUser* user, const ClientProtocol::MessageTagData& tagdata) CXX11_OVERRIDE
292         {
293                 return cap.get(user);
294         }
295 };
296
297 class ModuleIRCv3CTCTags
298         : public Module
299         , public CTCTags::EventListener
300 {
301  private:
302         Cap::Capability cap;
303         CommandTagMsg cmd;
304         C2CTags c2ctags;
305         ChanModeReference moderatedmode;
306         ChanModeReference noextmsgmode;
307
308         ModResult CopyClientTags(const ClientProtocol::TagMap& tags_in, ClientProtocol::TagMap& tags_out)
309         {
310                 for (ClientProtocol::TagMap::const_iterator i = tags_in.begin(); i != tags_in.end(); ++i)
311                 {
312                         const ClientProtocol::MessageTagData& tagdata = i->second;
313                         if (tagdata.tagprov == &c2ctags)
314                                 tags_out.insert(*i);
315                 }
316                 return MOD_RES_PASSTHRU;
317         }
318
319  public:
320         ModuleIRCv3CTCTags()
321                 : CTCTags::EventListener(this)
322                 , cap(this, "message-tags")
323                 , cmd(this, cap)
324                 , c2ctags(this, cap)
325                 , moderatedmode(this, "moderated")
326                 , noextmsgmode(this, "noextmsg")
327         {
328         }
329
330         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
331         {
332                 c2ctags.allowclientonlytags = ServerInstance->Config->ConfValue("ctctags")->getBool("allowclientonlytags", true);
333         }
334
335         void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
336         {
337                 if (!c2ctags.allowclientonlytags)
338                         tokens["CLIENTTAGDENY"] = "*";
339         }
340
341         ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
342         {
343                 return CopyClientTags(details.tags_in, details.tags_out);
344         }
345
346         ModResult OnUserPreTagMessage(User* user, const MessageTarget& target, CTCTags::TagMessageDetails& details) CXX11_OVERRIDE
347         {
348                 if (IS_LOCAL(user) && target.type == MessageTarget::TYPE_CHANNEL)
349                 {
350                         Channel* chan = target.Get<Channel>();
351                         if (chan->IsModeSet(noextmsgmode) && !chan->HasUser(user))
352                         {
353                                 // The noextmsg mode is set and the user is not in the channel.
354                                 user->WriteNumeric(Numerics::CannotSendTo(chan, "external messages", *noextmsgmode));
355                                 return MOD_RES_DENY;
356                         }
357
358                         bool no_chan_priv = chan->GetPrefixValue(user) < VOICE_VALUE;
359                         if (no_chan_priv && chan->IsModeSet(moderatedmode))
360                         {
361                                 // The moderated mode is set and the user has no status rank.
362                                 user->WriteNumeric(Numerics::CannotSendTo(chan, "messages", *noextmsgmode));
363                                 return MOD_RES_DENY;
364                         }
365
366                         if (no_chan_priv && ServerInstance->Config->RestrictBannedUsers != ServerConfig::BUT_NORMAL && chan->IsBanned(user))
367                         {
368                                 // The user is banned in the channel and restrictbannedusers is enabled.
369                                 if (ServerInstance->Config->RestrictBannedUsers == ServerConfig::BUT_RESTRICT_NOTIFY)
370                                         user->WriteNumeric(Numerics::CannotSendTo(chan, "You cannot send messages to this channel whilst banned."));
371                                 return MOD_RES_DENY;
372                         }
373                 }
374
375                 return CopyClientTags(details.tags_in, details.tags_out);
376         }
377
378         Version GetVersion() CXX11_OVERRIDE
379         {
380                 return Version("Provides the IRCv3 message-tags client capability.", VF_VENDOR | VF_COMMON);
381         }
382 };
383
384 MODULE_INIT(ModuleIRCv3CTCTags)