2 * InspIRCd -- Internet Relay Chat Daemon
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>
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.
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
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/>.
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
29 class CommandPrivmsg : public Command
32 /** Constructor for privmsg.
34 CommandPrivmsg ( Module* parent) : Command(parent,"PRIVMSG",2,2) { syntax = "<target>{,<target>} <message>"; }
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.
41 CmdResult Handle(const std::vector<std::string>& parameters, User *user);
43 RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
46 // This is handled by the OnUserMessage hook to split the LoopCall pieces
47 return ROUTE_LOCALONLY;
49 return ROUTE_MESSAGE(parameters[0]);
53 CmdResult CommandPrivmsg::Handle (const std::vector<std::string>& parameters, User *user)
59 user->idle_lastmsg = ServerInstance->Time();
61 if (ServerInstance->Parser->LoopCall(user, this, parameters, 0))
64 if (parameters[0][0] == '$')
66 if (!user->HasPrivPermission("users/mass-message"))
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)
75 const char* text = temp.c_str();
76 const char* servermask = (parameters[0].c_str()) + 1;
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))
81 user->SendAll("PRIVMSG", "%s", text);
83 FOREACH_MOD(I_OnUserMessage,OnUserMessage(user, (void*)parameters[0].c_str(), TYPE_SERVER, text, 0, except_list));
87 const char* target = parameters[0].c_str();
89 if (ServerInstance->Modes->FindPrefix(*target))
96 chan = ServerInstance->FindChan(target);
98 except_list.insert(user);
102 if (IS_LOCAL(user) && chan->GetPrefixValue(user) < VOICE_VALUE)
104 if (chan->IsModeSet('n') && !chan->HasUser(user))
106 user->WriteNumeric(404, "%s %s :Cannot send to channel (no external messages)", user->nick.c_str(), chan->name.c_str());
110 if (chan->IsModeSet('m'))
112 user->WriteNumeric(404, "%s %s :Cannot send to channel (+m)", user->nick.c_str(), chan->name.c_str());
116 if (ServerInstance->Config->RestrictBannedUsers)
118 if (chan->IsBanned(user))
120 user->WriteNumeric(404, "%s %s :Cannot send to channel (you're banned)", user->nick.c_str(), chan->name.c_str());
125 ModResult MOD_RESULT;
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)
132 const char* text = temp.c_str();
134 /* Check again, a module may have zapped the input string */
137 user->WriteNumeric(412, "%s :No text to send", user->nick.c_str());
141 FOREACH_MOD(I_OnText,OnText(user,chan,TYPE_CHANNEL,text,status,except_list));
145 if (ServerInstance->Config->UndernetMsgPrefix)
147 chan->WriteAllExcept(user, false, status, except_list, "PRIVMSG %c%s :%c %s", status, chan->name.c_str(), status, text);
151 chan->WriteAllExcept(user, false, status, except_list, "PRIVMSG %c%s :%s", status, chan->name.c_str(), text);
156 chan->WriteAllExcept(user, false, status, except_list, "PRIVMSG %s :%s", chan->name.c_str(), text);
159 FOREACH_MOD(I_OnUserMessage,OnUserMessage(user,chan,TYPE_CHANNEL,text,status,except_list));
163 /* no such nick/channel */
164 user->WriteNumeric(401, "%s %s :No such nick/channel", user->nick.c_str(), target);
170 const char* destnick = parameters[0].c_str();
174 const char* targetserver = strchr(destnick, '@');
178 std::string nickonly;
180 nickonly.assign(destnick, 0, targetserver - destnick);
181 dest = ServerInstance->FindNickOnly(nickonly);
182 if (dest && strcasecmp(dest->server.c_str(), targetserver + 1))
184 /* Incorrect server for user */
185 user->WriteNumeric(401, "%s %s :No such nick/channel",user->nick.c_str(), parameters[0].c_str());
190 dest = ServerInstance->FindNickOnly(destnick);
193 dest = ServerInstance->FindNick(destnick);
197 if (parameters[1].empty())
199 user->WriteNumeric(412, "%s :No text to send", user->nick.c_str());
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());
209 ModResult MOD_RESULT;
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)
216 const char* text = temp.c_str();
218 FOREACH_MOD(I_OnText,OnText(user, dest, TYPE_USER, text, 0, except_list));
222 // direct write, same server
223 user->WriteTo(dest, "PRIVMSG %s :%s", dest->nick.c_str(), text);
226 FOREACH_MOD(I_OnUserMessage,OnUserMessage(user, dest, TYPE_USER, text, 0, except_list));
230 /* no such nick/channel */
231 user->WriteNumeric(401, "%s %s :No such nick/channel",user->nick.c_str(), parameters[0].c_str());
237 COMMAND_INIT(CommandPrivmsg)