1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 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 pars.resize(MAXPARAMETERS);
80 void Implements(char* List)
82 List[I_OnPreCommand] = List[I_OnRehash] = 1;
85 virtual ~ModuleAlias()
89 virtual Version GetVersion()
91 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
94 std::string GetVar(std::string varname, const std::string &original_line)
96 irc::spacesepstream ss(original_line);
97 varname.erase(varname.begin());
98 int index = *(varname.begin()) - 48;
99 varname.erase(varname.begin());
100 bool everything_after = (varname == "-");
103 for (int j = 0; j < index; j++)
106 if (everything_after)
109 while (ss.GetToken(more))
119 void SearchAndReplace(std::string& newline, const std::string &find, const std::string &replace)
121 std::string::size_type x = newline.find(find);
122 while (x != std::string::npos)
124 newline.erase(x, find.length());
125 newline.insert(x, replace);
126 x = newline.find(find);
130 virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated, const std::string &original_line)
134 /* If theyre not registered yet, we dont want
137 if (user->registered != REG_ALL)
140 /* We dont have any commands looking like this, dont bother with the loop */
141 if (AliasMap.find(command) == AliasMap.end())
144 irc::string c = command.c_str();
145 /* The parameters for the command in their original form, with the command stripped off */
146 std::string compare = original_line.substr(command.length());
147 while (*(compare.c_str()) == ' ')
148 compare.erase(compare.begin());
150 std::string safe(original_line);
152 /* Escape out any $ symbols in the user provided text */
154 SearchAndReplace(safe, "$", "\r");
156 for (unsigned int i = 0; i < Aliases.size(); i++)
158 if (Aliases[i].text == c)
160 /* Does it match the pattern? */
161 if (!Aliases[i].format.empty())
163 if (!match(Aliases[i].case_sensitive, compare.c_str(), Aliases[i].format.c_str()))
167 if ((Aliases[i].operonly) && (!IS_OPER(user)))
170 if (!Aliases[i].requires.empty())
172 u = ServerInstance->FindNick(Aliases[i].requires);
175 user->WriteServ("401 "+std::string(user->nick)+" "+Aliases[i].requires+" :is currently unavailable. Please try again later.");
179 if ((u != NULL) && (!Aliases[i].requires.empty()) && (Aliases[i].uline))
181 if (!ServerInstance->ULine(u->server))
183 ServerInstance->WriteOpers("*** 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!");
184 user->WriteServ("401 "+std::string(user->nick)+" "+Aliases[i].requires+" :is an imposter! Please inform an IRC operator as soon as possible.");
189 /* Now, search and replace in a copy of the original_line, replacing $1 through $9 and $1- etc */
191 std::string::size_type crlf = Aliases[i].replace_with.find('\n');
193 if (crlf == std::string::npos)
195 DoCommand(Aliases[i].replace_with, user, safe);
200 irc::sepstream commands(Aliases[i].replace_with, '\n');
202 while (commands.GetToken(command))
204 DoCommand(command, user, safe);
213 void DoCommand(std::string newline, userrec* user, const std::string &original_line)
215 for (int v = 1; v < 10; v++)
217 std::string var = "$";
218 var.append(ConvToStr(v));
220 std::string::size_type x = newline.find(var);
222 while (x != std::string::npos)
224 newline.erase(x, var.length());
225 newline.insert(x, GetVar(var, original_line));
226 x = newline.find(var);
230 var.append(ConvToStr(v));
231 x = newline.find(var);
233 while (x != std::string::npos)
235 newline.erase(x, var.length());
236 newline.insert(x, GetVar(var, original_line));
237 x = newline.find(var);
241 /* Special variables */
242 SearchAndReplace(newline, "$nick", user->nick);
243 SearchAndReplace(newline, "$ident", user->ident);
244 SearchAndReplace(newline, "$host", user->host);
245 SearchAndReplace(newline, "$vhost", user->dhost);
247 /* Unescape any variable names in the user text before sending */
248 SearchAndReplace(newline, "\r", "$");
250 irc::tokenstream ss(newline);
251 const char* parv[MAXPARAMETERS];
254 while (ss.GetToken(pars[x]) && x < MAXPARAMETERS)
256 parv[x] = pars[x].c_str();
260 ServerInstance->Parser->CallHandler(parv[0], &parv[1], x-1, user);
263 virtual void OnRehash(userrec* user, const std::string ¶meter)
269 MODULE_INIT(ModuleAlias)