1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/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 virtual void ReadAliases()
65 ConfigReader MyConf(ServerInstance);
67 std::string fpre = MyConf.ReadValue("fantasy","prefix",0);
68 fprefix = fpre.empty() ? '!' : fpre[0];
71 for (int i = 0; i < MyConf.Enumerate("alias"); i++)
75 txt = MyConf.ReadValue("alias", "text", i);
76 a.AliasedCommand = txt.c_str();
77 a.ReplaceFormat = MyConf.ReadValue("alias", "replace", i, true);
78 a.RequiredNick = MyConf.ReadValue("alias", "requires", i);
79 a.ULineOnly = MyConf.ReadFlag("alias", "uline", i);
80 a.ChannelCommand = MyConf.ReadFlag("alias", "channelcommand", "no", i);
81 a.UserCommand = MyConf.ReadFlag("alias", "usercommand", "yes", i);
82 a.OperOnly = MyConf.ReadFlag("alias", "operonly", i);
83 a.format = MyConf.ReadValue("alias", "format", i);
84 a.CaseSensitive = MyConf.ReadFlag("alias", "matchcase", i);
85 Aliases.insert(std::make_pair(txt, a));
91 ModuleAlias(InspIRCd* Me)
95 Me->Modules->Attach(I_OnPreCommand, this);
96 Me->Modules->Attach(I_OnRehash, this);
97 Me->Modules->Attach(I_OnUserPreMessage, this);
101 virtual ~ModuleAlias()
105 virtual Version GetVersion()
107 return Version("$Id$", VF_VENDOR,API_VERSION);
110 std::string GetVar(std::string varname, const std::string &original_line)
112 irc::spacesepstream ss(original_line);
113 varname.erase(varname.begin());
114 int index = *(varname.begin()) - 48;
115 varname.erase(varname.begin());
116 bool everything_after = (varname == "-");
119 for (int j = 0; j < index; j++)
122 if (everything_after)
125 while (ss.GetToken(more))
135 void SearchAndReplace(std::string& newline, const std::string &find, const std::string &replace)
137 std::string::size_type x = newline.find(find);
138 while (x != std::string::npos)
140 newline.erase(x, find.length());
141 newline.insert(x, replace);
142 x = newline.find(find);
146 virtual int OnPreCommand(std::string &command, std::vector<std::string> ¶meters, User *user, bool validated, const std::string &original_line)
148 std::multimap<std::string, Alias>::iterator i, upperbound;
150 /* If theyre not registered yet, we dont want
153 if (user->registered != REG_ALL)
156 /* We dont have any commands looking like this? Stop processing. */
157 i = Aliases.find(command);
158 if (i == Aliases.end())
160 /* Avoid iterating on to different aliases if no patterns match. */
161 upperbound = Aliases.upper_bound(command);
163 irc::string c = command.c_str();
164 /* The parameters for the command in their original form, with the command stripped off */
165 std::string compare = original_line.substr(command.length());
166 while (*(compare.c_str()) == ' ')
167 compare.erase(compare.begin());
169 std::string safe(original_line);
171 /* Escape out any $ symbols in the user provided text */
173 SearchAndReplace(safe, "$", "\r");
175 while (i != upperbound)
177 if (i->second.UserCommand)
179 if (DoAlias(user, NULL, &(i->second), compare, safe))
188 // If aliases have been processed, aliases took it.
192 virtual int OnUserPreMessage(User *user, void *dest, int target_type, std::string &text, char status, CUList &exempt_list)
194 if (target_type != TYPE_CHANNEL)
196 ServerInstance->Logs->Log("FANTASY", DEBUG, "fantasy: not a channel msg");
200 // fcommands are only for local users. Spanningtree will send them back out as their original cmd.
203 ServerInstance->Logs->Log("FANTASY", DEBUG, "fantasy: not local");
207 Channel *c = (Channel *)dest;
208 std::string fcommand;
210 // text is like "!moo cows bite me", we want "!moo" first
211 irc::spacesepstream ss(text);
212 ss.GetToken(fcommand);
214 if (fcommand.empty())
216 ServerInstance->Logs->Log("FANTASY", DEBUG, "fantasy: empty (?)");
220 ServerInstance->Logs->Log("FANTASY", DEBUG, "fantasy: looking at fcommand %s", fcommand.c_str());
222 // we don't want to touch non-fantasy stuff
223 if (*fcommand.c_str() != fprefix)
225 ServerInstance->Logs->Log("FANTASY", DEBUG, "fantasy: not a fcommand");
229 // nor do we give a shit about the prefix
230 fcommand.erase(fcommand.begin());
231 std::transform(fcommand.begin(), fcommand.end(), fcommand.begin(), ::toupper);
232 ServerInstance->Logs->Log("FANTASY", DEBUG, "fantasy: now got %s", fcommand.c_str());
235 std::multimap<std::string, Alias>::iterator i = Aliases.find(fcommand);
237 if (i == Aliases.end())
240 /* Avoid iterating on to other aliases if no patterns match */
241 std::multimap<std::string, Alias>::iterator upperbound = Aliases.upper_bound(fcommand);
244 /* The parameters for the command in their original form, with the command stripped off */
245 std::string compare = text.substr(fcommand.length() + 1);
246 while (*(compare.c_str()) == ' ')
247 compare.erase(compare.begin());
249 std::string safe(compare);
251 /* Escape out any $ symbols in the user provided text (ugly, but better than crashy) */
252 SearchAndReplace(safe, "$", "\r");
254 ServerInstance->Logs->Log("FANTASY", DEBUG, "fantasy: compare is %s and safe is %s", compare.c_str(), safe.c_str());
256 while (i != upperbound)
258 if (i->second.ChannelCommand)
260 if (DoAlias(user, c, &(i->second), compare, safe))
271 int DoAlias(User *user, Channel *c, Alias *a, const std::string compare, const std::string safe)
275 /* Does it match the pattern? */
276 if (!a->format.empty())
278 if (a->CaseSensitive)
280 if (!InspIRCd::Match(compare, a->format, rfc_case_sensitive_map))
285 if (!InspIRCd::Match(compare, a->format))
290 if ((a->OperOnly) && (!IS_OPER(user)))
293 if (!a->RequiredNick.empty())
295 u = ServerInstance->FindNick(a->RequiredNick);
298 user->WriteNumeric(401, ""+std::string(user->nick)+" "+a->RequiredNick+" :is currently unavailable. Please try again later.");
302 if ((u != NULL) && (!a->RequiredNick.empty()) && (a->ULineOnly))
304 if (!ServerInstance->ULine(u->server))
306 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!");
307 user->WriteNumeric(401, ""+std::string(user->nick)+" "+a->RequiredNick+" :is an imposter! Please inform an IRC operator as soon as possible.");
312 /* Now, search and replace in a copy of the original_line, replacing $1 through $9 and $1- etc */
314 std::string::size_type crlf = a->ReplaceFormat.find('\n');
316 if (crlf == std::string::npos)
318 DoCommand(a->ReplaceFormat, user, c, safe);
323 irc::sepstream commands(a->ReplaceFormat, '\n');
324 std::string scommand;
325 while (commands.GetToken(scommand))
327 DoCommand(scommand, user, c, safe);
333 void DoCommand(std::string newline, User* user, Channel *c, const std::string &original_line)
335 std::vector<std::string> pars;
337 for (int v = 1; v < 10; v++)
339 std::string var = "$";
340 var.append(ConvToStr(v));
342 std::string::size_type x = newline.find(var);
344 while (x != std::string::npos)
346 newline.erase(x, var.length());
347 newline.insert(x, GetVar(var, original_line));
348 x = newline.find(var);
352 var.append(ConvToStr(v));
353 x = newline.find(var);
355 while (x != std::string::npos)
357 newline.erase(x, var.length());
358 newline.insert(x, GetVar(var, original_line));
359 x = newline.find(var);
363 /* Special variables */
364 SearchAndReplace(newline, "$nick", user->nick);
365 SearchAndReplace(newline, "$ident", user->ident);
366 SearchAndReplace(newline, "$host", user->host);
367 SearchAndReplace(newline, "$vhost", user->dhost);
371 /* Channel specific variables */
372 SearchAndReplace(newline, "$chan", c->name);
375 /* Unescape any variable names in the user text before sending */
376 SearchAndReplace(newline, "\r", "$");
378 irc::tokenstream ss(newline);
380 std::string command, token;
382 ss.GetToken(command);
383 while (ss.GetToken(token) && (pars.size() <= MAXPARAMETERS))
385 pars.push_back(token);
387 ServerInstance->Parser->CallHandler(command, pars, user);
390 virtual void OnRehash(User* user, const std::string ¶meter)
396 MODULE_INIT(ModuleAlias)