]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/coremods/core_message.cpp
Allow a statusmsg to have multiple statuses and pick the lowest.
[user/henk/code/inspircd.git] / src / coremods / core_message.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2017-2020 Sadie Powell <sadie@witchery.services>
5  *   Copyright (C) 2013, 2017-2018 Attila Molnar <attilamolnar@hush.com>
6  *   Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
7  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
8  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
9  *   Copyright (C) 2006-2007, 2010 Craig Edwards <brain@inspircd.org>
10  *
11  * This file is part of InspIRCd.  InspIRCd is free software: you can
12  * redistribute it and/or modify it under the terms of the GNU General Public
13  * License as published by the Free Software Foundation, version 2.
14  *
15  * This program is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  */
23
24
25 #include "inspircd.h"
26
27
28 class MessageDetailsImpl : public MessageDetails
29 {
30 public:
31         MessageDetailsImpl(MessageType mt, const std::string& msg, const ClientProtocol::TagMap& tags)
32                 : MessageDetails(mt, msg, tags)
33         {
34         }
35
36         bool IsCTCP(std::string& name, std::string& body) const CXX11_OVERRIDE
37         {
38                 if (!this->IsCTCP())
39                         return false;
40
41                 size_t end_of_name = text.find(' ', 2);
42                 size_t end_of_ctcp = *text.rbegin() == '\x1' ? 1 : 0;
43                 if (end_of_name == std::string::npos)
44                 {
45                         // The CTCP only contains a name.
46                         name.assign(text, 1, text.length() - 1 - end_of_ctcp);
47                         body.clear();
48                         return true;
49                 }
50
51                 // The CTCP contains a name and a body.
52                 name.assign(text, 1, end_of_name - 1);
53
54                 size_t start_of_body = text.find_first_not_of(' ', end_of_name + 1);
55                 if (start_of_body == std::string::npos)
56                 {
57                         // The CTCP body is provided but empty.
58                         body.clear();
59                         return true;
60                 }
61
62                 // The CTCP body provided was non-empty.
63                 body.assign(text, start_of_body, text.length() - start_of_body - end_of_ctcp);
64                 return true;
65         }
66
67         bool IsCTCP(std::string& name) const CXX11_OVERRIDE
68         {
69                 if (!this->IsCTCP())
70                         return false;
71
72                 size_t end_of_name = text.find(' ', 2);
73                 if (end_of_name == std::string::npos)
74                 {
75                         // The CTCP only contains a name.
76                         size_t end_of_ctcp = *text.rbegin() == '\x1' ? 1 : 0;
77                         name.assign(text, 1, text.length() - 1 - end_of_ctcp);
78                         return true;
79                 }
80
81                 // The CTCP contains a name and a body.
82                 name.assign(text, 1, end_of_name - 1);
83                 return true;
84         }
85
86         bool IsCTCP() const CXX11_OVERRIDE
87         {
88                 // According to draft-oakley-irc-ctcp-02 a valid CTCP must begin with SOH and
89                 // contain at least one octet which is not NUL, SOH, CR, LF, or SPACE. As most
90                 // of these are restricted at the protocol level we only need to check for SOH
91                 // and SPACE.
92                 return (text.length() >= 2) && (text[0] == '\x1') &&  (text[1] != '\x1') && (text[1] != ' ');
93         }
94 };
95
96 namespace
97 {
98         bool FirePreEvents(User* source, MessageTarget& msgtarget, MessageDetails& msgdetails)
99         {
100                 // Inform modules that a message wants to be sent.
101                 ModResult modres;
102                 FIRST_MOD_RESULT(OnUserPreMessage, modres, (source, msgtarget, msgdetails));
103                 if (modres == MOD_RES_DENY)
104                 {
105                         // Inform modules that a module blocked the mssage.
106                         FOREACH_MOD(OnUserMessageBlocked, (source, msgtarget, msgdetails));
107                         return false;
108                 }
109
110                 // Check whether a module zapped the message body.
111                 if (msgdetails.text.empty())
112                 {
113                         source->WriteNumeric(ERR_NOTEXTTOSEND, "No text to send");
114                         return false;
115                 }
116
117                 // Inform modules that a message is about to be sent.
118                 FOREACH_MOD(OnUserMessage, (source, msgtarget, msgdetails));
119                 return true;
120         }
121
122         CmdResult FirePostEvent(User* source, const MessageTarget& msgtarget, const MessageDetails& msgdetails)
123         {
124                 // If the source is local and was not sending a CTCP reply then update their idle time.
125                 LocalUser* lsource = IS_LOCAL(source);
126                 if (lsource && msgdetails.update_idle && (msgdetails.type != MSG_NOTICE || !msgdetails.IsCTCP()))
127                         lsource->idle_lastmsg = ServerInstance->Time();
128
129                 // Inform modules that a message was sent.
130                 FOREACH_MOD(OnUserPostMessage, (source, msgtarget, msgdetails));
131                 return CMD_SUCCESS;
132         }
133 }
134
135 class CommandMessage : public Command
136 {
137  private:
138         const MessageType msgtype;
139
140         CmdResult HandleChannelTarget(User* source, const Params& parameters, const char* target, PrefixMode* pm)
141         {
142                 Channel* chan = ServerInstance->FindChan(target);
143                 if (!chan)
144                 {
145                         // The target channel does not exist.
146                         source->WriteNumeric(Numerics::NoSuchChannel(parameters[0]));
147                         return CMD_FAILURE;
148                 }
149
150                 // Fire the pre-message events.
151                 MessageTarget msgtarget(chan, pm ? pm->GetPrefix() : 0);
152                 MessageDetailsImpl msgdetails(msgtype, parameters[1], parameters.GetTags());
153                 msgdetails.exemptions.insert(source);
154                 if (!FirePreEvents(source, msgtarget, msgdetails))
155                         return CMD_FAILURE;
156
157                 // Send the message to the members of the channel.
158                 ClientProtocol::Messages::Privmsg privmsg(ClientProtocol::Messages::Privmsg::nocopy, source, chan, msgdetails.text, msgdetails.type, msgtarget.status);
159                 privmsg.AddTags(msgdetails.tags_out);
160                 privmsg.SetSideEffect(true);
161                 chan->Write(ServerInstance->GetRFCEvents().privmsg, privmsg, msgtarget.status, msgdetails.exemptions);
162
163                 // Create the outgoing message and message event.
164                 return FirePostEvent(source, msgtarget, msgdetails);
165         }
166
167         CmdResult HandleServerTarget(User* source, const Params& parameters)
168         {
169                 // If the source isn't allowed to mass message users then reject
170                 // the attempt to mass-message users.
171                 if (!source->HasPrivPermission("users/mass-message"))
172                 {
173                         source->WriteNumeric(ERR_NOPRIVILEGES, "Permission Denied - You do not have the required operator privileges");
174                         return CMD_FAILURE;
175                 }
176
177                 // Extract the server glob match from the target parameter.
178                 std::string servername(parameters[0], 1);
179
180                 // Fire the pre-message events.
181                 MessageTarget msgtarget(&servername);
182                 MessageDetailsImpl msgdetails(msgtype, parameters[1], parameters.GetTags());
183                 if (!FirePreEvents(source, msgtarget, msgdetails))
184                         return CMD_FAILURE;
185
186                 // If the current server name matches the server name glob then send
187                 // the message out to the local users.
188                 if (InspIRCd::Match(ServerInstance->Config->ServerName, servername))
189                 {
190                         // Create the outgoing message and message event.
191                         ClientProtocol::Messages::Privmsg message(ClientProtocol::Messages::Privmsg::nocopy, source, "$*", msgdetails.text, msgdetails.type);
192                         message.AddTags(msgdetails.tags_out);
193                         message.SetSideEffect(true);
194                         ClientProtocol::Event messageevent(ServerInstance->GetRFCEvents().privmsg, message);
195
196                         const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
197                         for (UserManager::LocalList::const_iterator i = list.begin(); i != list.end(); ++i)
198                         {
199                                 LocalUser* luser = *i;
200
201                                 // Don't send to unregistered users or the user who is the source.
202                                 if (luser->registered != REG_ALL || luser == source)
203                                         continue;
204
205                                 // Only send to non-exempt users.
206                                 if (!msgdetails.exemptions.count(luser))
207                                         luser->Send(messageevent);
208                         }
209                 }
210
211                 // Fire the post-message event.
212                 return FirePostEvent(source, msgtarget, msgdetails);
213         }
214
215         CmdResult HandleUserTarget(User* source, const Params& parameters)
216         {
217                 User* target;
218                 if (IS_LOCAL(source))
219                 {
220                         // Local sources can specify either a nick or a nick@server mask as the target.
221                         const char* targetserver = strchr(parameters[0].c_str(), '@');
222                         if (targetserver)
223                         {
224                                 // The target is a user on a specific server (e.g. jto@tolsun.oulu.fi).
225                                 target = ServerInstance->FindNickOnly(parameters[0].substr(0, targetserver - parameters[0].c_str()));
226                                 if (target && strcasecmp(target->server->GetName().c_str(), targetserver + 1))
227                                         target = NULL;
228                         }
229                         else
230                         {
231                                 // If the source is a local user then we only look up the target by nick.
232                                 target = ServerInstance->FindNickOnly(parameters[0]);
233                         }
234                 }
235                 else
236                 {
237                         // Remote users can only specify a nick or UUID as the target.
238                         target = ServerInstance->FindNick(parameters[0]);
239                 }
240
241                 if (!target || target->registered != REG_ALL)
242                 {
243                         // The target user does not exist or is not fully registered.
244                         source->WriteNumeric(Numerics::NoSuchNick(parameters[0]));
245                         return CMD_FAILURE;
246                 }
247
248                 // Fire the pre-message events.
249                 MessageTarget msgtarget(target);
250                 MessageDetailsImpl msgdetails(msgtype, parameters[1], parameters.GetTags());
251                 if (!FirePreEvents(source, msgtarget, msgdetails))
252                         return CMD_FAILURE;
253
254                 // If the target is away then inform the user.
255                 if (target->IsAway() && msgdetails.type == MSG_PRIVMSG)
256                         source->WriteNumeric(RPL_AWAY, target->nick, target->awaymsg);
257
258                 LocalUser* const localtarget = IS_LOCAL(target);
259                 if (localtarget)
260                 {
261                         // Send to the target if they are a local user.
262                         ClientProtocol::Messages::Privmsg privmsg(ClientProtocol::Messages::Privmsg::nocopy, source, localtarget->nick, msgdetails.text, msgdetails.type);
263                         privmsg.AddTags(msgdetails.tags_out);
264                         privmsg.SetSideEffect(true);
265                         localtarget->Send(ServerInstance->GetRFCEvents().privmsg, privmsg);
266                 }
267
268                 // Fire the post-message event.
269                 return FirePostEvent(source, msgtarget, msgdetails);
270         }
271
272  public:
273         CommandMessage(Module* parent, MessageType mt)
274                 : Command(parent, ClientProtocol::Messages::Privmsg::CommandStrFromMsgType(mt), 2, 2)
275                 , msgtype(mt)
276         {
277                 syntax = "<target>[,<target>]+ :<message>";
278         }
279
280         /** Handle command.
281          * @param parameters The parameters to the command
282          * @param user The user issuing the command
283          * @return A value from CmdResult to indicate command success or failure.
284          */
285         CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
286         {
287                 if (CommandParser::LoopCall(user, this, parameters, 0))
288                         return CMD_SUCCESS;
289
290                 // The specified message was empty.
291                 if (parameters[1].empty())
292                 {
293                         user->WriteNumeric(ERR_NOTEXTTOSEND, "No text to send");
294                         return CMD_FAILURE;
295                 }
296
297                 // The target is a server glob.
298                 if (parameters[0][0] == '$')
299                         return HandleServerTarget(user, parameters);
300
301                 // If the message begins with one or more status characters then look them up.
302                 const char* target = parameters[0].c_str();
303                 PrefixMode* targetpfx = NULL;
304                 for (PrefixMode* pfx; (pfx = ServerInstance->Modes->FindPrefix(target[0])); ++target)
305                 {
306                         // We want the lowest ranked prefix specified.
307                         if (!targetpfx || pfx->GetPrefixRank() < targetpfx->GetPrefixRank())
308                                 targetpfx = pfx;
309                 }
310
311                 if (!target[0])
312                 {
313                         // The target consisted solely of prefix modes.
314                         user->WriteNumeric(ERR_NORECIPIENT, "No recipient given");
315                         return CMD_FAILURE;
316                 }
317
318                 // The target is a channel name.
319                 if (*target == '#')
320                         return HandleChannelTarget(user, parameters, target, targetpfx);
321
322                 // The target is a nickname.
323                 return HandleUserTarget(user, parameters);
324         }
325
326         RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE
327         {
328                 if (IS_LOCAL(user))
329                         // This is handled by the OnUserPostMessage hook to split the LoopCall pieces
330                         return ROUTE_LOCALONLY;
331                 else
332                         return ROUTE_MESSAGE(parameters[0]);
333         }
334 };
335
336 class CommandSQuery : public SplitCommand
337 {
338  public:
339         CommandSQuery(Module* Creator)
340                 : SplitCommand(Creator, "SQUERY", 2, 2)
341         {
342                 syntax = "<service> :<message>";
343         }
344
345         CmdResult HandleLocal(LocalUser* user, const Params& parameters) CXX11_OVERRIDE
346         {
347                 // The specified message was empty.
348                 if (parameters[1].empty())
349                 {
350                         user->WriteNumeric(ERR_NOTEXTTOSEND, "No text to send");
351                         return CMD_FAILURE;
352                 }
353
354                 // The target can be either a nick or a nick@server mask.
355                 User* target;
356                 const char* targetserver = strchr(parameters[0].c_str(), '@');
357                 if (targetserver)
358                 {
359                         // The target is a user on a specific server (e.g. jto@tolsun.oulu.fi).
360                         target = ServerInstance->FindNickOnly(parameters[0].substr(0, targetserver - parameters[0].c_str()));
361                         if (target && strcasecmp(target->server->GetName().c_str(), targetserver + 1))
362                                 target = NULL;
363                 }
364                 else
365                 {
366                         // The targer can be on any server.
367                         target = ServerInstance->FindNickOnly(parameters[0]);
368                 }
369
370                 if (!target || target->registered != REG_ALL || !target->server->IsULine())
371                 {
372                         // The target user does not exist, is not fully registered, or is not a service.
373                         user->WriteNumeric(ERR_NOSUCHSERVICE, parameters[0], "No such service");
374                         return CMD_FAILURE;
375                 }
376
377                 // Fire the pre-message events.
378                 MessageTarget msgtarget(target);
379                 MessageDetailsImpl msgdetails(MSG_PRIVMSG, parameters[1], parameters.GetTags());
380                 if (!FirePreEvents(user, msgtarget, msgdetails))
381                         return CMD_FAILURE;
382
383                 // The SQUERY command targets a service on a U-lined server. This can never
384                 // be on the server local to the source so we don't need to do any routing
385                 // logic and can forward it as a PRIVMSG.
386
387                 // Fire the post-message event.
388                 return FirePostEvent(user, msgtarget, msgdetails);
389         }
390 };
391
392 class ModuleCoreMessage : public Module
393 {
394  private:
395         CommandMessage cmdprivmsg;
396         CommandMessage cmdnotice;
397         CommandSQuery cmdsquery;
398         ChanModeReference moderatedmode;
399         ChanModeReference noextmsgmode;
400
401  public:
402         ModuleCoreMessage()
403                 : cmdprivmsg(this, MSG_PRIVMSG)
404                 , cmdnotice(this, MSG_NOTICE)
405                 , cmdsquery(this)
406                 , moderatedmode(this, "moderated")
407                 , noextmsgmode(this, "noextmsg")
408         {
409         }
410
411         ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
412         {
413                 if (!IS_LOCAL(user) || target.type != MessageTarget::TYPE_CHANNEL)
414                         return MOD_RES_PASSTHRU;
415
416                 Channel* chan = target.Get<Channel>();
417                 if (chan->IsModeSet(noextmsgmode) && !chan->HasUser(user))
418                 {
419                         // The noextmsg mode is set and the user is not in the channel.
420                         user->WriteNumeric(Numerics::CannotSendTo(chan, "external messages", *noextmsgmode));
421                         return MOD_RES_DENY;
422                 }
423
424                 bool no_chan_priv = chan->GetPrefixValue(user) < VOICE_VALUE;
425                 if (no_chan_priv && chan->IsModeSet(moderatedmode))
426                 {
427                         // The moderated mode is set and the user has no status rank.
428                         user->WriteNumeric(Numerics::CannotSendTo(chan, "messages", *moderatedmode));
429                         return MOD_RES_DENY;
430                 }
431
432                 if (no_chan_priv && ServerInstance->Config->RestrictBannedUsers != ServerConfig::BUT_NORMAL && chan->IsBanned(user))
433                 {
434                         // The user is banned in the channel and restrictbannedusers is enabled.
435                         if (ServerInstance->Config->RestrictBannedUsers == ServerConfig::BUT_RESTRICT_NOTIFY)
436                                 user->WriteNumeric(Numerics::CannotSendTo(chan, "You cannot send messages to this channel whilst banned."));
437                         return MOD_RES_DENY;
438                 }
439
440                 return MOD_RES_PASSTHRU;
441         }
442
443         Version GetVersion() CXX11_OVERRIDE
444         {
445                 return Version("Provides the NOTICE, PRIVMSG, and SQUERY commands", VF_CORE|VF_VENDOR);
446         }
447 };
448
449 MODULE_INIT(ModuleCoreMessage)