]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/coremods/core_message.cpp
core_message: remove unnecessary inheritance logic.
[user/henk/code/inspircd.git] / src / coremods / core_message.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2007-2008 Craig Edwards <craigedwards@brainbox.cc>
6  *   Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
7  *
8  * This file is part of InspIRCd.  InspIRCd is free software: you can
9  * redistribute it and/or modify it under the terms of the GNU General Public
10  * License as published by the Free Software Foundation, version 2.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21
22 #include "inspircd.h"
23
24 class MessageDetailsImpl : public MessageDetails
25 {
26 public:
27         MessageDetailsImpl(MessageType mt, const std::string& msg, const ClientProtocol::TagMap& tags)
28                 : MessageDetails(mt, msg, tags)
29         {
30         }
31
32         bool IsCTCP(std::string& name, std::string& body) const CXX11_OVERRIDE
33         {
34                 if (!this->IsCTCP())
35                         return false;
36
37                 size_t end_of_name = text.find(' ', 2);
38                 size_t end_of_ctcp = *text.rbegin() == '\x1' ? 1 : 0;
39                 if (end_of_name == std::string::npos)
40                 {
41                         // The CTCP only contains a name.
42                         name.assign(text, 1, text.length() - 1 - end_of_ctcp);
43                         body.clear();
44                         return true;
45                 }
46
47                 // The CTCP contains a name and a body.
48                 name.assign(text, 1, end_of_name - 1);
49
50                 size_t start_of_body = text.find_first_not_of(' ', end_of_name + 1);
51                 if (start_of_body == std::string::npos)
52                 {
53                         // The CTCP body is provided but empty.
54                         body.clear();
55                         return true;
56                 }
57
58                 // The CTCP body provided was non-empty.
59                 body.assign(text, start_of_body, text.length() - start_of_body - end_of_ctcp);
60                 return true;
61         }
62
63         bool IsCTCP(std::string& name) const CXX11_OVERRIDE
64         {
65                 if (!this->IsCTCP())
66                         return false;
67
68                 size_t end_of_name = text.find(' ', 2);
69                 if (end_of_name == std::string::npos)
70                 {
71                         // The CTCP only contains a name.
72                         size_t end_of_ctcp = *text.rbegin() == '\x1' ? 1 : 0;
73                         name.assign(text, 1, text.length() - 1 - end_of_ctcp);
74                         return true;
75                 }
76
77                 // The CTCP contains a name and a body.
78                 name.assign(text, 1, end_of_name - 1);
79                 return true;
80         }
81
82         bool IsCTCP() const CXX11_OVERRIDE
83         {
84                 // According to draft-oakley-irc-ctcp-02 a valid CTCP must begin with SOH and
85                 // contain at least one octet which is not NUL, SOH, CR, LF, or SPACE. As most
86                 // of these are restricted at the protocol level we only need to check for SOH
87                 // and SPACE.
88                 return (text.length() >= 2) && (text[0] == '\x1') &&  (text[1] != '\x1') && (text[1] != ' ');
89         }
90 };
91
92 class CommandMessage : public Command
93 {
94  private:
95         const MessageType msgtype;
96         ChanModeReference moderatedmode;
97         ChanModeReference noextmsgmode;
98
99         /** Send a PRIVMSG or NOTICE message to all local users from the given user
100          * @param source The user sending the message.
101          * @param msg The details of the message to send.
102          */
103         static void SendAll(User* source, const MessageDetails& details)
104         {
105                 ClientProtocol::Messages::Privmsg message(ClientProtocol::Messages::Privmsg::nocopy, source, "$*", details.text, details.type);
106                 message.AddTags(details.tags_out);
107                 message.SetSideEffect(true);
108                 ClientProtocol::Event messageevent(ServerInstance->GetRFCEvents().privmsg, message);
109
110                 const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
111                 for (UserManager::LocalList::const_iterator i = list.begin(); i != list.end(); ++i)
112                 {
113                         LocalUser* user = *i;
114                         if ((user->registered == REG_ALL) && (!details.exemptions.count(user)))
115                                 user->Send(messageevent);
116                 }
117         }
118
119  public:
120         CommandMessage(Module* parent, MessageType mt)
121                 : Command(parent, ClientProtocol::Messages::Privmsg::CommandStrFromMsgType(mt), 2, 2)
122                 , msgtype(mt)
123                 , moderatedmode(parent, "moderated")
124                 , noextmsgmode(parent, "noextmsg")
125         {
126                 syntax = "<target>{,<target>} <message>";
127         }
128
129         /** Handle command.
130          * @param parameters The parameters to the command
131          * @param user The user issuing the command
132          * @return A value from CmdResult to indicate command success or failure.
133          */
134         CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE;
135
136         RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE
137         {
138                 if (IS_LOCAL(user))
139                         // This is handled by the OnUserPostMessage hook to split the LoopCall pieces
140                         return ROUTE_LOCALONLY;
141                 else
142                         return ROUTE_MESSAGE(parameters[0]);
143         }
144 };
145
146 CmdResult CommandMessage::Handle(User* user, const Params& parameters)
147 {
148         User *dest;
149         Channel *chan;
150
151         if (CommandParser::LoopCall(user, this, parameters, 0))
152                 return CMD_SUCCESS;
153
154         if (parameters[0][0] == '$')
155         {
156                 if (!user->HasPrivPermission("users/mass-message"))
157                         return CMD_SUCCESS;
158
159                 std::string servername(parameters[0], 1);
160                 MessageTarget msgtarget(&servername);
161                 MessageDetailsImpl msgdetails(msgtype, parameters[1], parameters.GetTags());
162
163                 ModResult MOD_RESULT;
164                 FIRST_MOD_RESULT(OnUserPreMessage, MOD_RESULT, (user, msgtarget, msgdetails));
165                 if (MOD_RESULT == MOD_RES_DENY)
166                 {
167                         FOREACH_MOD(OnUserMessageBlocked, (user, msgtarget, msgdetails));
168                         return CMD_FAILURE;
169                 }
170
171                 FOREACH_MOD(OnUserMessage, (user, msgtarget, msgdetails));
172                 if (InspIRCd::Match(ServerInstance->Config->ServerName, servername, NULL))
173                 {
174                         SendAll(user, msgdetails);
175                 }
176                 FOREACH_MOD(OnUserPostMessage, (user, msgtarget, msgdetails));
177                 return CMD_SUCCESS;
178         }
179
180         char status = 0;
181         const char* target = parameters[0].c_str();
182
183         if (ServerInstance->Modes->FindPrefix(*target))
184         {
185                 status = *target;
186                 target++;
187         }
188         if (*target == '#')
189         {
190                 chan = ServerInstance->FindChan(target);
191
192                 if (chan)
193                 {
194                         if (IS_LOCAL(user) && chan->GetPrefixValue(user) < VOICE_VALUE)
195                         {
196                                 if (chan->IsModeSet(noextmsgmode) && !chan->HasUser(user))
197                                 {
198                                         user->WriteNumeric(ERR_CANNOTSENDTOCHAN, chan->name, "Cannot send to channel (no external messages)");
199                                         return CMD_FAILURE;
200                                 }
201
202                                 if (chan->IsModeSet(moderatedmode))
203                                 {
204                                         user->WriteNumeric(ERR_CANNOTSENDTOCHAN, chan->name, "Cannot send to channel (+m)");
205                                         return CMD_FAILURE;
206                                 }
207
208                                 if (ServerInstance->Config->RestrictBannedUsers != ServerConfig::BUT_NORMAL)
209                                 {
210                                         if (chan->IsBanned(user))
211                                         {
212                                                 if (ServerInstance->Config->RestrictBannedUsers == ServerConfig::BUT_RESTRICT_NOTIFY)
213                                                         user->WriteNumeric(ERR_CANNOTSENDTOCHAN, chan->name, "Cannot send to channel (you're banned)");
214                                                 return CMD_FAILURE;
215                                         }
216                                 }
217                         }
218
219                         MessageTarget msgtarget(chan, status);
220                         MessageDetailsImpl msgdetails(msgtype, parameters[1], parameters.GetTags());
221                         msgdetails.exemptions.insert(user);
222
223                         ModResult MOD_RESULT;
224                         FIRST_MOD_RESULT(OnUserPreMessage, MOD_RESULT, (user, msgtarget, msgdetails));
225                         if (MOD_RESULT == MOD_RES_DENY)
226                         {
227                                 FOREACH_MOD(OnUserMessageBlocked, (user, msgtarget, msgdetails));
228                                 return CMD_FAILURE;
229                         }
230
231                         /* Check again, a module may have zapped the input string */
232                         if (msgdetails.text.empty())
233                         {
234                                 user->WriteNumeric(ERR_NOTEXTTOSEND, "No text to send");
235                                 return CMD_FAILURE;
236                         }
237
238                         FOREACH_MOD(OnUserMessage, (user, msgtarget, msgdetails));
239
240                         ClientProtocol::Messages::Privmsg privmsg(ClientProtocol::Messages::Privmsg::nocopy, user, chan, msgdetails.text, msgdetails.type, msgtarget.status);
241                         privmsg.AddTags(msgdetails.tags_out);
242                         privmsg.SetSideEffect(true);
243                         chan->Write(ServerInstance->GetRFCEvents().privmsg, privmsg, msgtarget.status, msgdetails.exemptions);
244
245                         FOREACH_MOD(OnUserPostMessage, (user, msgtarget, msgdetails));
246                 }
247                 else
248                 {
249                         /* channel does not exist */
250                         user->WriteNumeric(Numerics::NoSuchChannel(parameters[0]));
251                         return CMD_FAILURE;
252                 }
253                 return CMD_SUCCESS;
254         }
255
256         const char* destnick = parameters[0].c_str();
257
258         if (IS_LOCAL(user))
259         {
260                 const char* targetserver = strchr(destnick, '@');
261
262                 if (targetserver)
263                 {
264                         std::string nickonly;
265
266                         nickonly.assign(destnick, 0, targetserver - destnick);
267                         dest = ServerInstance->FindNickOnly(nickonly);
268                         if (dest && strcasecmp(dest->server->GetName().c_str(), targetserver + 1))
269                         {
270                                 /* Incorrect server for user */
271                                 user->WriteNumeric(Numerics::NoSuchNick(parameters[0]));
272                                 return CMD_FAILURE;
273                         }
274                 }
275                 else
276                         dest = ServerInstance->FindNickOnly(destnick);
277         }
278         else
279                 dest = ServerInstance->FindNick(destnick);
280
281         if ((dest) && (dest->registered == REG_ALL))
282         {
283                 if (parameters[1].empty())
284                 {
285                         user->WriteNumeric(ERR_NOTEXTTOSEND, "No text to send");
286                         return CMD_FAILURE;
287                 }
288
289                 if ((dest->IsAway()) && (msgtype == MSG_PRIVMSG))
290                 {
291                         /* auto respond with aweh msg */
292                         user->WriteNumeric(RPL_AWAY, dest->nick, dest->awaymsg);
293                 }
294
295                 MessageTarget msgtarget(dest);
296                 MessageDetailsImpl msgdetails(msgtype, parameters[1], parameters.GetTags());
297
298
299                 ModResult MOD_RESULT;
300                 FIRST_MOD_RESULT(OnUserPreMessage, MOD_RESULT, (user, msgtarget, msgdetails));
301                 if (MOD_RESULT == MOD_RES_DENY)
302                 {
303                         FOREACH_MOD(OnUserMessageBlocked, (user, msgtarget, msgdetails));
304                         return CMD_FAILURE;
305                 }
306
307                 FOREACH_MOD(OnUserMessage, (user, msgtarget, msgdetails));
308
309                 LocalUser* const localtarget = IS_LOCAL(dest);
310                 if (localtarget)
311                 {
312                         // direct write, same server
313                         ClientProtocol::Messages::Privmsg privmsg(ClientProtocol::Messages::Privmsg::nocopy, user, localtarget->nick, msgdetails.text, msgtype);
314                         privmsg.AddTags(msgdetails.tags_out);
315                         privmsg.SetSideEffect(true);
316                         localtarget->Send(ServerInstance->GetRFCEvents().privmsg, privmsg);
317                 }
318
319                 FOREACH_MOD(OnUserPostMessage, (user, msgtarget, msgdetails));
320         }
321         else
322         {
323                 /* no such nick/channel */
324                 user->WriteNumeric(Numerics::NoSuchNick(parameters[0]));
325                 return CMD_FAILURE;
326         }
327         return CMD_SUCCESS;
328 }
329
330 class ModuleCoreMessage : public Module
331 {
332  private:
333         CommandMessage cmdprivmsg;
334         CommandMessage cmdnotice;
335
336  public:
337         ModuleCoreMessage()
338                 : cmdprivmsg(this, MSG_PRIVMSG)
339                 , cmdnotice(this, MSG_NOTICE)
340         {
341         }
342
343         void OnUserPostMessage(User* user, const MessageTarget& target, const MessageDetails& details) CXX11_OVERRIDE
344         {
345                 // We only handle the idle times of local users.
346                 LocalUser* luser = IS_LOCAL(user);
347                 if (!luser)
348                         return;
349
350                 // We don't update the idle time when a CTCP reply is sent.
351                 if (details.type == MSG_NOTICE && details.IsCTCP())
352                         return;
353
354                 luser->idle_lastmsg = ServerInstance->Time();
355         }
356
357         Version GetVersion() CXX11_OVERRIDE
358         {
359                 return Version("PRIVMSG, NOTICE", VF_CORE|VF_VENDOR);
360         }
361 };
362
363 MODULE_INIT(ModuleCoreMessage)