]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/coremods/core_privmsg.cpp
Use an oper priv instead of a config flag for overriding nonicks.
[user/henk/code/inspircd.git] / src / coremods / core_privmsg.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 namespace
25 {
26         const char* MessageTypeString[] = { "PRIVMSG", "NOTICE" };
27 }
28
29 class MessageCommandBase : public Command
30 {
31         ChanModeReference moderatedmode;
32         ChanModeReference noextmsgmode;
33
34         /** Send a PRIVMSG or NOTICE message to all local users from the given user
35          * @param user User sending the message
36          * @param msg The message to send
37          * @param mt Type of the message (MSG_PRIVMSG or MSG_NOTICE)
38          */
39         static void SendAll(User* user, const std::string& msg, MessageType mt);
40
41  public:
42         MessageCommandBase(Module* parent, MessageType mt)
43                 : Command(parent, MessageTypeString[mt], 2, 2)
44                 , moderatedmode(parent, "moderated")
45                 , noextmsgmode(parent, "noextmsg")
46         {
47                 syntax = "<target>{,<target>} <message>";
48         }
49
50         /** Handle command.
51          * @param parameters The parameters to the command
52          * @param user The user issuing the command
53          * @return A value from CmdResult to indicate command success or failure.
54          */
55         CmdResult HandleMessage(const std::vector<std::string>& parameters, User* user, MessageType mt);
56
57         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters) CXX11_OVERRIDE
58         {
59                 if (IS_LOCAL(user))
60                         // This is handled by the OnUserPostMessage hook to split the LoopCall pieces
61                         return ROUTE_LOCALONLY;
62                 else
63                         return ROUTE_MESSAGE(parameters[0]);
64         }
65 };
66
67 void MessageCommandBase::SendAll(User* user, const std::string& msg, MessageType mt)
68 {
69         const std::string message = ":" + user->GetFullHost() + " " + MessageTypeString[mt] + " $* :" + msg;
70         const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
71         for (UserManager::LocalList::const_iterator i = list.begin(); i != list.end(); ++i)
72         {
73                 if ((*i)->registered == REG_ALL)
74                         (*i)->Write(message);
75         }
76 }
77
78 CmdResult MessageCommandBase::HandleMessage(const std::vector<std::string>& parameters, User* user, MessageType mt)
79 {
80         User *dest;
81         Channel *chan;
82
83         LocalUser* localuser = IS_LOCAL(user);
84         if (localuser)
85                 localuser->idle_lastmsg = ServerInstance->Time();
86
87         if (CommandParser::LoopCall(user, this, parameters, 0))
88                 return CMD_SUCCESS;
89
90         if (parameters[0][0] == '$')
91         {
92                 if (!user->HasPrivPermission("users/mass-message"))
93                         return CMD_SUCCESS;
94
95                 std::string servername(parameters[0], 1);
96                 MessageTarget msgtarget(&servername);
97                 MessageDetails msgdetails(mt, parameters[1]);
98
99                 ModResult MOD_RESULT;
100                 FIRST_MOD_RESULT(OnUserPreMessage, MOD_RESULT, (user, msgtarget, msgdetails));
101                 if (MOD_RESULT == MOD_RES_DENY)
102                         return CMD_FAILURE;
103
104                 FOREACH_MOD(OnUserMessage, (user, msgtarget, msgdetails));
105                 if (InspIRCd::Match(ServerInstance->Config->ServerName, servername, NULL))
106                 {
107                         SendAll(user, msgdetails.text, mt);
108                 }
109                 FOREACH_MOD(OnUserPostMessage, (user, msgtarget, msgdetails));
110                 return CMD_SUCCESS;
111         }
112
113         char status = 0;
114         const char* target = parameters[0].c_str();
115
116         if (ServerInstance->Modes->FindPrefix(*target))
117         {
118                 status = *target;
119                 target++;
120         }
121         if (*target == '#')
122         {
123                 chan = ServerInstance->FindChan(target);
124
125                 if (chan)
126                 {
127                         if (localuser && chan->GetPrefixValue(user) < VOICE_VALUE)
128                         {
129                                 if (chan->IsModeSet(noextmsgmode) && !chan->HasUser(user))
130                                 {
131                                         user->WriteNumeric(ERR_CANNOTSENDTOCHAN, chan->name, "Cannot send to channel (no external messages)");
132                                         return CMD_FAILURE;
133                                 }
134
135                                 if (chan->IsModeSet(moderatedmode))
136                                 {
137                                         user->WriteNumeric(ERR_CANNOTSENDTOCHAN, chan->name, "Cannot send to channel (+m)");
138                                         return CMD_FAILURE;
139                                 }
140
141                                 if (ServerInstance->Config->RestrictBannedUsers)
142                                 {
143                                         if (chan->IsBanned(user))
144                                         {
145                                                 user->WriteNumeric(ERR_CANNOTSENDTOCHAN, chan->name, "Cannot send to channel (you're banned)");
146                                                 return CMD_FAILURE;
147                                         }
148                                 }
149                         }
150
151                         MessageTarget msgtarget(chan, status);
152                         MessageDetails msgdetails(mt, parameters[1]);
153                         msgdetails.exemptions.insert(user);
154
155                         ModResult MOD_RESULT;
156                         FIRST_MOD_RESULT(OnUserPreMessage, MOD_RESULT, (user, msgtarget, msgdetails));
157                         if (MOD_RESULT == MOD_RES_DENY)
158                                 return CMD_FAILURE;
159
160                         /* Check again, a module may have zapped the input string */
161                         if (msgdetails.text.empty())
162                         {
163                                 user->WriteNumeric(ERR_NOTEXTTOSEND, "No text to send");
164                                 return CMD_FAILURE;
165                         }
166
167                         FOREACH_MOD(OnUserMessage, (user, msgtarget, msgdetails));
168
169                         if (status)
170                         {
171                                 chan->WriteAllExcept(user, false, status, msgdetails.exemptions, "%s %c%s :%s", MessageTypeString[mt], status, chan->name.c_str(), msgdetails.text.c_str());
172                         }
173                         else
174                         {
175                                 chan->WriteAllExcept(user, false, status, msgdetails.exemptions, "%s %s :%s", MessageTypeString[mt], chan->name.c_str(), msgdetails.text.c_str());
176                         }
177
178                         FOREACH_MOD(OnUserPostMessage, (user, msgtarget, msgdetails));
179                 }
180                 else
181                 {
182                         /* channel does not exist */
183                         user->WriteNumeric(Numerics::NoSuchChannel(parameters[0]));
184                         return CMD_FAILURE;
185                 }
186                 return CMD_SUCCESS;
187         }
188
189         const char* destnick = parameters[0].c_str();
190
191         if (localuser)
192         {
193                 const char* targetserver = strchr(destnick, '@');
194
195                 if (targetserver)
196                 {
197                         std::string nickonly;
198
199                         nickonly.assign(destnick, 0, targetserver - destnick);
200                         dest = ServerInstance->FindNickOnly(nickonly);
201                         if (dest && strcasecmp(dest->server->GetName().c_str(), targetserver + 1))
202                         {
203                                 /* Incorrect server for user */
204                                 user->WriteNumeric(Numerics::NoSuchNick(parameters[0]));
205                                 return CMD_FAILURE;
206                         }
207                 }
208                 else
209                         dest = ServerInstance->FindNickOnly(destnick);
210         }
211         else
212                 dest = ServerInstance->FindNick(destnick);
213
214         if ((dest) && (dest->registered == REG_ALL))
215         {
216                 if (parameters[1].empty())
217                 {
218                         user->WriteNumeric(ERR_NOTEXTTOSEND, "No text to send");
219                         return CMD_FAILURE;
220                 }
221
222                 if ((dest->IsAway()) && (mt == MSG_PRIVMSG))
223                 {
224                         /* auto respond with aweh msg */
225                         user->WriteNumeric(RPL_AWAY, dest->nick, dest->awaymsg);
226                 }
227
228                 MessageTarget msgtarget(dest);
229                 MessageDetails msgdetails(mt, parameters[1]);
230
231                 ModResult MOD_RESULT;
232                 FIRST_MOD_RESULT(OnUserPreMessage, MOD_RESULT, (user, msgtarget, msgdetails));
233                 if (MOD_RESULT == MOD_RES_DENY)
234                         return CMD_FAILURE;
235
236                 FOREACH_MOD(OnUserMessage, (user, msgtarget, msgdetails));
237
238                 if (IS_LOCAL(dest))
239                 {
240                         // direct write, same server
241                         dest->WriteFrom(user, "%s %s :%s", MessageTypeString[mt], dest->nick.c_str(), msgdetails.text.c_str());
242                 }
243
244                 FOREACH_MOD(OnUserPostMessage, (user, msgtarget, msgdetails));
245         }
246         else
247         {
248                 /* no such nick/channel */
249                 user->WriteNumeric(Numerics::NoSuchNick(parameters[0]));
250                 return CMD_FAILURE;
251         }
252         return CMD_SUCCESS;
253 }
254
255 template<MessageType MT>
256 class CommandMessage : public MessageCommandBase
257 {
258  public:
259         CommandMessage(Module* parent)
260                 : MessageCommandBase(parent, MT)
261         {
262         }
263
264         CmdResult Handle(const std::vector<std::string>& parameters, User* user) CXX11_OVERRIDE
265         {
266                 return HandleMessage(parameters, user, MT);
267         }
268 };
269
270 class ModuleCoreMessage : public Module
271 {
272         CommandMessage<MSG_PRIVMSG> CommandPrivmsg;
273         CommandMessage<MSG_NOTICE> CommandNotice;
274
275  public:
276         ModuleCoreMessage()
277                 : CommandPrivmsg(this), CommandNotice(this)
278         {
279         }
280
281         Version GetVersion() CXX11_OVERRIDE
282         {
283                 return Version("PRIVMSG, NOTICE", VF_CORE|VF_VENDOR);
284         }
285 };
286
287 MODULE_INIT(ModuleCoreMessage)