2 * InspIRCd -- Internet Relay Chat Daemon
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 ??? <???@???>
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.
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
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/>.
28 /* $ModDesc: Provides support for the /DCCALLOW command */
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) { }
50 typedef std::vector<User *> userlist;
52 typedef std::vector<DCCAllow> dccallowlist;
54 typedef std::vector<BannedFileList> bannedfilelist;
56 SimpleExtItem<dccallowlist>* ext;
58 class CommandDccallow : public Command
61 CommandDccallow(Module* parent) : Command(parent, "DCCALLOW", 0)
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 */
67 CmdResult Handle(const std::vector<std::string> ¶meters, User *user)
69 /* syntax: DCCALLOW [+|-]<nick> (<time>) */
70 if (!parameters.size())
72 // display current DCCALLOW list
73 DisplayDCCAllowList(user);
76 else if (parameters.size() > 0)
78 char action = *parameters[0].c_str();
80 // if they didn't specify an action, this is probably a command
81 if (action != '+' && action != '-')
83 if (!strcasecmp(parameters[0].c_str(), "LIST"))
85 // list current DCCALLOW list
86 DisplayDCCAllowList(user);
89 else if (!strcasecmp(parameters[0].c_str(), "HELP"))
97 user->WriteNumeric(998, "%s :DCCALLOW command not understood. For help on DCCALLOW, type /DCCALLOW HELP", user->nick.c_str());
102 std::string nick = parameters[0].substr(1);
103 User *target = ServerInstance->FindNickOnly(nick);
105 if ((target) && (!IS_SERVER(target)) && (!target->quitting) && (target->registered == REG_ALL))
110 // check if it contains any entries
114 for (dccallowlist::iterator i = dl->begin(); i != dl->end(); ++i)
116 // search through list
117 if (i->nickname == target->nick)
120 user->WriteNumeric(995, "%s %s :Removed %s from your DCCALLOW list", user->nick.c_str(), user->nick.c_str(), target->nick.c_str());
126 else if (action == '+')
130 user->WriteNumeric(996, "%s %s :You cannot add yourself to your own DCCALLOW list!", user->nick.c_str(), user->nick.c_str());
137 dl = new dccallowlist;
139 // add this user to the userlist
143 for (dccallowlist::const_iterator k = dl->begin(); k != dl->end(); ++k)
145 if (k->nickname == target->nick)
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());
152 std::string mask = target->nick+"!"+target->ident+"@"+target->dhost;
153 std::string default_length = ServerInstance->Config->ConfValue("dccallow")->getString("length");
156 if (parameters.size() < 2)
158 length = ServerInstance->Duration(default_length);
160 else if (!atoi(parameters[1].c_str()))
166 length = ServerInstance->Duration(parameters[1]);
169 if (!ServerInstance->IsValidMask(mask.c_str()))
174 dl->push_back(DCCAllow(target->nick, mask, ServerInstance->Time(), length));
178 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);
182 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());
191 // nick doesn't exist
192 user->WriteNumeric(401, "%s %s :No such nick/channel", user->nick.c_str(), nick.c_str());
199 RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
201 return ROUTE_BROADCAST;
204 void DisplayHelp(User* user)
206 user->WriteNumeric(998, "%s :DCCALLOW [<+|->nick [time]] [list] [help]", user->nick.c_str());
207 user->WriteNumeric(998, "%s :You may allow DCCs from specific users by specifying a", user->nick.c_str());
208 user->WriteNumeric(998, "%s :DCC allow for the user you want to receive DCCs from.", user->nick.c_str());
209 user->WriteNumeric(998, "%s :For example, to allow the user Brain to send you inspircd.exe", user->nick.c_str());
210 user->WriteNumeric(998, "%s :you would type:", user->nick.c_str());
211 user->WriteNumeric(998, "%s :/DCCALLOW +Brain", user->nick.c_str());
212 user->WriteNumeric(998, "%s :Brain would then be able to send you files. They would have to", user->nick.c_str());
213 user->WriteNumeric(998, "%s :resend the file again if the server gave them an error message", user->nick.c_str());
214 user->WriteNumeric(998, "%s :before you added them to your DCCALLOW list.", user->nick.c_str());
215 user->WriteNumeric(998, "%s :DCCALLOW entries will be temporary by default, if you want to add", user->nick.c_str());
216 user->WriteNumeric(998, "%s :them to your DCCALLOW list until you leave IRC, type:", user->nick.c_str());
217 user->WriteNumeric(998, "%s :/DCCALLOW +Brain 0", user->nick.c_str());
218 user->WriteNumeric(998, "%s :To remove the user from your DCCALLOW list, type:", user->nick.c_str());
219 user->WriteNumeric(998, "%s :/DCCALLOW -Brain", user->nick.c_str());
220 user->WriteNumeric(998, "%s :To see the users in your DCCALLOW list, type:", user->nick.c_str());
221 user->WriteNumeric(998, "%s :/DCCALLOW LIST", user->nick.c_str());
222 user->WriteNumeric(998, "%s :NOTE: If the user leaves IRC or changes their nickname", user->nick.c_str());
223 user->WriteNumeric(998, "%s : they will be removed from your DCCALLOW list.", user->nick.c_str());
224 user->WriteNumeric(998, "%s : your DCCALLOW list will be deleted when you leave IRC.", user->nick.c_str());
225 user->WriteNumeric(999, "%s :End of DCCALLOW HELP", user->nick.c_str());
228 void DisplayDCCAllowList(User* user)
230 // display current DCCALLOW list
231 user->WriteNumeric(990, "%s :Users on your DCCALLOW list:", user->nick.c_str());
236 for (dccallowlist::const_iterator c = dl->begin(); c != dl->end(); ++c)
238 user->WriteNumeric(991, "%s %s :%s (%s)", user->nick.c_str(), user->nick.c_str(), c->nickname.c_str(), c->hostmask.c_str());
242 user->WriteNumeric(992, "%s :End of DCCALLOW list", user->nick.c_str());
247 class ModuleDCCAllow : public Module
260 ext = new SimpleExtItem<dccallowlist>("dccallow", this);
261 ServerInstance->Modules->AddService(*ext);
262 ServerInstance->Modules->AddService(cmd);
264 Implementation eventlist[] = { I_OnUserPreMessage, I_OnUserPreNotice, I_OnUserQuit, I_OnUserPostNick, I_OnRehash };
265 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
268 virtual void OnRehash(User* user)
273 virtual void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
275 dccallowlist* udl = ext->get(user);
277 // remove their DCCALLOW list if they have one
280 userlist::iterator it = std::find(ul.begin(), ul.end(), user);
285 // remove them from any DCCALLOW lists
286 // they are currently on
290 virtual void OnUserPostNick(User* user, const std::string &oldnick)
295 virtual ModResult OnUserPreMessage(User* user, void* dest, int target_type, std::string &text, char status, CUList &exempt_list)
297 return OnUserPreNotice(user, dest, target_type, text, status, exempt_list);
300 virtual ModResult OnUserPreNotice(User* user, void* dest, int target_type, std::string &text, char status, CUList &exempt_list)
303 return MOD_RES_PASSTHRU;
305 if (target_type == TYPE_USER)
307 User* u = (User*)dest;
309 /* Always allow a user to dcc themselves (although... why?) */
311 return MOD_RES_PASSTHRU;
313 if ((text.length()) && (text[0] == '\1'))
317 // :jamie!jamie@test-D4457903BA652E0F.silverdream.org PRIVMSG eimaj :DCC SEND m_dnsbl.cpp 3232235786 52650 9676
318 // :jamie!jamie@test-D4457903BA652E0F.silverdream.org PRIVMSG eimaj :VERSION
320 if (strncmp(text.c_str(), "\1DCC ", 5) == 0)
323 if (dl && dl->size())
325 for (dccallowlist::const_iterator iter = dl->begin(); iter != dl->end(); ++iter)
326 if (InspIRCd::Match(user->GetFullHost(), iter->hostmask))
327 return MOD_RES_PASSTHRU;
331 std::stringstream ss(text);
333 std::vector<std::string> tokens;
336 tokens.push_back(buf);
338 irc::string type = tokens[1].c_str();
340 ConfigTag* conftag = ServerInstance->Config->ConfValue("dccallow");
341 bool blockchat = conftag->getBool("blockchat");
345 std::string defaultaction = conftag->getString("action");
346 std::string filename = tokens[2];
349 for (unsigned int i = 0; i < bfl.size(); i++)
351 if (InspIRCd::Match(filename, bfl[i].filemask, ascii_case_insensitive_map))
353 /* We have a matching badfile entry, override whatever the default action is */
354 if (bfl[i].action == "allow")
355 return MOD_RES_PASSTHRU;
364 /* only follow the default action if no badfile matches were found above */
365 if ((!found) && (defaultaction == "allow"))
366 return MOD_RES_PASSTHRU;
368 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());
369 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());
370 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());
373 else if ((type == "CHAT") && (blockchat))
375 user->WriteServ("NOTICE %s :The user %s is not accepting DCC CHAT requests from you.", user->nick.c_str(), u->nick.c_str());
376 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());
377 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());
383 return MOD_RES_PASSTHRU;
388 for (userlist::iterator iter = ul.begin(); iter != ul.end();)
390 User* u = (User*)(*iter);
396 dccallowlist::iterator iter2 = dl->begin();
397 while (iter2 != dl->end())
399 if (iter2->length != 0 && (iter2->set_on + iter2->length) <= ServerInstance->Time())
401 u->WriteNumeric(997, "%s %s :DCCALLOW entry for %s has expired", u->nick.c_str(), u->nick.c_str(), iter2->nickname.c_str());
402 iter2 = dl->erase(iter2);
414 iter = ul.erase(iter);
419 void RemoveNick(User* user)
421 /* Iterate through all DCCALLOW lists and remove user */
422 for (userlist::iterator iter = ul.begin(); iter != ul.end();)
424 User *u = (User*)(*iter);
430 for (dccallowlist::iterator i = dl->begin(); i != dl->end(); ++i)
432 if (i->nickname == user->nick)
435 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());
436 u->WriteNumeric(995, "%s %s :Removed %s from your DCCALLOW list", u->nick.c_str(), u->nick.c_str(), i->nickname.c_str());
446 iter = ul.erase(iter);
451 void RemoveFromUserlist(User *user)
453 // remove user from userlist
454 for (userlist::iterator j = ul.begin(); j != ul.end(); ++j)
456 User* u = (User*)(*j);
468 ConfigTagList tags = ServerInstance->Config->ConfTags("banfile");
469 for (ConfigIter i = tags.first; i != tags.second; ++i)
472 bf.filemask = i->second->getString("pattern");
473 bf.action = i->second->getString("action");
478 virtual ~ModuleDCCAllow()
483 virtual Version GetVersion()
485 return Version("Provides support for the /DCCALLOW command", VF_COMMON | VF_VENDOR);
489 MODULE_INIT(ModuleDCCAllow)