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