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 /* is case sensitive params */
48 /* whether or not it may be executed via fantasy (default OFF) */
51 /* whether or not it may be executed via /command (default ON) */
54 /** Format that must be matched for use */
58 class ModuleAlias : public Module
62 /* We cant use a map, there may be multiple aliases with the same name.
63 * We can, however, use a fancy invention: the multimap. Maps a key to one or more values.
66 typedef insp::flat_multimap<std::string, Alias, irc::insensitive_swo> AliasMap;
70 /* whether or not +B users are allowed to use fantasy commands */
72 UserModeReference botmode;
75 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
77 ConfigTag* fantasy = ServerInstance->Config->ConfValue("fantasy");
78 AllowBots = fantasy->getBool("allowbots", false);
79 std::string fpre = fantasy->getString("prefix");
80 fprefix = fpre.empty() ? "!" : fpre;
83 ConfigTagList tags = ServerInstance->Config->ConfTags("alias");
84 for(ConfigIter i = tags.first; i != tags.second; ++i)
86 ConfigTag* tag = i->second;
88 a.AliasedCommand = tag->getString("text");
89 std::transform(a.AliasedCommand.begin(), a.AliasedCommand.end(), a.AliasedCommand.begin(), ::toupper);
90 tag->readString("replace", a.ReplaceFormat, true);
91 a.RequiredNick = tag->getString("requires");
92 a.ULineOnly = tag->getBool("uline");
93 a.ChannelCommand = tag->getBool("channelcommand", false);
94 a.UserCommand = tag->getBool("usercommand", true);
95 a.OperOnly = tag->getBool("operonly");
96 a.format = tag->getString("format");
97 a.CaseSensitive = tag->getBool("matchcase");
98 Aliases.insert(std::make_pair(a.AliasedCommand, a));
103 : botmode(this, "bot")
107 Version GetVersion() CXX11_OVERRIDE
109 return Version("Provides aliases of commands.", VF_VENDOR);
112 std::string GetVar(std::string varname, const std::string &original_line)
114 irc::spacesepstream ss(original_line);
115 varname.erase(varname.begin());
116 int index = *(varname.begin()) - 48;
117 varname.erase(varname.begin());
118 bool everything_after = (varname == "-");
121 for (int j = 0; j < index; j++)
124 if (everything_after)
127 while (ss.GetToken(more))
137 ModResult OnPreCommand(std::string &command, std::vector<std::string> ¶meters, LocalUser *user, bool validated, const std::string &original_line) CXX11_OVERRIDE
139 /* If theyre not registered yet, we dont want
142 if (user->registered != REG_ALL)
143 return MOD_RES_PASSTHRU;
145 /* We dont have any commands looking like this? Stop processing. */
146 std::pair<AliasMap::iterator, AliasMap::iterator> iters = Aliases.equal_range(command);
147 if (iters.first == iters.second)
148 return MOD_RES_PASSTHRU;
150 /* The parameters for the command in their original form, with the command stripped off */
151 std::string compare(original_line, command.length());
152 while (*(compare.c_str()) == ' ')
153 compare.erase(compare.begin());
155 for (AliasMap::iterator i = iters.first; i != iters.second; ++i)
157 if (i->second.UserCommand)
159 if (DoAlias(user, NULL, &(i->second), compare, original_line))
166 // If we made it here, no aliases actually matched.
167 return MOD_RES_PASSTHRU;
170 void OnUserMessage(User *user, void *dest, int target_type, const std::string &text, char status, const CUList &exempt_list, MessageType msgtype) CXX11_OVERRIDE
172 if ((target_type != TYPE_CHANNEL) || (msgtype != MSG_PRIVMSG))
177 // fcommands are only for local users. Spanningtree will send them back out as their original cmd.
183 /* Stop here if the user is +B and allowbot is set to no. */
184 if (!AllowBots && user->IsModeSet(botmode))
189 Channel *c = (Channel *)dest;
190 std::string scommand;
192 // text is like "!moo cows bite me", we want "!moo" first
193 irc::spacesepstream ss(text);
194 ss.GetToken(scommand);
196 if (scommand.size() <= fprefix.size())
201 // we don't want to touch non-fantasy stuff
202 if (scommand.compare(0, fprefix.size(), fprefix) != 0)
207 // nor do we give a shit about the prefix
208 scommand.erase(0, fprefix.size());
210 std::pair<AliasMap::iterator, AliasMap::iterator> iters = Aliases.equal_range(scommand);
211 if (iters.first == iters.second)
214 /* The parameters for the command in their original form, with the command stripped off */
215 std::string compare(text, scommand.length() + fprefix.size());
216 while (*(compare.c_str()) == ' ')
217 compare.erase(compare.begin());
219 for (AliasMap::iterator i = iters.first; i != iters.second; ++i)
221 if (i->second.ChannelCommand)
223 // We use substr here to remove the fantasy prefix
224 if (DoAlias(user, c, &(i->second), compare, text.substr(fprefix.size())))
231 int DoAlias(User *user, Channel *c, Alias *a, const std::string& compare, const std::string& safe)
233 /* Does it match the pattern? */
234 if (!a->format.empty())
236 if (a->CaseSensitive)
238 if (!InspIRCd::Match(compare, a->format, rfc_case_sensitive_map))
243 if (!InspIRCd::Match(compare, a->format))
248 if ((a->OperOnly) && (!user->IsOper()))
251 if (!a->RequiredNick.empty())
253 User* u = ServerInstance->FindNick(a->RequiredNick);
256 user->WriteNumeric(ERR_NOSUCHNICK, a->RequiredNick, "is currently unavailable. Please try again later.");
260 if ((a->ULineOnly) && (!u->server->IsULine()))
262 ServerInstance->SNO->WriteToSnoMask('a', "NOTICE -- Service "+a->RequiredNick+" required by alias "+a->AliasedCommand+" is not on a u-lined server, possibly underhanded antics detected!");
263 user->WriteNumeric(ERR_NOSUCHNICK, a->RequiredNick, "is an imposter! Please inform an IRC operator as soon as possible.");
268 /* Now, search and replace in a copy of the original_line, replacing $1 through $9 and $1- etc */
270 std::string::size_type crlf = a->ReplaceFormat.find('\n');
272 if (crlf == std::string::npos)
274 DoCommand(a->ReplaceFormat, user, c, safe);
279 irc::sepstream commands(a->ReplaceFormat, '\n');
280 std::string scommand;
281 while (commands.GetToken(scommand))
283 DoCommand(scommand, user, c, safe);
289 void DoCommand(const std::string& newline, User* user, Channel *chan, const std::string &original_line)
292 result.reserve(newline.length());
293 for (unsigned int i = 0; i < newline.length(); i++)
296 if ((c == '$') && (i + 1 < newline.length()))
298 if (isdigit(newline[i+1]))
300 int len = ((i + 2 < newline.length()) && (newline[i+2] == '-')) ? 3 : 2;
301 std::string var = newline.substr(i, len);
302 result.append(GetVar(var, original_line));
305 else if (!newline.compare(i, 5, "$nick", 5))
307 result.append(user->nick);
310 else if (!newline.compare(i, 5, "$host", 5))
312 result.append(user->host);
315 else if (!newline.compare(i, 5, "$chan", 5))
318 result.append(chan->name);
321 else if (!newline.compare(i, 6, "$ident", 6))
323 result.append(user->ident);
326 else if (!newline.compare(i, 6, "$vhost", 6))
328 result.append(user->dhost);
338 irc::tokenstream ss(result);
339 std::vector<std::string> pars;
340 std::string command, token;
342 ss.GetToken(command);
343 while (ss.GetToken(token))
345 pars.push_back(token);
347 ServerInstance->Parser.CallHandler(command, pars, user);
352 // Prioritise after spanningtree so that channel aliases show the alias before the effects.
353 Module* linkmod = ServerInstance->Modules->Find("m_spanningtree.so");
354 ServerInstance->Modules->SetPriority(this, I_OnUserMessage, PRIORITY_AFTER, linkmod);
358 MODULE_INIT(ModuleAlias)