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