]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_ircv3_ctctags.cpp
Only send ACCOUNT and CHGHOST to clients that have sent NICK/USER.
[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, 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)
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, "$*", 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)
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, msgdetails.tags_out);
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                 if (IS_LOCAL(user))
237                         // This is handled by the OnUserPostTagMessage hook to split the LoopCall pieces
238                         return ROUTE_LOCALONLY;
239                 else
240                         return ROUTE_MESSAGE(parameters[0]);
241         }
242 };
243
244 class C2CTags : public ClientProtocol::MessageTagProvider
245 {
246  private:
247         Cap::Capability& cap;
248
249  public:
250         C2CTags(Module* Creator, Cap::Capability& Cap)
251                 : ClientProtocol::MessageTagProvider(Creator)
252                 , cap(Cap)
253         {
254         }
255
256         ModResult OnProcessTag(User* user, const std::string& tagname, std::string& tagvalue) CXX11_OVERRIDE
257         {
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;
262
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))
267                         return MOD_RES_DENY;
268
269                 // Remote users have their client-only tags checked by their local server.
270                 return MOD_RES_ALLOW;
271         }
272
273         bool ShouldSendTag(LocalUser* user, const ClientProtocol::MessageTagData& tagdata) CXX11_OVERRIDE
274         {
275                 return cap.get(user);
276         }
277 };
278
279 class ModuleIRCv3CTCTags
280         : public Module
281         , public CTCTags::EventListener
282 {
283  private:
284         Cap::Capability cap;
285         CommandTagMsg cmd;
286         C2CTags c2ctags;
287         ChanModeReference moderatedmode;
288         ChanModeReference noextmsgmode;
289
290         ModResult CopyClientTags(const ClientProtocol::TagMap& tags_in, ClientProtocol::TagMap& tags_out)
291         {
292                 for (ClientProtocol::TagMap::const_iterator i = tags_in.begin(); i != tags_in.end(); ++i)
293                 {
294                         const ClientProtocol::MessageTagData& tagdata = i->second;
295                         if (tagdata.tagprov == &c2ctags)
296                                 tags_out.insert(*i);
297                 }
298                 return MOD_RES_PASSTHRU;
299         }
300
301  public:
302         ModuleIRCv3CTCTags()
303                 : CTCTags::EventListener(this)
304                 , cap(this, "message-tags")
305                 , cmd(this, cap)
306                 , c2ctags(this, cap)
307                 , moderatedmode(this, "moderated")
308                 , noextmsgmode(this, "noextmsg")
309         {
310         }
311
312         ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
313         {
314                 return CopyClientTags(details.tags_in, details.tags_out);
315         }
316
317         ModResult OnUserPreTagMessage(User* user, const MessageTarget& target, CTCTags::TagMessageDetails& details) CXX11_OVERRIDE
318         {
319                 if (IS_LOCAL(user) && target.type == MessageTarget::TYPE_CHANNEL)
320                 {
321                         Channel* chan = target.Get<Channel>();
322                         if (chan->IsModeSet(noextmsgmode) && !chan->HasUser(user))
323                         {
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)");
326                                 return MOD_RES_DENY;
327                         }
328
329                         bool no_chan_priv = chan->GetPrefixValue(user) < VOICE_VALUE;
330                         if (no_chan_priv && chan->IsModeSet(moderatedmode))
331                         {
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)");
334                                 return MOD_RES_DENY;
335                         }
336
337                         if (no_chan_priv && ServerInstance->Config->RestrictBannedUsers != ServerConfig::BUT_NORMAL && chan->IsBanned(user))
338                         {
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)");
342                                 return MOD_RES_DENY;
343                         }
344                 }
345
346                 return CopyClientTags(details.tags_in, details.tags_out);
347         }
348
349         Version GetVersion() CXX11_OVERRIDE
350         {
351                 return Version("Provides the message-tags IRCv3 extension", VF_VENDOR | VF_COMMON);
352         }
353 };
354
355 MODULE_INIT(ModuleIRCv3CTCTags)