]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_dccallow.cpp
Replace OnRehash() with ReadConfig() that is called on boot, on module load and on...
[user/henk/code/inspircd.git] / src / modules / m_dccallow.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2008 John Brooks <john.brooks@dereferenced.net>
6  *   Copyright (C) 2008 Pippijn van Steenhoven <pip88nl@gmail.com>
7  *   Copyright (C) 2006-2008 Craig Edwards <craigedwards@brainbox.cc>
8  *   Copyright (C) 2007-2008 Robin Burchell <robin+git@viroteck.net>
9  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
10  *   Copyright (C) 2006 Jamie ??? <???@???>
11  *
12  * This file is part of InspIRCd.  InspIRCd is free software: you can
13  * redistribute it and/or modify it under the terms of the GNU General Public
14  * License as published by the Free Software Foundation, version 2.
15  *
16  * This program is distributed in the hope that it will be useful, but WITHOUT
17  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
19  * details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  */
24
25
26 #include "inspircd.h"
27
28 class BannedFileList
29 {
30  public:
31         std::string filemask;
32         std::string action;
33 };
34
35 class DCCAllow
36 {
37  public:
38         std::string nickname;
39         std::string hostmask;
40         time_t set_on;
41         long length;
42
43         DCCAllow() { }
44
45         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 };
47
48 typedef std::vector<User *> userlist;
49 userlist ul;
50 typedef std::vector<DCCAllow> dccallowlist;
51 dccallowlist* dl;
52 typedef std::vector<BannedFileList> bannedfilelist;
53 bannedfilelist bfl;
54 SimpleExtItem<dccallowlist>* ext;
55
56 class CommandDccallow : public Command
57 {
58  public:
59         CommandDccallow(Module* parent) : Command(parent, "DCCALLOW", 0)
60         {
61                 syntax = "{[+|-]<nick> <time>|HELP|LIST}";
62                 /* XXX we need to fix this so it can work with translation stuff (i.e. move +- into a seperate param */
63         }
64
65         CmdResult Handle(const std::vector<std::string> &parameters, User *user)
66         {
67                 /* syntax: DCCALLOW [+|-]<nick> (<time>) */
68                 if (!parameters.size())
69                 {
70                         // display current DCCALLOW list
71                         DisplayDCCAllowList(user);
72                         return CMD_FAILURE;
73                 }
74                 else if (parameters.size() > 0)
75                 {
76                         char action = *parameters[0].c_str();
77
78                         // if they didn't specify an action, this is probably a command
79                         if (action != '+' && action != '-')
80                         {
81                                 if (!strcasecmp(parameters[0].c_str(), "LIST"))
82                                 {
83                                         // list current DCCALLOW list
84                                         DisplayDCCAllowList(user);
85                                         return CMD_FAILURE;
86                                 }
87                                 else if (!strcasecmp(parameters[0].c_str(), "HELP"))
88                                 {
89                                         // display help
90                                         DisplayHelp(user);
91                                         return CMD_FAILURE;
92                                 }
93                                 else
94                                 {
95                                         user->WriteNumeric(998, "%s :DCCALLOW command not understood. For help on DCCALLOW, type /DCCALLOW HELP", user->nick.c_str());
96                                         return CMD_FAILURE;
97                                 }
98                         }
99
100                         std::string nick = parameters[0].substr(1);
101                         User *target = ServerInstance->FindNickOnly(nick);
102
103                         if ((target) && (!IS_SERVER(target)) && (!target->quitting) && (target->registered == REG_ALL))
104                         {
105
106                                 if (action == '-')
107                                 {
108                                         // check if it contains any entries
109                                         dl = ext->get(user);
110                                         if (dl)
111                                         {
112                                                 for (dccallowlist::iterator i = dl->begin(); i != dl->end(); ++i)
113                                                 {
114                                                         // search through list
115                                                         if (i->nickname == target->nick)
116                                                         {
117                                                                 dl->erase(i);
118                                                                 user->WriteNumeric(995, "%s %s :Removed %s from your DCCALLOW list", user->nick.c_str(), user->nick.c_str(), target->nick.c_str());
119                                                                 break;
120                                                         }
121                                                 }
122                                         }
123                                 }
124                                 else if (action == '+')
125                                 {
126                                         if (target == user)
127                                         {
128                                                 user->WriteNumeric(996, "%s %s :You cannot add yourself to your own DCCALLOW list!", user->nick.c_str(), user->nick.c_str());
129                                                 return CMD_FAILURE;
130                                         }
131
132                                         dl = ext->get(user);
133                                         if (!dl)
134                                         {
135                                                 dl = new dccallowlist;
136                                                 ext->set(user, dl);
137                                                 // add this user to the userlist
138                                                 ul.push_back(user);
139                                         }
140
141                                         for (dccallowlist::const_iterator k = dl->begin(); k != dl->end(); ++k)
142                                         {
143                                                 if (k->nickname == target->nick)
144                                                 {
145                                                         user->WriteNumeric(996, "%s %s :%s is already on your DCCALLOW list", user->nick.c_str(), user->nick.c_str(), target->nick.c_str());
146                                                         return CMD_FAILURE;
147                                                 }
148                                         }
149
150                                         std::string mask = target->nick+"!"+target->ident+"@"+target->dhost;
151                                         std::string default_length = ServerInstance->Config->ConfValue("dccallow")->getString("length");
152
153                                         unsigned long length;
154                                         if (parameters.size() < 2)
155                                         {
156                                                 length = InspIRCd::Duration(default_length);
157                                         }
158                                         else if (!atoi(parameters[1].c_str()))
159                                         {
160                                                 length = 0;
161                                         }
162                                         else
163                                         {
164                                                 length = InspIRCd::Duration(parameters[1]);
165                                         }
166
167                                         if (!ServerInstance->IsValidMask(mask))
168                                         {
169                                                 return CMD_FAILURE;
170                                         }
171
172                                         dl->push_back(DCCAllow(target->nick, mask, ServerInstance->Time(), length));
173
174                                         if (length > 0)
175                                         {
176                                                 user->WriteNumeric(993, "%s %s :Added %s to DCCALLOW list for %ld seconds", user->nick.c_str(), user->nick.c_str(), target->nick.c_str(), length);
177                                         }
178                                         else
179                                         {
180                                                 user->WriteNumeric(994, "%s %s :Added %s to DCCALLOW list for this session", user->nick.c_str(), user->nick.c_str(), target->nick.c_str());
181                                         }
182
183                                         /* route it. */
184                                         return CMD_SUCCESS;
185                                 }
186                         }
187                         else
188                         {
189                                 // nick doesn't exist
190                                 user->WriteNumeric(401, "%s %s :No such nick/channel", user->nick.c_str(), nick.c_str());
191                                 return CMD_FAILURE;
192                         }
193                 }
194                 return CMD_FAILURE;
195         }
196
197         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
198         {
199                 return ROUTE_BROADCAST;
200         }
201
202         void DisplayHelp(User* user)
203         {
204                 user->WriteNumeric(998, "%s :DCCALLOW [<+|->nick [time]] [list] [help]", user->nick.c_str());
205                 user->WriteNumeric(998, "%s :You may allow DCCs from specific users by specifying a", user->nick.c_str());
206                 user->WriteNumeric(998, "%s :DCC allow for the user you want to receive DCCs from.", user->nick.c_str());
207                 user->WriteNumeric(998, "%s :For example, to allow the user Brain to send you inspircd.exe", user->nick.c_str());
208                 user->WriteNumeric(998, "%s :you would type:", user->nick.c_str());
209                 user->WriteNumeric(998, "%s :/DCCALLOW +Brain", user->nick.c_str());
210                 user->WriteNumeric(998, "%s :Brain would then be able to send you files. They would have to", user->nick.c_str());
211                 user->WriteNumeric(998, "%s :resend the file again if the server gave them an error message", user->nick.c_str());
212                 user->WriteNumeric(998, "%s :before you added them to your DCCALLOW list.", user->nick.c_str());
213                 user->WriteNumeric(998, "%s :DCCALLOW entries will be temporary by default, if you want to add", user->nick.c_str());
214                 user->WriteNumeric(998, "%s :them to your DCCALLOW list until you leave IRC, type:", user->nick.c_str());
215                 user->WriteNumeric(998, "%s :/DCCALLOW +Brain 0", user->nick.c_str());
216                 user->WriteNumeric(998, "%s :To remove the user from your DCCALLOW list, type:", user->nick.c_str());
217                 user->WriteNumeric(998, "%s :/DCCALLOW -Brain", user->nick.c_str());
218                 user->WriteNumeric(998, "%s :To see the users in your DCCALLOW list, type:", user->nick.c_str());
219                 user->WriteNumeric(998, "%s :/DCCALLOW LIST", user->nick.c_str());
220                 user->WriteNumeric(998, "%s :NOTE: If the user leaves IRC or changes their nickname", user->nick.c_str());
221                 user->WriteNumeric(998, "%s :  they will be removed from your DCCALLOW list.", user->nick.c_str());
222                 user->WriteNumeric(998, "%s :  your DCCALLOW list will be deleted when you leave IRC.", user->nick.c_str());
223                 user->WriteNumeric(999, "%s :End of DCCALLOW HELP", user->nick.c_str());
224         }
225
226         void DisplayDCCAllowList(User* user)
227         {
228                  // display current DCCALLOW list
229                 user->WriteNumeric(990, "%s :Users on your DCCALLOW list:", user->nick.c_str());
230
231                 dl = ext->get(user);
232                 if (dl)
233                 {
234                         for (dccallowlist::const_iterator c = dl->begin(); c != dl->end(); ++c)
235                         {
236                                 user->WriteNumeric(991, "%s %s :%s (%s)", user->nick.c_str(), user->nick.c_str(), c->nickname.c_str(), c->hostmask.c_str());
237                         }
238                 }
239
240                 user->WriteNumeric(992, "%s :End of DCCALLOW list", user->nick.c_str());
241         }
242
243 };
244
245 class ModuleDCCAllow : public Module
246 {
247         CommandDccallow cmd;
248
249  public:
250         ModuleDCCAllow()
251                 : cmd(this)
252         {
253                 ext = NULL;
254         }
255
256         void init() CXX11_OVERRIDE
257         {
258                 ext = new SimpleExtItem<dccallowlist>("dccallow", this);
259                 ServerInstance->Modules->AddService(*ext);
260                 ServerInstance->Modules->AddService(cmd);
261         }
262
263         void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message) CXX11_OVERRIDE
264         {
265                 dccallowlist* udl = ext->get(user);
266
267                 // remove their DCCALLOW list if they have one
268                 if (udl)
269                 {
270                         userlist::iterator it = std::find(ul.begin(), ul.end(), user);
271                         if (it != ul.end())
272                                 ul.erase(it);
273                 }
274
275                 // remove them from any DCCALLOW lists
276                 // they are currently on
277                 RemoveNick(user);
278         }
279
280         void OnUserPostNick(User* user, const std::string &oldnick) CXX11_OVERRIDE
281         {
282                 RemoveNick(user);
283         }
284
285         ModResult OnUserPreMessage(User* user, void* dest, int target_type, std::string& text, char status, CUList& exempt_list, MessageType msgtype) CXX11_OVERRIDE
286         {
287                 if (!IS_LOCAL(user))
288                         return MOD_RES_PASSTHRU;
289
290                 if (target_type == TYPE_USER)
291                 {
292                         User* u = (User*)dest;
293
294                         /* Always allow a user to dcc themselves (although... why?) */
295                         if (user == u)
296                                 return MOD_RES_PASSTHRU;
297
298                         if ((text.length()) && (text[0] == '\1'))
299                         {
300                                 Expire();
301
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
304
305                                 if (strncmp(text.c_str(), "\1DCC ", 5) == 0)
306                                 {
307                                         dl = ext->get(u);
308                                         if (dl && dl->size())
309                                         {
310                                                 for (dccallowlist::const_iterator iter = dl->begin(); iter != dl->end(); ++iter)
311                                                         if (InspIRCd::Match(user->GetFullHost(), iter->hostmask))
312                                                                 return MOD_RES_PASSTHRU;
313                                         }
314
315                                         // tokenize
316                                         std::stringstream ss(text);
317                                         std::string buf;
318                                         std::vector<std::string> tokens;
319
320                                         while (ss >> buf)
321                                                 tokens.push_back(buf);
322
323                                         irc::string type = tokens[1].c_str();
324
325                                         ConfigTag* conftag = ServerInstance->Config->ConfValue("dccallow");
326                                         bool blockchat = conftag->getBool("blockchat");
327
328                                         if (type == "SEND")
329                                         {
330                                                 std::string defaultaction = conftag->getString("action");
331                                                 std::string filename = tokens[2];
332
333                                                 bool found = false;
334                                                 for (unsigned int i = 0; i < bfl.size(); i++)
335                                                 {
336                                                         if (InspIRCd::Match(filename, bfl[i].filemask, ascii_case_insensitive_map))
337                                                         {
338                                                                 /* We have a matching badfile entry, override whatever the default action is */
339                                                                 if (bfl[i].action == "allow")
340                                                                         return MOD_RES_PASSTHRU;
341                                                                 else
342                                                                 {
343                                                                         found = true;
344                                                                         break;
345                                                                 }
346                                                         }
347                                                 }
348
349                                                 /* only follow the default action if no badfile matches were found above */
350                                                 if ((!found) && (defaultaction == "allow"))
351                                                         return MOD_RES_PASSTHRU;
352
353                                                 user->WriteNotice("The user " + u->nick + " is not accepting DCC SENDs from you. Your file " + filename + " was not sent.");
354                                                 u->WriteNotice(user->nick + " (" + user->ident + "@" + user->dhost + ") attempted to send you a file named " + filename + ", which was blocked.");
355                                                 u->WriteNotice("If you trust " + user->nick + " and were expecting this, you can type /DCCALLOW HELP for information on the DCCALLOW system.");
356                                                 return MOD_RES_DENY;
357                                         }
358                                         else if ((type == "CHAT") && (blockchat))
359                                         {
360                                                 user->WriteNotice("The user " + u->nick + " is not accepting DCC CHAT requests from you.");
361                                                 u->WriteNotice(user->nick + " (" + user->ident + "@" + user->dhost + ") attempted to initiate a DCC CHAT session, which was blocked.");
362                                                 u->WriteNotice("If you trust " + user->nick + " and were expecting this, you can type /DCCALLOW HELP for information on the DCCALLOW system.");
363                                                 return MOD_RES_DENY;
364                                         }
365                                 }
366                         }
367                 }
368                 return MOD_RES_PASSTHRU;
369         }
370
371         void Expire()
372         {
373                 for (userlist::iterator iter = ul.begin(); iter != ul.end();)
374                 {
375                         User* u = (User*)(*iter);
376                         dl = ext->get(u);
377                         if (dl)
378                         {
379                                 if (dl->size())
380                                 {
381                                         dccallowlist::iterator iter2 = dl->begin();
382                                         while (iter2 != dl->end())
383                                         {
384                                                 if (iter2->length != 0 && (iter2->set_on + iter2->length) <= ServerInstance->Time())
385                                                 {
386                                                         u->WriteNumeric(997, "%s %s :DCCALLOW entry for %s has expired", u->nick.c_str(), u->nick.c_str(), iter2->nickname.c_str());
387                                                         iter2 = dl->erase(iter2);
388                                                 }
389                                                 else
390                                                 {
391                                                         ++iter2;
392                                                 }
393                                         }
394                                 }
395                                 ++iter;
396                         }
397                         else
398                         {
399                                 iter = ul.erase(iter);
400                         }
401                 }
402         }
403
404         void RemoveNick(User* user)
405         {
406                 /* Iterate through all DCCALLOW lists and remove user */
407                 for (userlist::iterator iter = ul.begin(); iter != ul.end();)
408                 {
409                         User *u = (User*)(*iter);
410                         dl = ext->get(u);
411                         if (dl)
412                         {
413                                 if (dl->size())
414                                 {
415                                         for (dccallowlist::iterator i = dl->begin(); i != dl->end(); ++i)
416                                         {
417                                                 if (i->nickname == user->nick)
418                                                 {
419
420                                                         u->WriteNotice(i->nickname + " left the network or changed their nickname and has been removed from your DCCALLOW list");
421                                                         u->WriteNumeric(995, "%s %s :Removed %s from your DCCALLOW list", u->nick.c_str(), u->nick.c_str(), i->nickname.c_str());
422                                                         dl->erase(i);
423                                                         break;
424                                                 }
425                                         }
426                                 }
427                                 ++iter;
428                         }
429                         else
430                         {
431                                 iter = ul.erase(iter);
432                         }
433                 }
434         }
435
436         void RemoveFromUserlist(User *user)
437         {
438                 // remove user from userlist
439                 for (userlist::iterator j = ul.begin(); j != ul.end(); ++j)
440                 {
441                         User* u = (User*)(*j);
442                         if (u == user)
443                         {
444                                 ul.erase(j);
445                                 break;
446                         }
447                 }
448         }
449
450         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
451         {
452                 bfl.clear();
453                 ConfigTagList tags = ServerInstance->Config->ConfTags("banfile");
454                 for (ConfigIter i = tags.first; i != tags.second; ++i)
455                 {
456                         BannedFileList bf;
457                         bf.filemask = i->second->getString("pattern");
458                         bf.action = i->second->getString("action");
459                         bfl.push_back(bf);
460                 }
461         }
462
463         ~ModuleDCCAllow()
464         {
465                 delete ext;
466         }
467
468         Version GetVersion() CXX11_OVERRIDE
469         {
470                 return Version("Provides support for the /DCCALLOW command", VF_COMMON | VF_VENDOR);
471         }
472 };
473
474 MODULE_INIT(ModuleDCCAllow)