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 * ---------------------------------------------------
17 /* $ModDesc: Provides aliases of commands. */
19 /** An alias definition
21 class Alias : public classbase
24 /** The text of the alias command */
26 /** Text to replace with */
27 std::string replace_with;
28 /** Nickname required to perform alias */
30 /** Alias requires ulined server */
34 /* is case sensitive params */
36 /** Format that must be matched for use */
40 class ModuleAlias : public Module
43 /** We cant use a map, there may be multiple aliases with the same name */
44 std::vector<Alias> Aliases;
45 std::map<std::string, int> AliasMap;
46 std::vector<std::string> pars;
48 virtual void ReadAliases()
50 ConfigReader MyConf(ServerInstance);
54 for (int i = 0; i < MyConf.Enumerate("alias"); i++)
58 txt = MyConf.ReadValue("alias", "text", i);
60 a.replace_with = MyConf.ReadValue("alias", "replace", i, true);
61 a.requires = MyConf.ReadValue("alias", "requires", i);
62 a.uline = MyConf.ReadFlag("alias", "uline", i);
63 a.operonly = MyConf.ReadFlag("alias", "operonly", i);
64 a.format = MyConf.ReadValue("alias", "format", i);
65 a.case_sensitive = MyConf.ReadFlag("alias", "matchcase", i);
73 ModuleAlias(InspIRCd* Me)
77 Me->Modules->Attach(I_OnPreCommand, this);
78 Me->Modules->Attach(I_OnRehash, this);
82 virtual ~ModuleAlias()
86 virtual Version GetVersion()
88 return Version(1,2,0,1,VF_VENDOR,API_VERSION);
91 std::string GetVar(std::string varname, const std::string &original_line)
93 irc::spacesepstream ss(original_line);
94 varname.erase(varname.begin());
95 int index = *(varname.begin()) - 48;
96 varname.erase(varname.begin());
97 bool everything_after = (varname == "-");
100 for (int j = 0; j < index; j++)
103 if (everything_after)
106 while (ss.GetToken(more))
116 void SearchAndReplace(std::string& newline, const std::string &find, const std::string &replace)
118 std::string::size_type x = newline.find(find);
119 while (x != std::string::npos)
121 newline.erase(x, find.length());
122 newline.insert(x, replace);
123 x = newline.find(find);
127 virtual int OnPreCommand(const std::string &command, const std::vector<std::string> ¶meters, User *user, bool validated, const std::string &original_line)
131 /* If theyre not registered yet, we dont want
134 if (user->registered != REG_ALL)
137 /* We dont have any commands looking like this, dont bother with the loop */
138 if (AliasMap.find(command) == AliasMap.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 for (unsigned int i = 0; i < Aliases.size(); i++)
155 if (Aliases[i].text == c)
157 /* Does it match the pattern? */
158 if (!Aliases[i].format.empty())
160 if (!match(Aliases[i].case_sensitive, compare, Aliases[i].format))
164 if ((Aliases[i].operonly) && (!IS_OPER(user)))
167 if (!Aliases[i].requires.empty())
169 u = ServerInstance->FindNick(Aliases[i].requires);
172 user->WriteNumeric(401, ""+std::string(user->nick)+" "+Aliases[i].requires+" :is currently unavailable. Please try again later.");
176 if ((u != NULL) && (!Aliases[i].requires.empty()) && (Aliases[i].uline))
178 if (!ServerInstance->ULine(u->server))
180 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!");
181 user->WriteNumeric(401, ""+std::string(user->nick)+" "+Aliases[i].requires+" :is an imposter! Please inform an IRC operator as soon as possible.");
186 /* Now, search and replace in a copy of the original_line, replacing $1 through $9 and $1- etc */
188 std::string::size_type crlf = Aliases[i].replace_with.find('\n');
190 if (crlf == std::string::npos)
192 DoCommand(Aliases[i].replace_with, user, safe);
197 irc::sepstream commands(Aliases[i].replace_with, '\n');
198 std::string scommand;
199 while (commands.GetToken(scommand))
201 DoCommand(scommand, user, safe);
210 void DoCommand(std::string newline, User* user, const std::string &original_line)
212 for (int v = 1; v < 10; v++)
214 std::string var = "$";
215 var.append(ConvToStr(v));
217 std::string::size_type x = newline.find(var);
219 while (x != std::string::npos)
221 newline.erase(x, var.length());
222 newline.insert(x, GetVar(var, original_line));
223 x = newline.find(var);
227 var.append(ConvToStr(v));
228 x = newline.find(var);
230 while (x != std::string::npos)
232 newline.erase(x, var.length());
233 newline.insert(x, GetVar(var, original_line));
234 x = newline.find(var);
238 /* Special variables */
239 SearchAndReplace(newline, "$nick", user->nick);
240 SearchAndReplace(newline, "$ident", user->ident);
241 SearchAndReplace(newline, "$host", user->host);
242 SearchAndReplace(newline, "$vhost", user->dhost);
244 /* Unescape any variable names in the user text before sending */
245 SearchAndReplace(newline, "\r", "$");
247 irc::tokenstream ss(newline);
249 std::string command, token;
251 ss.GetToken(command);
252 while (ss.GetToken(token) && (pars.size() <= MAXPARAMETERS))
254 pars.push_back(token);
256 ServerInstance->Parser->CallHandler(command, pars, user);
259 virtual void OnRehash(User* user, const std::string ¶meter)
265 MODULE_INIT(ModuleAlias)