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