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