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