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_OnUserPreMessage, 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 int OnUserPreMessage(User *user, void *dest, int target_type, std::string &text, char status, CUList &exempt_list)
182 if (target_type != TYPE_CHANNEL)
184 ServerInstance->Logs->Log("FANTASY", DEBUG, "fantasy: not a channel msg");
188 // fcommands are only for local users. Spanningtree will send them back out as their original cmd.
191 ServerInstance->Logs->Log("FANTASY", DEBUG, "fantasy: not local");
195 /* Stop here if the user is +B and allowbot is set to no. */
196 if (!AllowBots && user->IsModeSet('B'))
198 ServerInstance->Logs->Log("FANTASY", DEBUG, "fantasy: user is +B and allowbot is set to no");
202 Channel *c = (Channel *)dest;
203 std::string fcommand;
205 // text is like "!moo cows bite me", we want "!moo" first
206 irc::spacesepstream ss(text);
207 ss.GetToken(fcommand);
209 if (fcommand.empty())
211 ServerInstance->Logs->Log("FANTASY", DEBUG, "fantasy: empty (?)");
215 ServerInstance->Logs->Log("FANTASY", DEBUG, "fantasy: looking at fcommand %s", fcommand.c_str());
217 // we don't want to touch non-fantasy stuff
218 if (*fcommand.c_str() != fprefix)
220 ServerInstance->Logs->Log("FANTASY", DEBUG, "fantasy: not a fcommand");
224 // nor do we give a shit about the prefix
225 fcommand.erase(fcommand.begin());
226 std::transform(fcommand.begin(), fcommand.end(), fcommand.begin(), ::toupper);
227 ServerInstance->Logs->Log("FANTASY", DEBUG, "fantasy: now got %s", fcommand.c_str());
230 std::multimap<std::string, Alias>::iterator i = Aliases.find(fcommand);
232 if (i == Aliases.end())
235 /* Avoid iterating on to other aliases if no patterns match */
236 std::multimap<std::string, Alias>::iterator upperbound = Aliases.upper_bound(fcommand);
239 /* The parameters for the command in their original form, with the command stripped off */
240 std::string compare = text.substr(fcommand.length() + 1);
241 while (*(compare.c_str()) == ' ')
242 compare.erase(compare.begin());
244 std::string safe(compare);
246 ServerInstance->Logs->Log("FANTASY", DEBUG, "fantasy: compare is %s and safe is %s", compare.c_str(), safe.c_str());
248 while (i != upperbound)
250 if (i->second.ChannelCommand)
252 if (DoAlias(user, c, &(i->second), compare, safe))
263 int DoAlias(User *user, Channel *c, Alias *a, const std::string compare, const std::string safe)
267 /* Does it match the pattern? */
268 if (!a->format.empty())
270 if (a->CaseSensitive)
272 if (!InspIRCd::Match(compare, a->format, rfc_case_sensitive_map))
277 if (!InspIRCd::Match(compare, a->format))
282 if ((a->OperOnly) && (!IS_OPER(user)))
285 if (!a->RequiredNick.empty())
287 u = ServerInstance->FindNick(a->RequiredNick);
290 user->WriteNumeric(401, ""+std::string(user->nick)+" "+a->RequiredNick+" :is currently unavailable. Please try again later.");
294 if ((u != NULL) && (!a->RequiredNick.empty()) && (a->ULineOnly))
296 if (!ServerInstance->ULine(u->server))
298 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!");
299 user->WriteNumeric(401, ""+std::string(user->nick)+" "+a->RequiredNick+" :is an imposter! Please inform an IRC operator as soon as possible.");
304 /* Now, search and replace in a copy of the original_line, replacing $1 through $9 and $1- etc */
306 std::string::size_type crlf = a->ReplaceFormat.find('\n');
308 if (crlf == std::string::npos)
310 DoCommand(a->ReplaceFormat, user, c, safe);
315 irc::sepstream commands(a->ReplaceFormat, '\n');
316 std::string scommand;
317 while (commands.GetToken(scommand))
319 DoCommand(scommand, user, c, safe);
325 void DoCommand(std::string newline, User* user, Channel *c, const std::string &original_line)
327 std::vector<std::string> pars;
329 for (int v = 1; v < 10; v++)
331 std::string var = "$";
332 var.append(ConvToStr(v));
334 std::string::size_type x = newline.find(var);
336 while (x != std::string::npos)
338 newline.erase(x, var.length());
339 newline.insert(x, GetVar(var, original_line));
340 x = newline.find(var);
344 var.append(ConvToStr(v));
345 x = newline.find(var);
347 while (x != std::string::npos)
349 newline.erase(x, var.length());
350 newline.insert(x, GetVar(var, original_line));
351 x = newline.find(var);
355 /* Special variables */
356 SearchAndReplace(newline, std::string("$nick"), user->nick);
357 SearchAndReplace(newline, std::string("$ident"), user->ident);
358 SearchAndReplace(newline, std::string("$host"), user->host);
359 SearchAndReplace(newline, std::string("$vhost"), user->dhost);
363 /* Channel specific variables */
364 SearchAndReplace(newline, std::string("$chan"), c->name);
367 irc::tokenstream ss(newline);
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, const std::string ¶meter)
385 MODULE_INIT(ModuleAlias)