]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_ircv3_ctctags.cpp
Update the module descriptions using mkversion.
[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                         return CMD_FAILURE;
113
114                 // Extract the server glob match from the target parameter.
115                 std::string servername(parameters[0], 1);
116
117                 // Fire the pre-message events.
118                 MessageTarget msgtarget(&servername);
119                 CTCTags::TagMessageDetails msgdetails(parameters.GetTags());
120                 if (!FirePreEvents(source, msgtarget, msgdetails))
121                         return CMD_FAILURE;
122
123                 // If the current server name matches the server name glob then send
124                 // the message out to the local users.
125                 if (InspIRCd::Match(ServerInstance->Config->ServerName, servername))
126                 {
127                         CTCTags::TagMessage message(source, "$*", msgdetails.tags_out);
128                         message.SetSideEffect(true);
129                         const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
130                         for (UserManager::LocalList::const_iterator iter = list.begin(); iter != list.end(); ++iter)
131                         {
132                                 LocalUser* luser = IS_LOCAL(*iter);
133
134                                 // Don't send to unregistered users or the user who is the source.
135                                 if (luser->registered != REG_ALL || luser == source)
136                                         continue;
137
138                                 // Don't send to exempt users.
139                                 if (msgdetails.exemptions.count(luser))
140                                         continue;
141
142                                 // Send to users if they have the capability.
143                                 if (cap.get(luser))
144                                         luser->Send(msgevprov, message);
145                         }
146                 }
147
148                 // Fire the post-message event.
149                 return FirePostEvent(source, msgtarget, msgdetails);
150         }
151
152         CmdResult HandleUserTarget(User* source, const Params& parameters)
153         {
154                 User* target;
155                 if (IS_LOCAL(source))
156                 {
157                         // Local sources can specify either a nick or a nick@server mask as the target.
158                         const char* targetserver = strchr(parameters[0].c_str(), '@');
159                         if (targetserver)
160                         {
161                                 // The target is a user on a specific server (e.g. jto@tolsun.oulu.fi).
162                                 target = ServerInstance->FindNickOnly(parameters[0].substr(0, targetserver - parameters[0].c_str()));
163                                 if (target && strcasecmp(target->server->GetName().c_str(), targetserver + 1))
164                                         target = NULL;
165                         }
166                         else
167                         {
168                                 // If the source is a local user then we only look up the target by nick.
169                                 target = ServerInstance->FindNickOnly(parameters[0]);
170                         }
171                 }
172                 else
173                 {
174                         // Remote users can only specify a nick or UUID as the target.
175                         target = ServerInstance->FindNick(parameters[0]);
176                 }
177
178                 if (!target || target->registered != REG_ALL)
179                 {
180                         // The target user does not exist or is not fully registered.
181                         source->WriteNumeric(Numerics::NoSuchNick(parameters[0]));
182                         return CMD_FAILURE;
183                 }
184
185                 // Fire the pre-message events.
186                 MessageTarget msgtarget(target);
187                 CTCTags::TagMessageDetails msgdetails(parameters.GetTags());
188                 if (!FirePreEvents(source, msgtarget, msgdetails))
189                         return CMD_FAILURE;
190
191                 LocalUser* const localtarget = IS_LOCAL(target);
192                 if (localtarget && cap.get(localtarget))
193                 {
194                         // Send to the target if they have the capability and are a local user.
195                         CTCTags::TagMessage message(source, localtarget, msgdetails.tags_out);
196                         message.SetSideEffect(true);
197                         localtarget->Send(msgevprov, message);
198                 }
199
200                 // Fire the post-message event.
201                 return FirePostEvent(source, msgtarget, msgdetails);
202         }
203
204  public:
205         CommandTagMsg(Module* Creator, Cap::Capability& Cap)
206                 : Command(Creator, "TAGMSG", 1)
207                 , cap(Cap)
208                 , tagevprov(Creator, "event/tagmsg")
209                 , msgevprov(Creator, "TAGMSG")
210         {
211                 allow_empty_last_param = false;
212         }
213
214         CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
215         {
216                 if (CommandParser::LoopCall(user, this, parameters, 0))
217                         return CMD_SUCCESS;
218
219                 // Check that the source has the message tags capability.
220                 if (IS_LOCAL(user) && !cap.get(user))
221                         return CMD_FAILURE;
222
223                 // The specified message tags were empty.
224                 if (parameters.GetTags().empty())
225                 {
226                         user->WriteNumeric(ERR_NOTEXTTOSEND, "No tags to send");
227                         return CMD_FAILURE;
228                 }
229
230                 // The target is a server glob.
231                 if (parameters[0][0] == '$')
232                         return HandleServerTarget(user, parameters);
233
234                 // If the message begins with a status character then look it up.
235                 const char* target = parameters[0].c_str();
236                 PrefixMode* pmh = ServerInstance->Modes->FindPrefix(target[0]);
237                 if (pmh)
238                         target++;
239
240                 // The target is a channel name.
241                 if (*target == '#')
242                         return HandleChannelTarget(user, parameters, target, pmh);
243
244                 // The target is a nickname.
245                 return HandleUserTarget(user, parameters);
246         }
247
248         RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE
249         {
250                 if (IS_LOCAL(user))
251                         // This is handled by the OnUserPostTagMessage hook to split the LoopCall pieces
252                         return ROUTE_LOCALONLY;
253                 else
254                         return ROUTE_MESSAGE(parameters[0]);
255         }
256 };
257
258 class C2CTags : public ClientProtocol::MessageTagProvider
259 {
260  private:
261         Cap::Capability& cap;
262
263  public:
264         C2CTags(Module* Creator, Cap::Capability& Cap)
265                 : ClientProtocol::MessageTagProvider(Creator)
266                 , cap(Cap)
267         {
268         }
269
270         ModResult OnProcessTag(User* user, const std::string& tagname, std::string& tagvalue) CXX11_OVERRIDE
271         {
272                 // A client-only tag is prefixed with a plus sign (+) and otherwise conforms
273                 // to the format specified in IRCv3.2 tags.
274                 if (tagname[0] != '+' || tagname.length() < 2)
275                         return MOD_RES_PASSTHRU;
276
277                 // If the user is local then we check whether they have the message-tags cap
278                 // enabled. If not then we reject all client-only tags originating from them.
279                 LocalUser* lu = IS_LOCAL(user);
280                 if (lu && !cap.get(lu))
281                         return MOD_RES_DENY;
282
283                 // Remote users have their client-only tags checked by their local server.
284                 return MOD_RES_ALLOW;
285         }
286
287         bool ShouldSendTag(LocalUser* user, const ClientProtocol::MessageTagData& tagdata) CXX11_OVERRIDE
288         {
289                 return cap.get(user);
290         }
291 };
292
293 class ModuleIRCv3CTCTags
294         : public Module
295         , public CTCTags::EventListener
296 {
297  private:
298         Cap::Capability cap;
299         CommandTagMsg cmd;
300         C2CTags c2ctags;
301         ChanModeReference moderatedmode;
302         ChanModeReference noextmsgmode;
303
304         ModResult CopyClientTags(const ClientProtocol::TagMap& tags_in, ClientProtocol::TagMap& tags_out)
305         {
306                 for (ClientProtocol::TagMap::const_iterator i = tags_in.begin(); i != tags_in.end(); ++i)
307                 {
308                         const ClientProtocol::MessageTagData& tagdata = i->second;
309                         if (tagdata.tagprov == &c2ctags)
310                                 tags_out.insert(*i);
311                 }
312                 return MOD_RES_PASSTHRU;
313         }
314
315  public:
316         ModuleIRCv3CTCTags()
317                 : CTCTags::EventListener(this)
318                 , cap(this, "message-tags")
319                 , cmd(this, cap)
320                 , c2ctags(this, cap)
321                 , moderatedmode(this, "moderated")
322                 , noextmsgmode(this, "noextmsg")
323         {
324         }
325
326         ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
327         {
328                 return CopyClientTags(details.tags_in, details.tags_out);
329         }
330
331         ModResult OnUserPreTagMessage(User* user, const MessageTarget& target, CTCTags::TagMessageDetails& details) CXX11_OVERRIDE
332         {
333                 if (IS_LOCAL(user) && target.type == MessageTarget::TYPE_CHANNEL)
334                 {
335                         Channel* chan = target.Get<Channel>();
336                         if (chan->IsModeSet(noextmsgmode) && !chan->HasUser(user))
337                         {
338                                 // The noextmsg mode is set and the user is not in the channel.
339                                 user->WriteNumeric(Numerics::CannotSendTo(chan, "external messages", *noextmsgmode));
340                                 return MOD_RES_DENY;
341                         }
342
343                         bool no_chan_priv = chan->GetPrefixValue(user) < VOICE_VALUE;
344                         if (no_chan_priv && chan->IsModeSet(moderatedmode))
345                         {
346                                 // The moderated mode is set and the user has no status rank.
347                                 user->WriteNumeric(Numerics::CannotSendTo(chan, "messages", *noextmsgmode));
348                                 return MOD_RES_DENY;
349                         }
350
351                         if (no_chan_priv && ServerInstance->Config->RestrictBannedUsers != ServerConfig::BUT_NORMAL && chan->IsBanned(user))
352                         {
353                                 // The user is banned in the channel and restrictbannedusers is enabled.
354                                 if (ServerInstance->Config->RestrictBannedUsers == ServerConfig::BUT_RESTRICT_NOTIFY)
355                                         user->WriteNumeric(Numerics::CannotSendTo(chan, "You cannot send messages to this channel whilst banned."));
356                                 return MOD_RES_DENY;
357                         }
358                 }
359
360                 return CopyClientTags(details.tags_in, details.tags_out);
361         }
362
363         Version GetVersion() CXX11_OVERRIDE
364         {
365                 return Version("Provides the IRCv3 message-tags client capability.", VF_VENDOR | VF_COMMON);
366         }
367 };
368
369 MODULE_INIT(ModuleIRCv3CTCTags)