2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2005, 2009 Robin Burchell <robin+git@viroteck.net>
6 * Copyright (C) 2004-2007, 2009 Craig Edwards <craigedwards@brainbox.cc>
7 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
9 * This file is part of InspIRCd. InspIRCd is free software: you can
10 * redistribute it and/or modify it under the terms of the GNU General Public
11 * License as published by the Free Software Foundation, version 2.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 /** An alias definition
30 /** The text of the alias command */
31 std::string AliasedCommand;
33 /** Text to replace with */
34 std::string ReplaceFormat;
36 /** Nickname required to perform alias */
37 std::string RequiredNick;
39 /** Alias requires ulined server */
45 /* whether or not it may be executed via fantasy (default OFF) */
48 /* whether or not it may be executed via /command (default ON) */
51 /** Format that must be matched for use */
55 class ModuleAlias : public Module
59 /* We cant use a map, there may be multiple aliases with the same name.
60 * We can, however, use a fancy invention: the multimap. Maps a key to one or more values.
63 typedef insp::flat_multimap<std::string, Alias, irc::insensitive_swo> AliasMap;
67 /* whether or not +B users are allowed to use fantasy commands */
69 UserModeReference botmode;
72 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
74 ConfigTag* fantasy = ServerInstance->Config->ConfValue("fantasy");
75 AllowBots = fantasy->getBool("allowbots", false);
76 std::string fpre = fantasy->getString("prefix");
77 fprefix = fpre.empty() ? "!" : fpre;
80 ConfigTagList tags = ServerInstance->Config->ConfTags("alias");
81 for(ConfigIter i = tags.first; i != tags.second; ++i)
83 ConfigTag* tag = i->second;
85 a.AliasedCommand = tag->getString("text");
86 std::transform(a.AliasedCommand.begin(), a.AliasedCommand.end(), a.AliasedCommand.begin(), ::toupper);
87 tag->readString("replace", a.ReplaceFormat, true);
88 a.RequiredNick = tag->getString("requires");
89 a.ULineOnly = tag->getBool("uline");
90 a.ChannelCommand = tag->getBool("channelcommand", false);
91 a.UserCommand = tag->getBool("usercommand", true);
92 a.OperOnly = tag->getBool("operonly");
93 a.format = tag->getString("format");
94 Aliases.insert(std::make_pair(a.AliasedCommand, a));
99 : botmode(this, "bot")
103 Version GetVersion() CXX11_OVERRIDE
105 return Version("Provides aliases of commands.", VF_VENDOR);
108 std::string GetVar(std::string varname, const std::string &original_line)
110 irc::spacesepstream ss(original_line);
111 varname.erase(varname.begin());
112 int index = *(varname.begin()) - 48;
113 varname.erase(varname.begin());
114 bool everything_after = (varname == "-");
117 for (int j = 0; j < index; j++)
120 if (everything_after)
123 while (ss.GetToken(more))
133 ModResult OnPreCommand(std::string &command, std::vector<std::string> ¶meters, LocalUser *user, bool validated, const std::string &original_line) CXX11_OVERRIDE
135 /* If theyre not registered yet, we dont want
138 if (user->registered != REG_ALL)
139 return MOD_RES_PASSTHRU;
141 /* We dont have any commands looking like this? Stop processing. */
142 std::pair<AliasMap::iterator, AliasMap::iterator> iters = Aliases.equal_range(command);
143 if (iters.first == iters.second)
144 return MOD_RES_PASSTHRU;
146 /* The parameters for the command in their original form, with the command stripped off */
147 std::string compare(original_line, command.length());
148 while (*(compare.c_str()) == ' ')
149 compare.erase(compare.begin());
151 for (AliasMap::iterator i = iters.first; i != iters.second; ++i)
153 if (i->second.UserCommand)
155 if (DoAlias(user, NULL, &(i->second), compare, original_line))
162 // If we made it here, no aliases actually matched.
163 return MOD_RES_PASSTHRU;
166 void OnUserMessage(User *user, void *dest, int target_type, const std::string &text, char status, const CUList &exempt_list, MessageType msgtype) CXX11_OVERRIDE
168 if ((target_type != TYPE_CHANNEL) || (msgtype != MSG_PRIVMSG))
173 // fcommands are only for local users. Spanningtree will send them back out as their original cmd.
179 /* Stop here if the user is +B and allowbot is set to no. */
180 if (!AllowBots && user->IsModeSet(botmode))
185 Channel *c = (Channel *)dest;
186 std::string scommand;
188 // text is like "!moo cows bite me", we want "!moo" first
189 irc::spacesepstream ss(text);
190 ss.GetToken(scommand);
192 if (scommand.size() <= fprefix.size())
197 // we don't want to touch non-fantasy stuff
198 if (scommand.compare(0, fprefix.size(), fprefix) != 0)
203 // nor do we give a shit about the prefix
204 scommand.erase(0, fprefix.size());
206 std::pair<AliasMap::iterator, AliasMap::iterator> iters = Aliases.equal_range(scommand);
207 if (iters.first == iters.second)
210 /* The parameters for the command in their original form, with the command stripped off */
211 std::string compare(text, scommand.length() + fprefix.size());
212 while (*(compare.c_str()) == ' ')
213 compare.erase(compare.begin());
215 for (AliasMap::iterator i = iters.first; i != iters.second; ++i)
217 if (i->second.ChannelCommand)
219 // We use substr here to remove the fantasy prefix
220 if (DoAlias(user, c, &(i->second), compare, text.substr(fprefix.size())))
227 int DoAlias(User *user, Channel *c, Alias *a, const std::string& compare, const std::string& safe)
229 /* Does it match the pattern? */
230 if (!a->format.empty())
232 if (!InspIRCd::Match(compare, a->format))
236 if ((a->OperOnly) && (!user->IsOper()))
239 if (!a->RequiredNick.empty())
241 User* u = ServerInstance->FindNick(a->RequiredNick);
244 user->WriteNumeric(ERR_NOSUCHNICK, a->RequiredNick, "is currently unavailable. Please try again later.");
248 if ((a->ULineOnly) && (!u->server->IsULine()))
250 ServerInstance->SNO->WriteToSnoMask('a', "NOTICE -- Service "+a->RequiredNick+" required by alias "+a->AliasedCommand+" is not on a u-lined server, possibly underhanded antics detected!");
251 user->WriteNumeric(ERR_NOSUCHNICK, a->RequiredNick, "is an imposter! Please inform an IRC operator as soon as possible.");
256 /* Now, search and replace in a copy of the original_line, replacing $1 through $9 and $1- etc */
258 std::string::size_type crlf = a->ReplaceFormat.find('\n');
260 if (crlf == std::string::npos)
262 DoCommand(a->ReplaceFormat, user, c, safe, a);
267 irc::sepstream commands(a->ReplaceFormat, '\n');
268 std::string scommand;
269 while (commands.GetToken(scommand))
271 DoCommand(scommand, user, c, safe, a);
277 void DoCommand(const std::string& newline, User* user, Channel *chan, const std::string &original_line, Alias* a)
280 result.reserve(newline.length());
281 for (unsigned int i = 0; i < newline.length(); i++)
284 if ((c == '$') && (i + 1 < newline.length()))
286 if (isdigit(newline[i+1]))
288 int len = ((i + 2 < newline.length()) && (newline[i+2] == '-')) ? 3 : 2;
289 std::string var = newline.substr(i, len);
290 result.append(GetVar(var, original_line));
293 else if (!newline.compare(i, 5, "$nick", 5))
295 result.append(user->nick);
298 else if (!newline.compare(i, 5, "$host", 5))
300 result.append(user->host);
303 else if (!newline.compare(i, 5, "$chan", 5))
306 result.append(chan->name);
309 else if (!newline.compare(i, 6, "$ident", 6))
311 result.append(user->ident);
314 else if (!newline.compare(i, 6, "$vhost", 6))
316 result.append(user->dhost);
319 else if (!newline.compare(i, 12, "$requirement", 12))
321 result.append(a->RequiredNick);
331 irc::tokenstream ss(result);
332 std::vector<std::string> pars;
333 std::string command, token;
335 ss.GetToken(command);
336 while (ss.GetToken(token))
338 pars.push_back(token);
340 ServerInstance->Parser.CallHandler(command, pars, user);
343 void Prioritize() CXX11_OVERRIDE
345 // Prioritise after spanningtree so that channel aliases show the alias before the effects.
346 Module* linkmod = ServerInstance->Modules->Find("m_spanningtree.so");
347 ServerInstance->Modules->SetPriority(this, I_OnUserMessage, PRIORITY_AFTER, linkmod);
351 MODULE_INIT(ModuleAlias)