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 * ---------------------------------------------------
21 /* $ModDesc: Provides aliases of commands. */
23 /** An alias definition
25 class Alias : public classbase
28 /** The text of the alias command */
30 /** Text to replace with */
31 std::string replace_with;
32 /** Nickname required to perform alias */
34 /** Alias requires ulined server */
38 /* is case sensitive params */
40 /** Format that must be matched for use */
44 class ModuleAlias : public Module
47 /** We cant use a map, there may be multiple aliases with the same name */
48 std::vector<Alias> Aliases;
49 std::map<std::string, int> AliasMap;
50 std::vector<std::string> pars;
52 virtual void ReadAliases()
54 ConfigReader MyConf(ServerInstance);
58 for (int i = 0; i < MyConf.Enumerate("alias"); i++)
62 txt = MyConf.ReadValue("alias", "text", i);
64 a.replace_with = MyConf.ReadValue("alias", "replace", i, true);
65 a.requires = MyConf.ReadValue("alias", "requires", i);
66 a.uline = MyConf.ReadFlag("alias", "uline", i);
67 a.operonly = MyConf.ReadFlag("alias", "operonly", i);
68 a.format = MyConf.ReadValue("alias", "format", i);
69 a.case_sensitive = MyConf.ReadFlag("alias", "matchcase", i);
77 ModuleAlias(InspIRCd* Me)
84 void Implements(char* List)
86 List[I_OnPreCommand] = List[I_OnRehash] = 1;
89 virtual ~ModuleAlias()
93 virtual Version GetVersion()
95 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
98 std::string GetVar(std::string varname, const std::string &original_line)
100 irc::spacesepstream ss(original_line);
101 varname.erase(varname.begin());
102 int index = *(varname.begin()) - 48;
103 varname.erase(varname.begin());
104 bool everything_after = (varname == "-");
105 std::string word = "";
107 ServerInstance->Log(DEBUG,"Get var %d%s", index , everything_after ? " and all after it" : "");
109 for (int j = 0; j < index; j++)
110 word = ss.GetToken();
112 if (everything_after)
114 std::string more = "*";
115 while ((more = ss.GetToken()) != "")
122 ServerInstance->Log(DEBUG,"Var is '%s'", word.c_str());
127 void SearchAndReplace(std::string& newline, const std::string &find, const std::string &replace)
129 std::string::size_type x = newline.find(find);
130 while (x != std::string::npos)
132 newline.erase(x, find.length());
133 newline.insert(x, replace);
134 x = newline.find(find);
138 virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated, const std::string &original_line)
142 /* If theyre not registered yet, we dont want
145 if (user->registered != REG_ALL)
148 /* We dont have any commands looking like this, dont bother with the loop */
149 if (AliasMap.find(command) == AliasMap.end())
152 irc::string c = command.c_str();
153 /* The parameters for the command in their original form, with the command stripped off */
154 std::string compare = original_line.substr(command.length());
155 while (*(compare.c_str()) == ' ')
156 compare.erase(compare.begin());
158 std::string safe(original_line);
160 /* Escape out any $ symbols in the user provided text */
162 SearchAndReplace(safe, "$", "\r");
164 for (unsigned int i = 0; i < Aliases.size(); i++)
166 if (Aliases[i].text == c)
168 /* Does it match the pattern? */
169 if (!Aliases[i].format.empty())
171 if (!match(Aliases[i].case_sensitive, compare.c_str(), Aliases[i].format.c_str()))
175 if ((Aliases[i].operonly) && (!*user->oper))
178 if (Aliases[i].requires != "")
180 u = ServerInstance->FindNick(Aliases[i].requires);
183 user->WriteServ("401 "+std::string(user->nick)+" "+Aliases[i].requires+" :is currently unavailable. Please try again later.");
187 if ((u != NULL) && (Aliases[i].requires != "") && (Aliases[i].uline))
189 if (!ServerInstance->ULine(u->server))
191 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!");
192 user->WriteServ("401 "+std::string(user->nick)+" "+Aliases[i].requires+" :is an imposter! Please inform an IRC operator as soon as possible.");
197 /* Now, search and replace in a copy of the original_line, replacing $1 through $9 and $1- etc */
199 std::string::size_type crlf = Aliases[i].replace_with.find('\n');
201 if (crlf == std::string::npos)
203 ServerInstance->Log(DEBUG,"Single line alias: '%s'", Aliases[i].replace_with.c_str());
204 DoCommand(Aliases[i].replace_with, user, safe);
209 ServerInstance->Log(DEBUG,"Multi line alias: '%s'", Aliases[i].replace_with.c_str());
210 irc::sepstream commands(Aliases[i].replace_with, '\n');
211 std::string command = "*";
212 while ((command = commands.GetToken()) != "")
214 ServerInstance->Log(DEBUG,"Execute: '%s'", command.c_str());
215 DoCommand(command, user, safe);
224 void DoCommand(std::string newline, userrec* user, const std::string &original_line)
226 for (int v = 1; v < 10; v++)
228 std::string var = "$";
229 var.append(ConvToStr(v));
231 std::string::size_type 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 var.append(ConvToStr(v));
242 x = newline.find(var);
244 while (x != std::string::npos)
246 newline.erase(x, var.length());
247 newline.insert(x, GetVar(var, original_line));
248 x = newline.find(var);
252 /* Special variables */
253 SearchAndReplace(newline, "$nick", user->nick);
254 SearchAndReplace(newline, "$ident", user->ident);
255 SearchAndReplace(newline, "$host", user->host);
256 SearchAndReplace(newline, "$vhost", user->dhost);
258 /* Unescape any variable names in the user text before sending */
259 SearchAndReplace(newline, "\r", "$");
261 irc::tokenstream ss(newline);
262 const char* parv[127];
265 while ((pars[x] = ss.GetToken()) != "")
267 parv[x] = pars[x].c_str();
268 ServerInstance->Log(DEBUG,"Parameter %d: %s", x, parv[x]);
272 ServerInstance->Log(DEBUG,"Call command handler on %s", parv[0]);
274 if (ServerInstance->Parser->CallHandler(parv[0], &parv[1], x-1, user) == CMD_INVALID)
276 ServerInstance->Log(DEBUG,"Unknown command or not enough parameters");
280 ServerInstance->Log(DEBUG,"Command handler called successfully.");
284 virtual void OnRehash(userrec* user, const std::string ¶meter)
291 class ModuleAliasFactory : public ModuleFactory
298 ~ModuleAliasFactory()
302 virtual Module * CreateModule(InspIRCd* Me)
304 return new ModuleAlias(Me);
309 extern "C" void * init_module( void )
311 return new ModuleAliasFactory;