]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_ircv3_ctctags.cpp
36e10a32584ba822c3fb6a174e58a34cfefd646a
[user/henk/code/inspircd.git] / src / modules / m_ircv3_ctctags.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2019 Peter Powell <petpow@saberuk.com>
5  *   Copyright (C) 2016 Attila Molnar <attilamolnar@hush.com>
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                 // Inform modules that a TAGMSG is about to be sent.
45                 FOREACH_MOD_CUSTOM(tagevprov, CTCTags::EventListener, OnUserTagMessage, (source, msgtarget, msgdetails));
46                 return true;
47         }
48
49         CmdResult FirePostEvent(User* source, const MessageTarget& msgtarget, const CTCTags::TagMessageDetails& msgdetails)
50         {
51                 // If the source is local then update its idle time.
52                 LocalUser* lsource = IS_LOCAL(source);
53                 if (lsource)
54                         lsource->idle_lastmsg = ServerInstance->Time();
55
56                 // Inform modules that a TAGMSG was sent.
57                 FOREACH_MOD_CUSTOM(tagevprov, CTCTags::EventListener, OnUserPostTagMessage, (source, msgtarget, msgdetails));
58                 return CMD_SUCCESS;
59         }
60
61         CmdResult HandleChannelTarget(User* source, const Params& parameters, const char* target, PrefixMode* pm)
62         {
63                 Channel* chan = ServerInstance->FindChan(target);
64                 if (!chan)
65                 {
66                         // The target channel does not exist.
67                         source->WriteNumeric(Numerics::NoSuchChannel(parameters[0]));
68                         return CMD_FAILURE;
69                 }
70
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))
75                         return CMD_FAILURE;
76
77                 unsigned int minrank = pm ? pm->GetPrefixRank() : 0;
78                 CTCTags::TagMessage message(source, chan, parameters.GetTags());
79                 message.SetSideEffect(true);
80                 const Channel::MemberMap& userlist = chan->GetUsers();
81                 for (Channel::MemberMap::const_iterator iter = userlist.begin(); iter != userlist.end(); ++iter)
82                 {
83                         LocalUser* luser = IS_LOCAL(iter->first);
84
85                         // Don't send to remote users or the user who is the source. 
86                         if (!luser || luser == source)
87                                 continue;
88
89                         // Don't send to unprivileged or exempt users.
90                         if (iter->second->getRank() < minrank || msgdetails.exemptions.count(luser))
91                                 continue;
92
93                         // Send to users if they have the capability.
94                         if (cap.get(luser))
95                                 luser->Send(msgevprov, message);
96                 }
97                 return FirePostEvent(source, msgtarget, msgdetails);
98         }
99
100         CmdResult HandleServerTarget(User* source, const Params& parameters)
101         {
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"))
105                         return CMD_FAILURE;
106
107                 // Extract the server glob match from the target parameter.
108                 std::string servername(parameters[0], 1);
109
110                 // Fire the pre-message events.
111                 MessageTarget msgtarget(&servername);
112                 CTCTags::TagMessageDetails msgdetails(parameters.GetTags());
113                 if (!FirePreEvents(source, msgtarget, msgdetails))
114                         return CMD_FAILURE;
115
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))
119                 {
120                         CTCTags::TagMessage message(source, "$*", parameters.GetTags());
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)
124                         {
125                                 LocalUser* luser = IS_LOCAL(*iter);
126
127                                 // Don't send to unregistered users or the user who is the source.
128                                 if (luser->registered != REG_ALL || luser == source)
129                                         continue;
130
131                                 // Don't send to exempt users.
132                                 if (msgdetails.exemptions.count(luser))
133                                         continue;
134
135                                 // Send to users if they have the capability.
136                                 if (cap.get(luser))
137                                         luser->Send(msgevprov, message);
138                         }
139                 }
140
141                 // Fire the post-message event.
142                 return FirePostEvent(source, msgtarget, msgdetails);
143         }
144
145         CmdResult HandleUserTarget(User* source, const Params& parameters)
146         {
147                 User* target;
148                 if (IS_LOCAL(source))
149                 {
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(), '@');
152                         if (targetserver)
153                         {
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))
157                                         target = NULL;
158                         }
159                         else
160                         {
161                                 // If the source is a local user then we only look up the target by nick.
162                                 target = ServerInstance->FindNickOnly(parameters[0]);
163                         }
164                 }
165                 else
166                 {
167                         // Remote users can only specify a nick or UUID as the target.
168                         target = ServerInstance->FindNick(parameters[0]);
169                 }
170
171                 if (!target || target->registered != REG_ALL)
172                 {
173                         // The target user does not exist or is not fully registered.
174                         source->WriteNumeric(Numerics::NoSuchNick(parameters[0]));
175                         return CMD_FAILURE;
176                 }
177
178                 // Fire the pre-message events.
179                 MessageTarget msgtarget(target);
180                 CTCTags::TagMessageDetails msgdetails(parameters.GetTags());
181                 if (!FirePreEvents(source, msgtarget, msgdetails))
182                         return CMD_FAILURE;
183
184                 LocalUser* const localtarget = IS_LOCAL(target);
185                 if (localtarget && cap.get(localtarget))
186                 {
187                         // Send to the target if they have the capability and are a local user.
188                         CTCTags::TagMessage message(source, localtarget, parameters.GetTags());
189                         message.SetSideEffect(true);
190                         localtarget->Send(msgevprov, message);
191                 }
192
193                 // Fire the post-message event.
194                 return FirePostEvent(source, msgtarget, msgdetails);
195         }
196
197  public:
198         CommandTagMsg(Module* Creator, Cap::Capability& Cap)
199                 : Command(Creator, "TAGMSG", 1)
200                 , cap(Cap)
201                 , tagevprov(Creator, "event/tagmsg")
202                 , msgevprov(Creator, "TAGMSG")
203         {
204                 allow_empty_last_param = false;
205         }
206
207         CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
208         {
209                 if (CommandParser::LoopCall(user, this, parameters, 0))
210                         return CMD_SUCCESS;
211
212                 // Check that the source has the message tags capability.
213                 if (IS_LOCAL(user) && !cap.get(user))
214                         return CMD_FAILURE;
215
216                 // The target is a server glob.
217                 if (parameters[0][0] == '$')
218                         return HandleServerTarget(user, parameters);
219
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]);
223                 if (pmh)
224                         target++;
225
226                 // The target is a channel name.
227                 if (*target == '#')
228                         return HandleChannelTarget(user, parameters, target, pmh);
229
230                 // The target is a nickname.
231                 return HandleUserTarget(user, parameters);
232         }
233
234         RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE
235         {
236                 return ROUTE_MESSAGE(parameters[0]);
237         }
238 };
239
240 class C2CTags : public ClientProtocol::MessageTagProvider
241 {
242  private:
243         Cap::Capability& cap;
244
245  public:
246         C2CTags(Module* Creator, Cap::Capability& Cap)
247                 : ClientProtocol::MessageTagProvider(Creator)
248                 , cap(Cap)
249         {
250         }
251
252         ModResult OnProcessTag(User* user, const std::string& tagname, std::string& tagvalue) CXX11_OVERRIDE
253         {
254                 // A client-only tag is prefixed with a plus sign (+) and otherwise conforms
255                 // to the format specified in IRCv3.2 tags.
256                 if (tagname[0] != '+' || tagname.length() < 2)
257                         return MOD_RES_PASSTHRU;
258
259                 // If the user is local then we check whether they have the message-tags cap
260                 // enabled. If not then we reject all client-only tags originating from them.
261                 LocalUser* lu = IS_LOCAL(user);
262                 if (lu && !cap.get(lu))
263                         return MOD_RES_DENY;
264
265                 // Remote users have their client-only tags checked by their local server.
266                 return MOD_RES_ALLOW;
267         }
268
269         bool ShouldSendTag(LocalUser* user, const ClientProtocol::MessageTagData& tagdata) CXX11_OVERRIDE
270         {
271                 return cap.get(user);
272         }
273 };
274
275 class ModuleIRCv3CTCTags
276         : public Module
277         , public CTCTags::EventListener
278 {
279  private:
280         Cap::Capability cap;
281         CommandTagMsg cmd;
282         C2CTags c2ctags;
283         ChanModeReference moderatedmode;
284         ChanModeReference noextmsgmode;
285
286         ModResult CopyClientTags(const ClientProtocol::TagMap& tags_in, ClientProtocol::TagMap& tags_out)
287         {
288                 for (ClientProtocol::TagMap::const_iterator i = tags_in.begin(); i != tags_in.end(); ++i)
289                 {
290                         const ClientProtocol::MessageTagData& tagdata = i->second;
291                         if (tagdata.tagprov == &c2ctags)
292                                 tags_out.insert(*i);
293                 }
294                 return MOD_RES_PASSTHRU;
295         }
296
297  public:
298         ModuleIRCv3CTCTags()
299                 : CTCTags::EventListener(this)
300                 , cap(this, "message-tags")
301                 , cmd(this, cap)
302                 , c2ctags(this, cap)
303                 , moderatedmode(this, "moderated")
304                 , noextmsgmode(this, "noextmsg")
305         {
306         }
307
308         ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
309         {
310                 return CopyClientTags(details.tags_in, details.tags_out);
311         }
312
313         ModResult OnUserPreTagMessage(User* user, const MessageTarget& target, CTCTags::TagMessageDetails& details) CXX11_OVERRIDE
314         {
315                 if (IS_LOCAL(user) && target.type == MessageTarget::TYPE_CHANNEL)
316                 {
317                         Channel* chan = target.Get<Channel>();
318                         if (chan->IsModeSet(noextmsgmode) && !chan->HasUser(user))
319                         {
320                                 // The noextmsg mode is set and the user is not in the channel.
321                                 user->WriteNumeric(ERR_CANNOTSENDTOCHAN, chan->name, "Cannot send to channel (no external messages)");
322                                 return MOD_RES_DENY;
323                         }
324
325                         bool no_chan_priv = chan->GetPrefixValue(user) < VOICE_VALUE;
326                         if (no_chan_priv && chan->IsModeSet(moderatedmode))
327                         {
328                                 // The moderated mode is set and the user has no status rank.
329                                 user->WriteNumeric(ERR_CANNOTSENDTOCHAN, chan->name, "Cannot send to channel (+m is set)");
330                                 return MOD_RES_DENY;
331                         }
332
333                         if (no_chan_priv && ServerInstance->Config->RestrictBannedUsers != ServerConfig::BUT_NORMAL && chan->IsBanned(user))
334                         {
335                                 // The user is banned in the channel and restrictbannedusers is enabled.
336                                 if (ServerInstance->Config->RestrictBannedUsers == ServerConfig::BUT_RESTRICT_NOTIFY)
337                                         user->WriteNumeric(ERR_CANNOTSENDTOCHAN, chan->name, "Cannot send to channel (you're banned)");
338                                 return MOD_RES_DENY;
339                         }
340                 }
341
342                 return CopyClientTags(details.tags_in, details.tags_out);
343         }
344
345         Version GetVersion() CXX11_OVERRIDE
346         {
347                 return Version("Provides the message-tags IRCv3 extension", VF_VENDOR | VF_COMMON);
348         }
349 };
350
351 MODULE_INIT(ModuleIRCv3CTCTags)