1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 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 * ---------------------------------------------------
22 /* $ModDesc: Povides support for the /DCCALLOW command */
24 static ConfigReader *Conf;
43 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 typedef std::vector<userrec *> userlist;
48 typedef std::vector<DCCAllow> dccallowlist;
50 typedef std::vector<BannedFileList> bannedfilelist;
53 class cmd_dccallow : public command_t
56 cmd_dccallow(InspIRCd* Me) : command_t(Me, "DCCALLOW", 0, 0)
58 this->source = "m_dccallow.so";
59 syntax = "{[+|-]<nick> <time>|HELP|LIST}";
62 CmdResult Handle(const char **parameters, int pcnt, userrec *user)
64 /* syntax: DCCALLOW [+|-]<nick> (<time>) */
67 // display current DCCALLOW list
68 DisplayDCCAllowList(user);
73 char action = *parameters[0];
75 // if they didn't specify an action, this is probably a command
76 if (action != '+' && action != '-')
78 if (!strcasecmp(parameters[0], "LIST"))
80 // list current DCCALLOW list
81 DisplayDCCAllowList(user);
84 else if (!strcasecmp(parameters[0], "HELP"))
92 std::string nick = parameters[0] + 1;
93 userrec *target = ServerInstance->FindNick(nick);
97 ServerInstance->Log(DEBUG, "m_dccallow.so: got target %s and action %c", target->nick, action);
101 user->GetExt("dccallow_list", dl);
102 // check if it contains any entries
107 for (dccallowlist::iterator i = dl->begin(); i != dl->end(); ++i)
109 // search through list
110 if (i->nickname == target->nick)
113 user->WriteServ("995 %s %s :Removed %s from your DCCALLOW list", user->nick, user->nick, target->nick);
122 user->Shrink("dccallow_list");
124 // remove from userlist
125 for (userlist::iterator j = ul.begin(); j != ul.end(); ++j)
127 userrec* u = (userrec*)(*j);
136 else if (action == '+')
138 // fetch current DCCALLOW list
139 user->GetExt("dccallow_list", dl);
140 // they don't have one, create it
143 dl = new dccallowlist;
144 user->Extend("dccallow_list", dl);
145 // add this user to the userlist
148 for (dccallowlist::const_iterator k = dl->begin(); k != dl->end(); ++k)
150 if (k->nickname == target->nick)
152 user->WriteServ("996 %s %s :%s is already on your DCCALLOW list", user->nick, user->nick, target->nick);
155 else if (ServerInstance->MatchText(user->GetFullHost(), k->hostmask))
157 user->WriteServ("996 %s %s :You cannot add yourself to your own DCCALLOW list!", user->nick, user->nick);
162 std::string mask = std::string(target->nick)+"!"+std::string(target->ident)+"@"+std::string(target->dhost);
163 std::string default_length = Conf->ReadValue("dccallow", "length", 0);
168 length = ServerInstance->Duration(default_length.c_str());
170 else if (!atoi(parameters[1]))
176 length = ServerInstance->Duration(parameters[1]);
179 if (!ServerInstance->IsValidMask(mask.c_str()))
184 dl->push_back(DCCAllow(target->nick, mask, ServerInstance->Time(), length));
188 user->WriteServ("993 %s %s :Added %s to DCCALLOW list for %d seconds", user->nick, user->nick, target->nick, length);
192 user->WriteServ("994 %s %s :Added %s to DCCALLOW list for this session", user->nick, user->nick, target->nick);
200 // nick doesn't exist
201 user->WriteServ("401 %s %s :No such nick/channel", user->nick, nick.c_str());
208 void DisplayHelp(userrec* user)
210 user->WriteServ("998 %s :DCCALLOW [<+|->nick [time]] [list] [help]", user->nick);
211 user->WriteServ("998 %s :You may allow DCCs from specific users by specifying a", user->nick);
212 user->WriteServ("998 %s :DCC allow for the user you want to receive DCCs from.", user->nick);
213 user->WriteServ("998 %s :For example, to allow the user Brain to send you inspircd.exe", user->nick);
214 user->WriteServ("998 %s :you would type:", user->nick);
215 user->WriteServ("998 %s :/DCCALLOW +Brain", user->nick);
216 user->WriteServ("998 %s :Brain would then be able to send you files. They would have to", user->nick);
217 user->WriteServ("998 %s :resend the file again if the server gave them an error message", user->nick);
218 user->WriteServ("998 %s :before you added them to your DCCALLOW list.", user->nick);
219 user->WriteServ("998 %s :DCCALLOW entries will be temporary by default, if you want to add", user->nick);
220 user->WriteServ("998 %s :them to your DCCALLOW list until you leave IRC, type:", user->nick);
221 user->WriteServ("998 %s :/DCCALLOW +Brain 0", user->nick);
222 user->WriteServ("998 %s :To remove the user from your DCCALLOW list, type:", user->nick);
223 user->WriteServ("998 %s :/DCCALLOW -Brain", user->nick);
224 user->WriteServ("998 %s :To see the users in your DCCALLOW list, type:", user->nick);
225 user->WriteServ("998 %s :/DCCALLOW LIST", user->nick);
226 user->WriteServ("998 %s :NOTE: If the user leaves IRC or changes their nickname", user->nick);
227 user->WriteServ("998 %s : they will be removed from your DCCALLOW list.", user->nick);
228 user->WriteServ("998 %s : your DCCALLOW list will be deleted when you leave IRC.", user->nick);
229 user->WriteServ("999 %s :End of DCCALLOW HELP", user->nick);
232 void DisplayDCCAllowList(userrec* user)
234 // display current DCCALLOW list
235 user->WriteServ("990 %s :Users on your DCCALLOW list:", user->nick);
236 user->GetExt("dccallow_list", dl);
240 for (dccallowlist::const_iterator c = dl->begin(); c != dl->end(); ++c)
242 user->WriteServ("991 %s %s :%s (%s)", user->nick, user->nick, c->nickname.c_str(), c->hostmask.c_str());
246 user->WriteServ("992 %s :End of DCCALLOW list", user->nick);
251 class ModuleDCCAllow : public Module
253 cmd_dccallow* mycommand;
256 ModuleDCCAllow(InspIRCd* Me)
259 Conf = new ConfigReader(ServerInstance);
260 mycommand = new cmd_dccallow(ServerInstance);
261 ServerInstance->AddCommand(mycommand);
265 void Implements(char* List)
267 List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = List[I_OnUserQuit] = List[I_OnUserPreNick] = List[I_OnRehash] = 1;
270 virtual void OnRehash(const std::string ¶meter)
273 Conf = new ConfigReader(ServerInstance);
276 virtual void OnUserQuit(userrec* user, const std::string &reason)
280 // remove their DCCALLOW list if they have one
281 user->GetExt("dccallow_list", dl);
285 user->Shrink("dccallow_list");
286 RemoveFromUserlist(user);
289 // remove them from any DCCALLOW lists
290 // they are currently on
295 virtual int OnUserPreNick(userrec* user, const std::string &newnick)
301 virtual int OnUserPreMessage(userrec* user, void* dest, int target_type, std::string &text, char status, CUList &exempt_list)
303 return OnUserPreNotice(user, dest, target_type, text, status, exempt_list);
306 virtual int OnUserPreNotice(userrec* user, void* dest, int target_type, std::string &text, char status, CUList &exempt_list)
310 if (target_type == TYPE_USER)
312 userrec* u = (userrec*)dest;
314 /* Always allow a user to dcc themselves (although... why?) */
318 if ((text.length()) && (text[0] == '\1'))
320 // :jamie!jamie@test-D4457903BA652E0F.silverdream.org PRIVMSG eimaj :DCC SEND m_dnsbl.cpp 3232235786 52650 9676
321 // :jamie!jamie@test-D4457903BA652E0F.silverdream.org PRIVMSG eimaj :VERSION
323 if (strncmp(text.c_str(), "\1DCC ", 5) == 0)
325 u->GetExt("dccallow_list", dl);
331 for (dccallowlist::const_iterator iter = dl->begin(); iter != dl->end(); ++iter)
333 if (ServerInstance->MatchText(user->GetFullHost(), iter->hostmask))
342 stringstream ss(text);
344 vector<string> tokens;
347 tokens.push_back(buf);
349 irc::string type = tokens[1].c_str();
350 ServerInstance->Log(DEBUG, "m_dccallow.so: got DCC type %s", type.c_str());
352 bool blockchat = Conf->ReadFlag("dccallow", "blockchat", 0);
356 std::string defaultaction = Conf->ReadValue("dccallow", "action", 0);
357 std::string filename = tokens[2];
359 if (defaultaction == "allow")
364 for (unsigned int i = 0; i < bfl.size(); i++)
366 if (ServerInstance->MatchText(filename, bfl[i].filemask))
368 if (strcmp(bfl[i].action.c_str(), "allow") == 0)
375 if (defaultaction == "allow")
380 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());
381 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());
382 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);
385 else if ((type == "CHAT") && (blockchat))
387 user->WriteServ("NOTICE %s :The user %s is not accepting DCC CHAT requests from you.", user->nick, u->nick);
388 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);
389 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);
400 for (userlist::iterator iter = ul.begin(); iter != ul.end(); ++iter)
402 userrec* u = (userrec*)(*iter);
403 u->GetExt("dccallow_list", dl);
409 dccallowlist::iterator iter = dl->begin();
410 while (iter != dl->end())
412 if ((iter->set_on + iter->length) <= ServerInstance->Time())
414 u->WriteServ("997 %s %s :DCCALLOW entry for %s has expired", u->nick, u->nick, iter->nickname.c_str());
415 iter = dl->erase(iter);
426 RemoveFromUserlist(u);
427 ServerInstance->Log(DEBUG, "m_dccallow.so: UH OH! Couldn't get DCCALLOW list for %s", u->nick);
432 void RemoveNick(userrec* user)
434 /* Iterate through all DCCALLOW lists and remove user */
435 for (userlist::iterator iter = ul.begin(); iter != ul.end(); ++iter)
437 userrec *u = (userrec*)(*iter);
438 u->GetExt("dccallow_list", dl);
444 for (dccallowlist::iterator i = dl->begin(); i != dl->end(); ++i)
446 if (i->nickname == user->nick)
449 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());
450 u->WriteServ("995 %s %s :Removed %s from your DCCALLOW list", u->nick, u->nick, i->nickname.c_str());
459 RemoveFromUserlist(u);
464 void RemoveFromUserlist(userrec *user)
466 // remove user from userlist
467 for (userlist::iterator j = ul.begin(); j != ul.end(); ++j)
469 userrec* u = (userrec*)(*j);
481 for (int i = 0; i < Conf->Enumerate("banfile"); i++)
484 std::string fileglob = Conf->ReadValue("banfile", "pattern", i);
485 std::string action = Conf->ReadValue("banfile", "action", i);
486 bf.filemask = fileglob;
493 virtual ~ModuleDCCAllow()
497 virtual Version GetVersion()
499 return Version(1,1,0,0,VF_COMMON,API_VERSION);
503 class ModuleDCCAllowFactory : public ModuleFactory
506 ModuleDCCAllowFactory()
510 ~ModuleDCCAllowFactory()
514 virtual Module * CreateModule(InspIRCd* Me)
516 return new ModuleDCCAllow(Me);
521 extern "C" void * init_module( void )
523 return new ModuleDCCAllowFactory;