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 a.AliasedCommand = tag->getString("text").c_str();
91 tag->readString("replace", a.ReplaceFormat, true);
92 a.RequiredNick = tag->getString("requires");
93 a.ULineOnly = tag->getBool("uline");
94 a.ChannelCommand = tag->getBool("channelcommand", false);
95 a.UserCommand = tag->getBool("usercommand", true);
96 a.OperOnly = tag->getBool("operonly");
97 a.format = tag->getString("format");
98 a.CaseSensitive = tag->getBool("matchcase");
99 Aliases.insert(std::make_pair(a.AliasedCommand, a));
108 ServerInstance->Modules->Attach(I_OnPreCommand, this);
109 ServerInstance->Modules->Attach(I_OnRehash, this);
110 ServerInstance->Modules->Attach(I_OnUserMessage, this);
113 virtual ~ModuleAlias()
117 virtual Version GetVersion()
119 return Version("Provides aliases of commands.", VF_VENDOR);
122 std::string GetVar(std::string varname, const std::string &original_line)
124 irc::spacesepstream ss(original_line);
125 varname.erase(varname.begin());
126 int index = *(varname.begin()) - 48;
127 varname.erase(varname.begin());
128 bool everything_after = (varname == "-");
131 for (int j = 0; j < index; j++)
134 if (everything_after)
137 while (ss.GetToken(more))
147 virtual ModResult OnPreCommand(std::string &command, std::vector<std::string> ¶meters, LocalUser *user, bool validated, const std::string &original_line)
149 std::multimap<irc::string, Alias>::iterator i, upperbound;
151 /* If theyre not registered yet, we dont want
154 if (user->registered != REG_ALL)
155 return MOD_RES_PASSTHRU;
157 /* We dont have any commands looking like this? Stop processing. */
158 i = Aliases.find(command.c_str());
159 if (i == Aliases.end())
160 return MOD_RES_PASSTHRU;
161 /* Avoid iterating on to different aliases if no patterns match. */
162 upperbound = Aliases.upper_bound(command.c_str());
164 irc::string c = command.c_str();
165 /* The parameters for the command in their original form, with the command stripped off */
166 std::string compare = original_line.substr(command.length());
167 while (*(compare.c_str()) == ' ')
168 compare.erase(compare.begin());
170 while (i != upperbound)
172 if (i->second.UserCommand)
174 if (DoAlias(user, NULL, &(i->second), compare, original_line))
183 // If we made it here, no aliases actually matched.
184 return MOD_RES_PASSTHRU;
187 virtual void OnUserMessage(User *user, void *dest, int target_type, const std::string &text, char status, const CUList &exempt_list)
189 if (target_type != TYPE_CHANNEL)
194 // fcommands are only for local users. Spanningtree will send them back out as their original cmd.
195 if (!user || !IS_LOCAL(user))
200 /* Stop here if the user is +B and allowbot is set to no. */
201 if (!AllowBots && user->IsModeSet('B'))
206 Channel *c = (Channel *)dest;
207 std::string scommand;
209 // text is like "!moo cows bite me", we want "!moo" first
210 irc::spacesepstream ss(text);
211 ss.GetToken(scommand);
212 irc::string fcommand = scommand.c_str();
214 if (fcommand.empty())
219 // we don't want to touch non-fantasy stuff
220 if (*fcommand.c_str() != fprefix)
225 // nor do we give a shit about the prefix
226 fcommand.erase(fcommand.begin());
228 std::multimap<irc::string, Alias>::iterator i = Aliases.find(fcommand);
230 if (i == Aliases.end())
233 /* Avoid iterating on to other aliases if no patterns match */
234 std::multimap<irc::string, Alias>::iterator upperbound = Aliases.upper_bound(fcommand);
237 /* The parameters for the command in their original form, with the command stripped off */
238 std::string compare = text.substr(fcommand.length() + 1);
239 while (*(compare.c_str()) == ' ')
240 compare.erase(compare.begin());
242 while (i != upperbound)
244 if (i->second.ChannelCommand)
246 // We use substr(1) here to remove the fantasy prefix
247 if (DoAlias(user, c, &(i->second), compare, text.substr(1)))
256 int DoAlias(User *user, Channel *c, Alias *a, const std::string compare, const std::string safe)
260 /* Does it match the pattern? */
261 if (!a->format.empty())
263 if (a->CaseSensitive)
265 if (!InspIRCd::Match(compare, a->format, rfc_case_sensitive_map))
270 if (!InspIRCd::Match(compare, a->format))
275 if ((a->OperOnly) && (!IS_OPER(user)))
278 if (!a->RequiredNick.empty())
280 u = ServerInstance->FindNick(a->RequiredNick);
283 user->WriteNumeric(401, ""+std::string(user->nick)+" "+a->RequiredNick+" :is currently unavailable. Please try again later.");
287 if ((u != NULL) && (!a->RequiredNick.empty()) && (a->ULineOnly))
289 if (!ServerInstance->ULine(u->server))
291 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!");
292 user->WriteNumeric(401, ""+std::string(user->nick)+" "+a->RequiredNick+" :is an imposter! Please inform an IRC operator as soon as possible.");
297 /* Now, search and replace in a copy of the original_line, replacing $1 through $9 and $1- etc */
299 std::string::size_type crlf = a->ReplaceFormat.find('\n');
301 if (crlf == std::string::npos)
303 DoCommand(a->ReplaceFormat, user, c, safe);
308 irc::sepstream commands(a->ReplaceFormat, '\n');
309 std::string scommand;
310 while (commands.GetToken(scommand))
312 DoCommand(scommand, user, c, safe);
318 void DoCommand(const std::string& newline, User* user, Channel *chan, const std::string &original_line)
321 result.reserve(MAXBUF);
322 for (unsigned int i = 0; i < newline.length(); i++)
327 if (isdigit(newline[i+1]))
329 int len = (newline[i+2] == '-') ? 3 : 2;
330 std::string var = newline.substr(i, len);
331 result.append(GetVar(var, original_line));
334 else if (newline.substr(i, 5) == "$nick")
336 result.append(user->nick);
339 else if (newline.substr(i, 5) == "$host")
341 result.append(user->host);
344 else if (newline.substr(i, 5) == "$chan")
347 result.append(chan->name);
350 else if (newline.substr(i, 6) == "$ident")
352 result.append(user->ident);
355 else if (newline.substr(i, 6) == "$vhost")
357 result.append(user->dhost);
367 irc::tokenstream ss(result);
368 std::vector<std::string> pars;
369 std::string command, token;
371 ss.GetToken(command);
372 while (ss.GetToken(token) && (pars.size() <= MAXPARAMETERS))
374 pars.push_back(token);
376 ServerInstance->Parser->CallHandler(command, pars, user);
379 virtual void OnRehash(User* user)
384 virtual void Prioritize()
386 // Prioritise after spanningtree so that channel aliases show the alias before the effects.
387 Module* linkmod = ServerInstance->Modules->Find("m_spanningtree.so");
388 ServerInstance->Modules->SetPriority(this, I_OnUserMessage, PRIORITY_AFTER, &linkmod);
392 MODULE_INIT(ModuleAlias)