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