1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 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];
76 ConfigTagList tags = ServerInstance->Config->ConfTags("alias");
77 for(ConfigIter i = tags.first; i != tags.second; ++i)
79 ConfigTag* tag = i->second;
81 a.AliasedCommand = tag->getString("text").c_str();
82 tag->readString("replace", a.ReplaceFormat, true);
83 a.RequiredNick = tag->getString("requires");
84 a.ULineOnly = tag->getBool("uline");
85 a.ChannelCommand = tag->getBool("channelcommand", "no");
86 a.UserCommand = tag->getBool("usercommand", "yes");
87 a.OperOnly = tag->getBool("operonly");
88 a.format = tag->getString("format");
89 a.CaseSensitive = tag->getBool("matchcase");
90 Aliases.insert(std::make_pair(a.AliasedCommand, a));
99 ServerInstance->Modules->Attach(I_OnPreCommand, this);
100 ServerInstance->Modules->Attach(I_OnRehash, this);
101 ServerInstance->Modules->Attach(I_OnUserMessage, this);
104 virtual ~ModuleAlias()
108 virtual Version GetVersion()
110 return Version("Provides aliases of commands.", VF_VENDOR);
113 std::string GetVar(std::string varname, const std::string &original_line)
115 irc::spacesepstream ss(original_line);
116 varname.erase(varname.begin());
117 int index = *(varname.begin()) - 48;
118 varname.erase(varname.begin());
119 bool everything_after = (varname == "-");
122 for (int j = 0; j < index; j++)
125 if (everything_after)
128 while (ss.GetToken(more))
138 virtual ModResult OnPreCommand(std::string &command, std::vector<std::string> ¶meters, LocalUser *user, bool validated, const std::string &original_line)
140 std::multimap<irc::string, Alias>::iterator i, upperbound;
142 /* If theyre not registered yet, we dont want
145 if (user->registered != REG_ALL)
146 return MOD_RES_PASSTHRU;
148 /* We dont have any commands looking like this? Stop processing. */
149 i = Aliases.find(command.c_str());
150 if (i == Aliases.end())
151 return MOD_RES_PASSTHRU;
152 /* Avoid iterating on to different aliases if no patterns match. */
153 upperbound = Aliases.upper_bound(command.c_str());
155 irc::string c = command.c_str();
156 /* The parameters for the command in their original form, with the command stripped off */
157 std::string compare = original_line.substr(command.length());
158 while (*(compare.c_str()) == ' ')
159 compare.erase(compare.begin());
161 while (i != upperbound)
163 if (i->second.UserCommand)
165 if (DoAlias(user, NULL, &(i->second), compare, original_line))
174 // If we made it here, no aliases actually matched.
175 return MOD_RES_PASSTHRU;
178 virtual void OnUserMessage(User *user, void *dest, int target_type, const std::string &text, char status, const CUList &exempt_list)
180 if (target_type != TYPE_CHANNEL)
185 // fcommands are only for local users. Spanningtree will send them back out as their original cmd.
186 if (!user || !IS_LOCAL(user))
191 /* Stop here if the user is +B and allowbot is set to no. */
192 if (!AllowBots && user->IsModeSet('B'))
197 Channel *c = (Channel *)dest;
198 std::string scommand;
200 // text is like "!moo cows bite me", we want "!moo" first
201 irc::spacesepstream ss(text);
202 ss.GetToken(scommand);
203 irc::string fcommand = scommand.c_str();
205 if (fcommand.empty())
210 // we don't want to touch non-fantasy stuff
211 if (*fcommand.c_str() != fprefix)
216 // nor do we give a shit about the prefix
217 fcommand.erase(fcommand.begin());
219 std::multimap<irc::string, Alias>::iterator i = Aliases.find(fcommand);
221 if (i == Aliases.end())
224 /* Avoid iterating on to other aliases if no patterns match */
225 std::multimap<irc::string, Alias>::iterator upperbound = Aliases.upper_bound(fcommand);
228 /* The parameters for the command in their original form, with the command stripped off */
229 std::string compare = text.substr(fcommand.length() + 1);
230 while (*(compare.c_str()) == ' ')
231 compare.erase(compare.begin());
233 while (i != upperbound)
235 if (i->second.ChannelCommand)
237 // We use substr(1) here to remove the fantasy prefix
238 if (DoAlias(user, c, &(i->second), compare, text.substr(1)))
247 int DoAlias(User *user, Channel *c, Alias *a, const std::string compare, const std::string safe)
251 /* Does it match the pattern? */
252 if (!a->format.empty())
254 if (a->CaseSensitive)
256 if (!InspIRCd::Match(compare, a->format, rfc_case_sensitive_map))
261 if (!InspIRCd::Match(compare, a->format))
266 if ((a->OperOnly) && (!IS_OPER(user)))
269 if (!a->RequiredNick.empty())
271 u = ServerInstance->FindNick(a->RequiredNick);
274 user->WriteNumeric(401, ""+std::string(user->nick)+" "+a->RequiredNick+" :is currently unavailable. Please try again later.");
278 if ((u != NULL) && (!a->RequiredNick.empty()) && (a->ULineOnly))
280 if (!ServerInstance->ULine(u->server))
282 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!");
283 user->WriteNumeric(401, ""+std::string(user->nick)+" "+a->RequiredNick+" :is an imposter! Please inform an IRC operator as soon as possible.");
288 /* Now, search and replace in a copy of the original_line, replacing $1 through $9 and $1- etc */
290 std::string::size_type crlf = a->ReplaceFormat.find('\n');
292 if (crlf == std::string::npos)
294 DoCommand(a->ReplaceFormat, user, c, safe);
299 irc::sepstream commands(a->ReplaceFormat, '\n');
300 std::string scommand;
301 while (commands.GetToken(scommand))
303 DoCommand(scommand, user, c, safe);
309 void DoCommand(const std::string& newline, User* user, Channel *chan, const std::string &original_line)
312 result.reserve(MAXBUF);
313 for (unsigned int i = 0; i < newline.length(); i++)
318 if (isdigit(newline[i+1]))
320 int len = (newline[i+2] == '-') ? 3 : 2;
321 std::string var = newline.substr(i, len);
322 result.append(GetVar(var, original_line));
325 else if (newline.substr(i, 5) == "$nick")
327 result.append(user->nick);
330 else if (newline.substr(i, 5) == "$host")
332 result.append(user->host);
335 else if (newline.substr(i, 5) == "$chan")
338 result.append(chan->name);
341 else if (newline.substr(i, 6) == "$ident")
343 result.append(user->ident);
346 else if (newline.substr(i, 6) == "$vhost")
348 result.append(user->dhost);
358 irc::tokenstream ss(result);
359 std::vector<std::string> pars;
360 std::string command, token;
362 ss.GetToken(command);
363 while (ss.GetToken(token) && (pars.size() <= MAXPARAMETERS))
365 pars.push_back(token);
367 ServerInstance->Parser->CallHandler(command, pars, user);
370 virtual void OnRehash(User* user)
375 virtual void Prioritize()
377 // Prioritise after spanningtree so that channel aliases show the alias before the effects.
378 Module* linkmod = ServerInstance->Modules->Find("m_spanningtree.so");
379 ServerInstance->Modules->SetPriority(this, I_OnUserMessage, PRIORITY_AFTER, &linkmod);
383 MODULE_INIT(ModuleAlias)