1 /* m_dccallow - Jamie Penman-Smithson <jamie@silverdream.org> - September 2006 */
13 /* $ModDesc: Povides support for the /DCCALLOW command */
15 static ConfigReader *Conf;
34 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) { }
37 typedef std::vector<userrec *> userlist;
39 typedef std::vector<DCCAllow> dccallowlist;
41 typedef std::vector<BannedFileList> bannedfilelist;
44 class cmd_dccallow : public command_t
47 cmd_dccallow(InspIRCd* Me) : command_t(Me, "DCCALLOW", 0, 0)
49 this->source = "m_dccallow.so";
50 syntax = "{[+|-]<nick> <time>|HELP|LIST}";
53 CmdResult Handle(const char **parameters, int pcnt, userrec *user)
55 /* syntax: DCCALLOW [+|-]<nick> (<time>) */
58 // display current DCCALLOW list
59 DisplayDCCAllowList(user);
64 char action = *parameters[0];
66 // if they didn't specify an action, this is probably a command
67 if (action != '+' && action != '-')
69 if (!strcasecmp(parameters[0], "LIST"))
71 // list current DCCALLOW list
72 DisplayDCCAllowList(user);
75 else if (!strcasecmp(parameters[0], "HELP"))
83 std::string nick = parameters[0] + 1;
84 userrec *target = ServerInstance->FindNick(nick);
88 ServerInstance->Log(DEBUG, "m_dccallow.so: got target %s and action %c", target->nick, action);
92 user->GetExt("dccallow_list", dl);
93 // check if it contains any entries
98 for (dccallowlist::iterator i = dl->begin(); i != dl->end(); ++i)
100 // search through list
101 if (i->nickname == target->nick)
104 user->WriteServ("995 %s %s :Removed %s from your DCCALLOW list", user->nick, user->nick, target->nick);
113 user->Shrink("dccallow_list");
115 // remove from userlist
116 for (userlist::iterator j = ul.begin(); j != ul.end(); ++j)
118 userrec* u = (userrec*)(*j);
127 else if (action == '+')
129 // fetch current DCCALLOW list
130 user->GetExt("dccallow_list", dl);
131 // they don't have one, create it
134 dl = new dccallowlist;
135 user->Extend("dccallow_list", dl);
136 // add this user to the userlist
139 for (dccallowlist::const_iterator k = dl->begin(); k != dl->end(); ++k)
141 if (k->nickname == target->nick)
143 user->WriteServ("996 %s %s :%s is already on your DCCALLOW list", user->nick, user->nick, target->nick);
146 else if (ServerInstance->MatchText(user->GetFullHost(), k->hostmask))
148 user->WriteServ("996 %s %s :You cannot add yourself to your own DCCALLOW list!", user->nick, user->nick);
153 std::string mask = std::string(target->nick)+"!"+std::string(target->ident)+"@"+std::string(target->dhost);
154 std::string default_length = Conf->ReadValue("dccallow", "length", 0);
159 length = ServerInstance->Duration(default_length.c_str());
161 else if (!atoi(parameters[1]))
167 length = ServerInstance->Duration(parameters[1]);
170 if (!ServerInstance->IsValidMask(mask.c_str()))
175 dl->push_back(DCCAllow(target->nick, mask, ServerInstance->Time(), length));
179 user->WriteServ("993 %s %s :Added %s to DCCALLOW list for %d seconds", user->nick, user->nick, target->nick, length);
183 user->WriteServ("994 %s %s :Added %s to DCCALLOW list for this session", user->nick, user->nick, target->nick);
191 // nick doesn't exist
192 user->WriteServ("401 %s %s :No such nick/channel", user->nick, nick.c_str());
199 void DisplayHelp(userrec* user)
201 user->WriteServ("998 %s :DCCALLOW [<+|->nick [time]] [list] [help]", user->nick);
202 user->WriteServ("998 %s :You may allow DCCs from specific users by specifying a", user->nick);
203 user->WriteServ("998 %s :DCC allow for the user you want to receive DCCs from.", user->nick);
204 user->WriteServ("998 %s :For example, to allow the user Brain to send you inspircd.exe", user->nick);
205 user->WriteServ("998 %s :you would type:", user->nick);
206 user->WriteServ("998 %s :/DCCALLOW +Brain", user->nick);
207 user->WriteServ("998 %s :Brain would then be able to send you files. They would have to", user->nick);
208 user->WriteServ("998 %s :resend the file again if the server gave them an error message", user->nick);
209 user->WriteServ("998 %s :before you added them to your DCCALLOW list.", user->nick);
210 user->WriteServ("998 %s :DCCALLOW entries will be temporary by default, if you want to add", user->nick);
211 user->WriteServ("998 %s :them to your DCCALLOW list until you leave IRC, type:", user->nick);
212 user->WriteServ("998 %s :/DCCALLOW +Brain 0", user->nick);
213 user->WriteServ("998 %s :To remove the user from your DCCALLOW list, type:", user->nick);
214 user->WriteServ("998 %s :/DCCALLOW -Brain", user->nick);
215 user->WriteServ("998 %s :To see the users in your DCCALLOW list, type:", user->nick);
216 user->WriteServ("998 %s :/DCCALLOW LIST", user->nick);
217 user->WriteServ("998 %s :NOTE: If the user leaves IRC or changes their nickname", user->nick);
218 user->WriteServ("998 %s : they will be removed from your DCCALLOW list.", user->nick);
219 user->WriteServ("998 %s : your DCCALLOW list will be deleted when you leave IRC.", user->nick);
220 user->WriteServ("999 %s :End of DCCALLOW HELP", user->nick);
223 void DisplayDCCAllowList(userrec* user)
225 // display current DCCALLOW list
226 user->WriteServ("990 %s :Users on your DCCALLOW list:", user->nick);
227 user->GetExt("dccallow_list", dl);
231 for (dccallowlist::const_iterator c = dl->begin(); c != dl->end(); ++c)
233 user->WriteServ("991 %s %s :%s (%s)", user->nick, user->nick, c->nickname.c_str(), c->hostmask.c_str());
237 user->WriteServ("992 %s :End of DCCALLOW list", user->nick);
242 class ModuleDCCAllow : public Module
244 cmd_dccallow* mycommand;
247 ModuleDCCAllow(InspIRCd* Me)
250 Conf = new ConfigReader(ServerInstance);
251 mycommand = new cmd_dccallow(ServerInstance);
252 ServerInstance->AddCommand(mycommand);
256 void Implements(char* List)
258 List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = List[I_OnUserQuit] = List[I_OnUserPreNick] = List[I_OnRehash] = 1;
261 virtual void OnRehash(const std::string ¶meter)
264 Conf = new ConfigReader(ServerInstance);
267 virtual void OnUserQuit(userrec* user, const std::string &reason)
271 // remove their DCCALLOW list if they have one
272 user->GetExt("dccallow_list", dl);
276 user->Shrink("dccallow_list");
277 RemoveFromUserlist(user);
280 // remove them from any DCCALLOW lists
281 // they are currently on
286 virtual int OnUserPreNick(userrec* user, const std::string &newnick)
292 virtual int OnUserPreMessage(userrec* user, void* dest, int target_type, std::string &text, char status, CUList &exempt_list)
294 return OnUserPreNotice(user, dest, target_type, text, status, exempt_list);
297 virtual int OnUserPreNotice(userrec* user, void* dest, int target_type, std::string &text, char status, CUList &exempt_list)
301 if (target_type == TYPE_USER)
303 userrec* u = (userrec*)dest;
305 /* Always allow a user to dcc themselves (although... why?) */
309 if ((text.length()) && (text[0] == '\1'))
311 // :jamie!jamie@test-D4457903BA652E0F.silverdream.org PRIVMSG eimaj :DCC SEND m_dnsbl.cpp 3232235786 52650 9676
312 // :jamie!jamie@test-D4457903BA652E0F.silverdream.org PRIVMSG eimaj :VERSION
314 if (strncmp(text.c_str(), "\1DCC ", 5) == 0)
316 u->GetExt("dccallow_list", dl);
322 for (dccallowlist::const_iterator iter = dl->begin(); iter != dl->end(); ++iter)
324 if (ServerInstance->MatchText(user->GetFullHost(), iter->hostmask))
333 stringstream ss(text);
335 vector<string> tokens;
338 tokens.push_back(buf);
340 irc::string type = tokens[1].c_str();
341 ServerInstance->Log(DEBUG, "m_dccallow.so: got DCC type %s", type.c_str());
343 bool blockchat = Conf->ReadFlag("dccallow", "blockchat", 0);
347 std::string defaultaction = Conf->ReadValue("dccallow", "action", 0);
348 std::string filename = tokens[2];
350 if (defaultaction == "allow")
355 for (unsigned int i = 0; i < bfl.size(); i++)
357 if (ServerInstance->MatchText(filename, bfl[i].filemask))
359 if (strcmp(bfl[i].action.c_str(), "allow") == 0)
366 if (defaultaction == "allow")
371 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());
372 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());
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, user->nick);
376 else if ((type == "CHAT") && (blockchat))
378 user->WriteServ("NOTICE %s :The user %s is not accepting DCC CHAT requests from you.", user->nick, u->nick);
379 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);
380 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);
391 for (userlist::iterator iter = ul.begin(); iter != ul.end(); ++iter)
393 userrec* u = (userrec*)(*iter);
394 u->GetExt("dccallow_list", dl);
400 dccallowlist::iterator iter = dl->begin();
401 while (iter != dl->end())
403 if ((iter->set_on + iter->length) <= ServerInstance->Time())
405 u->WriteServ("997 %s %s :DCCALLOW entry for %s has expired", u->nick, u->nick, iter->nickname.c_str());
406 iter = dl->erase(iter);
417 RemoveFromUserlist(u);
418 ServerInstance->Log(DEBUG, "m_dccallow.so: UH OH! Couldn't get DCCALLOW list for %s", u->nick);
423 void RemoveNick(userrec* user)
425 /* Iterate through all DCCALLOW lists and remove user */
426 for (userlist::iterator iter = ul.begin(); iter != ul.end(); ++iter)
428 userrec *u = (userrec*)(*iter);
429 u->GetExt("dccallow_list", dl);
435 for (dccallowlist::iterator i = dl->begin(); i != dl->end(); ++i)
437 if (i->nickname == user->nick)
440 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());
441 u->WriteServ("995 %s %s :Removed %s from your DCCALLOW list", u->nick, u->nick, i->nickname.c_str());
450 RemoveFromUserlist(u);
455 void RemoveFromUserlist(userrec *user)
457 // remove user from userlist
458 for (userlist::iterator j = ul.begin(); j != ul.end(); ++j)
460 userrec* u = (userrec*)(*j);
472 for (int i = 0; i < Conf->Enumerate("banfile"); i++)
475 std::string fileglob = Conf->ReadValue("banfile", "pattern", i);
476 std::string action = Conf->ReadValue("banfile", "action", i);
477 bf.filemask = fileglob;
484 virtual ~ModuleDCCAllow()
488 virtual Version GetVersion()
490 return Version(1,1,0,0,VF_COMMON,API_VERSION);
494 class ModuleDCCAllowFactory : public ModuleFactory
497 ModuleDCCAllowFactory()
501 ~ModuleDCCAllowFactory()
505 virtual Module * CreateModule(InspIRCd* Me)
507 return new ModuleDCCAllow(Me);
512 extern "C" void * init_module( void )
514 return new ModuleDCCAllowFactory;