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/>.
26 const char* MessageTypeString[] = { "PRIVMSG", "NOTICE" };
29 class MessageCommandBase : public Command
31 ChanModeReference moderatedmode;
32 ChanModeReference noextmsgmode;
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)
39 static void SendAll(User* user, const std::string& msg, MessageType mt);
42 MessageCommandBase(Module* parent, MessageType mt)
43 : Command(parent, MessageTypeString[mt], 2, 2)
44 , moderatedmode(parent, "moderated")
45 , noextmsgmode(parent, "noextmsg")
47 syntax = "<target>{,<target>} <message>";
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.
55 CmdResult HandleMessage(const std::vector<std::string>& parameters, User* user, MessageType mt);
57 RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters) CXX11_OVERRIDE
60 // This is handled by the OnUserMessage hook to split the LoopCall pieces
61 return ROUTE_LOCALONLY;
63 return ROUTE_MESSAGE(parameters[0]);
67 void MessageCommandBase::SendAll(User* user, const std::string& msg, MessageType mt)
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)
73 if ((*i)->registered == REG_ALL)
78 CmdResult MessageCommandBase::HandleMessage(const std::vector<std::string>& parameters, User* user, MessageType mt)
84 LocalUser* localuser = IS_LOCAL(user);
86 localuser->idle_lastmsg = ServerInstance->Time();
88 if (CommandParser::LoopCall(user, this, parameters, 0))
91 if (parameters[0][0] == '$')
93 if (!user->HasPrivPermission("users/mass-message"))
97 std::string temp = parameters[1];
98 FIRST_MOD_RESULT(OnUserPreMessage, MOD_RESULT, (user, (void*)parameters[0].c_str(), TYPE_SERVER, temp, 0, except_list, mt));
99 if (MOD_RESULT == MOD_RES_DENY)
102 const char* text = temp.c_str();
103 const char* servermask = (parameters[0].c_str()) + 1;
105 FOREACH_MOD(OnText, (user, (void*)parameters[0].c_str(), TYPE_SERVER, text, 0, except_list));
106 if (InspIRCd::Match(ServerInstance->Config->ServerName, servermask, NULL))
108 SendAll(user, text, mt);
110 FOREACH_MOD(OnUserMessage, (user, (void*)parameters[0].c_str(), TYPE_SERVER, text, 0, except_list, mt));
114 const char* target = parameters[0].c_str();
116 if (ServerInstance->Modes->FindPrefix(*target))
123 chan = ServerInstance->FindChan(target);
125 except_list.insert(user);
129 if (localuser && chan->GetPrefixValue(user) < VOICE_VALUE)
131 if (chan->IsModeSet(noextmsgmode) && !chan->HasUser(user))
133 user->WriteNumeric(ERR_CANNOTSENDTOCHAN, chan->name, "Cannot send to channel (no external messages)");
137 if (chan->IsModeSet(moderatedmode))
139 user->WriteNumeric(ERR_CANNOTSENDTOCHAN, chan->name, "Cannot send to channel (+m)");
143 if (ServerInstance->Config->RestrictBannedUsers)
145 if (chan->IsBanned(user))
147 user->WriteNumeric(ERR_CANNOTSENDTOCHAN, chan->name, "Cannot send to channel (you're banned)");
152 ModResult MOD_RESULT;
154 std::string temp = parameters[1];
155 FIRST_MOD_RESULT(OnUserPreMessage, MOD_RESULT, (user, chan, TYPE_CHANNEL, temp, status, except_list, mt));
156 if (MOD_RESULT == MOD_RES_DENY)
159 const char* text = temp.c_str();
161 /* Check again, a module may have zapped the input string */
164 user->WriteNumeric(ERR_NOTEXTTOSEND, "No text to send");
168 FOREACH_MOD(OnText, (user,chan,TYPE_CHANNEL,text,status,except_list));
172 chan->WriteAllExcept(user, false, status, except_list, "%s %c%s :%s", MessageTypeString[mt], status, chan->name.c_str(), text);
176 chan->WriteAllExcept(user, false, status, except_list, "%s %s :%s", MessageTypeString[mt], chan->name.c_str(), text);
179 FOREACH_MOD(OnUserMessage, (user,chan, TYPE_CHANNEL, text, status, except_list, mt));
183 /* no such nick/channel */
184 user->WriteNumeric(Numerics::NoSuchNick(target));
190 const char* destnick = parameters[0].c_str();
194 const char* targetserver = strchr(destnick, '@');
198 std::string nickonly;
200 nickonly.assign(destnick, 0, targetserver - destnick);
201 dest = ServerInstance->FindNickOnly(nickonly);
202 if (dest && strcasecmp(dest->server->GetName().c_str(), targetserver + 1))
204 /* Incorrect server for user */
205 user->WriteNumeric(Numerics::NoSuchNick(parameters[0]));
210 dest = ServerInstance->FindNickOnly(destnick);
213 dest = ServerInstance->FindNick(destnick);
215 if ((dest) && (dest->registered == REG_ALL))
217 if (parameters[1].empty())
219 user->WriteNumeric(ERR_NOTEXTTOSEND, "No text to send");
223 if ((dest->IsAway()) && (mt == MSG_PRIVMSG))
225 /* auto respond with aweh msg */
226 user->WriteNumeric(RPL_AWAY, dest->nick, dest->awaymsg);
229 ModResult MOD_RESULT;
231 std::string temp = parameters[1];
232 FIRST_MOD_RESULT(OnUserPreMessage, MOD_RESULT, (user, dest, TYPE_USER, temp, 0, except_list, mt));
233 if (MOD_RESULT == MOD_RES_DENY)
236 const char* text = temp.c_str();
238 FOREACH_MOD(OnText, (user, dest, TYPE_USER, text, 0, except_list));
242 // direct write, same server
243 dest->WriteFrom(user, "%s %s :%s", MessageTypeString[mt], dest->nick.c_str(), text);
246 FOREACH_MOD(OnUserMessage, (user, dest, TYPE_USER, text, 0, except_list, mt));
250 /* no such nick/channel */
251 user->WriteNumeric(Numerics::NoSuchNick(parameters[0]));
257 template<MessageType MT>
258 class CommandMessage : public MessageCommandBase
261 CommandMessage(Module* parent)
262 : MessageCommandBase(parent, MT)
266 CmdResult Handle(const std::vector<std::string>& parameters, User* user) CXX11_OVERRIDE
268 return HandleMessage(parameters, user, MT);
272 class ModuleCoreMessage : public Module
274 CommandMessage<MSG_PRIVMSG> CommandPrivmsg;
275 CommandMessage<MSG_NOTICE> CommandNotice;
279 : CommandPrivmsg(this), CommandNotice(this)
283 Version GetVersion() CXX11_OVERRIDE
285 return Version("PRIVMSG, NOTICE", VF_CORE|VF_VENDOR);
289 MODULE_INIT(ModuleCoreMessage)