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