]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_alias.cpp
Framework for central regex module, and a bare-bone implementation based on InspIRCd...
[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 text;
25         /** Text to replace with */
26         std::string replace_with;
27         /** Nickname required to perform alias */
28         std::string requires;
29         /** Alias requires ulined server */
30         bool uline;
31         /** Requires oper? */
32         bool operonly;
33         /* is case sensitive params */
34         bool case_sensitive;
35         /** Format that must be matched for use */
36         std::string format;
37 };
38
39 class ModuleAlias : public Module
40 {
41  private:
42         /** We cant use a map, there may be multiple aliases with the same name */
43         std::vector<Alias> Aliases;
44         std::map<std::string, int> AliasMap;
45         std::vector<std::string> pars;
46
47         virtual void ReadAliases()
48         {
49                 ConfigReader MyConf(ServerInstance);
50
51                 Aliases.clear();
52                 AliasMap.clear();
53                 for (int i = 0; i < MyConf.Enumerate("alias"); i++)
54                 {
55                         Alias a;
56                         std::string txt;
57                         txt = MyConf.ReadValue("alias", "text", i);
58                         a.text = txt.c_str();
59                         a.replace_with = MyConf.ReadValue("alias", "replace", i, true);
60                         a.requires = MyConf.ReadValue("alias", "requires", i);
61                         a.uline = MyConf.ReadFlag("alias", "uline", i);
62                         a.operonly = MyConf.ReadFlag("alias", "operonly", i);
63                         a.format = MyConf.ReadValue("alias", "format", i);
64                         a.case_sensitive = MyConf.ReadFlag("alias", "matchcase", i);
65                         Aliases.push_back(a);
66                         AliasMap[txt] = 1;
67                 }
68         }
69
70  public:
71
72         ModuleAlias(InspIRCd* Me)
73                 : Module(Me)
74         {
75                 ReadAliases();
76                 Me->Modules->Attach(I_OnPreCommand, this);
77                 Me->Modules->Attach(I_OnRehash, this);
78
79         }
80
81         virtual ~ModuleAlias()
82         {
83         }
84
85         virtual Version GetVersion()
86         {
87                 return Version("$Id$", VF_VENDOR,API_VERSION);
88         }
89
90         std::string GetVar(std::string varname, const std::string &original_line)
91         {
92                 irc::spacesepstream ss(original_line);
93                 varname.erase(varname.begin());
94                 int index = *(varname.begin()) - 48;
95                 varname.erase(varname.begin());
96                 bool everything_after = (varname == "-");
97                 std::string word;
98
99                 for (int j = 0; j < index; j++)
100                         ss.GetToken(word);
101
102                 if (everything_after)
103                 {
104                         std::string more;
105                         while (ss.GetToken(more))
106                         {
107                                 word.append(" ");
108                                 word.append(more);
109                         }
110                 }
111
112                 return word;
113         }
114
115         void SearchAndReplace(std::string& newline, const std::string &find, const std::string &replace)
116         {
117                 std::string::size_type x = newline.find(find);
118                 while (x != std::string::npos)
119                 {
120                         newline.erase(x, find.length());
121                         newline.insert(x, replace);
122                         x = newline.find(find);
123                 }
124         }
125
126         virtual int OnPreCommand(std::string &command, std::vector<std::string> &parameters, User *user, bool validated, const std::string &original_line)
127         {
128                 User *u = NULL;
129
130                 /* If theyre not registered yet, we dont want
131                  * to know.
132                  */
133                 if (user->registered != REG_ALL)
134                         return 0;
135
136                 /* We dont have any commands looking like this, dont bother with the loop */
137                 if (AliasMap.find(command) == AliasMap.end())
138                         return 0;
139
140                 irc::string c = command.c_str();
141                 /* The parameters for the command in their original form, with the command stripped off */
142                 std::string compare = original_line.substr(command.length());
143                 while (*(compare.c_str()) == ' ')
144                         compare.erase(compare.begin());
145
146                 std::string safe(original_line);
147
148                 /* Escape out any $ symbols in the user provided text */
149
150                 SearchAndReplace(safe, "$", "\r");
151
152                 for (unsigned int i = 0; i < Aliases.size(); i++)
153                 {
154                         if (Aliases[i].text == c)
155                         {
156                                 /* Does it match the pattern? */
157                                 if (!Aliases[i].format.empty())
158                                 {
159                                         if (Aliases[i].case_sensitive)
160                                         {
161                                                 if (InspIRCd::Match(compare, Aliases[i].format, case_sensitive_map))
162                                                         continue;
163                                         }
164                                         else
165                                         {
166                                                 if (InspIRCd::Match(compare, Aliases[i].format))
167                                                         continue;
168                                         }
169                                 }
170
171                                 if ((Aliases[i].operonly) && (!IS_OPER(user)))
172                                         return 0;
173
174                                 if (!Aliases[i].requires.empty())
175                                 {
176                                         u = ServerInstance->FindNick(Aliases[i].requires);
177                                         if (!u)
178                                         {
179                                                 user->WriteNumeric(401, ""+std::string(user->nick)+" "+Aliases[i].requires+" :is currently unavailable. Please try again later.");
180                                                 return 1;
181                                         }
182                                 }
183                                 if ((u != NULL) && (!Aliases[i].requires.empty()) && (Aliases[i].uline))
184                                 {
185                                         if (!ServerInstance->ULine(u->server))
186                                         {
187                                                 ServerInstance->SNO->WriteToSnoMask('A', "NOTICE -- Service "+Aliases[i].requires+" required by alias "+std::string(Aliases[i].text.c_str())+" is not on a u-lined server, possibly underhanded antics detected!");
188                                                 user->WriteNumeric(401, ""+std::string(user->nick)+" "+Aliases[i].requires+" :is an imposter! Please inform an IRC operator as soon as possible.");
189                                                 return 1;
190                                         }
191                                 }
192
193                                 /* Now, search and replace in a copy of the original_line, replacing $1 through $9 and $1- etc */
194
195                                 std::string::size_type crlf = Aliases[i].replace_with.find('\n');
196
197                                 if (crlf == std::string::npos)
198                                 {
199                                         DoCommand(Aliases[i].replace_with, user, safe);
200                                         return 1;
201                                 }
202                                 else
203                                 {
204                                         irc::sepstream commands(Aliases[i].replace_with, '\n');
205                                         std::string scommand;
206                                         while (commands.GetToken(scommand))
207                                         {
208                                                 DoCommand(scommand, user, safe);
209                                         }
210                                         return 1;
211                                 }
212                         }
213                 }
214                 return 0;
215         }
216
217         void DoCommand(std::string newline, User* user, const std::string &original_line)
218         {
219                 for (int v = 1; v < 10; v++)
220                 {
221                         std::string var = "$";
222                         var.append(ConvToStr(v));
223                         var.append("-");
224                         std::string::size_type x = newline.find(var);
225
226                         while (x != std::string::npos)
227                         {
228                                 newline.erase(x, var.length());
229                                 newline.insert(x, GetVar(var, original_line));
230                                 x = newline.find(var);
231                         }
232
233                         var = "$";
234                         var.append(ConvToStr(v));
235                         x = newline.find(var);
236
237                         while (x != std::string::npos)
238                         {
239                                 newline.erase(x, var.length());
240                                 newline.insert(x, GetVar(var, original_line));
241                                 x = newline.find(var);
242                         }
243                 }
244
245                 /* Special variables */
246                 SearchAndReplace(newline, "$nick", user->nick);
247                 SearchAndReplace(newline, "$ident", user->ident);
248                 SearchAndReplace(newline, "$host", user->host);
249                 SearchAndReplace(newline, "$vhost", user->dhost);
250
251                 /* Unescape any variable names in the user text before sending */
252                 SearchAndReplace(newline, "\r", "$");
253
254                 irc::tokenstream ss(newline);
255                 pars.clear();
256                 std::string command, token;
257
258                 ss.GetToken(command);
259                 while (ss.GetToken(token) && (pars.size() <= MAXPARAMETERS))
260                 {
261                         pars.push_back(token);
262                 }
263                 ServerInstance->Parser->CallHandler(command, pars, user);
264         }
265
266         virtual void OnRehash(User* user, const std::string &parameter)
267         {
268                 ReadAliases();
269         }
270 };
271
272 MODULE_INIT(ModuleAlias)