]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_alias.cpp
Improvements and bugfixes to the cgiirc module.
[user/henk/code/inspircd.git] / src / modules / m_alias.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2005, 2009 Robin Burchell <robin+git@viroteck.net>
6  *   Copyright (C) 2004-2007, 2009 Craig Edwards <craigedwards@brainbox.cc>
7  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
8  *
9  * This file is part of InspIRCd.  InspIRCd is free software: you can
10  * redistribute it and/or modify it under the terms of the GNU General Public
11  * License as published by the Free Software Foundation, version 2.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22
23 #include "inspircd.h"
24
25 /** An alias definition
26  */
27 class Alias
28 {
29  public:
30         /** The text of the alias command */
31         std::string AliasedCommand;
32
33         /** Text to replace with */
34         std::string ReplaceFormat;
35
36         /** Nickname required to perform alias */
37         std::string RequiredNick;
38
39         /** Alias requires ulined server */
40         bool ULineOnly;
41
42         /** Requires oper? */
43         bool OperOnly;
44
45         /* whether or not it may be executed via fantasy (default OFF) */
46         bool ChannelCommand;
47
48         /* whether or not it may be executed via /command (default ON) */
49         bool UserCommand;
50
51         /** Format that must be matched for use */
52         std::string format;
53 };
54
55 class ModuleAlias : public Module
56 {
57         std::string fprefix;
58
59         /* We cant use a map, there may be multiple aliases with the same name.
60          * We can, however, use a fancy invention: the multimap. Maps a key to one or more values.
61          *              -- w00t
62          */
63         typedef insp::flat_multimap<std::string, Alias, irc::insensitive_swo> AliasMap;
64
65         AliasMap Aliases;
66
67         /* whether or not +B users are allowed to use fantasy commands */
68         bool AllowBots;
69         UserModeReference botmode;
70
71         // Whether we are actively executing an alias.
72         bool active;
73
74  public:
75         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
76         {
77                 ConfigTag* fantasy = ServerInstance->Config->ConfValue("fantasy");
78                 AllowBots = fantasy->getBool("allowbots", false);
79                 fprefix = fantasy->getString("prefix", "!", 1, ServerInstance->Config->Limits.MaxLine);
80
81                 Aliases.clear();
82                 ConfigTagList tags = ServerInstance->Config->ConfTags("alias");
83                 for(ConfigIter i = tags.first; i != tags.second; ++i)
84                 {
85                         ConfigTag* tag = i->second;
86                         Alias a;
87                         a.AliasedCommand = tag->getString("text");
88                         std::transform(a.AliasedCommand.begin(), a.AliasedCommand.end(), a.AliasedCommand.begin(), ::toupper);
89                         tag->readString("replace", a.ReplaceFormat, true);
90                         a.RequiredNick = tag->getString("requires");
91                         a.ULineOnly = tag->getBool("uline");
92                         a.ChannelCommand = tag->getBool("channelcommand", false);
93                         a.UserCommand = tag->getBool("usercommand", true);
94                         a.OperOnly = tag->getBool("operonly");
95                         a.format = tag->getString("format");
96                         Aliases.insert(std::make_pair(a.AliasedCommand, a));
97                 }
98         }
99
100         ModuleAlias()
101                 : botmode(this, "bot")
102         {
103         }
104
105         Version GetVersion() CXX11_OVERRIDE
106         {
107                 return Version("Provides aliases of commands.", VF_VENDOR);
108         }
109
110         std::string GetVar(std::string varname, const std::string &original_line)
111         {
112                 irc::spacesepstream ss(original_line);
113                 varname.erase(varname.begin());
114                 int index = *(varname.begin()) - 48;
115                 varname.erase(varname.begin());
116                 bool everything_after = (varname == "-");
117                 std::string word;
118
119                 for (int j = 0; j < index; j++)
120                         ss.GetToken(word);
121
122                 if (everything_after)
123                 {
124                         std::string more;
125                         while (ss.GetToken(more))
126                         {
127                                 word.append(" ");
128                                 word.append(more);
129                         }
130                 }
131
132                 return word;
133         }
134
135         std::string CreateRFCMessage(const std::string& command, CommandBase::Params& parameters)
136         {
137                 std::string message(command);
138                 for (CommandBase::Params::const_iterator iter = parameters.begin(); iter != parameters.end();)
139                 {
140                         const std::string& parameter = *iter++;
141                         message.push_back(' ');
142                         if (iter == parameters.end() && (parameter.empty() || parameter.find(' ') != std::string::npos))
143                                 message.push_back(':');
144                         message.append(parameter);
145                 }
146                 return message;
147         }
148
149         ModResult OnPreCommand(std::string& command, CommandBase::Params& parameters, LocalUser* user, bool validated) CXX11_OVERRIDE
150         {
151                 /* If theyre not registered yet, we dont want
152                  * to know.
153                  */
154                 if (user->registered != REG_ALL)
155                         return MOD_RES_PASSTHRU;
156
157                 /* We dont have any commands looking like this? Stop processing. */
158                 std::pair<AliasMap::iterator, AliasMap::iterator> iters = Aliases.equal_range(command);
159                 if (iters.first == iters.second)
160                         return MOD_RES_PASSTHRU;
161
162                 /* The parameters for the command in their original form, with the command stripped off */
163                 std::string original_line = CreateRFCMessage(command, parameters);
164                 std::string compare(original_line, command.length());
165                 while (*(compare.c_str()) == ' ')
166                         compare.erase(compare.begin());
167
168                 for (AliasMap::iterator i = iters.first; i != iters.second; ++i)
169                 {
170                         if (i->second.UserCommand)
171                         {
172                                 if (DoAlias(user, NULL, &(i->second), compare, original_line))
173                                 {
174                                         return MOD_RES_DENY;
175                                 }
176                         }
177                 }
178
179                 // If we made it here, no aliases actually matched.
180                 return MOD_RES_PASSTHRU;
181         }
182
183         ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
184         {
185                 // Don't echo anything which is caused by an alias.
186                 if (active)
187                         details.echo = false;
188
189                 return MOD_RES_PASSTHRU;
190         }
191
192         void OnUserPostMessage(User* user, const MessageTarget& target, const MessageDetails& details) CXX11_OVERRIDE
193         {
194                 if ((target.type != MessageTarget::TYPE_CHANNEL) || (details.type != MSG_PRIVMSG))
195                 {
196                         return;
197                 }
198
199                 // fcommands are only for local users. Spanningtree will send them back out as their original cmd.
200                 if (!IS_LOCAL(user))
201                 {
202                         return;
203                 }
204
205                 /* Stop here if the user is +B and allowbot is set to no. */
206                 if (!AllowBots && user->IsModeSet(botmode))
207                 {
208                         return;
209                 }
210
211                 Channel *c = target.Get<Channel>();
212                 std::string scommand;
213
214                 // text is like "!moo cows bite me", we want "!moo" first
215                 irc::spacesepstream ss(details.text);
216                 ss.GetToken(scommand);
217
218                 if (scommand.size() <= fprefix.size())
219                 {
220                         return; // wtfbbq
221                 }
222
223                 // we don't want to touch non-fantasy stuff
224                 if (scommand.compare(0, fprefix.size(), fprefix) != 0)
225                 {
226                         return;
227                 }
228
229                 // nor do we give a shit about the prefix
230                 scommand.erase(0, fprefix.size());
231
232                 std::pair<AliasMap::iterator, AliasMap::iterator> iters = Aliases.equal_range(scommand);
233                 if (iters.first == iters.second)
234                         return;
235
236                 /* The parameters for the command in their original form, with the command stripped off */
237                 std::string compare(details.text, scommand.length() + fprefix.size());
238                 while (*(compare.c_str()) == ' ')
239                         compare.erase(compare.begin());
240
241                 for (AliasMap::iterator i = iters.first; i != iters.second; ++i)
242                 {
243                         if (i->second.ChannelCommand)
244                         {
245                                 // We use substr here to remove the fantasy prefix
246                                 if (DoAlias(user, c, &(i->second), compare, details.text.substr(fprefix.size())))
247                                         return;
248                         }
249                 }
250         }
251
252
253         int DoAlias(User *user, Channel *c, Alias *a, const std::string& compare, const std::string& safe)
254         {
255                 /* Does it match the pattern? */
256                 if (!a->format.empty())
257                 {
258                         if (!InspIRCd::Match(compare, a->format))
259                                 return 0;
260                 }
261
262                 if ((a->OperOnly) && (!user->IsOper()))
263                         return 0;
264
265                 if (!a->RequiredNick.empty())
266                 {
267                         User* u = ServerInstance->FindNick(a->RequiredNick);
268                         if (!u)
269                         {
270                                 user->WriteNumeric(ERR_NOSUCHNICK, a->RequiredNick, "is currently unavailable. Please try again later.");
271                                 return 1;
272                         }
273
274                         if ((a->ULineOnly) && (!u->server->IsULine()))
275                         {
276                                 ServerInstance->SNO->WriteToSnoMask('a', "NOTICE -- Service "+a->RequiredNick+" required by alias "+a->AliasedCommand+" is not on a u-lined server, possibly underhanded antics detected!");
277                                 user->WriteNumeric(ERR_NOSUCHNICK, a->RequiredNick, "is an imposter! Please inform an IRC operator as soon as possible.");
278                                 return 1;
279                         }
280                 }
281
282                 /* Now, search and replace in a copy of the original_line, replacing $1 through $9 and $1- etc */
283
284                 std::string::size_type crlf = a->ReplaceFormat.find('\n');
285
286                 if (crlf == std::string::npos)
287                 {
288                         DoCommand(a->ReplaceFormat, user, c, safe, a);
289                         return 1;
290                 }
291                 else
292                 {
293                         irc::sepstream commands(a->ReplaceFormat, '\n');
294                         std::string scommand;
295                         while (commands.GetToken(scommand))
296                         {
297                                 DoCommand(scommand, user, c, safe, a);
298                         }
299                         return 1;
300                 }
301         }
302
303         void DoCommand(const std::string& newline, User* user, Channel *chan, const std::string &original_line, Alias* a)
304         {
305                 std::string result;
306                 result.reserve(newline.length());
307                 for (unsigned int i = 0; i < newline.length(); i++)
308                 {
309                         char c = newline[i];
310                         if ((c == '$') && (i + 1 < newline.length()))
311                         {
312                                 if (isdigit(newline[i+1]))
313                                 {
314                                         size_t len = ((i + 2 < newline.length()) && (newline[i+2] == '-')) ? 3 : 2;
315                                         std::string var = newline.substr(i, len);
316                                         result.append(GetVar(var, original_line));
317                                         i += len - 1;
318                                 }
319                                 else if (!newline.compare(i, 5, "$nick", 5))
320                                 {
321                                         result.append(user->nick);
322                                         i += 4;
323                                 }
324                                 else if (!newline.compare(i, 5, "$host", 5))
325                                 {
326                                         result.append(user->GetRealHost());
327                                         i += 4;
328                                 }
329                                 else if (!newline.compare(i, 5, "$chan", 5))
330                                 {
331                                         if (chan)
332                                                 result.append(chan->name);
333                                         i += 4;
334                                 }
335                                 else if (!newline.compare(i, 6, "$ident", 6))
336                                 {
337                                         result.append(user->ident);
338                                         i += 5;
339                                 }
340                                 else if (!newline.compare(i, 6, "$vhost", 6))
341                                 {
342                                         result.append(user->GetDisplayedHost());
343                                         i += 5;
344                                 }
345                                 else if (!newline.compare(i, 12, "$requirement", 12))
346                                 {
347                                         result.append(a->RequiredNick);
348                                         i += 11;
349                                 }
350                                 else
351                                         result.push_back(c);
352                         }
353                         else
354                                 result.push_back(c);
355                 }
356
357                 irc::tokenstream ss(result);
358                 CommandBase::Params pars;
359                 std::string command, token;
360
361                 ss.GetMiddle(command);
362                 while (ss.GetTrailing(token))
363                 {
364                         pars.push_back(token);
365                 }
366
367                 active = true;
368                 ServerInstance->Parser.CallHandler(command, pars, user);
369                 active = false;
370         }
371
372         void Prioritize() CXX11_OVERRIDE
373         {
374                 // Prioritise after spanningtree so that channel aliases show the alias before the effects.
375                 Module* linkmod = ServerInstance->Modules->Find("m_spanningtree.so");
376                 ServerInstance->Modules->SetPriority(this, I_OnUserPostMessage, PRIORITY_AFTER, linkmod);
377         }
378 };
379
380 MODULE_INIT(ModuleAlias)