]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_dccallow.cpp
c202a97b43f9c9878d74f65e45adf3e59fec0b06
[user/henk/code/inspircd.git] / src / modules / m_dccallow.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/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: Povides support for the /DCCALLOW command */
17
18 static ConfigReader *Conf;
19
20 class BannedFileList : public classbase
21 {
22  public:
23         std::string filemask;
24         std::string action;
25 };
26
27 class DCCAllow : public classbase
28 {
29  public:
30         std::string nickname;
31         std::string hostmask;
32         time_t set_on;
33         long length;
34
35         DCCAllow() { }
36
37         DCCAllow(const std::string &nick, const std::string &hm, const time_t so, const long ln) : nickname(nick), hostmask(hm), set_on(so), length(ln) { }
38 };
39
40 typedef std::vector<User *> userlist;
41 userlist ul;
42 typedef std::vector<DCCAllow> dccallowlist;
43 dccallowlist* dl;
44 typedef std::vector<BannedFileList> bannedfilelist;
45 bannedfilelist bfl;
46 SimpleExtItem<dccallowlist>* ext;
47
48 class CommandDccallow : public Command
49 {
50  public:
51         CommandDccallow(Module* parent) : Command(parent, "DCCALLOW", 0)
52         {
53                 syntax = "{[+|-]<nick> <time>|HELP|LIST}";
54                 /* XXX we need to fix this so it can work with translation stuff (i.e. move +- into a seperate param */
55         }
56
57         CmdResult Handle(const std::vector<std::string> &parameters, User *user)
58         {
59                 /* syntax: DCCALLOW [+|-]<nick> (<time>) */
60                 if (!parameters.size())
61                 {
62                         // display current DCCALLOW list
63                         DisplayDCCAllowList(user);
64                         return CMD_FAILURE;
65                 }
66                 else if (parameters.size() > 0)
67                 {
68                         char action = *parameters[0].c_str();
69
70                         // if they didn't specify an action, this is probably a command
71                         if (action != '+' && action != '-')
72                         {
73                                 if (!strcasecmp(parameters[0].c_str(), "LIST"))
74                                 {
75                                         // list current DCCALLOW list
76                                         DisplayDCCAllowList(user);
77                                         return CMD_FAILURE;
78                                 }
79                                 else if (!strcasecmp(parameters[0].c_str(), "HELP"))
80                                 {
81                                         // display help
82                                         DisplayHelp(user);
83                                         return CMD_FAILURE;
84                                 }
85                                 else
86                                 {
87                                         user->WriteNumeric(998, "%s :DCCALLOW command not understood. For help on DCCALLOW, type /DCCALLOW HELP", user->nick.c_str());
88                                         return CMD_FAILURE;
89                                 }
90                         }
91
92                         std::string nick = parameters[0].substr(1);
93                         User *target = ServerInstance->FindNick(nick);
94
95                         if (target)
96                         {
97
98                                 if (action == '-')
99                                 {
100                                         // check if it contains any entries
101                                         dl = ext->get(user);
102                                         if (dl)
103                                         {
104                                                 for (dccallowlist::iterator i = dl->begin(); i != dl->end(); ++i)
105                                                 {
106                                                         // search through list
107                                                         if (i->nickname == target->nick)
108                                                         {
109                                                                 dl->erase(i);
110                                                                 user->WriteNumeric(995, "%s %s :Removed %s from your DCCALLOW list", user->nick.c_str(), user->nick.c_str(), target->nick.c_str());
111                                                                 break;
112                                                         }
113                                                 }
114                                         }
115                                 }
116                                 else if (action == '+')
117                                 {
118                                         if (target == user)
119                                         {
120                                                 user->WriteNumeric(996, "%s %s :You cannot add yourself to your own DCCALLOW list!", user->nick.c_str(), user->nick.c_str());
121                                                 return CMD_FAILURE;
122                                         }
123
124                                         dl = ext->get(user);
125                                         if (!dl)
126                                         {
127                                                 dl = new dccallowlist;
128                                                 ext->set(user, dl);
129                                                 // add this user to the userlist
130                                                 ul.push_back(user);
131                                         }
132
133                                         for (dccallowlist::const_iterator k = dl->begin(); k != dl->end(); ++k)
134                                         {
135                                                 if (k->nickname == target->nick)
136                                                 {
137                                                         user->WriteNumeric(996, "%s %s :%s is already on your DCCALLOW list", user->nick.c_str(), user->nick.c_str(), target->nick.c_str());
138                                                         return CMD_FAILURE;
139                                                 }
140                                         }
141
142                                         std::string mask = std::string(target->nick)+"!"+std::string(target->ident)+"@"+std::string(target->dhost);
143                                         std::string default_length = Conf->ReadValue("dccallow", "length", 0);
144
145                                         long length;
146                                         if (parameters.size() < 2)
147                                         {
148                                                 length = ServerInstance->Duration(default_length);
149                                         }
150                                         else if (!atoi(parameters[1].c_str()))
151                                         {
152                                                 length = 0;
153                                         }
154                                         else
155                                         {
156                                                 length = ServerInstance->Duration(parameters[1]);
157                                         }
158
159                                         if (!ServerInstance->IsValidMask(mask.c_str()))
160                                         {
161                                                 return CMD_FAILURE;
162                                         }
163
164                                         dl->push_back(DCCAllow(target->nick, mask, ServerInstance->Time(), length));
165
166                                         if (length > 0)
167                                         {
168                                                 user->WriteNumeric(993, "%s %s :Added %s to DCCALLOW list for %ld seconds", user->nick.c_str(), user->nick.c_str(), target->nick.c_str(), length);
169                                         }
170                                         else
171                                         {
172                                                 user->WriteNumeric(994, "%s %s :Added %s to DCCALLOW list for this session", user->nick.c_str(), user->nick.c_str(), target->nick.c_str());
173                                         }
174
175                                         /* route it. */
176                                         return CMD_SUCCESS;
177                                 }
178                         }
179                         else
180                         {
181                                 // nick doesn't exist
182                                 user->WriteNumeric(401, "%s %s :No such nick/channel", user->nick.c_str(), nick.c_str());
183                                 return CMD_FAILURE;
184                         }
185                 }
186                 return CMD_FAILURE;
187         }
188
189         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
190         {
191                 return ROUTE_BROADCAST;
192         }
193
194         void DisplayHelp(User* user)
195         {
196                 user->WriteNumeric(998, "%s :DCCALLOW [<+|->nick [time]] [list] [help]", user->nick.c_str());
197                 user->WriteNumeric(998, "%s :You may allow DCCs from specific users by specifying a", user->nick.c_str());
198                 user->WriteNumeric(998, "%s :DCC allow for the user you want to receive DCCs from.", user->nick.c_str());
199                 user->WriteNumeric(998, "%s :For example, to allow the user Brain to send you inspircd.exe", user->nick.c_str());
200                 user->WriteNumeric(998, "%s :you would type:", user->nick.c_str());
201                 user->WriteNumeric(998, "%s :/DCCALLOW +Brain", user->nick.c_str());
202                 user->WriteNumeric(998, "%s :Brain would then be able to send you files. They would have to", user->nick.c_str());
203                 user->WriteNumeric(998, "%s :resend the file again if the server gave them an error message", user->nick.c_str());
204                 user->WriteNumeric(998, "%s :before you added them to your DCCALLOW list.", user->nick.c_str());
205                 user->WriteNumeric(998, "%s :DCCALLOW entries will be temporary by default, if you want to add", user->nick.c_str());
206                 user->WriteNumeric(998, "%s :them to your DCCALLOW list until you leave IRC, type:", user->nick.c_str());
207                 user->WriteNumeric(998, "%s :/DCCALLOW +Brain 0", user->nick.c_str());
208                 user->WriteNumeric(998, "%s :To remove the user from your DCCALLOW list, type:", user->nick.c_str());
209                 user->WriteNumeric(998, "%s :/DCCALLOW -Brain", user->nick.c_str());
210                 user->WriteNumeric(998, "%s :To see the users in your DCCALLOW list, type:", user->nick.c_str());
211                 user->WriteNumeric(998, "%s :/DCCALLOW LIST", user->nick.c_str());
212                 user->WriteNumeric(998, "%s :NOTE: If the user leaves IRC or changes their nickname", user->nick.c_str());
213                 user->WriteNumeric(998, "%s :  they will be removed from your DCCALLOW list.", user->nick.c_str());
214                 user->WriteNumeric(998, "%s :  your DCCALLOW list will be deleted when you leave IRC.", user->nick.c_str());
215                 user->WriteNumeric(999, "%s :End of DCCALLOW HELP", user->nick.c_str());
216         }
217
218         void DisplayDCCAllowList(User* user)
219         {
220                  // display current DCCALLOW list
221                 user->WriteNumeric(990, "%s :Users on your DCCALLOW list:", user->nick.c_str());
222
223                 dl = ext->get(user);
224                 if (dl)
225                 {
226                         for (dccallowlist::const_iterator c = dl->begin(); c != dl->end(); ++c)
227                         {
228                                 user->WriteNumeric(991, "%s %s :%s (%s)", user->nick.c_str(), user->nick.c_str(), c->nickname.c_str(), c->hostmask.c_str());
229                         }
230                 }
231
232                 user->WriteNumeric(992, "%s :End of DCCALLOW list", user->nick.c_str());
233         }
234
235 };
236
237 class ModuleDCCAllow : public Module
238 {
239         CommandDccallow cmd;
240  public:
241
242         ModuleDCCAllow()
243                 : cmd(this)
244         {
245                 Conf = new ConfigReader;
246                 ext = new SimpleExtItem<dccallowlist>("dccallow", this);
247                 ServerInstance->Extensions.Register(ext);
248                 ServerInstance->AddCommand(&cmd);
249                 ReadFileConf();
250                 Implementation eventlist[] = { I_OnUserPreMessage, I_OnUserPreNotice, I_OnUserQuit, I_OnUserPreNick, I_OnRehash };
251                 ServerInstance->Modules->Attach(eventlist, this, 5);
252         }
253
254
255         virtual void OnRehash(User* user)
256         {
257                 delete Conf;
258                 Conf = new ConfigReader;
259                 ReadFileConf();
260         }
261
262         virtual void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
263         {
264                 dccallowlist* udl = ext->get(user);
265
266                 // remove their DCCALLOW list if they have one
267                 if (udl)
268                 {
269                         RemoveFromUserlist(user);
270                 }
271
272                 // remove them from any DCCALLOW lists
273                 // they are currently on
274                 RemoveNick(user);
275         }
276
277         virtual ModResult OnUserPreNick(User* user, const std::string &newnick)
278         {
279                 RemoveNick(user);
280                 return MOD_RES_PASSTHRU;
281         }
282
283         virtual ModResult OnUserPreMessage(User* user, void* dest, int target_type, std::string &text, char status, CUList &exempt_list)
284         {
285                 return OnUserPreNotice(user, dest, target_type, text, status, exempt_list);
286         }
287
288         virtual ModResult OnUserPreNotice(User* user, void* dest, int target_type, std::string &text, char status, CUList &exempt_list)
289         {
290                 if (!IS_LOCAL(user))
291                         return MOD_RES_PASSTHRU;
292
293                 if (target_type == TYPE_USER)
294                 {
295                         User* u = (User*)dest;
296
297                         /* Always allow a user to dcc themselves (although... why?) */
298                         if (user == u)
299                                 return MOD_RES_PASSTHRU;
300
301                         if ((text.length()) && (text[0] == '\1'))
302                         {
303                                 Expire();
304
305                                 // :jamie!jamie@test-D4457903BA652E0F.silverdream.org PRIVMSG eimaj :DCC SEND m_dnsbl.cpp 3232235786 52650 9676
306                                 // :jamie!jamie@test-D4457903BA652E0F.silverdream.org PRIVMSG eimaj :VERSION
307
308                                 if (strncmp(text.c_str(), "\1DCC ", 5) == 0)
309                                 {
310                                         dl = ext->get(u);
311                                         if (dl && dl->size())
312                                         {
313                                                 for (dccallowlist::const_iterator iter = dl->begin(); iter != dl->end(); ++iter)
314                                                         if (InspIRCd::Match(user->GetFullHost(), iter->hostmask))
315                                                                 return MOD_RES_PASSTHRU;
316                                         }
317
318                                         // tokenize
319                                         std::stringstream ss(text);
320                                         std::string buf;
321                                         std::vector<std::string> tokens;
322
323                                         while (ss >> buf)
324                                                 tokens.push_back(buf);
325
326                                         irc::string type = tokens[1].c_str();
327
328                                         bool blockchat = Conf->ReadFlag("dccallow", "blockchat", 0);
329
330                                         if (type == "SEND")
331                                         {
332                                                 std::string defaultaction = Conf->ReadValue("dccallow", "action", 0);
333                                                 std::string filename = tokens[2];
334
335                                                 bool found = false;
336                                                 for (unsigned int i = 0; i < bfl.size(); i++)
337                                                 {
338                                                         if (InspIRCd::Match(filename, bfl[i].filemask, ascii_case_insensitive_map))
339                                                         {
340                                                                 /* We have a matching badfile entry, override whatever the default action is */
341                                                                 if (bfl[i].action == "allow")
342                                                                         return MOD_RES_PASSTHRU;
343                                                                 else
344                                                                 {
345                                                                         found = true;
346                                                                         break;
347                                                                 }
348                                                         }
349                                                 }
350
351                                                 /* only follow the default action if no badfile matches were found above */
352                                                 if ((!found) && (defaultaction == "allow"))
353                                                         return MOD_RES_PASSTHRU;
354
355                                                 user->WriteServ("NOTICE %s :The user %s is not accepting DCC SENDs from you. Your file %s was not sent.", user->nick.c_str(), u->nick.c_str(), filename.c_str());
356                                                 u->WriteServ("NOTICE %s :%s (%s@%s) attempted to send you a file named %s, which was blocked.", u->nick.c_str(), user->nick.c_str(), user->ident.c_str(), user->dhost.c_str(), filename.c_str());
357                                                 u->WriteServ("NOTICE %s :If you trust %s and were expecting this, you can type /DCCALLOW HELP for information on the DCCALLOW system.", u->nick.c_str(), user->nick.c_str());
358                                                 return MOD_RES_DENY;
359                                         }
360                                         else if ((type == "CHAT") && (blockchat))
361                                         {
362                                                 user->WriteServ("NOTICE %s :The user %s is not accepting DCC CHAT requests from you.", user->nick.c_str(), u->nick.c_str());
363                                                 u->WriteServ("NOTICE %s :%s (%s@%s) attempted to initiate a DCC CHAT session, which was blocked.", u->nick.c_str(), user->nick.c_str(), user->ident.c_str(), user->dhost.c_str());
364                                                 u->WriteServ("NOTICE %s :If you trust %s and were expecting this, you can type /DCCALLOW HELP for information on the DCCALLOW system.", u->nick.c_str(), user->nick.c_str());
365                                                 return MOD_RES_DENY;
366                                         }
367                                 }
368                         }
369                 }
370                 return MOD_RES_PASSTHRU;
371         }
372
373         void Expire()
374         {
375                 for (userlist::iterator iter = ul.begin(); iter != ul.end(); ++iter)
376                 {
377                         User* u = (User*)(*iter);
378                         dl = ext->get(u);
379                         if (dl)
380                         {
381                                 if (dl->size())
382                                 {
383                                         dccallowlist::iterator iter2 = dl->begin();
384                                         while (iter2 != dl->end())
385                                         {
386                                                 if (iter2->length != 0 && (iter2->set_on + iter2->length) <= ServerInstance->Time())
387                                                 {
388                                                         u->WriteNumeric(997, "%s %s :DCCALLOW entry for %s has expired", u->nick.c_str(), u->nick.c_str(), iter2->nickname.c_str());
389                                                         iter2 = dl->erase(iter2);
390                                                 }
391                                                 else
392                                                 {
393                                                         ++iter2;
394                                                 }
395                                         }
396                                 }
397                         }
398                         else
399                         {
400                                 RemoveFromUserlist(u);
401                         }
402                 }
403         }
404
405         void RemoveNick(User* user)
406         {
407                 /* Iterate through all DCCALLOW lists and remove user */
408                 for (userlist::iterator iter = ul.begin(); iter != ul.end(); ++iter)
409                 {
410                         User *u = (User*)(*iter);
411                         dl = ext->get(u);
412                         if (dl)
413                         {
414                                 if (dl->size())
415                                 {
416                                         for (dccallowlist::iterator i = dl->begin(); i != dl->end(); ++i)
417                                         {
418                                                 if (i->nickname == user->nick)
419                                                 {
420
421                                                         u->WriteServ("NOTICE %s :%s left the network or changed their nickname and has been removed from your DCCALLOW list", u->nick.c_str(), i->nickname.c_str());
422                                                         u->WriteNumeric(995, "%s %s :Removed %s from your DCCALLOW list", u->nick.c_str(), u->nick.c_str(), i->nickname.c_str());
423                                                         dl->erase(i);
424                                                         break;
425                                                 }
426                                         }
427                                 }
428                         }
429                         else
430                         {
431                                 RemoveFromUserlist(u);
432                         }
433                 }
434         }
435
436         void RemoveFromUserlist(User *user)
437         {
438                 // remove user from userlist
439                 for (userlist::iterator j = ul.begin(); j != ul.end(); ++j)
440                 {
441                         User* u = (User*)(*j);
442                         if (u == user)
443                         {
444                                 ul.erase(j);
445                                 break;
446                         }
447                 }
448         }
449
450         void ReadFileConf()
451         {
452                 bfl.clear();
453                 for (int i = 0; i < Conf->Enumerate("banfile"); i++)
454                 {
455                         BannedFileList bf;
456                         std::string fileglob = Conf->ReadValue("banfile", "pattern", i);
457                         std::string action = Conf->ReadValue("banfile", "action", i);
458                         bf.filemask = fileglob;
459                         bf.action = action;
460                         bfl.push_back(bf);
461                 }
462
463         }
464
465         virtual ~ModuleDCCAllow()
466         {
467                 delete Conf;
468                 delete ext;
469                 Conf = NULL;
470         }
471
472         virtual Version GetVersion()
473         {
474                 return Version("Povides support for the /DCCALLOW command", VF_COMMON | VF_VENDOR);
475         }
476 };
477
478 MODULE_INIT(ModuleDCCAllow)