]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_alias.cpp
Header update: 2007 -> 2008
[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                 pars.resize(MAXPARAMETERS);
78
79                 Me->Modules->Attach(I_OnPreCommand, this);
80                 Me->Modules->Attach(I_OnRehash, this);
81
82         }
83
84         virtual ~ModuleAlias()
85         {
86         }
87
88         virtual Version GetVersion()
89         {
90                 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
91         }
92
93         std::string GetVar(std::string varname, const std::string &original_line)
94         {
95                 irc::spacesepstream ss(original_line);
96                 varname.erase(varname.begin());
97                 int index = *(varname.begin()) - 48;
98                 varname.erase(varname.begin());
99                 bool everything_after = (varname == "-");
100                 std::string word;
101
102                 for (int j = 0; j < index; j++)
103                         ss.GetToken(word);
104
105                 if (everything_after)
106                 {
107                         std::string more;
108                         while (ss.GetToken(more))
109                         {
110                                 word.append(" ");
111                                 word.append(more);
112                         }
113                 }
114
115                 return word;
116         }
117
118         void SearchAndReplace(std::string& newline, const std::string &find, const std::string &replace)
119         {
120                 std::string::size_type x = newline.find(find);
121                 while (x != std::string::npos)
122                 {
123                         newline.erase(x, find.length());
124                         newline.insert(x, replace);
125                         x = newline.find(find);
126                 }
127         }
128
129         virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, User *user, bool validated, const std::string &original_line)
130         {
131                 User *u = NULL;
132
133                 /* If theyre not registered yet, we dont want
134                  * to know.
135                  */
136                 if (user->registered != REG_ALL)
137                         return 0;
138
139                 /* We dont have any commands looking like this, dont bother with the loop */
140                 if (AliasMap.find(command) == AliasMap.end())
141                         return 0;
142
143                 irc::string c = command.c_str();
144                 /* The parameters for the command in their original form, with the command stripped off */
145                 std::string compare = original_line.substr(command.length());
146                 while (*(compare.c_str()) == ' ')
147                         compare.erase(compare.begin());
148
149                 std::string safe(original_line);
150
151                 /* Escape out any $ symbols in the user provided text */
152
153                 SearchAndReplace(safe, "$", "\r");
154
155                 for (unsigned int i = 0; i < Aliases.size(); i++)
156                 {
157                         if (Aliases[i].text == c)
158                         {
159                                 /* Does it match the pattern? */
160                                 if (!Aliases[i].format.empty())
161                                 {
162                                         if (!match(Aliases[i].case_sensitive, compare.c_str(), Aliases[i].format.c_str()))
163                                                 continue;
164                                 }
165
166                                 if ((Aliases[i].operonly) && (!IS_OPER(user)))
167                                         return 0;
168
169                                 if (!Aliases[i].requires.empty())
170                                 {
171                                         u = ServerInstance->FindNick(Aliases[i].requires);
172                                         if (!u)
173                                         {
174                                                 user->WriteServ("401 "+std::string(user->nick)+" "+Aliases[i].requires+" :is currently unavailable. Please try again later.");
175                                                 return 1;
176                                         }
177                                 }
178                                 if ((u != NULL) && (!Aliases[i].requires.empty()) && (Aliases[i].uline))
179                                 {
180                                         if (!ServerInstance->ULine(u->server))
181                                         {
182                                                 ServerInstance->WriteOpers("*** 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!"); 
183                                                 user->WriteServ("401 "+std::string(user->nick)+" "+Aliases[i].requires+" :is an imposter! Please inform an IRC operator as soon as possible.");
184                                                 return 1;
185                                         }
186                                 }
187
188                                 /* Now, search and replace in a copy of the original_line, replacing $1 through $9 and $1- etc */
189
190                                 std::string::size_type crlf = Aliases[i].replace_with.find('\n');
191
192                                 if (crlf == std::string::npos)
193                                 {
194                                         DoCommand(Aliases[i].replace_with, user, safe);
195                                         return 1;
196                                 }
197                                 else
198                                 {
199                                         irc::sepstream commands(Aliases[i].replace_with, '\n');
200                                         std::string command;
201                                         while (commands.GetToken(command))
202                                         {
203                                                 DoCommand(command, user, safe);
204                                         }
205                                         return 1;
206                                 }
207                         }
208                 }
209                 return 0;
210         }
211
212         void DoCommand(std::string newline, User* user, const std::string &original_line)
213         {
214                 for (int v = 1; v < 10; v++)
215                 {
216                         std::string var = "$";
217                         var.append(ConvToStr(v));
218                         var.append("-");
219                         std::string::size_type x = newline.find(var);
220
221                         while (x != std::string::npos)
222                         {
223                                 newline.erase(x, var.length());
224                                 newline.insert(x, GetVar(var, original_line));
225                                 x = newline.find(var);
226                         }
227
228                         var = "$";
229                         var.append(ConvToStr(v));
230                         x = newline.find(var);
231
232                         while (x != std::string::npos)
233                         {
234                                 newline.erase(x, var.length());
235                                 newline.insert(x, GetVar(var, original_line));
236                                 x = newline.find(var);
237                         }
238                 }
239
240                 /* Special variables */
241                 SearchAndReplace(newline, "$nick", user->nick);
242                 SearchAndReplace(newline, "$ident", user->ident);
243                 SearchAndReplace(newline, "$host", user->host);
244                 SearchAndReplace(newline, "$vhost", user->dhost);
245
246                 /* Unescape any variable names in the user text before sending */
247                 SearchAndReplace(newline, "\r", "$");
248
249                 irc::tokenstream ss(newline);
250                 const char* parv[MAXPARAMETERS];
251                 int x = 0;
252
253                 while (ss.GetToken(pars[x]) && x < MAXPARAMETERS)
254                 {
255                         parv[x] = pars[x].c_str();
256                         x++;
257                 }
258
259                 ServerInstance->Parser->CallHandler(parv[0], &parv[1], x-1, user);
260         }
261  
262         virtual void OnRehash(User* user, const std::string &parameter)
263         {
264                 ReadAliases();
265         }
266 };
267
268 MODULE_INIT(ModuleAlias)