1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 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", i);
75 a.UserCommand = MyConf.ReadFlag("alias", "usercommand", 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);
94 virtual ~ModuleAlias()
98 virtual Version GetVersion()
100 return Version("$Id$", VF_VENDOR,API_VERSION);
103 std::string GetVar(std::string varname, const std::string &original_line)
105 irc::spacesepstream ss(original_line);
106 varname.erase(varname.begin());
107 int index = *(varname.begin()) - 48;
108 varname.erase(varname.begin());
109 bool everything_after = (varname == "-");
112 for (int j = 0; j < index; j++)
115 if (everything_after)
118 while (ss.GetToken(more))
128 void SearchAndReplace(std::string& newline, const std::string &find, const std::string &replace)
130 std::string::size_type x = newline.find(find);
131 while (x != std::string::npos)
133 newline.erase(x, find.length());
134 newline.insert(x, replace);
135 x = newline.find(find);
139 virtual int OnPreCommand(std::string &command, std::vector<std::string> ¶meters, User *user, bool validated, const std::string &original_line)
141 std::multimap<std::string, Alias>::iterator i;
143 /* If theyre not registered yet, we dont want
146 if (user->registered != REG_ALL)
149 /* We dont have any commands looking like this? Stop processing. */
150 i = Aliases.find(command);
151 if (i == Aliases.end())
154 irc::string c = command.c_str();
155 /* The parameters for the command in their original form, with the command stripped off */
156 std::string compare = original_line.substr(command.length());
157 while (*(compare.c_str()) == ' ')
158 compare.erase(compare.begin());
160 std::string safe(original_line);
162 /* Escape out any $ symbols in the user provided text */
164 SearchAndReplace(safe, "$", "\r");
166 while (i != Aliases.end())
168 if (i->second.UserCommand)
169 DoAlias(user, &(i->second), compare, safe);
174 // If aliases have been processed, aliases took it.
178 void DoAlias(User *user, Alias *a, const std::string compare, const std::string safe)
181 /* Does it match the pattern? */
182 if (!a->format.empty())
184 if (a->CaseSensitive)
186 if (InspIRCd::Match(compare, a->format, case_sensitive_map))
191 if (InspIRCd::Match(compare, a->format))
196 if ((a->OperOnly) && (!IS_OPER(user)))
199 if (!a->RequiredNick.empty())
201 u = ServerInstance->FindNick(a->RequiredNick);
204 user->WriteNumeric(401, ""+std::string(user->nick)+" "+a->RequiredNick+" :is currently unavailable. Please try again later.");
208 if ((u != NULL) && (!a->RequiredNick.empty()) && (a->ULineOnly))
210 if (!ServerInstance->ULine(u->server))
212 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!");
213 user->WriteNumeric(401, ""+std::string(user->nick)+" "+a->RequiredNick+" :is an imposter! Please inform an IRC operator as soon as possible.");
218 /* Now, search and replace in a copy of the original_line, replacing $1 through $9 and $1- etc */
220 std::string::size_type crlf = a->ReplaceFormat.find('\n');
222 if (crlf == std::string::npos)
224 DoCommand(a->ReplaceFormat, user, safe);
229 irc::sepstream commands(a->ReplaceFormat, '\n');
230 std::string scommand;
231 while (commands.GetToken(scommand))
233 DoCommand(scommand, user, safe);
239 void DoCommand(std::string newline, User* user, const std::string &original_line)
241 std::vector<std::string> pars;
243 for (int v = 1; v < 10; v++)
245 std::string var = "$";
246 var.append(ConvToStr(v));
248 std::string::size_type x = newline.find(var);
250 while (x != std::string::npos)
252 newline.erase(x, var.length());
253 newline.insert(x, GetVar(var, original_line));
254 x = newline.find(var);
258 var.append(ConvToStr(v));
259 x = newline.find(var);
261 while (x != std::string::npos)
263 newline.erase(x, var.length());
264 newline.insert(x, GetVar(var, original_line));
265 x = newline.find(var);
269 /* Special variables */
270 SearchAndReplace(newline, "$nick", user->nick);
271 SearchAndReplace(newline, "$ident", user->ident);
272 SearchAndReplace(newline, "$host", user->host);
273 SearchAndReplace(newline, "$vhost", user->dhost);
275 /* Unescape any variable names in the user text before sending */
276 SearchAndReplace(newline, "\r", "$");
278 irc::tokenstream ss(newline);
280 std::string command, token;
282 ss.GetToken(command);
283 while (ss.GetToken(token) && (pars.size() <= MAXPARAMETERS))
285 pars.push_back(token);
287 ServerInstance->Parser->CallHandler(command, pars, user);
290 virtual void OnRehash(User* user, const std::string ¶meter)
296 MODULE_INIT(ModuleAlias)