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 * ---------------------------------------------------
20 /* $ModDesc: Provides aliases of commands. */
22 /** An alias definition
24 class Alias : public classbase
27 /** The text of the alias command */
29 /** Text to replace with */
30 std::string replace_with;
31 /** Nickname required to perform alias */
33 /** Alias requires ulined server */
37 /** Format that must be matched for use */
41 class ModuleAlias : public Module
44 /** We cant use a map, there may be multiple aliases with the same name */
45 std::vector<Alias> Aliases;
46 std::vector<std::string> pars;
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);
70 ModuleAlias(InspIRCd* Me)
77 void Implements(char* List)
79 List[I_OnPreCommand] = List[I_OnRehash] = 1;
82 virtual ~ModuleAlias()
86 virtual Version GetVersion()
88 return Version(1,1,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 == "-");
98 std::string word = "";
100 ServerInstance->Log(DEBUG,"Get var %d%s", index , everything_after ? " and all after it" : "");
102 for (int j = 0; j < index; j++)
103 word = ss.GetToken();
105 if (everything_after)
107 std::string more = "*";
108 while ((more = ss.GetToken()) != "")
115 ServerInstance->Log(DEBUG,"Var is '%s'", word.c_str());
120 void SearchAndReplace(std::string& newline, const std::string &find, const std::string &replace)
122 std::string::size_type x = newline.find(find);
123 while (x != std::string::npos)
125 newline.erase(x, find.length());
126 newline.insert(x, replace);
127 x = newline.find(find);
131 virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated, const std::string &original_line)
135 /* If the command is valid, we dont want to know,
136 * and if theyre not registered yet, we dont want
139 if ((validated) || (user->registered != REG_ALL))
142 irc::string c = command.c_str();
143 /* The parameters for the command in their original form, with the command stripped off */
144 std::string compare = original_line.substr(command.length());
145 while (*(compare.c_str()) == ' ')
146 compare.erase(compare.begin());
148 std::string safe(original_line);
150 /* Escape out any $ symbols in the user provided text */
152 SearchAndReplace(safe, "$", "\r");
154 for (unsigned int i = 0; i < Aliases.size(); i++)
156 if (Aliases[i].text == c)
158 /* Does it match the pattern? */
159 if ((!Aliases[i].format.empty()) && (!ServerInstance->MatchText(compare, Aliases[i].format)))
164 if ((Aliases[i].operonly) && (!*user->oper))
167 if (Aliases[i].requires != "")
169 u = ServerInstance->FindNick(Aliases[i].requires);
172 user->WriteServ("401 "+std::string(user->nick)+" "+Aliases[i].requires+" :is currently unavailable. Please try again later.");
176 if ((u != NULL) && (Aliases[i].requires != "") && (Aliases[i].uline))
178 if (!ServerInstance->ULine(u->server))
180 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!");
181 user->WriteServ("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 ServerInstance->Log(DEBUG,"Single line alias: '%s'", Aliases[i].replace_with.c_str());
193 DoCommand(Aliases[i].replace_with, user, safe);
198 ServerInstance->Log(DEBUG,"Multi line alias: '%s'", Aliases[i].replace_with.c_str());
199 irc::sepstream commands(Aliases[i].replace_with, '\n');
200 std::string command = "*";
201 while ((command = commands.GetToken()) != "")
203 ServerInstance->Log(DEBUG,"Execute: '%s'", command.c_str());
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[127];
254 while ((pars[x] = ss.GetToken()) != "")
256 parv[x] = pars[x].c_str();
257 ServerInstance->Log(DEBUG,"Parameter %d: %s", x, parv[x]);
261 ServerInstance->Log(DEBUG,"Call command handler on %s", parv[0]);
263 if (ServerInstance->Parser->CallHandler(parv[0], &parv[1], x-1, user) == CMD_INVALID)
265 ServerInstance->Log(DEBUG,"Unknown command or not enough parameters");
269 ServerInstance->Log(DEBUG,"Command handler called successfully.");
273 virtual void OnRehash(userrec* user, const std::string ¶meter)
280 class ModuleAliasFactory : public ModuleFactory
287 ~ModuleAliasFactory()
291 virtual Module * CreateModule(InspIRCd* Me)
293 return new ModuleAlias(Me);
298 extern "C" void * init_module( void )
300 return new ModuleAliasFactory;