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