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