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 */
25 /** Text to replace with */
26 std::string replace_with;
27 /** Nickname required to perform alias */
29 /** Alias requires ulined server */
33 /* is case sensitive params */
35 /** Format that must be matched for use */
39 class ModuleAlias : public Module
42 /** We cant use a map, there may be multiple aliases with the same name */
43 std::vector<Alias> Aliases;
44 std::map<std::string, int> AliasMap;
45 std::vector<std::string> pars;
47 virtual void ReadAliases()
49 ConfigReader MyConf(ServerInstance);
53 for (int i = 0; i < MyConf.Enumerate("alias"); i++)
57 txt = MyConf.ReadValue("alias", "text", i);
59 a.replace_with = MyConf.ReadValue("alias", "replace", i, true);
60 a.requires = MyConf.ReadValue("alias", "requires", i);
61 a.uline = MyConf.ReadFlag("alias", "uline", i);
62 a.operonly = MyConf.ReadFlag("alias", "operonly", i);
63 a.format = MyConf.ReadValue("alias", "format", i);
64 a.case_sensitive = MyConf.ReadFlag("alias", "matchcase", i);
72 ModuleAlias(InspIRCd* Me)
76 Me->Modules->Attach(I_OnPreCommand, this);
77 Me->Modules->Attach(I_OnRehash, this);
81 virtual ~ModuleAlias()
85 virtual Version GetVersion()
87 return Version("$Id$", VF_VENDOR,API_VERSION);
90 std::string GetVar(std::string varname, const std::string &original_line)
92 irc::spacesepstream ss(original_line);
93 varname.erase(varname.begin());
94 int index = *(varname.begin()) - 48;
95 varname.erase(varname.begin());
96 bool everything_after = (varname == "-");
99 for (int j = 0; j < index; j++)
102 if (everything_after)
105 while (ss.GetToken(more))
115 void SearchAndReplace(std::string& newline, const std::string &find, const std::string &replace)
117 std::string::size_type x = newline.find(find);
118 while (x != std::string::npos)
120 newline.erase(x, find.length());
121 newline.insert(x, replace);
122 x = newline.find(find);
126 virtual int OnPreCommand(std::string &command, std::vector<std::string> ¶meters, User *user, bool validated, const std::string &original_line)
130 /* If theyre not registered yet, we dont want
133 if (user->registered != REG_ALL)
136 /* We dont have any commands looking like this, dont bother with the loop */
137 if (AliasMap.find(command) == AliasMap.end())
140 irc::string c = command.c_str();
141 /* The parameters for the command in their original form, with the command stripped off */
142 std::string compare = original_line.substr(command.length());
143 while (*(compare.c_str()) == ' ')
144 compare.erase(compare.begin());
146 std::string safe(original_line);
148 /* Escape out any $ symbols in the user provided text */
150 SearchAndReplace(safe, "$", "\r");
152 for (unsigned int i = 0; i < Aliases.size(); i++)
154 if (Aliases[i].text == c)
156 /* Does it match the pattern? */
157 if (!Aliases[i].format.empty())
159 if (Aliases[i].case_sensitive)
161 if (InspIRCd::Match(compare, Aliases[i].format, case_sensitive_map))
166 if (InspIRCd::Match(compare, Aliases[i].format))
171 if ((Aliases[i].operonly) && (!IS_OPER(user)))
174 if (!Aliases[i].requires.empty())
176 u = ServerInstance->FindNick(Aliases[i].requires);
179 user->WriteNumeric(401, ""+std::string(user->nick)+" "+Aliases[i].requires+" :is currently unavailable. Please try again later.");
183 if ((u != NULL) && (!Aliases[i].requires.empty()) && (Aliases[i].uline))
185 if (!ServerInstance->ULine(u->server))
187 ServerInstance->SNO->WriteToSnoMask('A', "NOTICE -- Service "+Aliases[i].requires+" required by alias "+std::string(Aliases[i].text.c_str())+" is not on a u-lined server, possibly underhanded antics detected!");
188 user->WriteNumeric(401, ""+std::string(user->nick)+" "+Aliases[i].requires+" :is an imposter! Please inform an IRC operator as soon as possible.");
193 /* Now, search and replace in a copy of the original_line, replacing $1 through $9 and $1- etc */
195 std::string::size_type crlf = Aliases[i].replace_with.find('\n');
197 if (crlf == std::string::npos)
199 DoCommand(Aliases[i].replace_with, user, safe);
204 irc::sepstream commands(Aliases[i].replace_with, '\n');
205 std::string scommand;
206 while (commands.GetToken(scommand))
208 DoCommand(scommand, user, safe);
217 void DoCommand(std::string newline, User* user, const std::string &original_line)
219 for (int v = 1; v < 10; v++)
221 std::string var = "$";
222 var.append(ConvToStr(v));
224 std::string::size_type x = newline.find(var);
226 while (x != std::string::npos)
228 newline.erase(x, var.length());
229 newline.insert(x, GetVar(var, original_line));
230 x = newline.find(var);
234 var.append(ConvToStr(v));
235 x = newline.find(var);
237 while (x != std::string::npos)
239 newline.erase(x, var.length());
240 newline.insert(x, GetVar(var, original_line));
241 x = newline.find(var);
245 /* Special variables */
246 SearchAndReplace(newline, "$nick", user->nick);
247 SearchAndReplace(newline, "$ident", user->ident);
248 SearchAndReplace(newline, "$host", user->host);
249 SearchAndReplace(newline, "$vhost", user->dhost);
251 /* Unescape any variable names in the user text before sending */
252 SearchAndReplace(newline, "\r", "$");
254 irc::tokenstream ss(newline);
256 std::string command, token;
258 ss.GetToken(command);
259 while (ss.GetToken(token) && (pars.size() <= MAXPARAMETERS))
261 pars.push_back(token);
263 ServerInstance->Parser->CallHandler(command, pars, user);
266 virtual void OnRehash(User* user, const std::string ¶meter)
272 MODULE_INIT(ModuleAlias)