]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_dccallow.cpp
In the grand tradition of huge fucking commits:
[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
16 /* $ModDesc: Povides support for the /DCCALLOW command */
17
18 static ConfigReader *Conf;
19
20 class BannedFileList
21 {
22  public:
23         std::string filemask;
24         std::string action;
25 };
26
27 class DCCAllow
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 cmd_dccallow : public Command
48 {
49  public:
50         cmd_dccallow(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 **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                                         user->GetExt("dccallow_list", dl);
96                                         // check if it contains any entries
97                                         if (dl)
98                                         {
99                                                 if (dl->size())
100                                                 {
101                                                         for (dccallowlist::iterator i = dl->begin(); i != dl->end(); ++i)
102                                                         {
103                                                                 // search through list
104                                                                 if (i->nickname == target->nick)
105                                                                 {
106                                                                         dl->erase(i);
107                                                                         user->WriteServ("995 %s %s :Removed %s from your DCCALLOW list", user->nick, user->nick, target->nick);
108                                                                         break;
109                                                                 }
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                                         // fetch current DCCALLOW list
133                                         user->GetExt("dccallow_list", dl);
134                                         // they don't have one, create it
135                                         if (!dl)
136                                         {
137                                                 dl = new dccallowlist;
138                                                 user->Extend("dccallow_list", dl);
139                                                 // add this user to the userlist
140                                                 ul.push_back(user);
141                                         }
142                                         for (dccallowlist::const_iterator k = dl->begin(); k != dl->end(); ++k)
143                                         {
144                                                 if (k->nickname == target->nick)
145                                                 {
146                                                         user->WriteServ("996 %s %s :%s is already on your DCCALLOW list", user->nick, user->nick, target->nick);
147                                                         return CMD_FAILURE;
148                                                 }
149                                                 else if (ServerInstance->MatchText(user->GetFullHost(), k->hostmask))
150                                                 {
151                                                         user->WriteServ("996 %s %s :You cannot add yourself to your own DCCALLOW list!", user->nick, user->nick);
152                                                         return CMD_FAILURE;
153                                                 }
154                                         }
155                                 
156                                         std::string mask = std::string(target->nick)+"!"+std::string(target->ident)+"@"+std::string(target->dhost);
157                                         std::string default_length = Conf->ReadValue("dccallow", "length", 0);
158                 
159                                         long length;
160                                         if (pcnt < 2)
161                                         {
162                                                 length = ServerInstance->Duration(default_length);
163                                         } 
164                                         else if (!atoi(parameters[1]))
165                                         {
166                                                 length = 0;
167                                         }
168                                         else
169                                         {
170                                                 length = ServerInstance->Duration(parameters[1]);
171                                         }
172         
173                                         if (!ServerInstance->IsValidMask(mask.c_str()))
174                                         {
175                                                 return CMD_FAILURE;
176                                         }
177                         
178                                         dl->push_back(DCCAllow(target->nick, mask, ServerInstance->Time(), length));
179                         
180                                         if (length > 0)
181                                         {
182                                                 user->WriteServ("993 %s %s :Added %s to DCCALLOW list for %d seconds", user->nick, user->nick, target->nick, length);
183                                         }
184                                         else
185                                         {
186                                                 user->WriteServ("994 %s %s :Added %s to DCCALLOW list for this session", user->nick, user->nick, target->nick);
187                                         }
188
189                                         /* route it. */
190                                         return CMD_SUCCESS;
191                                 }
192                         }
193                         else
194                         {
195                                 // nick doesn't exist
196                                 user->WriteServ("401 %s %s :No such nick/channel", user->nick, nick.c_str());
197                                 return CMD_FAILURE;
198                         }
199                 }
200                 return CMD_FAILURE;
201         }
202
203         void DisplayHelp(User* user)
204         {
205                 user->WriteServ("998 %s :DCCALLOW [<+|->nick [time]] [list] [help]", user->nick);
206                 user->WriteServ("998 %s :You may allow DCCs from specific users by specifying a", user->nick);
207                 user->WriteServ("998 %s :DCC allow for the user you want to receive DCCs from.", user->nick);
208                 user->WriteServ("998 %s :For example, to allow the user Brain to send you inspircd.exe", user->nick);
209                 user->WriteServ("998 %s :you would type:", user->nick);
210                 user->WriteServ("998 %s :/DCCALLOW +Brain", user->nick);
211                 user->WriteServ("998 %s :Brain would then be able to send you files. They would have to", user->nick);
212                 user->WriteServ("998 %s :resend the file again if the server gave them an error message", user->nick);
213                 user->WriteServ("998 %s :before you added them to your DCCALLOW list.", user->nick);
214                 user->WriteServ("998 %s :DCCALLOW entries will be temporary by default, if you want to add", user->nick);
215                 user->WriteServ("998 %s :them to your DCCALLOW list until you leave IRC, type:", user->nick);
216                 user->WriteServ("998 %s :/DCCALLOW +Brain 0", user->nick);
217                 user->WriteServ("998 %s :To remove the user from your DCCALLOW list, type:", user->nick);
218                 user->WriteServ("998 %s :/DCCALLOW -Brain", user->nick);
219                 user->WriteServ("998 %s :To see the users in your DCCALLOW list, type:", user->nick);
220                 user->WriteServ("998 %s :/DCCALLOW LIST", user->nick);
221                 user->WriteServ("998 %s :NOTE: If the user leaves IRC or changes their nickname", user->nick);
222                 user->WriteServ("998 %s :  they will be removed from your DCCALLOW list.", user->nick);
223                 user->WriteServ("998 %s :  your DCCALLOW list will be deleted when you leave IRC.", user->nick);
224                 user->WriteServ("999 %s :End of DCCALLOW HELP", user->nick);
225         }
226         
227         void DisplayDCCAllowList(User* user)
228         {
229                  // display current DCCALLOW list
230                 user->WriteServ("990 %s :Users on your DCCALLOW list:", user->nick);
231                 user->GetExt("dccallow_list", dl);
232                 
233                 if (dl)
234                 {
235                         for (dccallowlist::const_iterator c = dl->begin(); c != dl->end(); ++c)
236                         {
237                                 user->WriteServ("991 %s %s :%s (%s)", user->nick, user->nick, c->nickname.c_str(), c->hostmask.c_str());
238                         }
239                 }
240                 
241                 user->WriteServ("992 %s :End of DCCALLOW list", user->nick);
242         }                       
243
244 };
245         
246 class ModuleDCCAllow : public Module
247 {
248         cmd_dccallow* mycommand;
249  public:
250
251         ModuleDCCAllow(InspIRCd* Me)
252                 : Module(Me)
253         {
254                 Conf = new ConfigReader(ServerInstance);
255                 mycommand = new cmd_dccallow(ServerInstance);
256                 ServerInstance->AddCommand(mycommand);
257                 ReadFileConf();
258         }
259
260         void Implements(char* List)
261         {
262                 List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = List[I_OnUserQuit] = List[I_OnUserPreNick] = List[I_OnRehash] = 1;
263         }
264
265         virtual void OnRehash(User* user, const std::string &parameter)
266         {
267                 delete Conf;
268                 Conf = new ConfigReader(ServerInstance);
269         }
270
271         virtual void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
272         {
273                 dccallowlist* dl;
274         
275                 // remove their DCCALLOW list if they have one
276                 user->GetExt("dccallow_list", dl);
277                 if (dl)
278                 {
279                         delete dl;
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 int OnUserPreNick(User* user, const std::string &newnick)
291         {
292                 RemoveNick(user);
293                 return 0;
294         }
295
296         virtual int 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 int OnUserPreNotice(User* user, void* dest, int target_type, std::string &text, char status, CUList &exempt_list)
302         {
303                 if (!IS_LOCAL(user))
304                         return 0;
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 0;
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                                         u->GetExt("dccallow_list", dl);
324                 
325                                         if (dl && dl->size())
326                                         {
327                                                 for (dccallowlist::const_iterator iter = dl->begin(); iter != dl->end(); ++iter)
328                                                         if (ServerInstance->MatchText(user->GetFullHost(), iter->hostmask))
329                                                                 return 0;
330                                         }
331                 
332                                         // tokenize
333                                         std::stringstream ss(text);
334                                         std::string buf;
335                                         std::vector<std::string> tokens;
336                 
337                                         while (ss >> buf)
338                                                 tokens.push_back(buf);
339                 
340                                         irc::string type = tokens[1].c_str();
341                 
342                                         bool blockchat = Conf->ReadFlag("dccallow", "blockchat", 0);
343                 
344                                         if (type == "SEND")
345                                         {
346                                                 std::string defaultaction = Conf->ReadValue("dccallow", "action", 0);
347                                                 std::string filename = tokens[2];
348                                         
349                                                 if (defaultaction == "allow") 
350                                                         return 0;
351                                 
352                                                 for (unsigned int i = 0; i < bfl.size(); i++)
353                                                 {
354                                                         if (ServerInstance->MatchText(filename, bfl[i].filemask))
355                                                         {
356                                                                 if (bfl[i].action == "allow")
357                                                                         return 0;
358                                                         }
359                                                         else
360                                                         {
361                                                                 if (defaultaction == "allow")
362                                                                         return 0;
363                                                         }
364                                                         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());
365                                                         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());
366                                                         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);
367                                                         return 1;
368                                                 }
369                                         }
370                                         else if ((type == "CHAT") && (blockchat))
371                                         {
372                                                 user->WriteServ("NOTICE %s :The user %s is not accepting DCC CHAT requests from you.", user->nick, u->nick);
373                                                 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);
374                                                 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);
375                                                 return 1;
376                                         }
377                                 }
378                         }
379                 }
380                 return 0;
381         }
382         
383         void Expire()
384         {
385                 for (userlist::iterator iter = ul.begin(); iter != ul.end(); ++iter)
386                 {
387                         User* u = (User*)(*iter);
388                         u->GetExt("dccallow_list", dl);
389         
390                         if (dl)
391                         {
392                                 if (dl->size())
393                                 {
394                                         dccallowlist::iterator iter = dl->begin();
395                                         while (iter != dl->end())
396                                         {
397                                                 if ((iter->set_on + iter->length) <= ServerInstance->Time())
398                                                 {
399                                                         u->WriteServ("997 %s %s :DCCALLOW entry for %s has expired", u->nick, u->nick, iter->nickname.c_str());
400                                                         iter = dl->erase(iter);
401                                                 }
402                                                 else
403                                                 {
404                                                         ++iter;
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                         u->GetExt("dccallow_list", dl);
423         
424                         if (dl)
425                         {
426                                 if (dl->size())
427                                 {
428                                         for (dccallowlist::iterator i = dl->begin(); i != dl->end(); ++i)
429                                         {
430                                                 if (i->nickname == user->nick)
431                                                 {
432                                         
433                                                         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());
434                                                         u->WriteServ("995 %s %s :Removed %s from your DCCALLOW list", u->nick, u->nick, i->nickname.c_str());
435                                                         dl->erase(i);
436                                                         break;
437                                                 }
438                                         }
439                                 }
440                         }
441                         else
442                         {
443                                 RemoveFromUserlist(u);
444                         }
445                 }
446         }
447
448         void RemoveFromUserlist(User *user)
449         {
450                 // remove user from userlist
451                 for (userlist::iterator j = ul.begin(); j != ul.end(); ++j)
452                 {
453                         User* u = (User*)(*j);
454                         if (u == user)
455                         {
456                                 ul.erase(j);
457                                 break;
458                         }
459                 }
460         }
461
462         void ReadFileConf()
463         {
464                 bfl.clear();
465                 for (int i = 0; i < Conf->Enumerate("banfile"); i++)
466                 {
467                         BannedFileList bf;
468                         std::string fileglob = Conf->ReadValue("banfile", "pattern", i);
469                         std::string action = Conf->ReadValue("banfile", "action", i);
470                         bf.filemask = fileglob;
471                         bf.action = action;
472                         bfl.push_back(bf);
473                 }
474         
475         }
476
477         virtual ~ModuleDCCAllow()
478         {
479         }
480
481         virtual Version GetVersion()
482         {
483                 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
484         }
485 };
486
487 MODULE_INIT(ModuleDCCAllow)