]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_alias.cpp
6d8e3a1c64cbab135466443f2d62bc18ddf2937b
[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  public:
72         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
73         {
74                 ConfigTag* fantasy = ServerInstance->Config->ConfValue("fantasy");
75                 AllowBots = fantasy->getBool("allowbots", false);
76                 std::string fpre = fantasy->getString("prefix");
77                 fprefix = fpre.empty() ? "!" : fpre;
78
79                 Aliases.clear();
80                 ConfigTagList tags = ServerInstance->Config->ConfTags("alias");
81                 for(ConfigIter i = tags.first; i != tags.second; ++i)
82                 {
83                         ConfigTag* tag = i->second;
84                         Alias a;
85                         a.AliasedCommand = tag->getString("text");
86                         std::transform(a.AliasedCommand.begin(), a.AliasedCommand.end(), a.AliasedCommand.begin(), ::toupper);
87                         tag->readString("replace", a.ReplaceFormat, true);
88                         a.RequiredNick = tag->getString("requires");
89                         a.ULineOnly = tag->getBool("uline");
90                         a.ChannelCommand = tag->getBool("channelcommand", false);
91                         a.UserCommand = tag->getBool("usercommand", true);
92                         a.OperOnly = tag->getBool("operonly");
93                         a.format = tag->getString("format");
94                         Aliases.insert(std::make_pair(a.AliasedCommand, a));
95                 }
96         }
97
98         ModuleAlias()
99                 : botmode(this, "bot")
100         {
101         }
102
103         Version GetVersion() CXX11_OVERRIDE
104         {
105                 return Version("Provides aliases of commands.", VF_VENDOR);
106         }
107
108         std::string GetVar(std::string varname, const std::string &original_line)
109         {
110                 irc::spacesepstream ss(original_line);
111                 varname.erase(varname.begin());
112                 int index = *(varname.begin()) - 48;
113                 varname.erase(varname.begin());
114                 bool everything_after = (varname == "-");
115                 std::string word;
116
117                 for (int j = 0; j < index; j++)
118                         ss.GetToken(word);
119
120                 if (everything_after)
121                 {
122                         std::string more;
123                         while (ss.GetToken(more))
124                         {
125                                 word.append(" ");
126                                 word.append(more);
127                         }
128                 }
129
130                 return word;
131         }
132
133         ModResult OnPreCommand(std::string &command, std::vector<std::string> &parameters, LocalUser *user, bool validated, const std::string &original_line) CXX11_OVERRIDE
134         {
135                 /* If theyre not registered yet, we dont want
136                  * to know.
137                  */
138                 if (user->registered != REG_ALL)
139                         return MOD_RES_PASSTHRU;
140
141                 /* We dont have any commands looking like this? Stop processing. */
142                 std::pair<AliasMap::iterator, AliasMap::iterator> iters = Aliases.equal_range(command);
143                 if (iters.first == iters.second)
144                         return MOD_RES_PASSTHRU;
145
146                 /* The parameters for the command in their original form, with the command stripped off */
147                 std::string compare(original_line, command.length());
148                 while (*(compare.c_str()) == ' ')
149                         compare.erase(compare.begin());
150
151                 for (AliasMap::iterator i = iters.first; i != iters.second; ++i)
152                 {
153                         if (i->second.UserCommand)
154                         {
155                                 if (DoAlias(user, NULL, &(i->second), compare, original_line))
156                                 {
157                                         return MOD_RES_DENY;
158                                 }
159                         }
160                 }
161
162                 // If we made it here, no aliases actually matched.
163                 return MOD_RES_PASSTHRU;
164         }
165
166         void OnUserMessage(User *user, void *dest, int target_type, const std::string &text, char status, const CUList &exempt_list, MessageType msgtype) CXX11_OVERRIDE
167         {
168                 if ((target_type != TYPE_CHANNEL) || (msgtype != MSG_PRIVMSG))
169                 {
170                         return;
171                 }
172
173                 // fcommands are only for local users. Spanningtree will send them back out as their original cmd.
174                 if (!IS_LOCAL(user))
175                 {
176                         return;
177                 }
178
179                 /* Stop here if the user is +B and allowbot is set to no. */
180                 if (!AllowBots && user->IsModeSet(botmode))
181                 {
182                         return;
183                 }
184
185                 Channel *c = (Channel *)dest;
186                 std::string scommand;
187
188                 // text is like "!moo cows bite me", we want "!moo" first
189                 irc::spacesepstream ss(text);
190                 ss.GetToken(scommand);
191
192                 if (scommand.size() <= fprefix.size())
193                 {
194                         return; // wtfbbq
195                 }
196
197                 // we don't want to touch non-fantasy stuff
198                 if (scommand.compare(0, fprefix.size(), fprefix) != 0)
199                 {
200                         return;
201                 }
202
203                 // nor do we give a shit about the prefix
204                 scommand.erase(0, fprefix.size());
205
206                 std::pair<AliasMap::iterator, AliasMap::iterator> iters = Aliases.equal_range(scommand);
207                 if (iters.first == iters.second)
208                         return;
209
210                 /* The parameters for the command in their original form, with the command stripped off */
211                 std::string compare(text, scommand.length() + fprefix.size());
212                 while (*(compare.c_str()) == ' ')
213                         compare.erase(compare.begin());
214
215                 for (AliasMap::iterator i = iters.first; i != iters.second; ++i)
216                 {
217                         if (i->second.ChannelCommand)
218                         {
219                                 // We use substr here to remove the fantasy prefix
220                                 if (DoAlias(user, c, &(i->second), compare, text.substr(fprefix.size())))
221                                         return;
222                         }
223                 }
224         }
225
226
227         int DoAlias(User *user, Channel *c, Alias *a, const std::string& compare, const std::string& safe)
228         {
229                 /* Does it match the pattern? */
230                 if (!a->format.empty())
231                 {
232                         if (!InspIRCd::Match(compare, a->format))
233                                 return 0;
234                 }
235
236                 if ((a->OperOnly) && (!user->IsOper()))
237                         return 0;
238
239                 if (!a->RequiredNick.empty())
240                 {
241                         User* u = ServerInstance->FindNick(a->RequiredNick);
242                         if (!u)
243                         {
244                                 user->WriteNumeric(ERR_NOSUCHNICK, a->RequiredNick, "is currently unavailable. Please try again later.");
245                                 return 1;
246                         }
247
248                         if ((a->ULineOnly) && (!u->server->IsULine()))
249                         {
250                                 ServerInstance->SNO->WriteToSnoMask('a', "NOTICE -- Service "+a->RequiredNick+" required by alias "+a->AliasedCommand+" is not on a u-lined server, possibly underhanded antics detected!");
251                                 user->WriteNumeric(ERR_NOSUCHNICK, a->RequiredNick, "is an imposter! Please inform an IRC operator as soon as possible.");
252                                 return 1;
253                         }
254                 }
255
256                 /* Now, search and replace in a copy of the original_line, replacing $1 through $9 and $1- etc */
257
258                 std::string::size_type crlf = a->ReplaceFormat.find('\n');
259
260                 if (crlf == std::string::npos)
261                 {
262                         DoCommand(a->ReplaceFormat, user, c, safe, a);
263                         return 1;
264                 }
265                 else
266                 {
267                         irc::sepstream commands(a->ReplaceFormat, '\n');
268                         std::string scommand;
269                         while (commands.GetToken(scommand))
270                         {
271                                 DoCommand(scommand, user, c, safe, a);
272                         }
273                         return 1;
274                 }
275         }
276
277         void DoCommand(const std::string& newline, User* user, Channel *chan, const std::string &original_line, Alias* a)
278         {
279                 std::string result;
280                 result.reserve(newline.length());
281                 for (unsigned int i = 0; i < newline.length(); i++)
282                 {
283                         char c = newline[i];
284                         if ((c == '$') && (i + 1 < newline.length()))
285                         {
286                                 if (isdigit(newline[i+1]))
287                                 {
288                                         int len = ((i + 2 < newline.length()) && (newline[i+2] == '-')) ? 3 : 2;
289                                         std::string var = newline.substr(i, len);
290                                         result.append(GetVar(var, original_line));
291                                         i += len - 1;
292                                 }
293                                 else if (!newline.compare(i, 5, "$nick", 5))
294                                 {
295                                         result.append(user->nick);
296                                         i += 4;
297                                 }
298                                 else if (!newline.compare(i, 5, "$host", 5))
299                                 {
300                                         result.append(user->host);
301                                         i += 4;
302                                 }
303                                 else if (!newline.compare(i, 5, "$chan", 5))
304                                 {
305                                         if (chan)
306                                                 result.append(chan->name);
307                                         i += 4;
308                                 }
309                                 else if (!newline.compare(i, 6, "$ident", 6))
310                                 {
311                                         result.append(user->ident);
312                                         i += 5;
313                                 }
314                                 else if (!newline.compare(i, 6, "$vhost", 6))
315                                 {
316                                         result.append(user->dhost);
317                                         i += 5;
318                                 }
319                                 else if (!newline.compare(i, 12, "$requirement", 12))
320                                 {
321                                         result.append(a->RequiredNick);
322                                         i += 11;
323                                 }
324                                 else
325                                         result.push_back(c);
326                         }
327                         else
328                                 result.push_back(c);
329                 }
330
331                 irc::tokenstream ss(result);
332                 std::vector<std::string> pars;
333                 std::string command, token;
334
335                 ss.GetToken(command);
336                 while (ss.GetToken(token))
337                 {
338                         pars.push_back(token);
339                 }
340                 ServerInstance->Parser.CallHandler(command, pars, user);
341         }
342
343         void Prioritize() CXX11_OVERRIDE
344         {
345                 // Prioritise after spanningtree so that channel aliases show the alias before the effects.
346                 Module* linkmod = ServerInstance->Modules->Find("m_spanningtree.so");
347                 ServerInstance->Modules->SetPriority(this, I_OnUserMessage, PRIORITY_AFTER, linkmod);
348         }
349 };
350
351 MODULE_INIT(ModuleAlias)