]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_alias.cpp
b0fda70bbdbe16b23f1b0964b0ad64c41ecf2b26
[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         irc::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         std::multimap<irc::string, Alias> Aliases;
67
68         /* whether or not +B users are allowed to use fantasy commands */
69         bool AllowBots;
70         UserModeReference botmode;
71
72  public:
73         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
74         {
75                 ConfigTag* fantasy = ServerInstance->Config->ConfValue("fantasy");
76                 AllowBots = fantasy->getBool("allowbots", false);
77                 std::string fpre = fantasy->getString("prefix", "!");
78                 fprefix = fpre.empty() ? '!' : fpre[0];
79
80                 Aliases.clear();
81                 ConfigTagList tags = ServerInstance->Config->ConfTags("alias");
82                 for(ConfigIter i = tags.first; i != tags.second; ++i)
83                 {
84                         ConfigTag* tag = i->second;
85                         Alias a;
86                         std::string aliastext = tag->getString("text");
87                         a.AliasedCommand = aliastext.c_str();
88                         tag->readString("replace", a.ReplaceFormat, true);
89                         a.RequiredNick = tag->getString("requires");
90                         a.ULineOnly = tag->getBool("uline");
91                         a.ChannelCommand = tag->getBool("channelcommand", false);
92                         a.UserCommand = tag->getBool("usercommand", true);
93                         a.OperOnly = tag->getBool("operonly");
94                         a.format = tag->getString("format");
95                         a.CaseSensitive = tag->getBool("matchcase");
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         ModResult OnPreCommand(std::string &command, std::vector<std::string> &parameters, LocalUser *user, bool validated, const std::string &original_line) CXX11_OVERRIDE
136         {
137                 std::multimap<irc::string, Alias>::iterator i, upperbound;
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                 i = Aliases.find(command.c_str());
147                 if (i == Aliases.end())
148                         return MOD_RES_PASSTHRU;
149                 /* Avoid iterating on to different aliases if no patterns match. */
150                 upperbound = Aliases.upper_bound(command.c_str());
151
152                 irc::string c = command.c_str();
153                 /* The parameters for the command in their original form, with the command stripped off */
154                 std::string compare = original_line.substr(command.length());
155                 while (*(compare.c_str()) == ' ')
156                         compare.erase(compare.begin());
157
158                 while (i != upperbound)
159                 {
160                         if (i->second.UserCommand)
161                         {
162                                 if (DoAlias(user, NULL, &(i->second), compare, original_line))
163                                 {
164                                         return MOD_RES_DENY;
165                                 }
166                         }
167
168                         i++;
169                 }
170
171                 // If we made it here, no aliases actually matched.
172                 return MOD_RES_PASSTHRU;
173         }
174
175         void OnUserMessage(User *user, void *dest, int target_type, const std::string &text, char status, const CUList &exempt_list, MessageType msgtype) CXX11_OVERRIDE
176         {
177                 if ((target_type != TYPE_CHANNEL) || (msgtype != MSG_PRIVMSG))
178                 {
179                         return;
180                 }
181
182                 // fcommands are only for local users. Spanningtree will send them back out as their original cmd.
183                 if (!user || !IS_LOCAL(user))
184                 {
185                         return;
186                 }
187
188                 /* Stop here if the user is +B and allowbot is set to no. */
189                 if (!AllowBots && user->IsModeSet(botmode))
190                 {
191                         return;
192                 }
193
194                 Channel *c = (Channel *)dest;
195                 std::string scommand;
196
197                 // text is like "!moo cows bite me", we want "!moo" first
198                 irc::spacesepstream ss(text);
199                 ss.GetToken(scommand);
200                 irc::string fcommand = scommand.c_str();
201
202                 if (fcommand.empty())
203                 {
204                         return; // wtfbbq
205                 }
206
207                 // we don't want to touch non-fantasy stuff
208                 if (*fcommand.c_str() != fprefix)
209                 {
210                         return;
211                 }
212
213                 // nor do we give a shit about the prefix
214                 fcommand.erase(fcommand.begin());
215
216                 std::multimap<irc::string, Alias>::iterator i = Aliases.find(fcommand);
217
218                 if (i == Aliases.end())
219                         return;
220
221                 /* Avoid iterating on to other aliases if no patterns match */
222                 std::multimap<irc::string, Alias>::iterator upperbound = Aliases.upper_bound(fcommand);
223
224
225                 /* The parameters for the command in their original form, with the command stripped off */
226                 std::string compare = text.substr(fcommand.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 (!ServerInstance->ULine(u->server))
278                         {
279                                 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!");
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 == '$')
314                         {
315                                 if (isdigit(newline[i+1]))
316                                 {
317                                         int len = (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)