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 */
39 class ModuleAlias : public Module
42 std::vector<Alias> Aliases;
43 std::vector<std::string> pars;
45 virtual void ReadAliases()
47 ConfigReader MyConf(ServerInstance);
51 for (int i = 0; i < MyConf.Enumerate("alias"); i++)
55 txt = MyConf.ReadValue("alias", "text", i);
57 a.replace_with = MyConf.ReadValue("alias", "replace", i);
58 a.requires = MyConf.ReadValue("alias", "requires", i);
59 a.uline = MyConf.ReadFlag("alias", "uline", i);
60 a.operonly = MyConf.ReadFlag("alias", "operonly", i);
68 ModuleAlias(InspIRCd* Me)
75 void Implements(char* List)
77 List[I_OnPreCommand] = List[I_OnRehash] = 1;
80 virtual ~ModuleAlias()
84 virtual Version GetVersion()
86 return Version(1,1,0,1,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 == "-");
96 std::string word = "";
98 ServerInstance->Log(DEBUG,"Get var %d%s", index , everything_after ? " and all after it" : "");
100 for (int j = 0; j < index; j++)
101 word = ss.GetToken();
103 if (everything_after)
105 std::string more = "*";
106 while ((more = ss.GetToken()) != "")
113 ServerInstance->Log(DEBUG,"Var is '%s'", word.c_str());
118 void SearchAndReplace(std::string& newline, const std::string &find, const std::string &replace)
120 std::string::size_type x = newline.find(find);
121 while (x != std::string::npos)
123 newline.erase(x, find.length());
124 newline.insert(x, replace);
125 x = newline.find(find);
129 virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated, const std::string &original_line)
132 irc::string c = command.c_str();
133 /* If the command is valid, we dont want to know,
134 * and if theyre not registered yet, we dont want
137 if ((validated) || (user->registered != REG_ALL))
140 for (unsigned int i = 0; i < Aliases.size(); i++)
142 if (Aliases[i].text == c)
144 if ((Aliases[i].operonly) && (!*user->oper))
147 if (Aliases[i].requires != "")
149 u = ServerInstance->FindNick(Aliases[i].requires);
152 user->WriteServ("401 "+std::string(user->nick)+" "+Aliases[i].requires+" :is currently unavailable. Please try again later.");
156 if ((u != NULL) && (Aliases[i].requires != "") && (Aliases[i].uline))
158 if (!ServerInstance->ULine(u->server))
160 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!");
161 user->WriteServ("401 "+std::string(user->nick)+" "+Aliases[i].requires+" :is an imposter! Please inform an IRC operator as soon as possible.");
166 /* Now, search and replace in a copy of the original_line, replacing $1 through $9 and $1- etc */
168 std::string::size_type crlf = Aliases[i].replace_with.find('\n');
170 if (crlf == std::string::npos)
172 DoCommand(Aliases[i].replace_with, user, original_line);
177 irc::sepstream commands(Aliases[i].replace_with, '\n');
178 std::string command = "*";
179 while ((command = commands.GetToken()) != "")
181 DoCommand(command, user, original_line);
190 void DoCommand(std::string newline, userrec* user, const std::string &original_line)
192 for (int v = 1; v < 10; v++)
194 std::string var = "$";
195 var.append(ConvToStr(v));
197 std::string::size_type x = newline.find(var);
199 while (x != std::string::npos)
201 newline.erase(x, var.length());
202 newline.insert(x, GetVar(var, original_line));
203 x = newline.find(var);
207 var.append(ConvToStr(v));
208 x = newline.find(var);
210 while (x != std::string::npos)
212 newline.erase(x, var.length());
213 newline.insert(x, GetVar(var, original_line));
214 x = newline.find(var);
218 /* Special variables */
219 SearchAndReplace(newline, "$nick", user->nick);
220 SearchAndReplace(newline, "$ident", user->ident);
221 SearchAndReplace(newline, "$host", user->host);
222 SearchAndReplace(newline, "$vhost", user->dhost);
224 irc::tokenstream ss(newline);
225 const char* parv[127];
228 while ((pars[x] = ss.GetToken()) != "")
230 parv[x] = pars[x].c_str();
231 ServerInstance->Log(DEBUG,"Parameter %d: %s", x, parv[x]);
235 ServerInstance->Log(DEBUG,"Call command handler on %s", parv[0]);
237 if (ServerInstance->Parser->CallHandler(parv[0], &parv[1], x-1, user) == CMD_INVALID)
239 ServerInstance->Log(DEBUG,"Unknown command or not enough parameters");
243 ServerInstance->Log(DEBUG,"Command handler called successfully.");
247 virtual void OnRehash(const std::string ¶meter)
254 class ModuleAliasFactory : public ModuleFactory
261 ~ModuleAliasFactory()
265 virtual Module * CreateModule(InspIRCd* Me)
267 return new ModuleAlias(Me);
272 extern "C" void * init_module( void )
274 return new ModuleAliasFactory;