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