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