]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_alias.cpp
Ignore clients on ulined servers when reporting stats in LUSERS.
[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         /* whether or not it may be executed via fantasy (default OFF) */
46         bool ChannelCommand;
47
48         /* whether or not it may be executed via /command (default ON) */
49         bool UserCommand;
50
51         /** Format that must be matched for use */
52         std::string format;
53
54         /** Strip color codes before match? */
55         bool StripColor;
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         // Whether we are actively executing an alias.
75         bool active;
76
77  public:
78         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
79         {
80                 AliasMap newAliases;
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                         a.AliasedCommand = tag->getString("text");
87                         if (a.AliasedCommand.empty())
88                                 throw ModuleException("<alias:text> is empty! at " + tag->getTagLocation());
89
90                         tag->readString("replace", a.ReplaceFormat, true);
91                         if (a.ReplaceFormat.empty())
92                                 throw ModuleException("<alias:replace> is empty! at " + tag->getTagLocation());
93
94                         a.RequiredNick = tag->getString("requires");
95                         a.ULineOnly = tag->getBool("uline");
96                         a.ChannelCommand = tag->getBool("channelcommand", false);
97                         a.UserCommand = tag->getBool("usercommand", true);
98                         a.OperOnly = tag->getBool("operonly");
99                         a.format = tag->getString("format");
100                         a.StripColor = tag->getBool("stripcolor");
101
102                         std::transform(a.AliasedCommand.begin(), a.AliasedCommand.end(), a.AliasedCommand.begin(), ::toupper);
103                         newAliases.insert(std::make_pair(a.AliasedCommand, a));
104                 }
105
106                 ConfigTag* fantasy = ServerInstance->Config->ConfValue("fantasy");
107                 AllowBots = fantasy->getBool("allowbots", false);
108                 fprefix = fantasy->getString("prefix", "!", 1, ServerInstance->Config->Limits.MaxLine);
109                 Aliases.swap(newAliases);
110         }
111
112         ModuleAlias()
113                 : botmode(this, "bot")
114                 , active(false)
115         {
116         }
117
118         Version GetVersion() CXX11_OVERRIDE
119         {
120                 return Version("Provides aliases of commands", VF_VENDOR);
121         }
122
123         std::string GetVar(std::string varname, const std::string &original_line)
124         {
125                 irc::spacesepstream ss(original_line);
126                 varname.erase(varname.begin());
127                 int index = *(varname.begin()) - 48;
128                 varname.erase(varname.begin());
129                 bool everything_after = (varname == "-");
130                 std::string word;
131
132                 for (int j = 0; j < index; j++)
133                         ss.GetToken(word);
134
135                 if (everything_after)
136                 {
137                         std::string more;
138                         while (ss.GetToken(more))
139                         {
140                                 word.append(" ");
141                                 word.append(more);
142                         }
143                 }
144
145                 return word;
146         }
147
148         std::string CreateRFCMessage(const std::string& command, CommandBase::Params& parameters)
149         {
150                 std::string message(command);
151                 for (CommandBase::Params::const_iterator iter = parameters.begin(); iter != parameters.end();)
152                 {
153                         const std::string& parameter = *iter++;
154                         message.push_back(' ');
155                         if (iter == parameters.end() && (parameter.empty() || parameter.find(' ') != std::string::npos))
156                                 message.push_back(':');
157                         message.append(parameter);
158                 }
159                 return message;
160         }
161
162         ModResult OnPreCommand(std::string& command, CommandBase::Params& parameters, LocalUser* user, bool validated) CXX11_OVERRIDE
163         {
164                 /* If theyre not registered yet, we dont want
165                  * to know.
166                  */
167                 if (user->registered != REG_ALL)
168                         return MOD_RES_PASSTHRU;
169
170                 /* We dont have any commands looking like this? Stop processing. */
171                 std::pair<AliasMap::iterator, AliasMap::iterator> iters = Aliases.equal_range(command);
172                 if (iters.first == iters.second)
173                         return MOD_RES_PASSTHRU;
174
175                 /* The parameters for the command in their original form, with the command stripped off */
176                 std::string original_line = CreateRFCMessage(command, parameters);
177                 std::string compare(original_line, command.length());
178                 while (*(compare.c_str()) == ' ')
179                         compare.erase(compare.begin());
180
181                 for (AliasMap::iterator i = iters.first; i != iters.second; ++i)
182                 {
183                         if (i->second.UserCommand)
184                         {
185                                 if (DoAlias(user, NULL, &(i->second), compare, original_line))
186                                 {
187                                         return MOD_RES_DENY;
188                                 }
189                         }
190                 }
191
192                 // If we made it here, no aliases actually matched.
193                 return MOD_RES_PASSTHRU;
194         }
195
196         ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
197         {
198                 // Don't echo anything which is caused by an alias.
199                 if (active)
200                         details.echo = false;
201
202                 return MOD_RES_PASSTHRU;
203         }
204
205         void OnUserPostMessage(User* user, const MessageTarget& target, const MessageDetails& details) CXX11_OVERRIDE
206         {
207                 if ((target.type != MessageTarget::TYPE_CHANNEL) || (details.type != MSG_PRIVMSG))
208                 {
209                         return;
210                 }
211
212                 // fcommands are only for local users. Spanningtree will send them back out as their original cmd.
213                 if (!IS_LOCAL(user))
214                 {
215                         return;
216                 }
217
218                 /* Stop here if the user is +B and allowbot is set to no. */
219                 if (!AllowBots && user->IsModeSet(botmode))
220                 {
221                         return;
222                 }
223
224                 Channel *c = target.Get<Channel>();
225                 std::string scommand;
226
227                 // text is like "!moo cows bite me", we want "!moo" first
228                 irc::spacesepstream ss(details.text);
229                 ss.GetToken(scommand);
230
231                 if (scommand.size() <= fprefix.size())
232                 {
233                         return; // wtfbbq
234                 }
235
236                 // we don't want to touch non-fantasy stuff
237                 if (scommand.compare(0, fprefix.size(), fprefix) != 0)
238                 {
239                         return;
240                 }
241
242                 // nor do we give a shit about the prefix
243                 scommand.erase(0, fprefix.size());
244
245                 std::pair<AliasMap::iterator, AliasMap::iterator> iters = Aliases.equal_range(scommand);
246                 if (iters.first == iters.second)
247                         return;
248
249                 /* The parameters for the command in their original form, with the command stripped off */
250                 std::string compare(details.text, scommand.length() + fprefix.size());
251                 while (*(compare.c_str()) == ' ')
252                         compare.erase(compare.begin());
253
254                 for (AliasMap::iterator i = iters.first; i != iters.second; ++i)
255                 {
256                         if (i->second.ChannelCommand)
257                         {
258                                 // We use substr here to remove the fantasy prefix
259                                 if (DoAlias(user, c, &(i->second), compare, details.text.substr(fprefix.size())))
260                                         return;
261                         }
262                 }
263         }
264
265
266         int DoAlias(User *user, Channel *c, Alias *a, const std::string& compare, const std::string& safe)
267         {
268                 std::string stripped(compare);
269                 if (a->StripColor)
270                         InspIRCd::StripColor(stripped);
271
272                 /* Does it match the pattern? */
273                 if (!a->format.empty())
274                 {
275                         if (!InspIRCd::Match(stripped, a->format))
276                                 return 0;
277                 }
278
279                 if ((a->OperOnly) && (!user->IsOper()))
280                         return 0;
281
282                 if (!a->RequiredNick.empty())
283                 {
284                         User* u = ServerInstance->FindNickOnly(a->RequiredNick);
285                         if (!u)
286                         {
287                                 user->WriteNumeric(ERR_NOSUCHNICK, a->RequiredNick, "is currently unavailable. Please try again later.");
288                                 return 1;
289                         }
290
291                         if ((a->ULineOnly) && (!u->server->IsULine()))
292                         {
293                                 ServerInstance->SNO->WriteToSnoMask('a', "NOTICE -- Service "+a->RequiredNick+" required by alias "+a->AliasedCommand+" is not on a U-lined server, possibly underhanded antics detected!");
294                                 user->WriteNumeric(ERR_NOSUCHNICK, a->RequiredNick, "is not a network service! Please inform a server operator as soon as possible.");
295                                 return 1;
296                         }
297                 }
298
299                 /* Now, search and replace in a copy of the original_line, replacing $1 through $9 and $1- etc */
300
301                 std::string::size_type crlf = a->ReplaceFormat.find('\n');
302
303                 if (crlf == std::string::npos)
304                 {
305                         DoCommand(a->ReplaceFormat, user, c, safe, a);
306                         return 1;
307                 }
308                 else
309                 {
310                         irc::sepstream commands(a->ReplaceFormat, '\n');
311                         std::string scommand;
312                         while (commands.GetToken(scommand))
313                         {
314                                 DoCommand(scommand, user, c, safe, a);
315                         }
316                         return 1;
317                 }
318         }
319
320         void DoCommand(const std::string& newline, User* user, Channel *chan, const std::string &original_line, Alias* a)
321         {
322                 std::string result;
323                 result.reserve(newline.length());
324                 for (unsigned int i = 0; i < newline.length(); i++)
325                 {
326                         char c = newline[i];
327                         if ((c == '$') && (i + 1 < newline.length()))
328                         {
329                                 if (isdigit(newline[i+1]))
330                                 {
331                                         size_t len = ((i + 2 < newline.length()) && (newline[i+2] == '-')) ? 3 : 2;
332                                         std::string var = newline.substr(i, len);
333                                         result.append(GetVar(var, original_line));
334                                         i += len - 1;
335                                 }
336                                 else if (!newline.compare(i, 5, "$nick", 5))
337                                 {
338                                         result.append(user->nick);
339                                         i += 4;
340                                 }
341                                 else if (!newline.compare(i, 5, "$host", 5))
342                                 {
343                                         result.append(user->GetRealHost());
344                                         i += 4;
345                                 }
346                                 else if (!newline.compare(i, 5, "$chan", 5))
347                                 {
348                                         if (chan)
349                                                 result.append(chan->name);
350                                         i += 4;
351                                 }
352                                 else if (!newline.compare(i, 6, "$ident", 6))
353                                 {
354                                         result.append(user->ident);
355                                         i += 5;
356                                 }
357                                 else if (!newline.compare(i, 6, "$vhost", 6))
358                                 {
359                                         result.append(user->GetDisplayedHost());
360                                         i += 5;
361                                 }
362                                 else if (!newline.compare(i, 12, "$requirement", 12))
363                                 {
364                                         result.append(a->RequiredNick);
365                                         i += 11;
366                                 }
367                                 else
368                                         result.push_back(c);
369                         }
370                         else
371                                 result.push_back(c);
372                 }
373
374                 irc::tokenstream ss(result);
375                 CommandBase::Params pars;
376                 std::string command, token;
377
378                 ss.GetMiddle(command);
379                 while (ss.GetTrailing(token))
380                 {
381                         pars.push_back(token);
382                 }
383
384                 active = true;
385                 ServerInstance->Parser.CallHandler(command, pars, user);
386                 active = false;
387         }
388
389         void Prioritize() CXX11_OVERRIDE
390         {
391                 // Prioritise after spanningtree so that channel aliases show the alias before the effects.
392                 Module* linkmod = ServerInstance->Modules->Find("m_spanningtree.so");
393                 ServerInstance->Modules->SetPriority(this, I_OnUserPostMessage, PRIORITY_AFTER, linkmod);
394         }
395 };
396
397 MODULE_INIT(ModuleAlias)