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 * We can, however, use a fancy invention: the multimap. Maps a key to one or more values.
46 std::multimap<std::string, Alias> Aliases;
48 virtual void ReadAliases()
50 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);
65 Aliases.insert(std::make_pair(txt, a));
71 ModuleAlias(InspIRCd* Me)
75 Me->Modules->Attach(I_OnPreCommand, this);
76 Me->Modules->Attach(I_OnRehash, this);
80 virtual ~ModuleAlias()
84 virtual Version GetVersion()
86 return Version("$Id$", VF_VENDOR,API_VERSION);
89 std::string GetVar(std::string varname, const std::string &original_line)
91 irc::spacesepstream ss(original_line);
92 varname.erase(varname.begin());
93 int index = *(varname.begin()) - 48;
94 varname.erase(varname.begin());
95 bool everything_after = (varname == "-");
98 for (int j = 0; j < index; j++)
101 if (everything_after)
104 while (ss.GetToken(more))
114 void SearchAndReplace(std::string& newline, const std::string &find, const std::string &replace)
116 std::string::size_type x = newline.find(find);
117 while (x != std::string::npos)
119 newline.erase(x, find.length());
120 newline.insert(x, replace);
121 x = newline.find(find);
125 virtual int OnPreCommand(std::string &command, std::vector<std::string> ¶meters, User *user, bool validated, const std::string &original_line)
128 std::multimap<std::string, Alias>::iterator i;
130 /* If theyre not registered yet, we dont want
133 if (user->registered != REG_ALL)
136 /* We dont have any commands looking like this? Stop processing. */
137 i = Aliases.find(command);
138 if (i == Aliases.end())
141 irc::string c = command.c_str();
142 /* The parameters for the command in their original form, with the command stripped off */
143 std::string compare = original_line.substr(command.length());
144 while (*(compare.c_str()) == ' ')
145 compare.erase(compare.begin());
147 std::string safe(original_line);
149 /* Escape out any $ symbols in the user provided text */
151 SearchAndReplace(safe, "$", "\r");
153 while (i != Aliases.end())
155 /* Does it match the pattern? */
156 if (!i->second.format.empty())
158 if (i->second.case_sensitive)
160 if (InspIRCd::Match(compare, i->second.format, case_sensitive_map))
165 if (InspIRCd::Match(compare, i->second.format))
170 if ((i->second.operonly) && (!IS_OPER(user)))
173 if (!i->second.requires.empty())
175 u = ServerInstance->FindNick(i->second.requires);
178 user->WriteNumeric(401, ""+std::string(user->nick)+" "+i->second.requires+" :is currently unavailable. Please try again later.");
182 if ((u != NULL) && (!i->second.requires.empty()) && (i->second.uline))
184 if (!ServerInstance->ULine(u->server))
186 ServerInstance->SNO->WriteToSnoMask('A', "NOTICE -- Service "+i->second.requires+" required by alias "+std::string(i->second.text.c_str())+" is not on a u-lined server, possibly underhanded antics detected!");
187 user->WriteNumeric(401, ""+std::string(user->nick)+" "+i->second.requires+" :is an imposter! Please inform an IRC operator as soon as possible.");
192 /* Now, search and replace in a copy of the original_line, replacing $1 through $9 and $1- etc */
194 std::string::size_type crlf = i->second.replace_with.find('\n');
196 if (crlf == std::string::npos)
198 DoCommand(i->second.replace_with, user, safe);
203 irc::sepstream commands(i->second.replace_with, '\n');
204 std::string scommand;
205 while (commands.GetToken(scommand))
207 DoCommand(scommand, user, safe);
217 void DoCommand(std::string newline, User* user, const std::string &original_line)
219 std::vector<std::string> pars;
221 for (int v = 1; v < 10; v++)
223 std::string var = "$";
224 var.append(ConvToStr(v));
226 std::string::size_type x = newline.find(var);
228 while (x != std::string::npos)
230 newline.erase(x, var.length());
231 newline.insert(x, GetVar(var, original_line));
232 x = newline.find(var);
236 var.append(ConvToStr(v));
237 x = newline.find(var);
239 while (x != std::string::npos)
241 newline.erase(x, var.length());
242 newline.insert(x, GetVar(var, original_line));
243 x = newline.find(var);
247 /* Special variables */
248 SearchAndReplace(newline, "$nick", user->nick);
249 SearchAndReplace(newline, "$ident", user->ident);
250 SearchAndReplace(newline, "$host", user->host);
251 SearchAndReplace(newline, "$vhost", user->dhost);
253 /* Unescape any variable names in the user text before sending */
254 SearchAndReplace(newline, "\r", "$");
256 irc::tokenstream ss(newline);
258 std::string command, token;
260 ss.GetToken(command);
261 while (ss.GetToken(token) && (pars.size() <= MAXPARAMETERS))
263 pars.push_back(token);
265 ServerInstance->Parser->CallHandler(command, pars, user);
268 virtual void OnRehash(User* user, const std::string ¶meter)
274 MODULE_INIT(ModuleAlias)