]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/commands/cmd_privmsg.cpp
Release v2.0.20
[user/henk/code/inspircd.git] / src / commands / cmd_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 /** Handle /PRIVMSG. These command handlers can be reloaded by the core,
25  * and handle basic RFC1459 commands. Commands within modules work
26  * the same way, however, they can be fully unloaded, where these
27  * may not.
28  */
29 class CommandPrivmsg : public Command
30 {
31  public:
32         /** Constructor for privmsg.
33          */
34         CommandPrivmsg ( Module* parent) : Command(parent,"PRIVMSG",2,2) { syntax = "<target>{,<target>} <message>"; }
35         /** Handle command.
36          * @param parameters The parameters to the comamnd
37          * @param pcnt The number of parameters passed to teh command
38          * @param user The user issuing the command
39          * @return A value from CmdResult to indicate command success or failure.
40          */
41         CmdResult Handle(const std::vector<std::string>& parameters, User *user);
42
43         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
44         {
45                 if (IS_LOCAL(user))
46                         // This is handled by the OnUserMessage hook to split the LoopCall pieces
47                         return ROUTE_LOCALONLY;
48                 else
49                         return ROUTE_MESSAGE(parameters[0]);
50         }
51 };
52
53 CmdResult CommandPrivmsg::Handle (const std::vector<std::string>& parameters, User *user)
54 {
55         User *dest;
56         Channel *chan;
57         CUList except_list;
58
59         user->idle_lastmsg = ServerInstance->Time();
60
61         if (ServerInstance->Parser->LoopCall(user, this, parameters, 0))
62                 return CMD_SUCCESS;
63
64         if (parameters[0][0] == '$')
65         {
66                 if (!user->HasPrivPermission("users/mass-message"))
67                         return CMD_SUCCESS;
68
69                 ModResult MOD_RESULT;
70                 std::string temp = parameters[1];
71                 FIRST_MOD_RESULT(OnUserPreMessage, MOD_RESULT, (user, (void*)parameters[0].c_str(), TYPE_SERVER, temp, 0, except_list));
72                 if (MOD_RESULT == MOD_RES_DENY)
73                         return CMD_FAILURE;
74
75                 const char* text = temp.c_str();
76                 const char* servermask = (parameters[0].c_str()) + 1;
77
78                 FOREACH_MOD(I_OnText,OnText(user, (void*)parameters[0].c_str(), TYPE_SERVER, text, 0, except_list));
79                 if (InspIRCd::Match(ServerInstance->Config->ServerName, servermask, NULL))
80                 {
81                         user->SendAll("PRIVMSG", "%s", text);
82                 }
83                 FOREACH_MOD(I_OnUserMessage,OnUserMessage(user, (void*)parameters[0].c_str(), TYPE_SERVER, text, 0, except_list));
84                 return CMD_SUCCESS;
85         }
86         char status = 0;
87         const char* target = parameters[0].c_str();
88
89         if (ServerInstance->Modes->FindPrefix(*target))
90         {
91                 status = *target;
92                 target++;
93         }
94         if (*target == '#')
95         {
96                 chan = ServerInstance->FindChan(target);
97
98                 except_list.insert(user);
99
100                 if (chan)
101                 {
102                         if (IS_LOCAL(user) && chan->GetPrefixValue(user) < VOICE_VALUE)
103                         {
104                                 if (chan->IsModeSet('n') && !chan->HasUser(user))
105                                 {
106                                         user->WriteNumeric(404, "%s %s :Cannot send to channel (no external messages)", user->nick.c_str(), chan->name.c_str());
107                                         return CMD_FAILURE;
108                                 }
109
110                                 if (chan->IsModeSet('m'))
111                                 {
112                                         user->WriteNumeric(404, "%s %s :Cannot send to channel (+m)", user->nick.c_str(), chan->name.c_str());
113                                         return CMD_FAILURE;
114                                 }
115
116                                 if (ServerInstance->Config->RestrictBannedUsers)
117                                 {
118                                         if (chan->IsBanned(user))
119                                         {
120                                                 user->WriteNumeric(404, "%s %s :Cannot send to channel (you're banned)", user->nick.c_str(), chan->name.c_str());
121                                                 return CMD_FAILURE;
122                                         }
123                                 }
124                         }
125                         ModResult MOD_RESULT;
126
127                         std::string temp = parameters[1];
128                         FIRST_MOD_RESULT(OnUserPreMessage, MOD_RESULT, (user,chan,TYPE_CHANNEL,temp,status,except_list));
129                         if (MOD_RESULT == MOD_RES_DENY)
130                                 return CMD_FAILURE;
131
132                         const char* text = temp.c_str();
133
134                         /* Check again, a module may have zapped the input string */
135                         if (temp.empty())
136                         {
137                                 user->WriteNumeric(412, "%s :No text to send", user->nick.c_str());
138                                 return CMD_FAILURE;
139                         }
140
141                         FOREACH_MOD(I_OnText,OnText(user,chan,TYPE_CHANNEL,text,status,except_list));
142
143                         if (status)
144                         {
145                                 if (ServerInstance->Config->UndernetMsgPrefix)
146                                 {
147                                         chan->WriteAllExcept(user, false, status, except_list, "PRIVMSG %c%s :%c %s", status, chan->name.c_str(), status, text);
148                                 }
149                                 else
150                                 {
151                                         chan->WriteAllExcept(user, false, status, except_list, "PRIVMSG %c%s :%s", status, chan->name.c_str(), text);
152                                 }
153                         }
154                         else
155                         {
156                                 chan->WriteAllExcept(user, false, status, except_list, "PRIVMSG %s :%s", chan->name.c_str(), text);
157                         }
158
159                         FOREACH_MOD(I_OnUserMessage,OnUserMessage(user,chan,TYPE_CHANNEL,text,status,except_list));
160                 }
161                 else
162                 {
163                         /* no such nick/channel */
164                         user->WriteNumeric(401, "%s %s :No such nick/channel", user->nick.c_str(), target);
165                         return CMD_FAILURE;
166                 }
167                 return CMD_SUCCESS;
168         }
169
170         const char* destnick = parameters[0].c_str();
171
172         if (IS_LOCAL(user))
173         {
174                 const char* targetserver = strchr(destnick, '@');
175
176                 if (targetserver)
177                 {
178                         std::string nickonly;
179
180                         nickonly.assign(destnick, 0, targetserver - destnick);
181                         dest = ServerInstance->FindNickOnly(nickonly);
182                         if (dest && strcasecmp(dest->server.c_str(), targetserver + 1))
183                         {
184                                 /* Incorrect server for user */
185                                 user->WriteNumeric(401, "%s %s :No such nick/channel",user->nick.c_str(), parameters[0].c_str());
186                                 return CMD_FAILURE;
187                         }
188                 }
189                 else
190                         dest = ServerInstance->FindNickOnly(destnick);
191         }
192         else
193                 dest = ServerInstance->FindNick(destnick);
194
195         if ((dest) && (dest->registered == REG_ALL))
196         {
197                 if (parameters[1].empty())
198                 {
199                         user->WriteNumeric(412, "%s :No text to send", user->nick.c_str());
200                         return CMD_FAILURE;
201                 }
202
203                 if (IS_AWAY(dest))
204                 {
205                         /* auto respond with aweh msg */
206                         user->WriteNumeric(301, "%s %s :%s", user->nick.c_str(), dest->nick.c_str(), dest->awaymsg.c_str());
207                 }
208
209                 ModResult MOD_RESULT;
210
211                 std::string temp = parameters[1];
212                 FIRST_MOD_RESULT(OnUserPreMessage, MOD_RESULT, (user, dest, TYPE_USER, temp, 0, except_list));
213                 if (MOD_RESULT == MOD_RES_DENY)
214                         return CMD_FAILURE;
215
216                 const char* text = temp.c_str();
217
218                 FOREACH_MOD(I_OnText,OnText(user, dest, TYPE_USER, text, 0, except_list));
219
220                 if (IS_LOCAL(dest))
221                 {
222                         // direct write, same server
223                         user->WriteTo(dest, "PRIVMSG %s :%s", dest->nick.c_str(), text);
224                 }
225
226                 FOREACH_MOD(I_OnUserMessage,OnUserMessage(user, dest, TYPE_USER, text, 0, except_list));
227         }
228         else
229         {
230                 /* no such nick/channel */
231                 user->WriteNumeric(401, "%s %s :No such nick/channel",user->nick.c_str(), parameters[0].c_str());
232                 return CMD_FAILURE;
233         }
234         return CMD_SUCCESS;
235 }
236
237 COMMAND_INIT(CommandPrivmsg)