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
54 /* We cant use a map, there may be multiple aliases with the same name.
55 * We can, however, use a fancy invention: the multimap. Maps a key to one or more values.
58 std::multimap<std::string, Alias> Aliases;
60 virtual void ReadAliases()
62 ConfigReader MyConf(ServerInstance);
65 for (int i = 0; i < MyConf.Enumerate("alias"); i++)
69 txt = MyConf.ReadValue("alias", "text", i);
70 a.AliasedCommand = txt.c_str();
71 a.ReplaceFormat = MyConf.ReadValue("alias", "replace", i, true);
72 a.RequiredNick = MyConf.ReadValue("alias", "requires", i);
73 a.ULineOnly = MyConf.ReadFlag("alias", "uline", i);
74 a.ChannelCommand = MyConf.ReadFlag("alias", "channelcommand", "no", i);
75 a.UserCommand = MyConf.ReadFlag("alias", "usercommand", "yes", i);
76 a.OperOnly = MyConf.ReadFlag("alias", "operonly", i);
77 a.format = MyConf.ReadValue("alias", "format", i);
78 a.CaseSensitive = MyConf.ReadFlag("alias", "matchcase", i);
79 Aliases.insert(std::make_pair(txt, a));
85 ModuleAlias(InspIRCd* Me)
89 Me->Modules->Attach(I_OnPreCommand, this);
90 Me->Modules->Attach(I_OnRehash, this);
91 Me->Modules->Attach(I_OnUserPreMessage, this);
95 virtual ~ModuleAlias()
99 virtual Version GetVersion()
101 return Version("$Id$", VF_VENDOR,API_VERSION);
104 std::string GetVar(std::string varname, const std::string &original_line)
106 irc::spacesepstream ss(original_line);
107 varname.erase(varname.begin());
108 int index = *(varname.begin()) - 48;
109 varname.erase(varname.begin());
110 bool everything_after = (varname == "-");
113 for (int j = 0; j < index; j++)
116 if (everything_after)
119 while (ss.GetToken(more))
129 void SearchAndReplace(std::string& newline, const std::string &find, const std::string &replace)
131 std::string::size_type x = newline.find(find);
132 while (x != std::string::npos)
134 newline.erase(x, find.length());
135 newline.insert(x, replace);
136 x = newline.find(find);
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 std::string safe(original_line);
165 /* Escape out any $ symbols in the user provided text */
167 SearchAndReplace(safe, "$", "\r");
169 while (i != upperbound)
171 if (i->second.UserCommand)
173 if (DoAlias(user, NULL, &(i->second), compare, safe))
182 // If aliases have been processed, aliases took it.
186 virtual int OnUserPreMessage(User *user, void *dest, int target_type, std::string &text, char status, CUList &exempt_list)
188 if (target_type != TYPE_CHANNEL)
190 ServerInstance->Logs->Log("FANTASY", DEBUG, "fantasy: not a channel msg");
194 // fcommands are only for local users. Spanningtree will send them back out as their original cmd.
197 ServerInstance->Logs->Log("FANTASY", DEBUG, "fantasy: not local");
201 Channel *c = (Channel *)dest;
202 std::string fcommand;
204 // text is like "!moo cows bite me", we want "!moo" first
205 irc::spacesepstream ss(text);
206 ss.GetToken(fcommand);
208 if (fcommand.empty())
210 ServerInstance->Logs->Log("FANTASY", DEBUG, "fantasy: empty (?)");
214 ServerInstance->Logs->Log("FANTASY", DEBUG, "fantasy: looking at fcommand %s", fcommand.c_str());
216 // we don't want to touch non-fantasy stuff
217 if (*fcommand.c_str() != '!')
219 ServerInstance->Logs->Log("FANTASY", DEBUG, "fantasy: not a fcommand");
223 // nor do we give a shit about the !
224 fcommand.erase(fcommand.begin());
225 std::transform(fcommand.begin(), fcommand.end(), fcommand.begin(), ::toupper);
226 ServerInstance->Logs->Log("FANTASY", DEBUG, "fantasy: now got %s", fcommand.c_str());
229 std::multimap<std::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<std::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 std::string safe(compare);
245 /* Escape out any $ symbols in the user provided text (ugly, but better than crashy) */
246 SearchAndReplace(safe, "$", "\r");
248 ServerInstance->Logs->Log("FANTASY", DEBUG, "fantasy: compare is %s and safe is %s", compare.c_str(), safe.c_str());
250 while (i != upperbound)
252 if (i->second.ChannelCommand)
254 if (DoAlias(user, c, &(i->second), compare, safe))
265 int DoAlias(User *user, Channel *c, Alias *a, const std::string compare, const std::string safe)
269 /* Does it match the pattern? */
270 if (!a->format.empty())
272 if (a->CaseSensitive)
274 if (!InspIRCd::Match(compare, a->format, rfc_case_sensitive_map))
279 if (!InspIRCd::Match(compare, a->format))
284 if ((a->OperOnly) && (!IS_OPER(user)))
287 if (!a->RequiredNick.empty())
289 u = ServerInstance->FindNick(a->RequiredNick);
292 user->WriteNumeric(401, ""+std::string(user->nick)+" "+a->RequiredNick+" :is currently unavailable. Please try again later.");
296 if ((u != NULL) && (!a->RequiredNick.empty()) && (a->ULineOnly))
298 if (!ServerInstance->ULine(u->server))
300 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!");
301 user->WriteNumeric(401, ""+std::string(user->nick)+" "+a->RequiredNick+" :is an imposter! Please inform an IRC operator as soon as possible.");
306 /* Now, search and replace in a copy of the original_line, replacing $1 through $9 and $1- etc */
308 std::string::size_type crlf = a->ReplaceFormat.find('\n');
310 if (crlf == std::string::npos)
312 DoCommand(a->ReplaceFormat, user, c, safe);
317 irc::sepstream commands(a->ReplaceFormat, '\n');
318 std::string scommand;
319 while (commands.GetToken(scommand))
321 DoCommand(scommand, user, c, safe);
327 void DoCommand(std::string newline, User* user, Channel *c, const std::string &original_line)
329 std::vector<std::string> pars;
331 for (int v = 1; v < 10; v++)
333 std::string var = "$";
334 var.append(ConvToStr(v));
336 std::string::size_type x = newline.find(var);
338 while (x != std::string::npos)
340 newline.erase(x, var.length());
341 newline.insert(x, GetVar(var, original_line));
342 x = newline.find(var);
346 var.append(ConvToStr(v));
347 x = newline.find(var);
349 while (x != std::string::npos)
351 newline.erase(x, var.length());
352 newline.insert(x, GetVar(var, original_line));
353 x = newline.find(var);
357 /* Special variables */
358 SearchAndReplace(newline, "$nick", user->nick);
359 SearchAndReplace(newline, "$ident", user->ident);
360 SearchAndReplace(newline, "$host", user->host);
361 SearchAndReplace(newline, "$vhost", user->dhost);
365 /* Channel specific variables */
366 SearchAndReplace(newline, "$chan", c->name);
369 /* Unescape any variable names in the user text before sending */
370 SearchAndReplace(newline, "\r", "$");
372 irc::tokenstream ss(newline);
374 std::string command, token;
376 ss.GetToken(command);
377 while (ss.GetToken(token) && (pars.size() <= MAXPARAMETERS))
379 pars.push_back(token);
381 ServerInstance->Parser->CallHandler(command, pars, user);
384 virtual void OnRehash(User* user, const std::string ¶meter)
390 MODULE_INIT(ModuleAlias)