1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
16 /* $ModDesc: Povides support for the /DCCALLOW command */
18 static ConfigReader *Conf;
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) { }
40 typedef std::vector<User *> userlist;
42 typedef std::vector<DCCAllow> dccallowlist;
44 typedef std::vector<BannedFileList> bannedfilelist;
47 class CommandDccallow : public Command
50 CommandDccallow(InspIRCd* Me) : Command(Me, "DCCALLOW", 0, 0)
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 */
57 CmdResult Handle(const char **parameters, int pcnt, User *user)
59 /* syntax: DCCALLOW [+|-]<nick> (<time>) */
62 // display current DCCALLOW list
63 DisplayDCCAllowList(user);
68 char action = *parameters[0];
70 // if they didn't specify an action, this is probably a command
71 if (action != '+' && action != '-')
73 if (!strcasecmp(parameters[0], "LIST"))
75 // list current DCCALLOW list
76 DisplayDCCAllowList(user);
79 else if (!strcasecmp(parameters[0], "HELP"))
87 std::string nick = parameters[0] + 1;
88 User *target = ServerInstance->FindNick(nick);
95 user->GetExt("dccallow_list", dl);
96 // check if it contains any entries
101 for (dccallowlist::iterator i = dl->begin(); i != dl->end(); ++i)
103 // search through list
104 if (i->nickname == target->nick)
107 user->WriteServ("995 %s %s :Removed %s from your DCCALLOW list", user->nick, user->nick, target->nick);
116 user->Shrink("dccallow_list");
118 // remove from userlist
119 for (userlist::iterator j = ul.begin(); j != ul.end(); ++j)
121 User* u = (User*)(*j);
130 else if (action == '+')
132 // fetch current DCCALLOW list
133 user->GetExt("dccallow_list", dl);
134 // they don't have one, create it
137 dl = new dccallowlist;
138 user->Extend("dccallow_list", dl);
139 // add this user to the userlist
142 for (dccallowlist::const_iterator k = dl->begin(); k != dl->end(); ++k)
144 if (k->nickname == target->nick)
146 user->WriteServ("996 %s %s :%s is already on your DCCALLOW list", user->nick, user->nick, target->nick);
149 else if (ServerInstance->MatchText(user->GetFullHost(), k->hostmask))
151 user->WriteServ("996 %s %s :You cannot add yourself to your own DCCALLOW list!", user->nick, user->nick);
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);
162 length = ServerInstance->Duration(default_length);
164 else if (!atoi(parameters[1]))
170 length = ServerInstance->Duration(parameters[1]);
173 if (!ServerInstance->IsValidMask(mask.c_str()))
178 dl->push_back(DCCAllow(target->nick, mask, ServerInstance->Time(), length));
182 user->WriteServ("993 %s %s :Added %s to DCCALLOW list for %d seconds", user->nick, user->nick, target->nick, length);
186 user->WriteServ("994 %s %s :Added %s to DCCALLOW list for this session", user->nick, user->nick, target->nick);
195 // nick doesn't exist
196 user->WriteServ("401 %s %s :No such nick/channel", user->nick, nick.c_str());
203 void DisplayHelp(User* user)
205 user->WriteServ("998 %s :DCCALLOW [<+|->nick [time]] [list] [help]", user->nick);
206 user->WriteServ("998 %s :You may allow DCCs from specific users by specifying a", user->nick);
207 user->WriteServ("998 %s :DCC allow for the user you want to receive DCCs from.", user->nick);
208 user->WriteServ("998 %s :For example, to allow the user Brain to send you inspircd.exe", user->nick);
209 user->WriteServ("998 %s :you would type:", user->nick);
210 user->WriteServ("998 %s :/DCCALLOW +Brain", user->nick);
211 user->WriteServ("998 %s :Brain would then be able to send you files. They would have to", user->nick);
212 user->WriteServ("998 %s :resend the file again if the server gave them an error message", user->nick);
213 user->WriteServ("998 %s :before you added them to your DCCALLOW list.", user->nick);
214 user->WriteServ("998 %s :DCCALLOW entries will be temporary by default, if you want to add", user->nick);
215 user->WriteServ("998 %s :them to your DCCALLOW list until you leave IRC, type:", user->nick);
216 user->WriteServ("998 %s :/DCCALLOW +Brain 0", user->nick);
217 user->WriteServ("998 %s :To remove the user from your DCCALLOW list, type:", user->nick);
218 user->WriteServ("998 %s :/DCCALLOW -Brain", user->nick);
219 user->WriteServ("998 %s :To see the users in your DCCALLOW list, type:", user->nick);
220 user->WriteServ("998 %s :/DCCALLOW LIST", user->nick);
221 user->WriteServ("998 %s :NOTE: If the user leaves IRC or changes their nickname", user->nick);
222 user->WriteServ("998 %s : they will be removed from your DCCALLOW list.", user->nick);
223 user->WriteServ("998 %s : your DCCALLOW list will be deleted when you leave IRC.", user->nick);
224 user->WriteServ("999 %s :End of DCCALLOW HELP", user->nick);
227 void DisplayDCCAllowList(User* user)
229 // display current DCCALLOW list
230 user->WriteServ("990 %s :Users on your DCCALLOW list:", user->nick);
231 user->GetExt("dccallow_list", dl);
235 for (dccallowlist::const_iterator c = dl->begin(); c != dl->end(); ++c)
237 user->WriteServ("991 %s %s :%s (%s)", user->nick, user->nick, c->nickname.c_str(), c->hostmask.c_str());
241 user->WriteServ("992 %s :End of DCCALLOW list", user->nick);
246 class ModuleDCCAllow : public Module
248 CommandDccallow* mycommand;
251 ModuleDCCAllow(InspIRCd* Me)
254 Conf = new ConfigReader(ServerInstance);
255 mycommand = new CommandDccallow(ServerInstance);
256 ServerInstance->AddCommand(mycommand);
258 Implementation eventlist[] = { I_OnUserPreMessage, I_OnUserPreNotice, I_OnUserQuit, I_OnUserPreNick, I_OnRehash };
259 ServerInstance->Modules->Attach(eventlist, this, 5);
263 virtual void OnRehash(User* user, const std::string ¶meter)
266 Conf = new ConfigReader(ServerInstance);
269 virtual void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
273 // remove their DCCALLOW list if they have one
274 user->GetExt("dccallow_list", dl);
278 user->Shrink("dccallow_list");
279 RemoveFromUserlist(user);
282 // remove them from any DCCALLOW lists
283 // they are currently on
288 virtual int OnUserPreNick(User* user, const std::string &newnick)
294 virtual int OnUserPreMessage(User* user, void* dest, int target_type, std::string &text, char status, CUList &exempt_list)
296 return OnUserPreNotice(user, dest, target_type, text, status, exempt_list);
299 virtual int OnUserPreNotice(User* user, void* dest, int target_type, std::string &text, char status, CUList &exempt_list)
304 if (target_type == TYPE_USER)
306 User* u = (User*)dest;
308 /* Always allow a user to dcc themselves (although... why?) */
312 if ((text.length()) && (text[0] == '\1'))
316 // :jamie!jamie@test-D4457903BA652E0F.silverdream.org PRIVMSG eimaj :DCC SEND m_dnsbl.cpp 3232235786 52650 9676
317 // :jamie!jamie@test-D4457903BA652E0F.silverdream.org PRIVMSG eimaj :VERSION
319 if (strncmp(text.c_str(), "\1DCC ", 5) == 0)
321 u->GetExt("dccallow_list", dl);
323 if (dl && dl->size())
325 for (dccallowlist::const_iterator iter = dl->begin(); iter != dl->end(); ++iter)
326 if (ServerInstance->MatchText(user->GetFullHost(), iter->hostmask))
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 bool blockchat = Conf->ReadFlag("dccallow", "blockchat", 0);
344 std::string defaultaction = Conf->ReadValue("dccallow", "action", 0);
345 std::string filename = tokens[2];
347 if (defaultaction == "allow")
350 for (unsigned int i = 0; i < bfl.size(); i++)
352 if (ServerInstance->MatchText(filename, bfl[i].filemask))
354 if (bfl[i].action == "allow")
359 if (defaultaction == "allow")
362 user->WriteServ("NOTICE %s :The user %s is not accepting DCC SENDs from you. Your file %s was not sent.", user->nick, u->nick, filename.c_str());
363 u->WriteServ("NOTICE %s :%s (%s@%s) attempted to send you a file named %s, which was blocked.", u->nick, user->nick, user->ident, user->dhost, filename.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, user->nick);
368 else if ((type == "CHAT") && (blockchat))
370 user->WriteServ("NOTICE %s :The user %s is not accepting DCC CHAT requests from you.", user->nick, u->nick);
371 u->WriteServ("NOTICE %s :%s (%s@%s) attempted to initiate a DCC CHAT session, which was blocked.", u->nick, user->nick, user->ident, user->dhost);
372 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, user->nick);
383 for (userlist::iterator iter = ul.begin(); iter != ul.end(); ++iter)
385 User* u = (User*)(*iter);
386 u->GetExt("dccallow_list", dl);
392 dccallowlist::iterator iter = dl->begin();
393 while (iter != dl->end())
395 if ((iter->set_on + iter->length) <= ServerInstance->Time())
397 u->WriteServ("997 %s %s :DCCALLOW entry for %s has expired", u->nick, u->nick, iter->nickname.c_str());
398 iter = dl->erase(iter);
409 RemoveFromUserlist(u);
414 void RemoveNick(User* user)
416 /* Iterate through all DCCALLOW lists and remove user */
417 for (userlist::iterator iter = ul.begin(); iter != ul.end(); ++iter)
419 User *u = (User*)(*iter);
420 u->GetExt("dccallow_list", dl);
426 for (dccallowlist::iterator i = dl->begin(); i != dl->end(); ++i)
428 if (i->nickname == user->nick)
431 u->WriteServ("NOTICE %s :%s left the network or changed their nickname and has been removed from your DCCALLOW list", u->nick, i->nickname.c_str());
432 u->WriteServ("995 %s %s :Removed %s from your DCCALLOW list", u->nick, u->nick, i->nickname.c_str());
441 RemoveFromUserlist(u);
446 void RemoveFromUserlist(User *user)
448 // remove user from userlist
449 for (userlist::iterator j = ul.begin(); j != ul.end(); ++j)
451 User* u = (User*)(*j);
463 for (int i = 0; i < Conf->Enumerate("banfile"); i++)
466 std::string fileglob = Conf->ReadValue("banfile", "pattern", i);
467 std::string action = Conf->ReadValue("banfile", "action", i);
468 bf.filemask = fileglob;
475 virtual ~ModuleDCCAllow()
479 virtual Version GetVersion()
481 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
485 MODULE_INIT(ModuleDCCAllow)