]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_alias.cpp
Match conversion stuff.
[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(1,2,0,1,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))
162                                                         continue;
163                                         }
164                                         else
165                                         {
166                                                 throw "not implemented"; // XXX fixme
167                                         }
168                                 }
169
170                                 if ((Aliases[i].operonly) && (!IS_OPER(user)))
171                                         return 0;
172
173                                 if (!Aliases[i].requires.empty())
174                                 {
175                                         u = ServerInstance->FindNick(Aliases[i].requires);
176                                         if (!u)
177                                         {
178                                                 user->WriteNumeric(401, ""+std::string(user->nick)+" "+Aliases[i].requires+" :is currently unavailable. Please try again later.");
179                                                 return 1;
180                                         }
181                                 }
182                                 if ((u != NULL) && (!Aliases[i].requires.empty()) && (Aliases[i].uline))
183                                 {
184                                         if (!ServerInstance->ULine(u->server))
185                                         {
186                                                 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!");
187                                                 user->WriteNumeric(401, ""+std::string(user->nick)+" "+Aliases[i].requires+" :is an imposter! Please inform an IRC operator as soon as possible.");
188                                                 return 1;
189                                         }
190                                 }
191
192                                 /* Now, search and replace in a copy of the original_line, replacing $1 through $9 and $1- etc */
193
194                                 std::string::size_type crlf = Aliases[i].replace_with.find('\n');
195
196                                 if (crlf == std::string::npos)
197                                 {
198                                         DoCommand(Aliases[i].replace_with, user, safe);
199                                         return 1;
200                                 }
201                                 else
202                                 {
203                                         irc::sepstream commands(Aliases[i].replace_with, '\n');
204                                         std::string scommand;
205                                         while (commands.GetToken(scommand))
206                                         {
207                                                 DoCommand(scommand, user, safe);
208                                         }
209                                         return 1;
210                                 }
211                         }
212                 }
213                 return 0;
214         }
215
216         void DoCommand(std::string newline, User* user, const std::string &original_line)
217         {
218                 for (int v = 1; v < 10; v++)
219                 {
220                         std::string var = "$";
221                         var.append(ConvToStr(v));
222                         var.append("-");
223                         std::string::size_type x = newline.find(var);
224
225                         while (x != std::string::npos)
226                         {
227                                 newline.erase(x, var.length());
228                                 newline.insert(x, GetVar(var, original_line));
229                                 x = newline.find(var);
230                         }
231
232                         var = "$";
233                         var.append(ConvToStr(v));
234                         x = newline.find(var);
235
236                         while (x != std::string::npos)
237                         {
238                                 newline.erase(x, var.length());
239                                 newline.insert(x, GetVar(var, original_line));
240                                 x = newline.find(var);
241                         }
242                 }
243
244                 /* Special variables */
245                 SearchAndReplace(newline, "$nick", user->nick);
246                 SearchAndReplace(newline, "$ident", user->ident);
247                 SearchAndReplace(newline, "$host", user->host);
248                 SearchAndReplace(newline, "$vhost", user->dhost);
249
250                 /* Unescape any variable names in the user text before sending */
251                 SearchAndReplace(newline, "\r", "$");
252
253                 irc::tokenstream ss(newline);
254                 pars.clear();
255                 std::string command, token;
256
257                 ss.GetToken(command);
258                 while (ss.GetToken(token) && (pars.size() <= MAXPARAMETERS))
259                 {
260                         pars.push_back(token);
261                 }
262                 ServerInstance->Parser->CallHandler(command, pars, user);
263         }
264
265         virtual void OnRehash(User* user, const std::string &parameter)
266         {
267                 ReadAliases();
268         }
269 };
270
271 MODULE_INIT(ModuleAlias)