1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
16 /* $ModDesc: Provides aliases of commands. */
18 /** An alias definition
20 class Alias : public classbase
23 /** The text of the alias command */
24 irc::string AliasedCommand;
26 /** Text to replace with */
27 std::string ReplaceFormat;
29 /** Nickname required to perform alias */
30 std::string RequiredNick;
32 /** Alias requires ulined server */
38 /* is case sensitive params */
41 /* whether or not it may be executed via fantasy (default OFF) */
44 /* whether or not it may be executed via /command (default ON) */
47 /** Format that must be matched for use */
51 class ModuleAlias : public Module
57 /* We cant use a map, there may be multiple aliases with the same name.
58 * We can, however, use a fancy invention: the multimap. Maps a key to one or more values.
61 std::multimap<std::string, Alias> Aliases;
63 /* whether or not +B users are allowed to use fantasy commands */
66 virtual void ReadAliases()
68 ConfigReader MyConf(ServerInstance);
70 AllowBots = MyConf.ReadFlag("fantasy", "allowbots", "no", 0);
72 std::string fpre = MyConf.ReadValue("fantasy","prefix",0);
73 fprefix = fpre.empty() ? '!' : fpre[0];
76 for (int i = 0; i < MyConf.Enumerate("alias"); i++)
80 txt = MyConf.ReadValue("alias", "text", i);
81 a.AliasedCommand = txt.c_str();
82 a.ReplaceFormat = MyConf.ReadValue("alias", "replace", i, true);
83 a.RequiredNick = MyConf.ReadValue("alias", "requires", i);
84 a.ULineOnly = MyConf.ReadFlag("alias", "uline", i);
85 a.ChannelCommand = MyConf.ReadFlag("alias", "channelcommand", "no", i);
86 a.UserCommand = MyConf.ReadFlag("alias", "usercommand", "yes", i);
87 a.OperOnly = MyConf.ReadFlag("alias", "operonly", i);
88 a.format = MyConf.ReadValue("alias", "format", i);
89 a.CaseSensitive = MyConf.ReadFlag("alias", "matchcase", i);
90 Aliases.insert(std::make_pair(txt, a));
96 ModuleAlias(InspIRCd* Me)
100 Me->Modules->Attach(I_OnPreCommand, this);
101 Me->Modules->Attach(I_OnRehash, this);
102 Me->Modules->Attach(I_OnUserMessage, this);
106 virtual ~ModuleAlias()
110 virtual Version GetVersion()
112 return Version("$Id$", VF_VENDOR,API_VERSION);
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 virtual int OnPreCommand(std::string &command, std::vector<std::string> ¶meters, User *user, bool validated, const std::string &original_line)
142 std::multimap<std::string, Alias>::iterator i, upperbound;
144 /* If theyre not registered yet, we dont want
147 if (user->registered != REG_ALL)
150 /* We dont have any commands looking like this? Stop processing. */
151 i = Aliases.find(command);
152 if (i == Aliases.end())
154 /* Avoid iterating on to different aliases if no patterns match. */
155 upperbound = Aliases.upper_bound(command);
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.
180 virtual void OnUserMessage(User *user, void *dest, int target_type, const std::string &text, char status, const CUList &exempt_list)
182 if (target_type != TYPE_CHANNEL)
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('B'))
199 Channel *c = (Channel *)dest;
200 std::string fcommand;
202 // text is like "!moo cows bite me", we want "!moo" first
203 irc::spacesepstream ss(text);
204 ss.GetToken(fcommand);
206 if (fcommand.empty())
211 // we don't want to touch non-fantasy stuff
212 if (*fcommand.c_str() != fprefix)
217 // nor do we give a shit about the prefix
218 fcommand.erase(fcommand.begin());
219 std::transform(fcommand.begin(), fcommand.end(), fcommand.begin(), ::toupper);
221 std::multimap<std::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<std::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)))
251 int DoAlias(User *user, Channel *c, Alias *a, const std::string compare, const std::string safe)
255 /* Does it match the pattern? */
256 if (!a->format.empty())
258 if (a->CaseSensitive)
260 if (!InspIRCd::Match(compare, a->format, rfc_case_sensitive_map))
265 if (!InspIRCd::Match(compare, a->format))
270 if ((a->OperOnly) && (!IS_OPER(user)))
273 if (!a->RequiredNick.empty())
275 u = ServerInstance->FindNick(a->RequiredNick);
278 user->WriteNumeric(401, ""+std::string(user->nick)+" "+a->RequiredNick+" :is currently unavailable. Please try again later.");
282 if ((u != NULL) && (!a->RequiredNick.empty()) && (a->ULineOnly))
284 if (!ServerInstance->ULine(u->server))
286 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!");
287 user->WriteNumeric(401, ""+std::string(user->nick)+" "+a->RequiredNick+" :is an imposter! Please inform an IRC operator as soon as possible.");
292 /* Now, search and replace in a copy of the original_line, replacing $1 through $9 and $1- etc */
294 std::string::size_type crlf = a->ReplaceFormat.find('\n');
296 if (crlf == std::string::npos)
298 DoCommand(a->ReplaceFormat, user, c, safe);
303 irc::sepstream commands(a->ReplaceFormat, '\n');
304 std::string scommand;
305 while (commands.GetToken(scommand))
307 DoCommand(scommand, user, c, safe);
313 void DoCommand(std::string newline, User* user, Channel *c, const std::string &original_line)
315 std::vector<std::string> pars;
317 for (int v = 1; v < 10; v++)
319 std::string var = "$";
320 var.append(ConvToStr(v));
322 std::string::size_type x = newline.find(var);
324 while (x != std::string::npos)
326 newline.erase(x, var.length());
327 newline.insert(x, GetVar(var, original_line));
328 x = newline.find(var);
332 var.append(ConvToStr(v));
333 x = newline.find(var);
335 while (x != std::string::npos)
337 newline.erase(x, var.length());
338 newline.insert(x, GetVar(var, original_line));
339 x = newline.find(var);
343 /* Special variables */
344 SearchAndReplace(newline, std::string("$nick"), user->nick);
345 SearchAndReplace(newline, std::string("$ident"), user->ident);
346 SearchAndReplace(newline, std::string("$host"), user->host);
347 SearchAndReplace(newline, std::string("$vhost"), user->dhost);
351 /* Channel specific variables */
352 SearchAndReplace(newline, std::string("$chan"), c->name);
356 /* We don't want these in a user alias */
357 SearchAndReplace(newline, std::string("$chan"), std::string(""));
360 irc::tokenstream ss(newline);
362 std::string command, token;
364 ss.GetToken(command);
365 while (ss.GetToken(token) && (pars.size() <= MAXPARAMETERS))
367 pars.push_back(token);
369 ServerInstance->Parser->CallHandler(command, pars, user);
372 virtual void OnRehash(User* user)
377 virtual void Prioritize()
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)