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