1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/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
23 /** The text of the alias command */
24 irc::string AliasedCommand;
26 /** Text to replace with */
27 std::string ReplaceFormat;
29 /** Nickname required to perform alias */
30 std::string RequiredNick;
32 /** Alias requires ulined server */
38 /* is case sensitive params */
41 /* whether or not it may be executed via fantasy (default OFF) */
44 /* whether or not it may be executed via /command (default ON) */
47 /** Format that must be matched for use */
51 class ModuleAlias : public Module
57 /* We cant use a map, there may be multiple aliases with the same name.
58 * We can, however, use a fancy invention: the multimap. Maps a key to one or more values.
61 std::multimap<irc::string, Alias> Aliases;
63 /* whether or not +B users are allowed to use fantasy commands */
66 virtual void ReadAliases()
70 AllowBots = MyConf.ReadFlag("fantasy", "allowbots", "no", 0);
72 std::string fpre = MyConf.ReadValue("fantasy","prefix",0);
73 fprefix = fpre.empty() ? '!' : fpre[0];
78 ConfigTag* tag = ServerInstance->Config->ConfValue("alias", i);
82 a.AliasedCommand = tag->getString("text").c_str();
83 tag->readString("replace", a.ReplaceFormat, true);
84 a.RequiredNick = tag->getString("requires");
85 a.ULineOnly = tag->getBool("uline");
86 a.ChannelCommand = tag->getBool("channelcommand", "no");
87 a.UserCommand = tag->getBool("usercommand", "yes");
88 a.OperOnly = tag->getBool("operonly");
89 a.format = tag->getString("format");
90 a.CaseSensitive = tag->getBool("matchcase");
91 Aliases.insert(std::make_pair(a.AliasedCommand, a));
100 ServerInstance->Modules->Attach(I_OnPreCommand, this);
101 ServerInstance->Modules->Attach(I_OnRehash, this);
102 ServerInstance->Modules->Attach(I_OnUserMessage, this);
105 virtual ~ModuleAlias()
109 virtual Version GetVersion()
111 return Version("Provides aliases of commands.", VF_VENDOR);
114 std::string GetVar(std::string varname, const std::string &original_line)
116 irc::spacesepstream ss(original_line);
117 varname.erase(varname.begin());
118 int index = *(varname.begin()) - 48;
119 varname.erase(varname.begin());
120 bool everything_after = (varname == "-");
123 for (int j = 0; j < index; j++)
126 if (everything_after)
129 while (ss.GetToken(more))
139 virtual ModResult OnPreCommand(std::string &command, std::vector<std::string> ¶meters, User *user, bool validated, const std::string &original_line)
141 std::multimap<irc::string, Alias>::iterator i, upperbound;
143 /* If theyre not registered yet, we dont want
146 if (user->registered != REG_ALL)
147 return MOD_RES_PASSTHRU;
149 /* We dont have any commands looking like this? Stop processing. */
150 i = Aliases.find(command.c_str());
151 if (i == Aliases.end())
152 return MOD_RES_PASSTHRU;
153 /* Avoid iterating on to different aliases if no patterns match. */
154 upperbound = Aliases.upper_bound(command.c_str());
156 irc::string c = command.c_str();
157 /* The parameters for the command in their original form, with the command stripped off */
158 std::string compare = original_line.substr(command.length());
159 while (*(compare.c_str()) == ' ')
160 compare.erase(compare.begin());
162 while (i != upperbound)
164 if (i->second.UserCommand)
166 if (DoAlias(user, NULL, &(i->second), compare, original_line))
175 // If we made it here, no aliases actually matched.
176 return MOD_RES_PASSTHRU;
179 virtual void OnUserMessage(User *user, void *dest, int target_type, const std::string &text, char status, const CUList &exempt_list)
181 if (target_type != TYPE_CHANNEL)
186 // fcommands are only for local users. Spanningtree will send them back out as their original cmd.
187 if (!user || !IS_LOCAL(user))
192 /* Stop here if the user is +B and allowbot is set to no. */
193 if (!AllowBots && user->IsModeSet('B'))
198 Channel *c = (Channel *)dest;
199 std::string scommand;
201 // text is like "!moo cows bite me", we want "!moo" first
202 irc::spacesepstream ss(text);
203 ss.GetToken(scommand);
204 irc::string fcommand = scommand.c_str();
206 if (fcommand.empty())
211 // we don't want to touch non-fantasy stuff
212 if (*fcommand.c_str() != fprefix)
217 // nor do we give a shit about the prefix
218 fcommand.erase(fcommand.begin());
220 std::multimap<irc::string, Alias>::iterator i = Aliases.find(fcommand);
222 if (i == Aliases.end())
225 /* Avoid iterating on to other aliases if no patterns match */
226 std::multimap<irc::string, Alias>::iterator upperbound = Aliases.upper_bound(fcommand);
229 /* The parameters for the command in their original form, with the command stripped off */
230 std::string compare = text.substr(fcommand.length() + 1);
231 while (*(compare.c_str()) == ' ')
232 compare.erase(compare.begin());
234 while (i != upperbound)
236 if (i->second.ChannelCommand)
238 // We use substr(1) here to remove the fantasy prefix
239 if (DoAlias(user, c, &(i->second), compare, text.substr(1)))
248 int DoAlias(User *user, Channel *c, Alias *a, const std::string compare, const std::string safe)
252 /* Does it match the pattern? */
253 if (!a->format.empty())
255 if (a->CaseSensitive)
257 if (!InspIRCd::Match(compare, a->format, rfc_case_sensitive_map))
262 if (!InspIRCd::Match(compare, a->format))
267 if ((a->OperOnly) && (!IS_OPER(user)))
270 if (!a->RequiredNick.empty())
272 u = ServerInstance->FindNick(a->RequiredNick);
275 user->WriteNumeric(401, ""+std::string(user->nick)+" "+a->RequiredNick+" :is currently unavailable. Please try again later.");
279 if ((u != NULL) && (!a->RequiredNick.empty()) && (a->ULineOnly))
281 if (!ServerInstance->ULine(u->server))
283 ServerInstance->SNO->WriteToSnoMask('a', "NOTICE -- Service "+a->RequiredNick+" required by alias "+std::string(a->AliasedCommand.c_str())+" is not on a u-lined server, possibly underhanded antics detected!");
284 user->WriteNumeric(401, ""+std::string(user->nick)+" "+a->RequiredNick+" :is an imposter! Please inform an IRC operator as soon as possible.");
289 /* Now, search and replace in a copy of the original_line, replacing $1 through $9 and $1- etc */
291 std::string::size_type crlf = a->ReplaceFormat.find('\n');
293 if (crlf == std::string::npos)
295 DoCommand(a->ReplaceFormat, user, c, safe);
300 irc::sepstream commands(a->ReplaceFormat, '\n');
301 std::string scommand;
302 while (commands.GetToken(scommand))
304 DoCommand(scommand, user, c, safe);
310 void DoCommand(const std::string& newline, User* user, Channel *chan, const std::string &original_line)
313 result.reserve(MAXBUF);
314 for (unsigned int i = 0; i < newline.length(); i++)
319 if (isdigit(newline[i+1]))
321 int len = (newline[i+2] == '-') ? 3 : 2;
322 std::string var = newline.substr(i, len);
323 result.append(GetVar(var, original_line));
326 else if (newline.substr(i, 5) == "$nick")
328 result.append(user->nick);
331 else if (newline.substr(i, 5) == "$host")
333 result.append(user->host);
336 else if (newline.substr(i, 5) == "$chan")
339 result.append(chan->name);
342 else if (newline.substr(i, 6) == "$ident")
344 result.append(user->ident);
347 else if (newline.substr(i, 6) == "$vhost")
349 result.append(user->dhost);
359 irc::tokenstream ss(result);
360 std::vector<std::string> pars;
361 std::string command, token;
363 ss.GetToken(command);
364 while (ss.GetToken(token) && (pars.size() <= MAXPARAMETERS))
366 pars.push_back(token);
368 ServerInstance->Parser->CallHandler(command, pars, user);
371 virtual void OnRehash(User* user)
376 virtual void Prioritize()
378 // Prioritise after spanningtree so that channel aliases show the alias before the effects.
379 Module* linkmod = ServerInstance->Modules->Find("m_spanningtree.so");
380 ServerInstance->Modules->SetPriority(this, I_OnUserMessage, PRIORITY_AFTER, &linkmod);
384 MODULE_INIT(ModuleAlias)