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 irc::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 std::multimap<irc::string, Alias> Aliases;
68 /* whether or not +B users are allowed to use fantasy commands */
70 UserModeReference botmode;
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[0];
80 ConfigTagList tags = ServerInstance->Config->ConfTags("alias");
81 for(ConfigIter i = tags.first; i != tags.second; ++i)
83 ConfigTag* tag = i->second;
85 std::string aliastext = tag->getString("text");
86 a.AliasedCommand = aliastext.c_str();
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 a.CaseSensitive = tag->getBool("matchcase");
95 Aliases.insert(std::make_pair(a.AliasedCommand, a));
101 : botmode(this, "bot")
105 void init() CXX11_OVERRIDE
110 Version GetVersion() CXX11_OVERRIDE
112 return Version("Provides aliases of commands.", VF_VENDOR);
115 std::string GetVar(std::string varname, const std::string &original_line)
117 irc::spacesepstream ss(original_line);
118 varname.erase(varname.begin());
119 int index = *(varname.begin()) - 48;
120 varname.erase(varname.begin());
121 bool everything_after = (varname == "-");
124 for (int j = 0; j < index; j++)
127 if (everything_after)
130 while (ss.GetToken(more))
140 ModResult OnPreCommand(std::string &command, std::vector<std::string> ¶meters, LocalUser *user, bool validated, const std::string &original_line) CXX11_OVERRIDE
142 std::multimap<irc::string, Alias>::iterator i, upperbound;
144 /* If theyre not registered yet, we dont want
147 if (user->registered != REG_ALL)
148 return MOD_RES_PASSTHRU;
150 /* We dont have any commands looking like this? Stop processing. */
151 i = Aliases.find(command.c_str());
152 if (i == Aliases.end())
153 return MOD_RES_PASSTHRU;
154 /* Avoid iterating on to different aliases if no patterns match. */
155 upperbound = Aliases.upper_bound(command.c_str());
157 irc::string c = command.c_str();
158 /* The parameters for the command in their original form, with the command stripped off */
159 std::string compare = original_line.substr(command.length());
160 while (*(compare.c_str()) == ' ')
161 compare.erase(compare.begin());
163 while (i != upperbound)
165 if (i->second.UserCommand)
167 if (DoAlias(user, NULL, &(i->second), compare, original_line))
176 // If we made it here, no aliases actually matched.
177 return MOD_RES_PASSTHRU;
180 void OnUserMessage(User *user, void *dest, int target_type, const std::string &text, char status, const CUList &exempt_list, MessageType msgtype) CXX11_OVERRIDE
182 if ((target_type != TYPE_CHANNEL) || (msgtype != MSG_PRIVMSG))
187 // fcommands are only for local users. Spanningtree will send them back out as their original cmd.
188 if (!user || !IS_LOCAL(user))
193 /* Stop here if the user is +B and allowbot is set to no. */
194 if (!AllowBots && user->IsModeSet(botmode))
199 Channel *c = (Channel *)dest;
200 std::string scommand;
202 // text is like "!moo cows bite me", we want "!moo" first
203 irc::spacesepstream ss(text);
204 ss.GetToken(scommand);
205 irc::string fcommand = scommand.c_str();
207 if (fcommand.empty())
212 // we don't want to touch non-fantasy stuff
213 if (*fcommand.c_str() != fprefix)
218 // nor do we give a shit about the prefix
219 fcommand.erase(fcommand.begin());
221 std::multimap<irc::string, Alias>::iterator i = Aliases.find(fcommand);
223 if (i == Aliases.end())
226 /* Avoid iterating on to other aliases if no patterns match */
227 std::multimap<irc::string, Alias>::iterator upperbound = Aliases.upper_bound(fcommand);
230 /* The parameters for the command in their original form, with the command stripped off */
231 std::string compare = text.substr(fcommand.length() + 1);
232 while (*(compare.c_str()) == ' ')
233 compare.erase(compare.begin());
235 while (i != upperbound)
237 if (i->second.ChannelCommand)
239 // We use substr(1) here to remove the fantasy prefix
240 if (DoAlias(user, c, &(i->second), compare, text.substr(1)))
249 int DoAlias(User *user, Channel *c, Alias *a, const std::string& compare, const std::string& safe)
253 /* Does it match the pattern? */
254 if (!a->format.empty())
256 if (a->CaseSensitive)
258 if (!InspIRCd::Match(compare, a->format, rfc_case_sensitive_map))
263 if (!InspIRCd::Match(compare, a->format))
268 if ((a->OperOnly) && (!user->IsOper()))
271 if (!a->RequiredNick.empty())
273 u = ServerInstance->FindNick(a->RequiredNick);
276 user->WriteNumeric(401, ""+user->nick+" "+a->RequiredNick+" :is currently unavailable. Please try again later.");
280 if ((u != NULL) && (!a->RequiredNick.empty()) && (a->ULineOnly))
282 if (!ServerInstance->ULine(u->server))
284 ServerInstance->SNO->WriteToSnoMask('a', "NOTICE -- Service "+a->RequiredNick+" required by alias "+std::string(a->AliasedCommand.c_str())+" is not on a u-lined server, possibly underhanded antics detected!");
285 user->WriteNumeric(401, ""+user->nick+" "+a->RequiredNick+" :is an imposter! Please inform an IRC operator as soon as possible.");
290 /* Now, search and replace in a copy of the original_line, replacing $1 through $9 and $1- etc */
292 std::string::size_type crlf = a->ReplaceFormat.find('\n');
294 if (crlf == std::string::npos)
296 DoCommand(a->ReplaceFormat, user, c, safe);
301 irc::sepstream commands(a->ReplaceFormat, '\n');
302 std::string scommand;
303 while (commands.GetToken(scommand))
305 DoCommand(scommand, user, c, safe);
311 void DoCommand(const std::string& newline, User* user, Channel *chan, const std::string &original_line)
314 result.reserve(newline.length());
315 for (unsigned int i = 0; i < newline.length(); i++)
320 if (isdigit(newline[i+1]))
322 int len = (newline[i+2] == '-') ? 3 : 2;
323 std::string var = newline.substr(i, len);
324 result.append(GetVar(var, original_line));
327 else if (newline.substr(i, 5) == "$nick")
329 result.append(user->nick);
332 else if (newline.substr(i, 5) == "$host")
334 result.append(user->host);
337 else if (newline.substr(i, 5) == "$chan")
340 result.append(chan->name);
343 else if (newline.substr(i, 6) == "$ident")
345 result.append(user->ident);
348 else if (newline.substr(i, 6) == "$vhost")
350 result.append(user->dhost);
360 irc::tokenstream ss(result);
361 std::vector<std::string> pars;
362 std::string command, token;
364 ss.GetToken(command);
365 while (ss.GetToken(token))
367 pars.push_back(token);
369 ServerInstance->Parser->CallHandler(command, pars, user);
372 void OnRehash(User* user) CXX11_OVERRIDE
379 // Prioritise after spanningtree so that channel aliases show the alias before the effects.
380 Module* linkmod = ServerInstance->Modules->Find("m_spanningtree.so");
381 ServerInstance->Modules->SetPriority(this, I_OnUserMessage, PRIORITY_AFTER, &linkmod);
385 MODULE_INIT(ModuleAlias)