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>}";
53 CmdResult Handle(const char **parameters, int pcnt, userrec *user)
55 /* syntax: DCCALLOW [+|-]<nick> (<time>) */
59 // display current DCCALLOW list
60 DisplayDCCAllowList(user);
66 char action = *parameters[0];
68 // if they didn't specify an action, this is probably a command
69 if (action != '+' && action != '-')
71 if (!strcasecmp(parameters[0], "LIST"))
73 // list current DCCALLOW list
74 DisplayDCCAllowList(user);
77 else if (!strcasecmp(parameters[0], "HELP"))
85 std::string nick = parameters[0] + 1;
86 userrec *target = ServerInstance->FindNick(nick);
90 ServerInstance->Log(DEBUG, "m_dccallow.so: got target %s and action %c", target->nick, action);
94 user->GetExt("dccallow_list", dl);
95 // check if it contains any entries
100 for (dccallowlist::iterator i = dl->begin(); i != dl->end(); ++i)
102 // search through list
103 if (i->nickname == target->nick)
106 user->WriteServ("995 %s %s :Removed %s from your DCCALLOW list", user->nick, user->nick, target->nick);
115 user->Shrink("dccallow_list");
117 // remove from userlist
118 for (userlist::iterator j = ul.begin(); j != ul.end(); ++j)
120 userrec* u = (userrec*)(*j);
121 if (u->nick == user->nick)
129 else if (action == '+')
131 // fetch current DCCALLOW list
132 user->GetExt("dccallow_list", dl);
133 // they don't have one, create it
136 dl = new dccallowlist;
137 user->Extend(std::string("dccallow_list"), dl);
138 // add this user to the userlist
141 for (dccallowlist::const_iterator k = dl->begin(); k != dl->end(); ++k)
143 if (k->nickname == target->nick)
145 user->WriteServ("996 %s %s :%s is already on your DCCALLOW list", user->nick, user->nick, target->nick);
150 std::string mask = std::string(target->nick)+"!"+std::string(target->ident)+"@"+std::string(target->dhost);
151 const char* default_length = Conf->ReadValue("dccallow", "length", 0).c_str();
154 if (pcnt == 1 || ServerInstance->Duration(parameters[1]) < 1)
156 length = ServerInstance->Duration(default_length);
158 else if (parameters[1] == 0)
164 length = ServerInstance->Duration(parameters[1]);
167 if (!ServerInstance->IsValidMask(mask.c_str()))
172 dl->push_back(DCCAllow(target->nick, mask, ServerInstance->Time(), length));
176 user->WriteServ("993 %s %s :Added %s to DCCALLOW list for %d seconds", user->nick, user->nick, target->nick, length);
180 user->WriteServ("994 %s %s :Added %s to DCCALLOW list for this session", user->nick, user->nick, target->nick);
188 // nick doesn't exist
189 user->WriteServ("401 %s %s :No such nick/channel", user->nick, nick.c_str());
196 void DisplayHelp(userrec* user)
198 user->WriteServ("998 %s :DCCALLOW [<+|->nick [time]] [list] [help]", user->nick);
199 user->WriteServ("998 %s :You may allow DCCs from specific users by specifying a", user->nick);
200 user->WriteServ("998 %s :DCC allow for the user you want to receive DCCs from.", user->nick);
201 user->WriteServ("998 %s :For example, to allow the user Brain to send you inspircd.exe", user->nick);
202 user->WriteServ("998 %s :you would type:", user->nick);
203 user->WriteServ("998 %s :/DCCALLOW +Brain", user->nick);
204 user->WriteServ("998 %s :Brain would then be able to send you files. They would have to", user->nick);
205 user->WriteServ("998 %s :resend the file again if the server gave them an error message", user->nick);
206 user->WriteServ("998 %s :before you added them to your DCCALLOW list.", user->nick);
207 user->WriteServ("998 %s :DCCALLOW entries will be temporary by default, if you want to add", user->nick);
208 user->WriteServ("998 %s :them to your DCCALLOW list until you leave IRC, type:", user->nick);
209 user->WriteServ("998 %s :/DCCALLOW +Brain 0", user->nick);
210 user->WriteServ("998 %s :To remove the user from your DCCALLOW list, type:", user->nick);
211 user->WriteServ("998 %s :/DCCALLOW -Brain", user->nick);
212 user->WriteServ("998 %s :To see the users in your DCCALLOW list, type:", user->nick);
213 user->WriteServ("998 %s :/DCCALLOW LIST", user->nick);
214 user->WriteServ("998 %s :NOTE: If the user leaves IRC or changes their nickname", user->nick);
215 user->WriteServ("998 %s : they will be removed from your DCCALLOW list.", user->nick);
216 user->WriteServ("998 %s : your DCCALLOW list will be deleted when you leave IRC.", user->nick);
217 user->WriteServ("999 %s :End of DCCALLOW HELP", user->nick);
220 void DisplayDCCAllowList(userrec* user)
222 // display current DCCALLOW list
223 user->WriteServ("990 %s :Users on your DCCALLOW list:", user->nick);
224 user->GetExt("dccallow_list", dl);
228 for (dccallowlist::const_iterator c = dl->begin(); c != dl->end(); ++c)
230 user->WriteServ("991 %s %s :%s (%s)", user->nick, user->nick, c->nickname.c_str(), c->hostmask.c_str());
234 user->WriteServ("992 %s :End of DCCALLOW list", user->nick);
239 class ModuleDCCAllow : public Module
241 cmd_dccallow* mycommand;
244 ModuleDCCAllow(InspIRCd* Me)
247 Conf = new ConfigReader(ServerInstance);
248 mycommand = new cmd_dccallow(ServerInstance);
249 ServerInstance->AddCommand(mycommand);
253 void Implements(char* List)
255 List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = List[I_OnUserQuit] = List[I_OnUserPreNick] = List[I_OnRehash] = 1;
258 virtual void OnRehash(const std::string ¶meter)
261 Conf = new ConfigReader(ServerInstance);
264 virtual void OnUserQuit(userrec* user, const std::string &reason)
268 // remove their DCCALLOW list if they have one
269 user->GetExt("dccallow_list", dl);
273 user->Shrink("dccallow_list");
274 RemoveFromUserlist(user);
277 // remove them from any DCCALLOW lists
278 // they are currently on
283 virtual int OnUserPreNick(userrec* user, const std::string &newnick)
289 virtual int OnUserPreMessage(userrec* user, void* dest, int target_type, std::string &text, char status)
291 return OnUserPreNotice(user, dest, target_type, text, status);
294 virtual int OnUserPreNotice(userrec* user, void* dest, int target_type, std::string &text, char status)
298 if (target_type == TYPE_USER)
300 userrec* u = (userrec*)dest;
302 if ((text.length()) && (text[0] == '\1'))
304 // :jamie!jamie@test-D4457903BA652E0F.silverdream.org PRIVMSG eimaj :DCC SEND m_dnsbl.cpp 3232235786 52650 9676
305 // :jamie!jamie@test-D4457903BA652E0F.silverdream.org PRIVMSG eimaj :VERSION
307 if (strncmp(text.c_str(), "\1DCC ", 5) == 0)
309 u->GetExt("dccallow_list", dl);
315 for (dccallowlist::const_iterator iter = dl->begin(); iter != dl->end(); ++iter)
317 if (ServerInstance->MatchText(user->GetFullHost(), iter->hostmask))
326 stringstream ss(text);
328 vector<string> tokens;
331 tokens.push_back(buf);
333 irc::string type = tokens[1].c_str();
334 ServerInstance->Log(DEBUG, "m_dccallow.so: got DCC type %s", type.c_str());
336 const char* blockchat = Conf->ReadValue("dccallow", "blockchat", 0).c_str();
337 ServerInstance->Log(DEBUG, "m_dccallow.so: got blockchat: %s", blockchat);
341 const char* defaultaction = Conf->ReadValue("dccallow", "action", 0).c_str();
342 std::string filename = tokens[2];
344 if (strcmp(defaultaction, "allow") == 0)
349 for (unsigned int i = 0; i < bfl.size(); i++)
351 if (ServerInstance->MatchText(filename, bfl[i].filemask))
353 if (strcmp(bfl[i].action.c_str(), "allow") == 0)
360 if (strcmp(defaultaction, "allow") == 0)
365 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());
366 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());
367 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);
370 else if ((type == "CHAT") && (strcmp(blockchat, "yes") == 0))
372 user->WriteServ("NOTICE %s :The user %s is not accepting DCC CHAT requests from you.", user->nick, u->nick);
373 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);
374 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 for (userlist::iterator iter = ul.begin(); iter != ul.end(); ++iter)
387 userrec* u = (userrec*)(*iter);
388 u->GetExt("dccallow_list", dl);
394 dccallowlist::iterator iter = dl->begin();
395 while (iter != dl->end())
397 if ((iter->set_on + iter->length) <= ServerInstance->Time())
399 u->WriteServ("997 %s %s :DCCALLOW entry for %s has expired", u->nick, u->nick, iter->nickname.c_str());
400 iter = dl->erase(iter);
412 u->Shrink("dccallow_list");
413 RemoveFromUserlist(u);
414 ServerInstance->Log(DEBUG, "m_dccallow.so: UH OH! Couldn't get DCCALLOW list for %s", u->nick);
419 void RemoveNick(userrec* user)
421 /* Iterate through all DCCALLOW lists and remove user */
422 for (userlist::iterator iter = ul.begin(); iter != ul.end(); ++iter)
424 userrec *u = (userrec*)(*iter);
425 u->GetExt("dccallow_list", dl);
431 for (dccallowlist::iterator i = dl->begin(); i != dl->end(); ++i)
433 if (i->nickname == user->nick)
436 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());
437 u->WriteServ("995 %s %s :Removed %s from your DCCALLOW list", u->nick, u->nick, i->nickname.c_str());
447 u->Shrink("dccallow_list");
448 RemoveFromUserlist(u);
453 void RemoveFromUserlist(userrec *user)
455 // remove user from userlist
456 for (userlist::iterator j = ul.begin(); j != ul.end(); ++j)
458 userrec* u = (userrec*)(*j);
459 if (u->nick == user->nick)
470 for (int i = 0; i < Conf->Enumerate("banfile"); i++)
473 std::string fileglob = Conf->ReadValue("banfile", "pattern", i);
474 std::string action = Conf->ReadValue("banfile", "action", i);
475 bf.filemask = fileglob;
482 virtual ~ModuleDCCAllow()
486 virtual Version GetVersion()
488 return Version(1,0,0,0,VF_COMMON,API_VERSION);
492 class ModuleDCCAllowFactory : public ModuleFactory
495 ModuleDCCAllowFactory()
499 ~ModuleDCCAllowFactory()
503 virtual Module * CreateModule(InspIRCd* Me)
505 return new ModuleDCCAllow(Me);
510 extern "C" void * init_module( void )
512 return new ModuleDCCAllowFactory;