]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_alias.cpp
Rename some member fields; add <alias:usercommand>, default true - whether or not...
[user/henk/code/inspircd.git] / src / modules / m_alias.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 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         /* We cant use a map, there may be multiple aliases with the same name.
55          * We can, however, use a fancy invention: the multimap. Maps a key to one or more values.
56          *              -- w00t
57    */
58         std::multimap<std::string, Alias> Aliases;
59
60         virtual void ReadAliases()
61         {
62                 ConfigReader MyConf(ServerInstance);
63
64                 Aliases.clear();
65                 for (int i = 0; i < MyConf.Enumerate("alias"); i++)
66                 {
67                         Alias a;
68                         std::string txt;
69                         txt = MyConf.ReadValue("alias", "text", i);
70                         a.AliasedCommand = txt.c_str();
71                         a.ReplaceFormat = MyConf.ReadValue("alias", "replace", i, true);
72                         a.RequiredNick = MyConf.ReadValue("alias", "requires", i);
73                         a.ULineOnly = MyConf.ReadFlag("alias", "uline", i);
74                         a.ChannelCommand = MyConf.ReadFlag("alias", "channelcommand", i);
75                         a.UserCommand = MyConf.ReadFlag("alias", "usercommand", i);
76                         a.OperOnly = MyConf.ReadFlag("alias", "operonly", i);
77                         a.format = MyConf.ReadValue("alias", "format", i);
78                         a.CaseSensitive = MyConf.ReadFlag("alias", "matchcase", i);
79                         Aliases.insert(std::make_pair(txt, a));
80                 }
81         }
82
83  public:
84
85         ModuleAlias(InspIRCd* Me)
86                 : Module(Me)
87         {
88                 ReadAliases();
89                 Me->Modules->Attach(I_OnPreCommand, this);
90                 Me->Modules->Attach(I_OnRehash, this);
91
92         }
93
94         virtual ~ModuleAlias()
95         {
96         }
97
98         virtual Version GetVersion()
99         {
100                 return Version("$Id$", VF_VENDOR,API_VERSION);
101         }
102
103         std::string GetVar(std::string varname, const std::string &original_line)
104         {
105                 irc::spacesepstream ss(original_line);
106                 varname.erase(varname.begin());
107                 int index = *(varname.begin()) - 48;
108                 varname.erase(varname.begin());
109                 bool everything_after = (varname == "-");
110                 std::string word;
111
112                 for (int j = 0; j < index; j++)
113                         ss.GetToken(word);
114
115                 if (everything_after)
116                 {
117                         std::string more;
118                         while (ss.GetToken(more))
119                         {
120                                 word.append(" ");
121                                 word.append(more);
122                         }
123                 }
124
125                 return word;
126         }
127
128         void SearchAndReplace(std::string& newline, const std::string &find, const std::string &replace)
129         {
130                 std::string::size_type x = newline.find(find);
131                 while (x != std::string::npos)
132                 {
133                         newline.erase(x, find.length());
134                         newline.insert(x, replace);
135                         x = newline.find(find);
136                 }
137         }
138
139         virtual int OnPreCommand(std::string &command, std::vector<std::string> &parameters, User *user, bool validated, const std::string &original_line)
140         {
141                 std::multimap<std::string, Alias>::iterator i;
142
143                 /* If theyre not registered yet, we dont want
144                  * to know.
145                  */
146                 if (user->registered != REG_ALL)
147                         return 0;
148
149                 /* We dont have any commands looking like this? Stop processing. */
150                 i = Aliases.find(command);
151                 if (i == Aliases.end())
152                         return 0;
153
154                 irc::string c = command.c_str();
155                 /* The parameters for the command in their original form, with the command stripped off */
156                 std::string compare = original_line.substr(command.length());
157                 while (*(compare.c_str()) == ' ')
158                         compare.erase(compare.begin());
159
160                 std::string safe(original_line);
161
162                 /* Escape out any $ symbols in the user provided text */
163
164                 SearchAndReplace(safe, "$", "\r");
165
166                 while (i != Aliases.end())
167                 {
168                         if (i->second.UserCommand)
169                                 DoAlias(user, &(i->second), compare, safe);
170
171                         i++;
172                 }
173
174                 // If aliases have been processed, aliases took it.
175                 return 1;
176         }
177
178         void DoAlias(User *user, Alias *a, const std::string compare, const std::string safe)
179         {
180                 User *u = NULL;
181                 /* Does it match the pattern? */
182                 if (!a->format.empty())
183                 {
184                         if (a->CaseSensitive)
185                         {
186                                 if (InspIRCd::Match(compare, a->format, case_sensitive_map))
187                                         return;
188                         }
189                         else
190                         {
191                                 if (InspIRCd::Match(compare, a->format))
192                                         return;
193                         }
194                 }
195
196                 if ((a->OperOnly) && (!IS_OPER(user)))
197                         return;
198
199                 if (!a->RequiredNick.empty())
200                 {
201                         u = ServerInstance->FindNick(a->RequiredNick);
202                         if (!u)
203                         {
204                                 user->WriteNumeric(401, ""+std::string(user->nick)+" "+a->RequiredNick+" :is currently unavailable. Please try again later.");
205                                 return;
206                         }
207                 }
208                 if ((u != NULL) && (!a->RequiredNick.empty()) && (a->ULineOnly))
209                 {
210                         if (!ServerInstance->ULine(u->server))
211                         {
212                                 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!");
213                                 user->WriteNumeric(401, ""+std::string(user->nick)+" "+a->RequiredNick+" :is an imposter! Please inform an IRC operator as soon as possible.");
214                                 return;
215                         }
216                 }
217
218                 /* Now, search and replace in a copy of the original_line, replacing $1 through $9 and $1- etc */
219
220                 std::string::size_type crlf = a->ReplaceFormat.find('\n');
221
222                 if (crlf == std::string::npos)
223                 {
224                         DoCommand(a->ReplaceFormat, user, safe);
225                         return;
226                 }
227                 else
228                 {
229                         irc::sepstream commands(a->ReplaceFormat, '\n');
230                         std::string scommand;
231                         while (commands.GetToken(scommand))
232                         {
233                                 DoCommand(scommand, user, safe);
234                         }
235                         return;
236                 }
237         }
238
239         void DoCommand(std::string newline, User* user, const std::string &original_line)
240         {
241                 std::vector<std::string> pars;
242
243                 for (int v = 1; v < 10; v++)
244                 {
245                         std::string var = "$";
246                         var.append(ConvToStr(v));
247                         var.append("-");
248                         std::string::size_type x = newline.find(var);
249
250                         while (x != std::string::npos)
251                         {
252                                 newline.erase(x, var.length());
253                                 newline.insert(x, GetVar(var, original_line));
254                                 x = newline.find(var);
255                         }
256
257                         var = "$";
258                         var.append(ConvToStr(v));
259                         x = newline.find(var);
260
261                         while (x != std::string::npos)
262                         {
263                                 newline.erase(x, var.length());
264                                 newline.insert(x, GetVar(var, original_line));
265                                 x = newline.find(var);
266                         }
267                 }
268
269                 /* Special variables */
270                 SearchAndReplace(newline, "$nick", user->nick);
271                 SearchAndReplace(newline, "$ident", user->ident);
272                 SearchAndReplace(newline, "$host", user->host);
273                 SearchAndReplace(newline, "$vhost", user->dhost);
274
275                 /* Unescape any variable names in the user text before sending */
276                 SearchAndReplace(newline, "\r", "$");
277
278                 irc::tokenstream ss(newline);
279                 pars.clear();
280                 std::string command, token;
281
282                 ss.GetToken(command);
283                 while (ss.GetToken(token) && (pars.size() <= MAXPARAMETERS))
284                 {
285                         pars.push_back(token);
286                 }
287                 ServerInstance->Parser->CallHandler(command, pars, user);
288         }
289
290         virtual void OnRehash(User* user, const std::string &parameter)
291         {
292                 ReadAliases();
293         }
294 };
295
296 MODULE_INIT(ModuleAlias)