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