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