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()
79 AllowBots = MyConf.ReadFlag("fantasy", "allowbots", "no", 0);
81 std::string fpre = MyConf.ReadValue("fantasy","prefix",0);
82 fprefix = fpre.empty() ? '!' : fpre[0];
85 ConfigTagList tags = ServerInstance->Config->ConfTags("alias");
86 for(ConfigIter i = tags.first; i != tags.second; ++i)
88 ConfigTag* tag = i->second;
90 std::string aliastext = tag->getString("text");
91 a.AliasedCommand = aliastext.c_str();
92 tag->readString("replace", a.ReplaceFormat, true);
93 a.RequiredNick = tag->getString("requires");
94 a.ULineOnly = tag->getBool("uline");
95 a.ChannelCommand = tag->getBool("channelcommand", false);
96 a.UserCommand = tag->getBool("usercommand", true);
97 a.OperOnly = tag->getBool("operonly");
98 a.format = tag->getString("format");
99 a.CaseSensitive = tag->getBool("matchcase");
100 Aliases.insert(std::make_pair(a.AliasedCommand, a));
109 ServerInstance->Modules->Attach(I_OnPreCommand, this);
110 ServerInstance->Modules->Attach(I_OnRehash, this);
111 ServerInstance->Modules->Attach(I_OnUserMessage, this);
114 virtual ~ModuleAlias()
118 virtual Version GetVersion()
120 return Version("Provides aliases of commands.", VF_VENDOR);
123 std::string GetVar(std::string varname, const std::string &original_line)
125 irc::spacesepstream ss(original_line);
126 varname.erase(varname.begin());
127 int index = *(varname.begin()) - 48;
128 varname.erase(varname.begin());
129 bool everything_after = (varname == "-");
132 for (int j = 0; j < index; j++)
135 if (everything_after)
138 while (ss.GetToken(more))
148 virtual ModResult OnPreCommand(std::string &command, std::vector<std::string> ¶meters, LocalUser *user, bool validated, const std::string &original_line)
150 std::multimap<irc::string, Alias>::iterator i, upperbound;
152 /* If theyre not registered yet, we dont want
155 if (user->registered != REG_ALL)
156 return MOD_RES_PASSTHRU;
158 /* We dont have any commands looking like this? Stop processing. */
159 i = Aliases.find(command.c_str());
160 if (i == Aliases.end())
161 return MOD_RES_PASSTHRU;
162 /* Avoid iterating on to different aliases if no patterns match. */
163 upperbound = Aliases.upper_bound(command.c_str());
165 irc::string c = command.c_str();
166 /* The parameters for the command in their original form, with the command stripped off */
167 std::string compare = original_line.substr(command.length());
168 while (*(compare.c_str()) == ' ')
169 compare.erase(compare.begin());
171 while (i != upperbound)
173 if (i->second.UserCommand)
175 if (DoAlias(user, NULL, &(i->second), compare, original_line))
184 // If we made it here, no aliases actually matched.
185 return MOD_RES_PASSTHRU;
188 virtual void OnUserMessage(User *user, void *dest, int target_type, const std::string &text, char status, const CUList &exempt_list)
190 if (target_type != TYPE_CHANNEL)
195 // fcommands are only for local users. Spanningtree will send them back out as their original cmd.
196 if (!user || !IS_LOCAL(user))
201 /* Stop here if the user is +B and allowbot is set to no. */
202 if (!AllowBots && user->IsModeSet('B'))
207 Channel *c = (Channel *)dest;
208 std::string scommand;
210 // text is like "!moo cows bite me", we want "!moo" first
211 irc::spacesepstream ss(text);
212 ss.GetToken(scommand);
213 irc::string fcommand = scommand.c_str();
215 if (fcommand.empty())
220 // we don't want to touch non-fantasy stuff
221 if (*fcommand.c_str() != fprefix)
226 // nor do we give a shit about the prefix
227 fcommand.erase(fcommand.begin());
229 std::multimap<irc::string, Alias>::iterator i = Aliases.find(fcommand);
231 if (i == Aliases.end())
234 /* Avoid iterating on to other aliases if no patterns match */
235 std::multimap<irc::string, Alias>::iterator upperbound = Aliases.upper_bound(fcommand);
238 /* The parameters for the command in their original form, with the command stripped off */
239 std::string compare = text.substr(fcommand.length() + 1);
240 while (*(compare.c_str()) == ' ')
241 compare.erase(compare.begin());
243 while (i != upperbound)
245 if (i->second.ChannelCommand)
247 // We use substr(1) here to remove the fantasy prefix
248 if (DoAlias(user, c, &(i->second), compare, text.substr(1)))
257 int DoAlias(User *user, Channel *c, Alias *a, const std::string compare, const std::string safe)
261 /* Does it match the pattern? */
262 if (!a->format.empty())
264 if (a->CaseSensitive)
266 if (!InspIRCd::Match(compare, a->format, rfc_case_sensitive_map))
271 if (!InspIRCd::Match(compare, a->format))
276 if ((a->OperOnly) && (!IS_OPER(user)))
279 if (!a->RequiredNick.empty())
281 u = ServerInstance->FindNick(a->RequiredNick);
284 user->WriteNumeric(401, ""+user->nick+" "+a->RequiredNick+" :is currently unavailable. Please try again later.");
288 if ((u != NULL) && (!a->RequiredNick.empty()) && (a->ULineOnly))
290 if (!ServerInstance->ULine(u->server))
292 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!");
293 user->WriteNumeric(401, ""+user->nick+" "+a->RequiredNick+" :is an imposter! Please inform an IRC operator as soon as possible.");
298 /* Now, search and replace in a copy of the original_line, replacing $1 through $9 and $1- etc */
300 std::string::size_type crlf = a->ReplaceFormat.find('\n');
302 if (crlf == std::string::npos)
304 DoCommand(a->ReplaceFormat, user, c, safe);
309 irc::sepstream commands(a->ReplaceFormat, '\n');
310 std::string scommand;
311 while (commands.GetToken(scommand))
313 DoCommand(scommand, user, c, safe);
319 void DoCommand(const std::string& newline, User* user, Channel *chan, const std::string &original_line)
322 result.reserve(MAXBUF);
323 for (unsigned int i = 0; i < newline.length(); i++)
328 if (isdigit(newline[i+1]))
330 int len = (newline[i+2] == '-') ? 3 : 2;
331 std::string var = newline.substr(i, len);
332 result.append(GetVar(var, original_line));
335 else if (newline.substr(i, 5) == "$nick")
337 result.append(user->nick);
340 else if (newline.substr(i, 5) == "$host")
342 result.append(user->host);
345 else if (newline.substr(i, 5) == "$chan")
348 result.append(chan->name);
351 else if (newline.substr(i, 6) == "$ident")
353 result.append(user->ident);
356 else if (newline.substr(i, 6) == "$vhost")
358 result.append(user->dhost);
368 irc::tokenstream ss(result);
369 std::vector<std::string> pars;
370 std::string command, token;
372 ss.GetToken(command);
373 while (ss.GetToken(token) && (pars.size() <= MAXPARAMETERS))
375 pars.push_back(token);
377 ServerInstance->Parser->CallHandler(command, pars, user);
380 virtual void OnRehash(User* user)
385 virtual void Prioritize()
387 // Prioritise after spanningtree so that channel aliases show the alias before the effects.
388 Module* linkmod = ServerInstance->Modules->Find("m_spanningtree.so");
389 ServerInstance->Modules->SetPriority(this, I_OnUserMessage, PRIORITY_AFTER, &linkmod);
393 MODULE_INIT(ModuleAlias)