1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 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 * ---------------------------------------------------
16 /* $ModDesc: Provides aliases of commands. */
18 /** An alias definition
20 class Alias : public classbase
23 /** The text of the alias command */
25 /** Text to replace with */
26 std::string replace_with;
27 /** Nickname required to perform alias */
29 /** Alias requires ulined server */
33 /* is case sensitive params */
35 /** Format that must be matched for use */
39 class ModuleAlias : public Module
42 /* We cant use a map, there may be multiple aliases with the same name.
43 * We can, however, use a fancy invention: the multimap. Maps a key to one or more values.
46 std::multimap<std::string, Alias> Aliases;
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);
64 a.case_sensitive = MyConf.ReadFlag("alias", "matchcase", i);
65 Aliases.insert(std::make_pair(txt, a));
71 ModuleAlias(InspIRCd* Me)
75 Me->Modules->Attach(I_OnPreCommand, this);
76 Me->Modules->Attach(I_OnRehash, this);
80 virtual ~ModuleAlias()
84 virtual Version GetVersion()
86 return Version("$Id$", 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 == "-");
98 for (int j = 0; j < index; j++)
101 if (everything_after)
104 while (ss.GetToken(more))
114 void SearchAndReplace(std::string& newline, const std::string &find, const std::string &replace)
116 std::string::size_type x = newline.find(find);
117 while (x != std::string::npos)
119 newline.erase(x, find.length());
120 newline.insert(x, replace);
121 x = newline.find(find);
125 virtual int OnPreCommand(std::string &command, std::vector<std::string> ¶meters, User *user, bool validated, const std::string &original_line)
127 std::multimap<std::string, Alias>::iterator i;
129 /* If theyre not registered yet, we dont want
132 if (user->registered != REG_ALL)
135 /* We dont have any commands looking like this? Stop processing. */
136 i = Aliases.find(command);
137 if (i == Aliases.end())
140 irc::string c = command.c_str();
141 /* The parameters for the command in their original form, with the command stripped off */
142 std::string compare = original_line.substr(command.length());
143 while (*(compare.c_str()) == ' ')
144 compare.erase(compare.begin());
146 std::string safe(original_line);
148 /* Escape out any $ symbols in the user provided text */
150 SearchAndReplace(safe, "$", "\r");
152 while (i != Aliases.end())
154 DoAlias(user, &(i->second), compare, safe);
159 // If aliases have been processed, aliases took it.
163 void DoAlias(User *user, Alias *a, const std::string compare, const std::string safe)
166 /* Does it match the pattern? */
167 if (!a->format.empty())
169 if (a->case_sensitive)
171 if (InspIRCd::Match(compare, a->format, case_sensitive_map))
176 if (InspIRCd::Match(compare, a->format))
181 if ((a->operonly) && (!IS_OPER(user)))
184 if (!a->requires.empty())
186 u = ServerInstance->FindNick(a->requires);
189 user->WriteNumeric(401, ""+std::string(user->nick)+" "+a->requires+" :is currently unavailable. Please try again later.");
193 if ((u != NULL) && (!a->requires.empty()) && (a->uline))
195 if (!ServerInstance->ULine(u->server))
197 ServerInstance->SNO->WriteToSnoMask('A', "NOTICE -- Service "+a->requires+" required by alias "+std::string(a->text.c_str())+" is not on a u-lined server, possibly underhanded antics detected!");
198 user->WriteNumeric(401, ""+std::string(user->nick)+" "+a->requires+" :is an imposter! Please inform an IRC operator as soon as possible.");
203 /* Now, search and replace in a copy of the original_line, replacing $1 through $9 and $1- etc */
205 std::string::size_type crlf = a->replace_with.find('\n');
207 if (crlf == std::string::npos)
209 DoCommand(a->replace_with, user, safe);
214 irc::sepstream commands(a->replace_with, '\n');
215 std::string scommand;
216 while (commands.GetToken(scommand))
218 DoCommand(scommand, user, safe);
224 void DoCommand(std::string newline, User* user, const std::string &original_line)
226 std::vector<std::string> pars;
228 for (int v = 1; v < 10; v++)
230 std::string var = "$";
231 var.append(ConvToStr(v));
233 std::string::size_type x = newline.find(var);
235 while (x != std::string::npos)
237 newline.erase(x, var.length());
238 newline.insert(x, GetVar(var, original_line));
239 x = newline.find(var);
243 var.append(ConvToStr(v));
244 x = newline.find(var);
246 while (x != std::string::npos)
248 newline.erase(x, var.length());
249 newline.insert(x, GetVar(var, original_line));
250 x = newline.find(var);
254 /* Special variables */
255 SearchAndReplace(newline, "$nick", user->nick);
256 SearchAndReplace(newline, "$ident", user->ident);
257 SearchAndReplace(newline, "$host", user->host);
258 SearchAndReplace(newline, "$vhost", user->dhost);
260 /* Unescape any variable names in the user text before sending */
261 SearchAndReplace(newline, "\r", "$");
263 irc::tokenstream ss(newline);
265 std::string command, token;
267 ss.GetToken(command);
268 while (ss.GetToken(token) && (pars.size() <= MAXPARAMETERS))
270 pars.push_back(token);
272 ServerInstance->Parser->CallHandler(command, pars, user);
275 virtual void OnRehash(User* user, const std::string ¶meter)
281 MODULE_INIT(ModuleAlias)