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