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(std::string nick, std::string hm, time_t so, 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(std::string("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);
148 std::string mask = std::string(target->nick)+"!"+std::string(target->ident)+"@"+std::string(target->dhost);
149 std::string default_length = Conf->ReadValue("dccallow", "length", 0).c_str();
154 length = ServerInstance->Duration(default_length.c_str());
156 else if (!atoi(parameters[1]))
162 length = ServerInstance->Duration(parameters[1]);
165 if (!ServerInstance->IsValidMask(mask.c_str()))
170 dl->push_back(DCCAllow(target->nick, mask, ServerInstance->Time(), length));
174 user->WriteServ("993 %s %s :Added %s to DCCALLOW list for %d seconds", user->nick, user->nick, target->nick, length);
178 user->WriteServ("994 %s %s :Added %s to DCCALLOW list for this session", user->nick, user->nick, target->nick);
186 // nick doesn't exist
187 user->WriteServ("401 %s %s :No such nick/channel", user->nick, nick.c_str());
194 void DisplayHelp(userrec* user)
196 user->WriteServ("998 %s :DCCALLOW [<+|->nick [time]] [list] [help]", user->nick);
197 user->WriteServ("998 %s :You may allow DCCs from specific users by specifying a", user->nick);
198 user->WriteServ("998 %s :DCC allow for the user you want to receive DCCs from.", user->nick);
199 user->WriteServ("998 %s :For example, to allow the user Brain to send you inspircd.exe", user->nick);
200 user->WriteServ("998 %s :you would type:", user->nick);
201 user->WriteServ("998 %s :/DCCALLOW +Brain", user->nick);
202 user->WriteServ("998 %s :Brain would then be able to send you files. They would have to", user->nick);
203 user->WriteServ("998 %s :resend the file again if the server gave them an error message", user->nick);
204 user->WriteServ("998 %s :before you added them to your DCCALLOW list.", user->nick);
205 user->WriteServ("998 %s :DCCALLOW entries will be temporary by default, if you want to add", user->nick);
206 user->WriteServ("998 %s :them to your DCCALLOW list until you leave IRC, type:", user->nick);
207 user->WriteServ("998 %s :/DCCALLOW +Brain 0", user->nick);
208 user->WriteServ("998 %s :To remove the user from your DCCALLOW list, type:", user->nick);
209 user->WriteServ("998 %s :/DCCALLOW -Brain", user->nick);
210 user->WriteServ("998 %s :To see the users in your DCCALLOW list, type:", user->nick);
211 user->WriteServ("998 %s :/DCCALLOW LIST", user->nick);
212 user->WriteServ("998 %s :NOTE: If the user leaves IRC or changes their nickname", user->nick);
213 user->WriteServ("998 %s : they will be removed from your DCCALLOW list.", user->nick);
214 user->WriteServ("998 %s : your DCCALLOW list will be deleted when you leave IRC.", user->nick);
215 user->WriteServ("999 %s :End of DCCALLOW HELP", user->nick);
218 void DisplayDCCAllowList(userrec* user)
220 // display current DCCALLOW list
221 user->WriteServ("990 %s :Users on your DCCALLOW list:", user->nick);
222 user->GetExt("dccallow_list", dl);
226 for (dccallowlist::const_iterator c = dl->begin(); c != dl->end(); ++c)
228 user->WriteServ("991 %s %s :%s (%s)", user->nick, user->nick, c->nickname.c_str(), c->hostmask.c_str());
232 user->WriteServ("992 %s :End of DCCALLOW list", user->nick);
237 class ModuleDCCAllow : public Module
239 cmd_dccallow* mycommand;
242 ModuleDCCAllow(InspIRCd* Me)
245 Conf = new ConfigReader(ServerInstance);
246 mycommand = new cmd_dccallow(ServerInstance);
247 ServerInstance->AddCommand(mycommand);
251 void Implements(char* List)
253 List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = List[I_OnUserQuit] = List[I_OnUserPreNick] = List[I_OnRehash] = 1;
256 virtual void OnRehash(const std::string ¶meter)
259 Conf = new ConfigReader(ServerInstance);
262 virtual void OnUserQuit(userrec* user, const std::string &reason)
266 // remove their DCCALLOW list if they have one
267 user->GetExt("dccallow_list", dl);
271 user->Shrink("dccallow_list");
272 RemoveFromUserlist(user);
275 // remove them from any DCCALLOW lists
276 // they are currently on
281 virtual int OnUserPreNick(userrec* user, const std::string &newnick)
287 virtual int OnUserPreMessage(userrec* user, void* dest, int target_type, std::string &text, char status)
289 return OnUserPreNotice(user, dest, target_type, text, status);
292 virtual int OnUserPreNotice(userrec* user, void* dest, int target_type, std::string &text, char status)
296 if (target_type == TYPE_USER)
298 userrec* u = (userrec*)dest;
300 if ((text.length()) && (text[0] == '\1'))
302 // :jamie!jamie@test-D4457903BA652E0F.silverdream.org PRIVMSG eimaj :DCC SEND m_dnsbl.cpp 3232235786 52650 9676
303 // :jamie!jamie@test-D4457903BA652E0F.silverdream.org PRIVMSG eimaj :VERSION
305 if (strncmp(text.c_str(), "\1DCC ", 5) == 0)
307 u->GetExt("dccallow_list", dl);
313 for (dccallowlist::const_iterator iter = dl->begin(); iter != dl->end(); ++iter)
315 if (ServerInstance->MatchText(user->GetFullHost(), iter->hostmask))
324 stringstream ss(text);
326 vector<string> tokens;
329 tokens.push_back(buf);
331 irc::string type = tokens[1].c_str();
332 ServerInstance->Log(DEBUG, "m_dccallow.so: got DCC type %s", type.c_str());
334 bool blockchat = Conf->ReadFlag("dccallow", "blockchat", 0);
338 std::string defaultaction = Conf->ReadValue("dccallow", "action", 0);
339 std::string filename = tokens[2];
341 if (defaultaction == "allow")
346 for (unsigned int i = 0; i < bfl.size(); i++)
348 if (ServerInstance->MatchText(filename, bfl[i].filemask))
350 if (strcmp(bfl[i].action.c_str(), "allow") == 0)
357 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);
367 else if ((type == "CHAT") && (blockchat))
369 user->WriteServ("NOTICE %s :The user %s is not accepting DCC CHAT requests from you.", user->nick, u->nick);
370 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);
371 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);
382 for (userlist::iterator iter = ul.begin(); iter != ul.end(); ++iter)
384 userrec* u = (userrec*)(*iter);
385 u->GetExt("dccallow_list", dl);
391 dccallowlist::iterator iter = dl->begin();
392 while (iter != dl->end())
394 if ((iter->set_on + iter->length) <= ServerInstance->Time())
396 u->WriteServ("997 %s %s :DCCALLOW entry for %s has expired", u->nick, u->nick, iter->nickname.c_str());
397 iter = dl->erase(iter);
408 RemoveFromUserlist(u);
409 ServerInstance->Log(DEBUG, "m_dccallow.so: UH OH! Couldn't get DCCALLOW list for %s", u->nick);
414 void RemoveNick(userrec* user)
416 /* Iterate through all DCCALLOW lists and remove user */
417 for (userlist::iterator iter = ul.begin(); iter != ul.end(); ++iter)
419 userrec *u = (userrec*)(*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(userrec *user)
448 // remove user from userlist
449 for (userlist::iterator j = ul.begin(); j != ul.end(); ++j)
451 userrec* u = (userrec*)(*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,0,0,0,VF_COMMON,API_VERSION);
485 class ModuleDCCAllowFactory : public ModuleFactory
488 ModuleDCCAllowFactory()
492 ~ModuleDCCAllowFactory()
496 virtual Module * CreateModule(InspIRCd* Me)
498 return new ModuleDCCAllow(Me);
503 extern "C" void * init_module( void )
505 return new ModuleDCCAllowFactory;