]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_safelist.cpp
af7fefc6a2e33507986f1ca95c3e9c7247ad1d69
[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
31         ListData() : list_start(0), list_position(0), list_ended(false) {};
32         ListData(long pos, time_t t, const std::string &pattern) : list_start(t), list_position(pos), list_ended(false), glob(pattern) {};
33 };
34
35 /* $ModDesc: A module overriding /list, and making it safe - stop those sendq problems. */
36  
37 typedef std::vector<userrec *> UserList;
38 UserList listusers;    /* vector of people doing a /list */
39 class ListTimer *timer;
40
41 /** To create a timer which recurs every second, we inherit from InspTimer.
42  * InspTimer is only one-shot however, so at the end of each Tick() we simply
43  * insert another of ourselves into the pending queue :)
44  */
45 class ListTimer : public InspTimer
46 {
47  private:
48
49         char buffer[MAXBUF];
50         chanrec *chan;
51         InspIRCd* ServerInstance;
52         const std::string glob;
53         size_t ServerNameSize;
54
55  public:
56
57         ListTimer(InspIRCd* Instance, long interval) : InspTimer(interval,Instance->Time()), ServerInstance(Instance)
58         {
59                 ServerNameSize = 4 + strlen(ServerInstance->Config->ServerName);
60         }
61
62         virtual void Tick(time_t TIME)
63         {
64                 bool go_again = true;
65
66                 while (go_again)
67                 {
68                         go_again = false;
69                         for (UserList::iterator iter = listusers.begin(); iter != listusers.end(); iter++)
70                         {
71                                 /*
72                                  * What we do here:
73                                  *  - Get where they are up to
74                                  *  - If it's more than total number of channels, erase
75                                  *    them from the iterator, set go_again to true
76                                  *  - If not, spool more channels
77                                  */
78                                 userrec* u = (userrec*)(*iter);
79                                 ListData* ld;
80                                 u->GetExt("safelist_cache", ld);
81                                 if ((size_t)ld->list_position > ServerInstance->chanlist->size())
82                                 {
83                                         u->Shrink("safelist_cache");
84                                         DELETE(ld);
85                                         listusers.erase(iter);
86                                         go_again = true;
87                                         break;
88                                 }
89                                 chan = NULL;
90                                 /* Attempt to fill up to 25% the user's sendq with /LIST output */
91                                 long amount_sent = 0;
92                                 do
93                                 {
94                                         if (!ld->list_position)
95                                                 u->WriteServ("321 %s Channel :Users Name",u->nick);
96                                         chan = ServerInstance->GetChannelIndex(ld->list_position);
97                                         /* spool details */
98                                         bool has_user = (chan && chan->HasUser(u));
99                                         if ((chan) && (chan->modes[CM_PRIVATE]))
100                                         {
101                                                 bool display = match(chan->name, ld->glob.c_str());
102                                                 long users = chan->GetUserCounter();
103                                                 if ((users) && (display))
104                                                 {
105                                                         int counter = snprintf(buffer, MAXBUF, "322 %s *", u->nick);
106                                                         amount_sent += counter + ServerNameSize;
107                                                         u->WriteServ(std::string(buffer));
108                                                 }
109                                         }
110                                         else if ((chan) && (((!(chan->modes[CM_PRIVATE])) && (!(chan->modes[CM_SECRET]))) || (has_user)))
111                                         {
112                                                 bool display = match(chan->name, ld->glob.c_str());
113                                                 long users = chan->GetUserCounter();
114
115                                                 if ((users) && (display))
116                                                 {
117                                                         int counter = snprintf(buffer, MAXBUF, "322 %s %s %ld :[+%s] %s",u->nick, chan->name, users, chan->ChanModes(has_user), chan->topic);
118                                                         /* Increment total plus linefeed */
119                                                         amount_sent += counter + ServerNameSize;
120                                                         u->WriteServ(std::string(buffer));
121                                                 }
122                                         }
123                                         else
124                                         {
125                                                 if (!chan)
126                                                 {
127                                                         if (!ld->list_ended)
128                                                         {
129                                                                 ld->list_ended = true;
130                                                                 u->WriteServ("323 %s :End of channel list.",u->nick);
131                                                         }
132                                                 }
133                                         }
134
135                                         ld->list_position++;
136                                 }
137                                 while ((chan != NULL) && (amount_sent < (u->sendqmax / 4)));
138                         }
139                 }
140
141                 if (listusers.size())
142                 {
143                         timer = new ListTimer(ServerInstance,1);
144                         ServerInstance->Timers->AddTimer(timer);
145                 }
146                 else
147                 {
148                         timer = NULL;
149                 }
150         }
151 };
152
153 class ModuleSafeList : public Module
154 {
155         time_t ThrottleSecs;
156  public:
157         ModuleSafeList(InspIRCd* Me) : Module::Module(Me)
158         {
159                 timer = NULL;
160                 OnRehash(NULL, "");
161         }
162  
163         virtual ~ModuleSafeList()
164         {
165                 if (timer)
166                         ServerInstance->Timers->DelTimer(timer);
167         }
168
169         virtual void OnRehash(userrec* user, const std::string &parameter)
170         {
171                 ConfigReader MyConf(ServerInstance);
172                 ThrottleSecs = MyConf.ReadInteger("safelist", "throttle", "60", 0, true);
173         }
174  
175         virtual Version GetVersion()
176         {
177                 return Version(1,1,0,0,VF_VENDOR,API_VERSION);
178         }
179  
180         void Implements(char* List)
181         {
182                 List[I_OnPreCommand] = List[I_OnCleanup] = List[I_OnUserQuit] = List[I_On005Numeric] = List[I_OnRehash] = 1;
183         }
184
185         /*
186          * OnPreCommand()
187          *   Intercept the LIST command.
188          */ 
189         virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated, const std::string &original_line)
190         {
191                 /* If the command doesnt appear to be valid, we dont want to mess with it. */
192                 if (!validated)
193                         return 0;
194  
195                 if (command == "LIST")
196                 {
197                         return this->HandleList(parameters, pcnt, user);
198                 }
199                 return 0;
200         }
201         
202         /*
203          * HandleList()
204          *   Handle (override) the LIST command.
205          */
206         int HandleList(const char** parameters, int pcnt, userrec* user)
207         {
208                 /* First, let's check if the user is currently /list'ing */
209                 ListData *ld;
210                 user->GetExt("safelist_cache", ld);
211  
212                 if (ld)
213                 {
214                         /* user is already /list'ing, we don't want to do shit. */
215                         return 1;
216                 }
217
218                 /* Work around mIRC suckyness. YOU SUCK, KHALED! */
219                 if ((pcnt == 1) && (*parameters[0] == '<'))
220                         pcnt = 0;
221
222                 time_t* last_list_time;
223                 user->GetExt("safelist_last", last_list_time);
224                 if (last_list_time)
225                 {
226                         if (ServerInstance->Time() < (*last_list_time)+ThrottleSecs)
227                         {
228                                 user->WriteServ("NOTICE %s :*** Woah there, slow down a little, you can't /LIST so often!",user->nick);
229                                 user->WriteServ("321 %s Channel :Users Name",user->nick);
230                                 user->WriteServ("323 %s :End of channel list.",user->nick);
231                                 return 1;
232                         }
233
234                         DELETE(last_list_time);
235                         user->Shrink("safelist_last");
236                 }
237  
238                 /*
239                  * start at channel 0! ;)
240                  */
241                 ld = new ListData(0,ServerInstance->Time(), pcnt ? parameters[0] : "*");
242                 user->Extend("safelist_cache", ld);
243                 listusers.push_back(user);
244
245                 time_t* llt = new time_t;
246                 *llt = ServerInstance->Time();
247                 user->Extend("safelist_last", llt);
248
249                 if (!timer)
250                 {
251                         timer = new ListTimer(ServerInstance,1);
252                         ServerInstance->Timers->AddTimer(timer);
253                 }
254
255                 return 1;
256         }
257
258         virtual void OnCleanup(int target_type, void* item)
259         {
260                 if(target_type == TYPE_USER)
261                 {
262                         userrec* u = (userrec*)item;
263                         ListData* ld;
264                         u->GetExt("safelist_cache", ld);
265                         if (ld)
266                         {
267                                 u->Shrink("safelist_cache");
268                                 DELETE(ld);
269                         }
270                         for (UserList::iterator iter = listusers.begin(); iter != listusers.end(); iter++)
271                         {
272                                 userrec* u2 = (userrec*)(*iter);
273                                 if (u2 == u)
274                                 {
275                                         listusers.erase(iter);
276                                         break;
277                                 }
278                         }
279                         time_t* last_list_time;
280                         u->GetExt("safelist_last", last_list_time);
281                         if (last_list_time)
282                         {
283                                 DELETE(last_list_time);
284                                 u->Shrink("safelist_last");
285                         }
286                 }
287         }
288
289         virtual void On005Numeric(std::string &output)
290         {
291                 output.append(" SAFELIST");
292         }
293
294         virtual void OnUserQuit(userrec* user, const std::string &message)
295         {
296                 this->OnCleanup(TYPE_USER,user);
297         }
298
299 };
300
301
302 class ModuleSafeListFactory : public ModuleFactory
303 {
304  public:
305         ModuleSafeListFactory()
306         {
307         }
308  
309         ~ModuleSafeListFactory()
310         {
311         }
312  
313         virtual Module * CreateModule(InspIRCd* Me)
314         {
315                 return new ModuleSafeList(Me);
316         }
317  
318 };
319  
320 extern "C" void * init_module( void )
321 {
322         return new ModuleSafeListFactory;
323 }