]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_alias.cpp
Merge insp20
[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         char 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 std::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[0];
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                 AliasMap::iterator i, upperbound;
140
141                 /* If theyre not registered yet, we dont want
142                  * to know.
143                  */
144                 if (user->registered != REG_ALL)
145                         return MOD_RES_PASSTHRU;
146
147                 /* We dont have any commands looking like this? Stop processing. */
148                 i = Aliases.find(command);
149                 if (i == Aliases.end())
150                         return MOD_RES_PASSTHRU;
151                 /* Avoid iterating on to different aliases if no patterns match. */
152                 upperbound = Aliases.upper_bound(command);
153
154                 /* The parameters for the command in their original form, with the command stripped off */
155                 std::string compare = original_line.substr(command.length());
156                 while (*(compare.c_str()) == ' ')
157                         compare.erase(compare.begin());
158
159                 while (i != upperbound)
160                 {
161                         if (i->second.UserCommand)
162                         {
163                                 if (DoAlias(user, NULL, &(i->second), compare, original_line))
164                                 {
165                                         return MOD_RES_DENY;
166                                 }
167                         }
168
169                         i++;
170                 }
171
172                 // If we made it here, no aliases actually matched.
173                 return MOD_RES_PASSTHRU;
174         }
175
176         void OnUserMessage(User *user, void *dest, int target_type, const std::string &text, char status, const CUList &exempt_list, MessageType msgtype) CXX11_OVERRIDE
177         {
178                 if ((target_type != TYPE_CHANNEL) || (msgtype != MSG_PRIVMSG))
179                 {
180                         return;
181                 }
182
183                 // fcommands are only for local users. Spanningtree will send them back out as their original cmd.
184                 if (!IS_LOCAL(user))
185                 {
186                         return;
187                 }
188
189                 /* Stop here if the user is +B and allowbot is set to no. */
190                 if (!AllowBots && user->IsModeSet(botmode))
191                 {
192                         return;
193                 }
194
195                 Channel *c = (Channel *)dest;
196                 std::string scommand;
197
198                 // text is like "!moo cows bite me", we want "!moo" first
199                 irc::spacesepstream ss(text);
200                 ss.GetToken(scommand);
201
202                 if (scommand.empty())
203                 {
204                         return; // wtfbbq
205                 }
206
207                 // we don't want to touch non-fantasy stuff
208                 if (*scommand.c_str() != fprefix)
209                 {
210                         return;
211                 }
212
213                 // nor do we give a shit about the prefix
214                 scommand.erase(scommand.begin());
215
216                 AliasMap::iterator i = Aliases.find(scommand);
217
218                 if (i == Aliases.end())
219                         return;
220
221                 /* Avoid iterating on to other aliases if no patterns match */
222                 AliasMap::iterator upperbound = Aliases.upper_bound(scommand);
223
224
225                 /* The parameters for the command in their original form, with the command stripped off */
226                 std::string compare = text.substr(scommand.length() + 1);
227                 while (*(compare.c_str()) == ' ')
228                         compare.erase(compare.begin());
229
230                 while (i != upperbound)
231                 {
232                         if (i->second.ChannelCommand)
233                         {
234                                 // We use substr(1) here to remove the fantasy prefix
235                                 if (DoAlias(user, c, &(i->second), compare, text.substr(1)))
236                                         return;
237                         }
238
239                         i++;
240                 }
241         }
242
243
244         int DoAlias(User *user, Channel *c, Alias *a, const std::string& compare, const std::string& safe)
245         {
246                 User *u = NULL;
247
248                 /* Does it match the pattern? */
249                 if (!a->format.empty())
250                 {
251                         if (a->CaseSensitive)
252                         {
253                                 if (!InspIRCd::Match(compare, a->format, rfc_case_sensitive_map))
254                                         return 0;
255                         }
256                         else
257                         {
258                                 if (!InspIRCd::Match(compare, a->format))
259                                         return 0;
260                         }
261                 }
262
263                 if ((a->OperOnly) && (!user->IsOper()))
264                         return 0;
265
266                 if (!a->RequiredNick.empty())
267                 {
268                         u = ServerInstance->FindNick(a->RequiredNick);
269                         if (!u)
270                         {
271                                 user->WriteNumeric(ERR_NOSUCHNICK, a->RequiredNick + " :is currently unavailable. Please try again later.");
272                                 return 1;
273                         }
274                 }
275                 if ((u != NULL) && (!a->RequiredNick.empty()) && (a->ULineOnly))
276                 {
277                         if (!u->server->IsULine())
278                         {
279                                 ServerInstance->SNO->WriteToSnoMask('a', "NOTICE -- Service "+a->RequiredNick+" required by alias "+a->AliasedCommand+" is not on a u-lined server, possibly underhanded antics detected!");
280                                 user->WriteNumeric(ERR_NOSUCHNICK, a->RequiredNick + " :is an imposter! Please inform an IRC operator as soon as possible.");
281                                 return 1;
282                         }
283                 }
284
285                 /* Now, search and replace in a copy of the original_line, replacing $1 through $9 and $1- etc */
286
287                 std::string::size_type crlf = a->ReplaceFormat.find('\n');
288
289                 if (crlf == std::string::npos)
290                 {
291                         DoCommand(a->ReplaceFormat, user, c, safe);
292                         return 1;
293                 }
294                 else
295                 {
296                         irc::sepstream commands(a->ReplaceFormat, '\n');
297                         std::string scommand;
298                         while (commands.GetToken(scommand))
299                         {
300                                 DoCommand(scommand, user, c, safe);
301                         }
302                         return 1;
303                 }
304         }
305
306         void DoCommand(const std::string& newline, User* user, Channel *chan, const std::string &original_line)
307         {
308                 std::string result;
309                 result.reserve(newline.length());
310                 for (unsigned int i = 0; i < newline.length(); i++)
311                 {
312                         char c = newline[i];
313                         if ((c == '$') && (i + 1 < newline.length()))
314                         {
315                                 if (isdigit(newline[i+1]))
316                                 {
317                                         int len = ((i + 2 < newline.length()) && (newline[i+2] == '-')) ? 3 : 2;
318                                         std::string var = newline.substr(i, len);
319                                         result.append(GetVar(var, original_line));
320                                         i += len - 1;
321                                 }
322                                 else if (newline.substr(i, 5) == "$nick")
323                                 {
324                                         result.append(user->nick);
325                                         i += 4;
326                                 }
327                                 else if (newline.substr(i, 5) == "$host")
328                                 {
329                                         result.append(user->host);
330                                         i += 4;
331                                 }
332                                 else if (newline.substr(i, 5) == "$chan")
333                                 {
334                                         if (chan)
335                                                 result.append(chan->name);
336                                         i += 4;
337                                 }
338                                 else if (newline.substr(i, 6) == "$ident")
339                                 {
340                                         result.append(user->ident);
341                                         i += 5;
342                                 }
343                                 else if (newline.substr(i, 6) == "$vhost")
344                                 {
345                                         result.append(user->dhost);
346                                         i += 5;
347                                 }
348                                 else
349                                         result.push_back(c);
350                         }
351                         else
352                                 result.push_back(c);
353                 }
354
355                 irc::tokenstream ss(result);
356                 std::vector<std::string> pars;
357                 std::string command, token;
358
359                 ss.GetToken(command);
360                 while (ss.GetToken(token))
361                 {
362                         pars.push_back(token);
363                 }
364                 ServerInstance->Parser->CallHandler(command, pars, user);
365         }
366
367         void Prioritize()
368         {
369                 // Prioritise after spanningtree so that channel aliases show the alias before the effects.
370                 Module* linkmod = ServerInstance->Modules->Find("m_spanningtree.so");
371                 ServerInstance->Modules->SetPriority(this, I_OnUserMessage, PRIORITY_AFTER, &linkmod);
372         }
373 };
374
375 MODULE_INIT(ModuleAlias)