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