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