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