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