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