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