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;
35 MessageCommandBase(Module* parent, MessageType mt)
36 : Command(parent, MessageTypeString[mt], 2, 2)
37 , moderatedmode(parent, "moderated")
38 , noextmsgmode(parent, "noextmsg")
40 syntax = "<target>{,<target>} <message>";
44 * @param parameters The parameters to the command
45 * @param user The user issuing the command
46 * @return A value from CmdResult to indicate command success or failure.
48 CmdResult HandleMessage(const std::vector<std::string>& parameters, User* user, MessageType mt);
50 RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
53 // This is handled by the OnUserMessage hook to split the LoopCall pieces
54 return ROUTE_LOCALONLY;
56 return ROUTE_MESSAGE(parameters[0]);
60 CmdResult MessageCommandBase::HandleMessage(const std::vector<std::string>& parameters, User* user, MessageType mt)
66 LocalUser* localuser = IS_LOCAL(user);
68 localuser->idle_lastmsg = ServerInstance->Time();
70 if (CommandParser::LoopCall(user, this, parameters, 0))
73 if (parameters[0][0] == '$')
75 if (!user->HasPrivPermission("users/mass-message"))
79 std::string temp = parameters[1];
80 FIRST_MOD_RESULT(OnUserPreMessage, MOD_RESULT, (user, (void*)parameters[0].c_str(), TYPE_SERVER, temp, 0, except_list, mt));
81 if (MOD_RESULT == MOD_RES_DENY)
84 const char* text = temp.c_str();
85 const char* servermask = (parameters[0].c_str()) + 1;
87 FOREACH_MOD(OnText, (user, (void*)parameters[0].c_str(), TYPE_SERVER, text, 0, except_list));
88 if (InspIRCd::Match(ServerInstance->Config->ServerName, servermask, NULL))
90 user->SendAll(MessageTypeString[mt], "%s", text);
92 FOREACH_MOD(OnUserMessage, (user, (void*)parameters[0].c_str(), TYPE_SERVER, text, 0, except_list, mt));
96 const char* target = parameters[0].c_str();
98 if (ServerInstance->Modes->FindPrefix(*target))
105 chan = ServerInstance->FindChan(target);
107 except_list.insert(user);
111 if (localuser && chan->GetPrefixValue(user) < VOICE_VALUE)
113 if (chan->IsModeSet(noextmsgmode) && !chan->HasUser(user))
115 user->WriteNumeric(ERR_CANNOTSENDTOCHAN, "%s :Cannot send to channel (no external messages)", chan->name.c_str());
119 if (chan->IsModeSet(moderatedmode))
121 user->WriteNumeric(ERR_CANNOTSENDTOCHAN, "%s :Cannot send to channel (+m)", chan->name.c_str());
125 if (ServerInstance->Config->RestrictBannedUsers)
127 if (chan->IsBanned(user))
129 user->WriteNumeric(ERR_CANNOTSENDTOCHAN, "%s :Cannot send to channel (you're banned)", chan->name.c_str());
134 ModResult MOD_RESULT;
136 std::string temp = parameters[1];
137 FIRST_MOD_RESULT(OnUserPreMessage, MOD_RESULT, (user, chan, TYPE_CHANNEL, temp, status, except_list, mt));
138 if (MOD_RESULT == MOD_RES_DENY)
141 const char* text = temp.c_str();
143 /* Check again, a module may have zapped the input string */
146 user->WriteNumeric(ERR_NOTEXTTOSEND, ":No text to send");
150 FOREACH_MOD(OnText, (user,chan,TYPE_CHANNEL,text,status,except_list));
154 if (ServerInstance->Config->UndernetMsgPrefix)
156 chan->WriteAllExcept(user, false, status, except_list, "%s %c%s :%c %s", MessageTypeString[mt], status, chan->name.c_str(), status, text);
160 chan->WriteAllExcept(user, false, status, except_list, "%s %c%s :%s", MessageTypeString[mt], status, chan->name.c_str(), text);
165 chan->WriteAllExcept(user, false, status, except_list, "%s %s :%s", MessageTypeString[mt], chan->name.c_str(), text);
168 FOREACH_MOD(OnUserMessage, (user,chan, TYPE_CHANNEL, text, status, except_list, mt));
172 /* no such nick/channel */
173 user->WriteNumeric(ERR_NOSUCHNICK, "%s :No such nick/channel", target);
179 const char* destnick = parameters[0].c_str();
183 const char* targetserver = strchr(destnick, '@');
187 std::string nickonly;
189 nickonly.assign(destnick, 0, targetserver - destnick);
190 dest = ServerInstance->FindNickOnly(nickonly);
191 if (dest && strcasecmp(dest->server->GetName().c_str(), targetserver + 1))
193 /* Incorrect server for user */
194 user->WriteNumeric(ERR_NOSUCHNICK, "%s :No such nick/channel", parameters[0].c_str());
199 dest = ServerInstance->FindNickOnly(destnick);
202 dest = ServerInstance->FindNick(destnick);
204 if ((dest) && (dest->registered == REG_ALL))
206 if (parameters[1].empty())
208 user->WriteNumeric(ERR_NOTEXTTOSEND, ":No text to send");
212 if ((dest->IsAway()) && (mt == MSG_PRIVMSG))
214 /* auto respond with aweh msg */
215 user->WriteNumeric(RPL_AWAY, "%s :%s", dest->nick.c_str(), dest->awaymsg.c_str());
218 ModResult MOD_RESULT;
220 std::string temp = parameters[1];
221 FIRST_MOD_RESULT(OnUserPreMessage, MOD_RESULT, (user, dest, TYPE_USER, temp, 0, except_list, mt));
222 if (MOD_RESULT == MOD_RES_DENY)
225 const char* text = temp.c_str();
227 FOREACH_MOD(OnText, (user, dest, TYPE_USER, text, 0, except_list));
231 // direct write, same server
232 user->WriteTo(dest, "%s %s :%s", MessageTypeString[mt], dest->nick.c_str(), text);
235 FOREACH_MOD(OnUserMessage, (user, dest, TYPE_USER, text, 0, except_list, mt));
239 /* no such nick/channel */
240 user->WriteNumeric(ERR_NOSUCHNICK, "%s :No such nick/channel", parameters[0].c_str());
246 template<MessageType MT>
247 class CommandMessage : public MessageCommandBase
250 CommandMessage(Module* parent)
251 : MessageCommandBase(parent, MT)
255 CmdResult Handle(const std::vector<std::string>& parameters, User* user)
257 return HandleMessage(parameters, user, MT);
261 class ModuleCoreMessage : public Module
263 CommandMessage<MSG_PRIVMSG> CommandPrivmsg;
264 CommandMessage<MSG_NOTICE> CommandNotice;
268 : CommandPrivmsg(this), CommandNotice(this)
274 return Version("PRIVMSG, NOTICE", VF_CORE|VF_VENDOR);
278 MODULE_INIT(ModuleCoreMessage)