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