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 /* $ModDesc: Provides aliases of commands. */
27 /** An alias definition
32 /** The text of the alias command */
33 irc::string AliasedCommand;
35 /** Text to replace with */
36 std::string ReplaceFormat;
38 /** Nickname required to perform alias */
39 std::string RequiredNick;
41 /** Alias requires ulined server */
47 /* is case sensitive params */
50 /* whether or not it may be executed via fantasy (default OFF) */
53 /* whether or not it may be executed via /command (default ON) */
56 /** Format that must be matched for use */
60 class ModuleAlias : public Module
66 /* We cant use a map, there may be multiple aliases with the same name.
67 * We can, however, use a fancy invention: the multimap. Maps a key to one or more values.
70 std::multimap<irc::string, Alias> Aliases;
72 /* whether or not +B users are allowed to use fantasy commands */
75 virtual void ReadAliases()
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[0];
83 ConfigTagList tags = ServerInstance->Config->ConfTags("alias");
84 for(ConfigIter i = tags.first; i != tags.second; ++i)
86 ConfigTag* tag = i->second;
88 std::string aliastext = tag->getString("text");
89 a.AliasedCommand = aliastext.c_str();
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));
107 Implementation eventlist[] = { I_OnPreCommand, I_OnRehash, I_OnUserMessage };
108 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
111 virtual ~ModuleAlias()
115 virtual Version GetVersion()
117 return Version("Provides aliases of commands.", VF_VENDOR);
120 std::string GetVar(std::string varname, const std::string &original_line)
122 irc::spacesepstream ss(original_line);
123 varname.erase(varname.begin());
124 int index = *(varname.begin()) - 48;
125 varname.erase(varname.begin());
126 bool everything_after = (varname == "-");
129 for (int j = 0; j < index; j++)
132 if (everything_after)
135 while (ss.GetToken(more))
145 virtual ModResult OnPreCommand(std::string &command, std::vector<std::string> ¶meters, LocalUser *user, bool validated, const std::string &original_line)
147 std::multimap<irc::string, Alias>::iterator i, upperbound;
149 /* If theyre not registered yet, we dont want
152 if (user->registered != REG_ALL)
153 return MOD_RES_PASSTHRU;
155 /* We dont have any commands looking like this? Stop processing. */
156 i = Aliases.find(command.c_str());
157 if (i == Aliases.end())
158 return MOD_RES_PASSTHRU;
159 /* Avoid iterating on to different aliases if no patterns match. */
160 upperbound = Aliases.upper_bound(command.c_str());
162 irc::string c = command.c_str();
163 /* The parameters for the command in their original form, with the command stripped off */
164 std::string compare = original_line.substr(command.length());
165 while (*(compare.c_str()) == ' ')
166 compare.erase(compare.begin());
168 while (i != upperbound)
170 if (i->second.UserCommand)
172 if (DoAlias(user, NULL, &(i->second), compare, original_line))
181 // If we made it here, no aliases actually matched.
182 return MOD_RES_PASSTHRU;
185 virtual void OnUserMessage(User *user, void *dest, int target_type, const std::string &text, char status, const CUList &exempt_list)
187 if (target_type != TYPE_CHANNEL)
192 // fcommands are only for local users. Spanningtree will send them back out as their original cmd.
193 if (!user || !IS_LOCAL(user))
198 /* Stop here if the user is +B and allowbot is set to no. */
199 if (!AllowBots && user->IsModeSet('B'))
204 Channel *c = (Channel *)dest;
205 std::string scommand;
207 // text is like "!moo cows bite me", we want "!moo" first
208 irc::spacesepstream ss(text);
209 ss.GetToken(scommand);
210 irc::string fcommand = scommand.c_str();
212 if (fcommand.empty())
217 // we don't want to touch non-fantasy stuff
218 if (*fcommand.c_str() != fprefix)
223 // nor do we give a shit about the prefix
224 fcommand.erase(fcommand.begin());
226 std::multimap<irc::string, Alias>::iterator i = Aliases.find(fcommand);
228 if (i == Aliases.end())
231 /* Avoid iterating on to other aliases if no patterns match */
232 std::multimap<irc::string, Alias>::iterator upperbound = Aliases.upper_bound(fcommand);
235 /* The parameters for the command in their original form, with the command stripped off */
236 std::string compare = text.substr(fcommand.length() + 1);
237 while (*(compare.c_str()) == ' ')
238 compare.erase(compare.begin());
240 while (i != upperbound)
242 if (i->second.ChannelCommand)
244 // We use substr(1) here to remove the fantasy prefix
245 if (DoAlias(user, c, &(i->second), compare, text.substr(1)))
254 int DoAlias(User *user, Channel *c, Alias *a, const std::string compare, const std::string safe)
258 /* Does it match the pattern? */
259 if (!a->format.empty())
261 if (a->CaseSensitive)
263 if (!InspIRCd::Match(compare, a->format, rfc_case_sensitive_map))
268 if (!InspIRCd::Match(compare, a->format))
273 if ((a->OperOnly) && (!IS_OPER(user)))
276 if (!a->RequiredNick.empty())
278 u = ServerInstance->FindNick(a->RequiredNick);
281 user->WriteNumeric(401, ""+user->nick+" "+a->RequiredNick+" :is currently unavailable. Please try again later.");
285 if ((u != NULL) && (!a->RequiredNick.empty()) && (a->ULineOnly))
287 if (!ServerInstance->ULine(u->server))
289 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!");
290 user->WriteNumeric(401, ""+user->nick+" "+a->RequiredNick+" :is an imposter! Please inform an IRC operator as soon as possible.");
295 /* Now, search and replace in a copy of the original_line, replacing $1 through $9 and $1- etc */
297 std::string::size_type crlf = a->ReplaceFormat.find('\n');
299 if (crlf == std::string::npos)
301 DoCommand(a->ReplaceFormat, user, c, safe);
306 irc::sepstream commands(a->ReplaceFormat, '\n');
307 std::string scommand;
308 while (commands.GetToken(scommand))
310 DoCommand(scommand, user, c, safe);
316 void DoCommand(const std::string& newline, User* user, Channel *chan, const std::string &original_line)
319 result.reserve(MAXBUF);
320 for (unsigned int i = 0; i < newline.length(); i++)
325 if (isdigit(newline[i+1]))
327 int len = (newline[i+2] == '-') ? 3 : 2;
328 std::string var = newline.substr(i, len);
329 result.append(GetVar(var, original_line));
332 else if (newline.substr(i, 5) == "$nick")
334 result.append(user->nick);
337 else if (newline.substr(i, 5) == "$host")
339 result.append(user->host);
342 else if (newline.substr(i, 5) == "$chan")
345 result.append(chan->name);
348 else if (newline.substr(i, 6) == "$ident")
350 result.append(user->ident);
353 else if (newline.substr(i, 6) == "$vhost")
355 result.append(user->dhost);
365 irc::tokenstream ss(result);
366 std::vector<std::string> pars;
367 std::string command, token;
369 ss.GetToken(command);
370 while (ss.GetToken(token) && (pars.size() <= MAXPARAMETERS))
372 pars.push_back(token);
374 ServerInstance->Parser->CallHandler(command, pars, user);
377 virtual void OnRehash(User* user)
382 virtual void Prioritize()
384 // Prioritise after spanningtree so that channel aliases show the alias before the effects.
385 Module* linkmod = ServerInstance->Modules->Find("m_spanningtree.so");
386 ServerInstance->Modules->SetPriority(this, I_OnUserMessage, PRIORITY_AFTER, &linkmod);
390 MODULE_INIT(ModuleAlias)