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);
79 Me->Modules->Attach(I_OnPreCommand, this);
80 Me->Modules->Attach(I_OnRehash, this);
84 virtual ~ModuleAlias()
88 virtual Version GetVersion()
90 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
93 std::string GetVar(std::string varname, const std::string &original_line)
95 irc::spacesepstream ss(original_line);
96 varname.erase(varname.begin());
97 int index = *(varname.begin()) - 48;
98 varname.erase(varname.begin());
99 bool everything_after = (varname == "-");
102 for (int j = 0; j < index; j++)
105 if (everything_after)
108 while (ss.GetToken(more))
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, User *user, bool validated, const std::string &original_line)
133 /* If theyre not registered yet, we dont want
136 if (user->registered != REG_ALL)
139 /* We dont have any commands looking like this, dont bother with the loop */
140 if (AliasMap.find(command) == AliasMap.end())
143 irc::string c = command.c_str();
144 /* The parameters for the command in their original form, with the command stripped off */
145 std::string compare = original_line.substr(command.length());
146 while (*(compare.c_str()) == ' ')
147 compare.erase(compare.begin());
149 std::string safe(original_line);
151 /* Escape out any $ symbols in the user provided text */
153 SearchAndReplace(safe, "$", "\r");
155 for (unsigned int i = 0; i < Aliases.size(); i++)
157 if (Aliases[i].text == c)
159 /* Does it match the pattern? */
160 if (!Aliases[i].format.empty())
162 if (!match(Aliases[i].case_sensitive, compare.c_str(), Aliases[i].format.c_str()))
166 if ((Aliases[i].operonly) && (!IS_OPER(user)))
169 if (!Aliases[i].requires.empty())
171 u = ServerInstance->FindNick(Aliases[i].requires);
174 user->WriteServ("401 "+std::string(user->nick)+" "+Aliases[i].requires+" :is currently unavailable. Please try again later.");
178 if ((u != NULL) && (!Aliases[i].requires.empty()) && (Aliases[i].uline))
180 if (!ServerInstance->ULine(u->server))
182 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!");
183 user->WriteServ("401 "+std::string(user->nick)+" "+Aliases[i].requires+" :is an imposter! Please inform an IRC operator as soon as possible.");
188 /* Now, search and replace in a copy of the original_line, replacing $1 through $9 and $1- etc */
190 std::string::size_type crlf = Aliases[i].replace_with.find('\n');
192 if (crlf == std::string::npos)
194 DoCommand(Aliases[i].replace_with, user, safe);
199 irc::sepstream commands(Aliases[i].replace_with, '\n');
201 while (commands.GetToken(command))
203 DoCommand(command, user, safe);
212 void DoCommand(std::string newline, User* user, const std::string &original_line)
214 for (int v = 1; v < 10; v++)
216 std::string var = "$";
217 var.append(ConvToStr(v));
219 std::string::size_type x = newline.find(var);
221 while (x != std::string::npos)
223 newline.erase(x, var.length());
224 newline.insert(x, GetVar(var, original_line));
225 x = newline.find(var);
229 var.append(ConvToStr(v));
230 x = newline.find(var);
232 while (x != std::string::npos)
234 newline.erase(x, var.length());
235 newline.insert(x, GetVar(var, original_line));
236 x = newline.find(var);
240 /* Special variables */
241 SearchAndReplace(newline, "$nick", user->nick);
242 SearchAndReplace(newline, "$ident", user->ident);
243 SearchAndReplace(newline, "$host", user->host);
244 SearchAndReplace(newline, "$vhost", user->dhost);
246 /* Unescape any variable names in the user text before sending */
247 SearchAndReplace(newline, "\r", "$");
249 irc::tokenstream ss(newline);
250 const char* parv[MAXPARAMETERS];
253 while (ss.GetToken(pars[x]) && x < MAXPARAMETERS)
255 parv[x] = pars[x].c_str();
259 ServerInstance->Parser->CallHandler(parv[0], &parv[1], x-1, user);
262 virtual void OnRehash(User* user, const std::string ¶meter)
268 MODULE_INIT(ModuleAlias)