]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_dccallow.cpp
Fix excessive snomask sending on fitler add/removal
[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
47 class CommandDccallow : public Command
48 {
49  public:
50         CommandDccallow(InspIRCd* Me) : Command(Me, "DCCALLOW", 0, 0)
51         {
52                 this->source = "m_dccallow.so";
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                                         if (user->GetExt("dccallow_list", dl))
102                                         {
103                                                 for (dccallowlist::iterator i = dl->begin(); i != dl->end(); ++i)
104                                                 {
105                                                         // search through list
106                                                         if (i->nickname == target->nick)
107                                                         {
108                                                                 dl->erase(i);
109                                                                 user->WriteNumeric(995, "%s %s :Removed %s from your DCCALLOW list", user->nick.c_str(), user->nick.c_str(), target->nick.c_str());
110                                                                 break;
111                                                         }
112                                                 }
113                                         }
114                                         else
115                                         {
116                                                 delete  dl;
117                                                 user->Shrink("dccallow_list");
118
119                                                 // remove from userlist
120                                                 for (userlist::iterator j = ul.begin(); j != ul.end(); ++j)
121                                                 {
122                                                         User* u = (User*)(*j);
123                                                         if (u == user)
124                                                         {
125                                                                 ul.erase(j);
126                                                                 break;
127                                                         }
128                                                 }
129                                         }
130                                 }
131                                 else if (action == '+')
132                                 {
133                                         if (target == user)
134                                         {
135                                                 user->WriteNumeric(996, "%s %s :You cannot add yourself to your own DCCALLOW list!", user->nick.c_str(), user->nick.c_str());
136                                                 return CMD_FAILURE;
137                                         }
138
139                                         if (!user->GetExt("dccallow_list", dl))
140                                         {
141                                                 dl = new dccallowlist;
142                                                 user->Extend("dccallow_list", dl);
143                                                 // add this user to the userlist
144                                                 ul.push_back(user);
145                                         }
146
147                                         for (dccallowlist::const_iterator k = dl->begin(); k != dl->end(); ++k)
148                                         {
149                                                 if (k->nickname == target->nick)
150                                                 {
151                                                         user->WriteNumeric(996, "%s %s :%s is already on your DCCALLOW list", user->nick.c_str(), user->nick.c_str(), target->nick.c_str());
152                                                         return CMD_FAILURE;
153                                                 }
154                                         }
155
156                                         std::string mask = std::string(target->nick)+"!"+std::string(target->ident)+"@"+std::string(target->dhost);
157                                         std::string default_length = Conf->ReadValue("dccallow", "length", 0);
158
159                                         long length;
160                                         if (parameters.size() < 2)
161                                         {
162                                                 length = ServerInstance->Duration(default_length);
163                                         }
164                                         else if (!atoi(parameters[1].c_str()))
165                                         {
166                                                 length = 0;
167                                         }
168                                         else
169                                         {
170                                                 length = ServerInstance->Duration(parameters[1]);
171                                         }
172
173                                         if (!ServerInstance->IsValidMask(mask.c_str()))
174                                         {
175                                                 return CMD_FAILURE;
176                                         }
177
178                                         dl->push_back(DCCAllow(target->nick, mask, ServerInstance->Time(), length));
179
180                                         if (length > 0)
181                                         {
182                                                 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);
183                                         }
184                                         else
185                                         {
186                                                 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());
187                                         }
188
189                                         /* route it. */
190                                         return CMD_SUCCESS;
191                                 }
192                         }
193                         else
194                         {
195                                 // nick doesn't exist
196                                 user->WriteNumeric(401, "%s %s :No such nick/channel", user->nick.c_str(), nick.c_str());
197                                 return CMD_FAILURE;
198                         }
199                 }
200                 return CMD_FAILURE;
201         }
202
203         void DisplayHelp(User* user)
204         {
205                 user->WriteNumeric(998, "%s :DCCALLOW [<+|->nick [time]] [list] [help]", user->nick.c_str());
206                 user->WriteNumeric(998, "%s :You may allow DCCs from specific users by specifying a", user->nick.c_str());
207                 user->WriteNumeric(998, "%s :DCC allow for the user you want to receive DCCs from.", user->nick.c_str());
208                 user->WriteNumeric(998, "%s :For example, to allow the user Brain to send you inspircd.exe", user->nick.c_str());
209                 user->WriteNumeric(998, "%s :you would type:", user->nick.c_str());
210                 user->WriteNumeric(998, "%s :/DCCALLOW +Brain", user->nick.c_str());
211                 user->WriteNumeric(998, "%s :Brain would then be able to send you files. They would have to", user->nick.c_str());
212                 user->WriteNumeric(998, "%s :resend the file again if the server gave them an error message", user->nick.c_str());
213                 user->WriteNumeric(998, "%s :before you added them to your DCCALLOW list.", user->nick.c_str());
214                 user->WriteNumeric(998, "%s :DCCALLOW entries will be temporary by default, if you want to add", user->nick.c_str());
215                 user->WriteNumeric(998, "%s :them to your DCCALLOW list until you leave IRC, type:", user->nick.c_str());
216                 user->WriteNumeric(998, "%s :/DCCALLOW +Brain 0", user->nick.c_str());
217                 user->WriteNumeric(998, "%s :To remove the user from your DCCALLOW list, type:", user->nick.c_str());
218                 user->WriteNumeric(998, "%s :/DCCALLOW -Brain", user->nick.c_str());
219                 user->WriteNumeric(998, "%s :To see the users in your DCCALLOW list, type:", user->nick.c_str());
220                 user->WriteNumeric(998, "%s :/DCCALLOW LIST", user->nick.c_str());
221                 user->WriteNumeric(998, "%s :NOTE: If the user leaves IRC or changes their nickname", user->nick.c_str());
222                 user->WriteNumeric(998, "%s :  they will be removed from your DCCALLOW list.", user->nick.c_str());
223                 user->WriteNumeric(998, "%s :  your DCCALLOW list will be deleted when you leave IRC.", user->nick.c_str());
224                 user->WriteNumeric(999, "%s :End of DCCALLOW HELP", user->nick.c_str());
225         }
226
227         void DisplayDCCAllowList(User* user)
228         {
229                  // display current DCCALLOW list
230                 user->WriteNumeric(990, "%s :Users on your DCCALLOW list:", user->nick.c_str());
231
232                 if (user->GetExt("dccallow_list", dl))
233                 {
234                         for (dccallowlist::const_iterator c = dl->begin(); c != dl->end(); ++c)
235                         {
236                                 user->WriteNumeric(991, "%s %s :%s (%s)", user->nick.c_str(), user->nick.c_str(), c->nickname.c_str(), c->hostmask.c_str());
237                         }
238                 }
239
240                 user->WriteNumeric(992, "%s :End of DCCALLOW list", user->nick.c_str());
241         }
242
243 };
244
245 class ModuleDCCAllow : public Module
246 {
247         CommandDccallow* mycommand;
248  public:
249
250         ModuleDCCAllow(InspIRCd* Me)
251                 : Module(Me)
252         {
253                 Conf = new ConfigReader(ServerInstance);
254                 mycommand = new CommandDccallow(ServerInstance);
255                 ServerInstance->AddCommand(mycommand);
256                 ReadFileConf();
257                 Implementation eventlist[] = { I_OnUserPreMessage, I_OnUserPreNotice, I_OnUserQuit, I_OnUserPreNick, I_OnRehash };
258                 ServerInstance->Modules->Attach(eventlist, this, 5);
259         }
260
261
262         virtual void OnRehash(User* user)
263         {
264                 delete Conf;
265                 Conf = new ConfigReader(ServerInstance);
266                 ReadFileConf();
267         }
268
269         virtual void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
270         {
271                 dccallowlist* udl;
272
273                 // remove their DCCALLOW list if they have one
274                 if (user->GetExt("dccallow_list", udl))
275                 {
276                         delete udl;
277                         user->Shrink("dccallow_list");
278                         RemoveFromUserlist(user);
279                 }
280
281                 // remove them from any DCCALLOW lists
282                 // they are currently on
283                 RemoveNick(user);
284         }
285
286
287         virtual int OnUserPreNick(User* user, const std::string &newnick)
288         {
289                 RemoveNick(user);
290                 return 0;
291         }
292
293         virtual int OnUserPreMessage(User* user, void* dest, int target_type, std::string &text, char status, CUList &exempt_list)
294         {
295                 return OnUserPreNotice(user, dest, target_type, text, status, exempt_list);
296         }
297
298         virtual int OnUserPreNotice(User* user, void* dest, int target_type, std::string &text, char status, CUList &exempt_list)
299         {
300                 if (!IS_LOCAL(user))
301                         return 0;
302
303                 if (target_type == TYPE_USER)
304                 {
305                         User* u = (User*)dest;
306
307                         /* Always allow a user to dcc themselves (although... why?) */
308                         if (user == u)
309                                 return 0;
310
311                         if ((text.length()) && (text[0] == '\1'))
312                         {
313                                 Expire();
314
315                                 // :jamie!jamie@test-D4457903BA652E0F.silverdream.org PRIVMSG eimaj :DCC SEND m_dnsbl.cpp 3232235786 52650 9676
316                                 // :jamie!jamie@test-D4457903BA652E0F.silverdream.org PRIVMSG eimaj :VERSION
317
318                                 if (strncmp(text.c_str(), "\1DCC ", 5) == 0)
319                                 {
320                                         if (u->GetExt("dccallow_list", dl) && dl->size())
321                                         {
322                                                 for (dccallowlist::const_iterator iter = dl->begin(); iter != dl->end(); ++iter)
323                                                         if (InspIRCd::Match(user->GetFullHost(), iter->hostmask))
324                                                                 return 0;
325                                         }
326
327                                         // tokenize
328                                         std::stringstream ss(text);
329                                         std::string buf;
330                                         std::vector<std::string> tokens;
331
332                                         while (ss >> buf)
333                                                 tokens.push_back(buf);
334
335                                         irc::string type = tokens[1].c_str();
336
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 0;
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 0;
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 1;
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 1;
375                                         }
376                                 }
377                         }
378                 }
379                 return 0;
380         }
381
382         void Expire()
383         {
384                 for (userlist::iterator iter = ul.begin(); iter != ul.end(); ++iter)
385                 {
386                         User* u = (User*)(*iter);
387                         if (u->GetExt("dccallow_list", dl))
388                         {
389                                 if (dl->size())
390                                 {
391                                         dccallowlist::iterator iter2 = dl->begin();
392                                         while (iter2 != dl->end())
393                                         {
394                                                 if (iter2->length != 0 && (iter2->set_on + iter2->length) <= ServerInstance->Time())
395                                                 {
396                                                         u->WriteNumeric(997, "%s %s :DCCALLOW entry for %s has expired", u->nick.c_str(), u->nick.c_str(), iter2->nickname.c_str());
397                                                         iter2 = dl->erase(iter2);
398                                                 }
399                                                 else
400                                                 {
401                                                         ++iter2;
402                                                 }
403                                         }
404                                 }
405                         }
406                         else
407                         {
408                                 RemoveFromUserlist(u);
409                         }
410                 }
411         }
412
413         void RemoveNick(User* user)
414         {
415                 /* Iterate through all DCCALLOW lists and remove user */
416                 for (userlist::iterator iter = ul.begin(); iter != ul.end(); ++iter)
417                 {
418                         User *u = (User*)(*iter);
419                         if (u->GetExt("dccallow_list", dl))
420                         {
421                                 if (dl->size())
422                                 {
423                                         for (dccallowlist::iterator i = dl->begin(); i != dl->end(); ++i)
424                                         {
425                                                 if (i->nickname == user->nick)
426                                                 {
427
428                                                         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());
429                                                         u->WriteNumeric(995, "%s %s :Removed %s from your DCCALLOW list", u->nick.c_str(), u->nick.c_str(), i->nickname.c_str());
430                                                         dl->erase(i);
431                                                         break;
432                                                 }
433                                         }
434                                 }
435                         }
436                         else
437                         {
438                                 RemoveFromUserlist(u);
439                         }
440                 }
441         }
442
443         void RemoveFromUserlist(User *user)
444         {
445                 // remove user from userlist
446                 for (userlist::iterator j = ul.begin(); j != ul.end(); ++j)
447                 {
448                         User* u = (User*)(*j);
449                         if (u == user)
450                         {
451                                 ul.erase(j);
452                                 break;
453                         }
454                 }
455         }
456
457         void ReadFileConf()
458         {
459                 bfl.clear();
460                 for (int i = 0; i < Conf->Enumerate("banfile"); i++)
461                 {
462                         BannedFileList bf;
463                         std::string fileglob = Conf->ReadValue("banfile", "pattern", i);
464                         std::string action = Conf->ReadValue("banfile", "action", i);
465                         bf.filemask = fileglob;
466                         bf.action = action;
467                         bfl.push_back(bf);
468                 }
469
470         }
471
472         virtual ~ModuleDCCAllow()
473         {
474         }
475
476         virtual Version GetVersion()
477         {
478                 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
479         }
480 };
481
482 MODULE_INIT(ModuleDCCAllow)