1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
16 /** Handle /PRIVMSG. These command handlers can be reloaded by the core,
17 * and handle basic RFC1459 commands. Commands within modules work
18 * the same way, however, they can be fully unloaded, where these
21 class CommandPrivmsg : public Command
24 /** Constructor for privmsg.
26 CommandPrivmsg ( Module* parent) : Command(parent,"PRIVMSG",2,2) { syntax = "<target>{,<target>} <message>"; }
28 * @param parameters The parameters to the comamnd
29 * @param pcnt The number of parameters passed to teh command
30 * @param user The user issuing the command
31 * @return A value from CmdResult to indicate command success or failure.
33 CmdResult Handle(const std::vector<std::string>& parameters, User *user);
35 RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
38 // This is handled by the OnUserMessage hook to split the LoopCall pieces
39 return ROUTE_LOCALONLY;
41 return ROUTE_MESSAGE(parameters[0]);
45 CmdResult CommandPrivmsg::Handle (const std::vector<std::string>& parameters, User *user)
51 user->idle_lastmsg = ServerInstance->Time();
53 if (ServerInstance->Parser->LoopCall(user, this, parameters, 0))
56 if (parameters[0][0] == '$')
58 if (!user->HasPrivPermission("users/mass-message"))
62 std::string temp = parameters[1];
63 FIRST_MOD_RESULT(OnUserPreMessage, MOD_RESULT, (user, (void*)parameters[0].c_str(), TYPE_SERVER, temp, 0, except_list));
64 if (MOD_RESULT == MOD_RES_DENY)
67 const char* text = temp.c_str();
68 const char* servermask = (parameters[0].c_str()) + 1;
70 FOREACH_MOD(I_OnText,OnText(user, (void*)parameters[0].c_str(), TYPE_SERVER, text, 0, except_list));
71 if (InspIRCd::Match(ServerInstance->Config->ServerName, servermask, NULL))
73 user->SendAll("PRIVMSG", "%s", text);
75 FOREACH_MOD(I_OnUserMessage,OnUserMessage(user, (void*)parameters[0].c_str(), TYPE_SERVER, text, 0, except_list));
79 const char* target = parameters[0].c_str();
81 if (ServerInstance->Modes->FindPrefix(*target))
88 chan = ServerInstance->FindChan(target);
90 except_list.insert(user);
94 if (IS_LOCAL(user) && chan->GetPrefixValue(user) < VOICE_VALUE)
96 if (chan->IsModeSet('n') && !chan->HasUser(user))
98 user->WriteNumeric(404, "%s %s :Cannot send to channel (no external messages)", user->nick.c_str(), chan->name.c_str());
102 if (chan->IsModeSet('m'))
104 user->WriteNumeric(404, "%s %s :Cannot send to channel (+m)", user->nick.c_str(), chan->name.c_str());
108 if (ServerInstance->Config->RestrictBannedUsers)
110 if (chan->IsBanned(user))
112 user->WriteNumeric(404, "%s %s :Cannot send to channel (you're banned)", user->nick.c_str(), chan->name.c_str());
117 ModResult MOD_RESULT;
119 std::string temp = parameters[1];
120 FIRST_MOD_RESULT(OnUserPreMessage, MOD_RESULT, (user,chan,TYPE_CHANNEL,temp,status,except_list));
121 if (MOD_RESULT == MOD_RES_DENY)
124 const char* text = temp.c_str();
126 /* Check again, a module may have zapped the input string */
129 user->WriteNumeric(412, "%s :No text to send", user->nick.c_str());
133 FOREACH_MOD(I_OnText,OnText(user,chan,TYPE_CHANNEL,text,status,except_list));
137 if (ServerInstance->Config->UndernetMsgPrefix)
139 chan->WriteAllExcept(user, false, status, except_list, "PRIVMSG %c%s :%c %s", status, chan->name.c_str(), status, text);
143 chan->WriteAllExcept(user, false, status, except_list, "PRIVMSG %c%s :%s", status, chan->name.c_str(), text);
148 chan->WriteAllExcept(user, false, status, except_list, "PRIVMSG %s :%s", chan->name.c_str(), text);
151 FOREACH_MOD(I_OnUserMessage,OnUserMessage(user,chan,TYPE_CHANNEL,text,status,except_list));
155 /* no such nick/channel */
156 user->WriteNumeric(401, "%s %s :No such nick/channel", user->nick.c_str(), target);
162 const char* destnick = parameters[0].c_str();
166 const char* targetserver = strchr(destnick, '@');
170 std::string nickonly;
172 nickonly.assign(destnick, 0, targetserver - destnick);
173 dest = ServerInstance->FindNickOnly(nickonly);
174 if (dest && strcasecmp(dest->server.c_str(), targetserver + 1))
176 /* Incorrect server for user */
177 user->WriteNumeric(401, "%s %s :No such nick/channel",user->nick.c_str(), parameters[0].c_str());
182 dest = ServerInstance->FindNickOnly(destnick);
185 dest = ServerInstance->FindNick(destnick);
189 if (parameters[1].empty())
191 user->WriteNumeric(412, "%s :No text to send", user->nick.c_str());
197 /* auto respond with aweh msg */
198 user->WriteNumeric(301, "%s %s :%s", user->nick.c_str(), dest->nick.c_str(), dest->awaymsg.c_str());
201 ModResult MOD_RESULT;
203 std::string temp = parameters[1];
204 FIRST_MOD_RESULT(OnUserPreMessage, MOD_RESULT, (user, dest, TYPE_USER, temp, 0, except_list));
205 if (MOD_RESULT == MOD_RES_DENY)
208 const char* text = temp.c_str();
210 FOREACH_MOD(I_OnText,OnText(user, dest, TYPE_USER, text, 0, except_list));
214 // direct write, same server
215 user->WriteTo(dest, "PRIVMSG %s :%s", dest->nick.c_str(), text);
218 FOREACH_MOD(I_OnUserMessage,OnUserMessage(user, dest, TYPE_USER, text, 0, except_list));
222 /* no such nick/channel */
223 user->WriteNumeric(401, "%s %s :No such nick/channel",user->nick.c_str(), parameters[0].c_str());
229 COMMAND_INIT(CommandPrivmsg)