]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_safelist.cpp
Add config <options:disablehmac> to support disabling of HMAC, and tidy up to detect...
[user/henk/code/inspircd.git] / src / modules / m_safelist.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 "users.h"
15 #include "channels.h"
16 #include "modules.h"
17 #include "configreader.h"
18 #include "inspircd.h"
19 #include "wildcard.h"
20
21 /** Holds a users m_safelist state
22  */
23 class ListData : public classbase
24 {
25  public:
26         long list_start;
27         long list_position;
28         bool list_ended;
29         const std::string glob;
30         int minusers;
31         int maxusers;
32
33         ListData() : list_start(0), list_position(0), list_ended(false) {};
34         ListData(long pos, time_t t, const std::string &pattern, int mi, int ma) : list_start(t), list_position(pos), list_ended(false), glob(pattern), minusers(mi), maxusers(ma) {};
35 };
36
37 /* $ModDesc: A module overriding /list, and making it safe - stop those sendq problems. */
38
39 class ModuleSafeList : public Module
40 {
41         time_t ThrottleSecs;
42         size_t ServerNameSize;
43         int global_listing;
44         int LimitList;
45  public:
46         ModuleSafeList(InspIRCd* Me) : Module::Module(Me)
47         {
48                 OnRehash(NULL, "");
49         }
50  
51         virtual ~ModuleSafeList()
52         {
53         }
54
55         virtual void OnRehash(userrec* user, const std::string &parameter)
56         {
57                 ConfigReader MyConf(ServerInstance);
58                 ThrottleSecs = MyConf.ReadInteger("safelist", "throttle", "60", 0, true);
59                 LimitList = MyConf.ReadInteger("safelist", "maxlisters", "50", 0, true);
60                 ServerNameSize = strlen(ServerInstance->Config->ServerName) + 4;
61                 global_listing = 0;
62         }
63  
64         virtual Version GetVersion()
65         {
66                 return Version(1,1,0,0,VF_VENDOR,API_VERSION);
67         }
68  
69         void Implements(char* List)
70         {
71                 List[I_OnBufferFlushed] = List[I_OnPreCommand] = List[I_OnCleanup] = List[I_OnUserQuit] = List[I_On005Numeric] = List[I_OnRehash] = 1;
72         }
73
74         /*
75          * OnPreCommand()
76          *   Intercept the LIST command.
77          */ 
78         virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated, const std::string &original_line)
79         {
80                 /* If the command doesnt appear to be valid, we dont want to mess with it. */
81                 if (!validated)
82                         return 0;
83  
84                 if (command == "LIST")
85                 {
86                         return this->HandleList(parameters, pcnt, user);
87                 }
88                 return 0;
89         }
90         
91         /*
92          * HandleList()
93          *   Handle (override) the LIST command.
94          */
95         int HandleList(const char** parameters, int pcnt, userrec* user)
96         {
97                 int minusers = 0, maxusers = 0;
98
99                 if (global_listing >= LimitList)
100                 {
101                         user->WriteServ("NOTICE %s :*** Server load is currently too heavy. Please try again later.", user->nick);
102                         user->WriteServ("321 %s Channel :Users Name",user->nick);
103                         user->WriteServ("323 %s :End of channel list.",user->nick);
104                         return 1;
105                 }
106
107                 /* First, let's check if the user is currently /list'ing */
108                 ListData *ld;
109                 user->GetExt("safelist_cache", ld);
110  
111                 if (ld)
112                 {
113                         /* user is already /list'ing, we don't want to do shit. */
114                         return 1;
115                 }
116
117                 /* Work around mIRC suckyness. YOU SUCK, KHALED! */
118                 if (pcnt == 1)
119                 {
120                         if (*parameters[0] == '<')
121                         {
122                                 maxusers = atoi(parameters[0]+1);
123                                 ServerInstance->Log(DEBUG,"Max users: %d", maxusers);
124                                 pcnt = 0;
125                         }
126                         else if (*parameters[0] == '>')
127                         {
128                                 minusers = atoi(parameters[0]+1);
129                                 ServerInstance->Log(DEBUG,"Min users: %d", minusers);
130                                 pcnt = 0;
131                         }
132                 }
133
134                 time_t* last_list_time;
135                 user->GetExt("safelist_last", last_list_time);
136                 if (last_list_time)
137                 {
138                         if (ServerInstance->Time() < (*last_list_time)+ThrottleSecs)
139                         {
140                                 user->WriteServ("NOTICE %s :*** Woah there, slow down a little, you can't /LIST so often!",user->nick);
141                                 user->WriteServ("321 %s Channel :Users Name",user->nick);
142                                 user->WriteServ("323 %s :End of channel list.",user->nick);
143                                 return 1;
144                         }
145
146                         DELETE(last_list_time);
147                         user->Shrink("safelist_last");
148                 }
149
150  
151                 /*
152                  * start at channel 0! ;)
153                  */
154                 ld = new ListData(0,ServerInstance->Time(), pcnt ? parameters[0] : "*", minusers, maxusers);
155                 user->Extend("safelist_cache", ld);
156
157                 time_t* llt = new time_t;
158                 *llt = ServerInstance->Time();
159                 user->Extend("safelist_last", llt);
160
161                 user->WriteServ("321 %s Channel :Users Name",user->nick);
162
163                 global_listing++;
164
165                 return 1;
166         }
167
168         virtual void OnBufferFlushed(userrec* user)
169         {
170                 char buffer[MAXBUF];
171                 ListData* ld;
172                 if (user->GetExt("safelist_cache", ld))
173                 {
174                         chanrec* chan = NULL;
175                         long amount_sent = 0;
176                         do
177                         {
178                                 chan = ServerInstance->GetChannelIndex(ld->list_position);
179                                 bool has_user = (chan && chan->HasUser(user));
180                                 long users = chan ? chan->GetUserCounter() : 0;
181
182                                 bool too_few = (ld->minusers && (users <= ld->minusers));
183                                 bool too_many = (ld->maxusers && (users >= ld->maxusers));
184
185                                 if (chan && (too_many || too_few))
186                                 {
187                                         ld->list_position++;
188                                         continue;
189                                 }
190
191                                 if ((chan) && (chan->modes[CM_PRIVATE]))
192                                 {
193                                         bool display = (match(chan->name, ld->glob.c_str()) || (*chan->topic && match(chan->topic, ld->glob.c_str())));
194                                         if ((users) && (display))
195                                         {
196                                                 int counter = snprintf(buffer, MAXBUF, "322 %s *", user->nick);
197                                                 amount_sent += counter + ServerNameSize;
198                                                 user->WriteServ(std::string(buffer));
199                                         }
200                                 }
201                                 else if ((chan) && (((!(chan->modes[CM_PRIVATE])) && (!(chan->modes[CM_SECRET]))) || (has_user)))
202                                 {
203                                         bool display = (match(chan->name, ld->glob.c_str()) || (*chan->topic && match(chan->topic, ld->glob.c_str())));
204                                         if ((users) && (display))
205                                         {
206                                                 int counter = snprintf(buffer, MAXBUF, "322 %s %s %ld :[+%s] %s",user->nick, chan->name, users, chan->ChanModes(has_user), chan->topic);
207                                                 amount_sent += counter + ServerNameSize;
208                                                 user->WriteServ(std::string(buffer));
209                                         }
210                                 }
211                                 else
212                                 {
213                                         if (!chan)
214                                         {
215                                                 if (!ld->list_ended)
216                                                 {
217                                                         ld->list_ended = true;
218                                                         user->WriteServ("323 %s :End of channel list.",user->nick);
219                                                 }
220                                         }
221                                 }
222                                 ld->list_position++;
223                         }
224                         while ((chan != NULL) && (amount_sent < (user->sendqmax / 4)));
225                         if (ld->list_ended)
226                         {
227                                 user->Shrink("safelist_cache");
228                                 DELETE(ld);
229                                 global_listing--;
230                         }
231                 }
232         }
233
234         virtual void OnCleanup(int target_type, void* item)
235         {
236                 if(target_type == TYPE_USER)
237                 {
238                         userrec* u = (userrec*)item;
239                         ListData* ld;
240                         u->GetExt("safelist_cache", ld);
241                         if (ld)
242                         {
243                                 u->Shrink("safelist_cache");
244                                 DELETE(ld);
245                                 global_listing--;
246                         }
247                         time_t* last_list_time;
248                         u->GetExt("safelist_last", last_list_time);
249                         if (last_list_time)
250                         {
251                                 DELETE(last_list_time);
252                                 u->Shrink("safelist_last");
253                         }
254                 }
255         }
256
257         virtual void On005Numeric(std::string &output)
258         {
259                 output.append(" SAFELIST");
260         }
261
262         virtual void OnUserQuit(userrec* user, const std::string &message, const std::string &oper_message)
263         {
264                 this->OnCleanup(TYPE_USER,user);
265         }
266
267 };
268
269
270 class ModuleSafeListFactory : public ModuleFactory
271 {
272  public:
273         ModuleSafeListFactory()
274         {
275         }
276  
277         ~ModuleSafeListFactory()
278         {
279         }
280  
281         virtual Module * CreateModule(InspIRCd* Me)
282         {
283                 return new ModuleSafeList(Me);
284         }
285  
286 };
287  
288 extern "C" void * init_module( void )
289 {
290         return new ModuleSafeListFactory;
291 }