]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_alias.cpp
76aceffc97d81af7ee1d843def06627f35d0fae4
[user/henk/code/inspircd.git] / src / modules / m_alias.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15
16 /* $ModDesc: Provides aliases of commands. */
17
18 /** An alias definition
19  */
20 class Alias : public classbase
21 {
22  public:
23         /** The text of the alias command */
24         irc::string AliasedCommand;
25
26         /** Text to replace with */
27         std::string ReplaceFormat;
28
29         /** Nickname required to perform alias */
30         std::string RequiredNick;
31
32         /** Alias requires ulined server */
33         bool ULineOnly;
34
35         /** Requires oper? */
36         bool OperOnly;
37
38         /* is case sensitive params */
39         bool CaseSensitive;
40
41         /* whether or not it may be executed via fantasy (default OFF) */
42         bool ChannelCommand;
43
44         /* whether or not it may be executed via /command (default ON) */
45         bool UserCommand;
46
47         /** Format that must be matched for use */
48         std::string format;
49 };
50
51 class ModuleAlias : public Module
52 {
53  private:
54
55         char fprefix;
56
57         /* We cant use a map, there may be multiple aliases with the same name.
58          * We can, however, use a fancy invention: the multimap. Maps a key to one or more values.
59          *              -- w00t
60    */
61         std::multimap<std::string, Alias> Aliases;
62
63         virtual void ReadAliases()
64         {
65                 ConfigReader MyConf(ServerInstance);
66                 
67                 std::string fpre = MyConf.ReadValue("fantasy","prefix",0);
68                 fprefix = fpre.empty() ? '!' : fpre[0];
69
70                 Aliases.clear();
71                 for (int i = 0; i < MyConf.Enumerate("alias"); i++)
72                 {
73                         Alias a;
74                         std::string txt;
75                         txt = MyConf.ReadValue("alias", "text", i);
76                         a.AliasedCommand = txt.c_str();
77                         a.ReplaceFormat = MyConf.ReadValue("alias", "replace", i, true);
78                         a.RequiredNick = MyConf.ReadValue("alias", "requires", i);
79                         a.ULineOnly = MyConf.ReadFlag("alias", "uline", i);
80                         a.ChannelCommand = MyConf.ReadFlag("alias", "channelcommand", "no", i);
81                         a.UserCommand = MyConf.ReadFlag("alias", "usercommand", "yes", i);
82                         a.OperOnly = MyConf.ReadFlag("alias", "operonly", i);
83                         a.format = MyConf.ReadValue("alias", "format", i);
84                         a.CaseSensitive = MyConf.ReadFlag("alias", "matchcase", i);
85                         Aliases.insert(std::make_pair(txt, a));
86                 }
87         }
88
89  public:
90
91         ModuleAlias(InspIRCd* Me)
92                 : Module(Me)
93         {
94                 ReadAliases();
95                 Me->Modules->Attach(I_OnPreCommand, this);
96                 Me->Modules->Attach(I_OnRehash, this);
97                 Me->Modules->Attach(I_OnUserPreMessage, this);
98
99         }
100
101         virtual ~ModuleAlias()
102         {
103         }
104
105         virtual Version GetVersion()
106         {
107                 return Version("$Id$", VF_VENDOR,API_VERSION);
108         }
109
110         std::string GetVar(std::string varname, const std::string &original_line)
111         {
112                 irc::spacesepstream ss(original_line);
113                 varname.erase(varname.begin());
114                 int index = *(varname.begin()) - 48;
115                 varname.erase(varname.begin());
116                 bool everything_after = (varname == "-");
117                 std::string word;
118
119                 for (int j = 0; j < index; j++)
120                         ss.GetToken(word);
121
122                 if (everything_after)
123                 {
124                         std::string more;
125                         while (ss.GetToken(more))
126                         {
127                                 word.append(" ");
128                                 word.append(more);
129                         }
130                 }
131
132                 return word;
133         }
134
135         virtual int OnPreCommand(std::string &command, std::vector<std::string> &parameters, User *user, bool validated, const std::string &original_line)
136         {
137                 std::multimap<std::string, Alias>::iterator i, upperbound;
138
139                 /* If theyre not registered yet, we dont want
140                  * to know.
141                  */
142                 if (user->registered != REG_ALL)
143                         return 0;
144
145                 /* We dont have any commands looking like this? Stop processing. */
146                 i = Aliases.find(command);
147                 if (i == Aliases.end())
148                         return 0;
149                 /* Avoid iterating on to different aliases if no patterns match. */
150                 upperbound = Aliases.upper_bound(command);
151
152                 irc::string c = command.c_str();
153                 /* The parameters for the command in their original form, with the command stripped off */
154                 std::string compare = original_line.substr(command.length());
155                 while (*(compare.c_str()) == ' ')
156                         compare.erase(compare.begin());
157
158                 std::string safe(original_line);
159
160                 /* Escape out any $ symbols in the user provided text */
161
162                 SearchAndReplace(safe, "$", "\r");
163
164                 while (i != upperbound)
165                 {
166                         if (i->second.UserCommand)
167                         {
168                                 if (DoAlias(user, NULL, &(i->second), compare, safe))
169                                 {
170                                         return 1;
171                                 }
172                         }
173
174                         i++;
175                 }
176
177                 // If aliases have been processed, aliases took it.
178                 return 1;
179         }
180
181         virtual int OnUserPreMessage(User *user, void *dest, int target_type, std::string &text, char status, CUList &exempt_list)
182         {
183                 if (target_type != TYPE_CHANNEL)
184                 {
185                         ServerInstance->Logs->Log("FANTASY", DEBUG, "fantasy: not a channel msg");
186                         return 0;
187                 }
188
189                 // fcommands are only for local users. Spanningtree will send them back out as their original cmd.
190                 if (!IS_LOCAL(user))
191                 {
192                         ServerInstance->Logs->Log("FANTASY", DEBUG, "fantasy: not local");
193                         return 0;
194                 }
195
196                 Channel *c = (Channel *)dest;
197                 std::string fcommand;
198
199                 // text is like "!moo cows bite me", we want "!moo" first
200                 irc::spacesepstream ss(text);
201                 ss.GetToken(fcommand);
202
203                 if (fcommand.empty())
204                 {
205                         ServerInstance->Logs->Log("FANTASY", DEBUG, "fantasy: empty (?)");
206                         return 0; // wtfbbq
207                 }
208
209                 ServerInstance->Logs->Log("FANTASY", DEBUG, "fantasy: looking at fcommand %s", fcommand.c_str());
210
211                 // we don't want to touch non-fantasy stuff
212                 if (*fcommand.c_str() != fprefix)
213                 {
214                         ServerInstance->Logs->Log("FANTASY", DEBUG, "fantasy: not a fcommand");
215                         return 0;
216                 }
217
218                 // nor do we give a shit about the prefix
219                 fcommand.erase(fcommand.begin());
220                 std::transform(fcommand.begin(), fcommand.end(), fcommand.begin(), ::toupper);
221                 ServerInstance->Logs->Log("FANTASY", DEBUG, "fantasy: now got %s", fcommand.c_str());
222
223
224                 std::multimap<std::string, Alias>::iterator i = Aliases.find(fcommand);
225
226                 if (i == Aliases.end())
227                         return 0;
228                 
229                 /* Avoid iterating on to other aliases if no patterns match */
230                 std::multimap<std::string, Alias>::iterator upperbound = Aliases.upper_bound(fcommand);
231
232
233                 /* The parameters for the command in their original form, with the command stripped off */
234                 std::string compare = text.substr(fcommand.length() + 1);
235                 while (*(compare.c_str()) == ' ')
236                         compare.erase(compare.begin());
237
238                 std::string safe(compare);
239
240                 /* Escape out any $ symbols in the user provided text (ugly, but better than crashy) */
241                 SearchAndReplace(safe, "$", "\r");
242
243                 ServerInstance->Logs->Log("FANTASY", DEBUG, "fantasy: compare is %s and safe is %s", compare.c_str(), safe.c_str());
244
245                 while (i != upperbound)
246                 {
247                         if (i->second.ChannelCommand)
248                         {
249                                 if (DoAlias(user, c, &(i->second), compare, safe))
250                                         return 0;
251                         }
252
253                         i++;
254                 }
255                 
256                 return 0;
257         }
258
259
260         int DoAlias(User *user, Channel *c, Alias *a, const std::string compare, const std::string safe)
261         {
262                 User *u = NULL;
263
264                 /* Does it match the pattern? */
265                 if (!a->format.empty())
266                 {
267                         if (a->CaseSensitive)
268                         {
269                                 if (!InspIRCd::Match(compare, a->format, rfc_case_sensitive_map))
270                                         return 0;
271                         }
272                         else
273                         {
274                                 if (!InspIRCd::Match(compare, a->format))
275                                         return 0;
276                         }
277                 }
278
279                 if ((a->OperOnly) && (!IS_OPER(user)))
280                         return 0;
281
282                 if (!a->RequiredNick.empty())
283                 {
284                         u = ServerInstance->FindNick(a->RequiredNick);
285                         if (!u)
286                         {
287                                 user->WriteNumeric(401, ""+std::string(user->nick)+" "+a->RequiredNick+" :is currently unavailable. Please try again later.");
288                                 return 1;
289                         }
290                 }
291                 if ((u != NULL) && (!a->RequiredNick.empty()) && (a->ULineOnly))
292                 {
293                         if (!ServerInstance->ULine(u->server))
294                         {
295                                 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!");
296                                 user->WriteNumeric(401, ""+std::string(user->nick)+" "+a->RequiredNick+" :is an imposter! Please inform an IRC operator as soon as possible.");
297                                 return 1;
298                         }
299                 }
300
301                 /* Now, search and replace in a copy of the original_line, replacing $1 through $9 and $1- etc */
302
303                 std::string::size_type crlf = a->ReplaceFormat.find('\n');
304
305                 if (crlf == std::string::npos)
306                 {
307                         DoCommand(a->ReplaceFormat, user, c, safe);
308                         return 1;
309                 }
310                 else
311                 {
312                         irc::sepstream commands(a->ReplaceFormat, '\n');
313                         std::string scommand;
314                         while (commands.GetToken(scommand))
315                         {
316                                 DoCommand(scommand, user, c, safe);
317                         }
318                         return 1;
319                 }
320         }
321
322         void DoCommand(std::string newline, User* user, Channel *c, const std::string &original_line)
323         {
324                 std::vector<std::string> pars;
325
326                 for (int v = 1; v < 10; v++)
327                 {
328                         std::string var = "$";
329                         var.append(ConvToStr(v));
330                         var.append("-");
331                         std::string::size_type x = newline.find(var);
332
333                         while (x != std::string::npos)
334                         {
335                                 newline.erase(x, var.length());
336                                 newline.insert(x, GetVar(var, original_line));
337                                 x = newline.find(var);
338                         }
339
340                         var = "$";
341                         var.append(ConvToStr(v));
342                         x = newline.find(var);
343
344                         while (x != std::string::npos)
345                         {
346                                 newline.erase(x, var.length());
347                                 newline.insert(x, GetVar(var, original_line));
348                                 x = newline.find(var);
349                         }
350                 }
351
352                 /* Special variables */
353                 SearchAndReplace(newline, "$nick", user->nick);
354                 SearchAndReplace(newline, "$ident", user->ident);
355                 SearchAndReplace(newline, "$host", user->host);
356                 SearchAndReplace(newline, "$vhost", user->dhost);
357
358                 if (c)
359                 {
360                         /* Channel specific variables */
361                         SearchAndReplace(newline, "$chan", c->name);                    
362                 }
363
364                 /* Unescape any variable names in the user text before sending */
365                 SearchAndReplace(newline, "\r", "$");
366
367                 irc::tokenstream ss(newline);
368                 pars.clear();
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, const std::string &parameter)
380         {
381                 ReadAliases();
382         }
383 };
384
385 MODULE_INIT(ModuleAlias)