]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_safelist.cpp
fcf319c9070eb725b81c23c6e7d16df79437841c
[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 "helperfuncs.h"
23 #include "message.h"
24 #include <vector>
25  
26 extern time_t TIME;
27
28 class ListData
29 {
30  public:
31         long list_start;
32         long list_position;
33         bool list_ended;
34
35         ListData() : list_start(0), list_position(0), list_ended(false) {};
36         ListData(long pos, time_t t) : list_start(t), list_position(pos), list_ended(false) {};
37 };
38
39 /* $ModDesc: A module overriding /list, and making it safe - stop those sendq problems. */
40  
41 typedef std::vector<userrec *> UserList;
42 UserList listusers;    /* vector of people doing a /list */
43
44 class ListTimer : public InspTimer
45 {
46  private:
47         Server* Srv;
48  public:
49         ListTimer(long interval, Server* Me) : InspTimer(interval,TIME), Srv(Me)
50         {
51         }
52
53         virtual void Tick(time_t TIME)
54         {
55                 log(DEBUG,"*** Timer tick!");
56
57                 bool go_again = true;
58                 chanrec *chan;
59                 char buffer[MAXBUF];
60
61                 while (go_again)
62                 {
63                         go_again = false;
64                         for (UserList::iterator iter = listusers.begin(); iter != listusers.end(); iter++)
65                         {
66                                 /*
67                                  * What we do here:
68                                  *  - Get where they are up to
69                                  *  - If it's > GetChannelCount, erase them from the iterator, set go_again to true
70                                  *  - If not, spool the next 20 channels
71                                  */
72                                 userrec* u = (userrec*)(*iter);
73                                 ListData* ld = (ListData*)u->GetExt("safelist_cache");
74                                 if (ld->list_position > Srv->GetChannelCount())
75                                 {
76                                         u->Shrink("safelist_cache");
77                                         delete ld;
78                                         listusers.erase(iter);
79                                         go_again = true;
80                                         break;
81                                 }
82
83                                 log(DEBUG, "m_safelist.so: resuming spool of list to client %s at channel %ld", u->nick, ld->list_position);
84                                 chan = NULL;
85                                 /* Attempt to fill up to half the user's sendq with /LIST output */
86                                 long amount_sent = 0;
87                                 do
88                                 {
89                                         log(DEBUG,"Channel %ld",ld->list_position);
90                                         chan = Srv->GetChannelIndex(ld->list_position);
91                                         /* spool details */
92                                         if (chan)
93                                         {
94                                                 /* Increment total plus linefeed */
95                                                 int counter = snprintf(buffer,MAXBUF,"322 %s %s %d :[+%s] %s",u->nick,chan->name,usercount_i(chan),chanmodes(chan,has_channel(u,chan)),chan->topic);
96                                                 amount_sent += counter + 4 + Srv->GetServerName().length();
97                                                 log(DEBUG,"m_safelist.so: Sent %ld of safe %ld / 2",amount_sent,u->sendqmax);
98                                                 WriteServ_NoFormat(u->fd,buffer);
99                                         }
100                                         else
101                                         {
102                                                 if (!ld->list_ended)
103                                                 {
104                                                         ld->list_ended = true;
105                                                         WriteServ(u->fd,"323 %s :End of channel list.",u->nick);
106                                                 }
107                                         }
108
109                                         ld->list_position++;
110                                 }
111                                 while ((chan != NULL) && (amount_sent < (u->sendqmax / 2)));
112                         }
113                 }
114
115                 ListTimer* MyTimer = new ListTimer(1,Srv);
116                 Srv->AddTimer(MyTimer);
117         }
118 };
119
120 class ModuleSafeList : public Module
121 {
122  private:
123          Server *Srv;
124          ListTimer* MyTimer;
125  public:
126         ModuleSafeList(Server* Me) : Module::Module(Me)
127         {
128                 Srv = Me;
129
130                 MyTimer = new ListTimer(1,Srv);
131                 Srv->AddTimer(MyTimer);
132         }
133  
134         virtual ~ModuleSafeList()
135         {
136         }
137  
138         virtual Version GetVersion()
139         {
140                 return Version(1,0,0,0,VF_VENDOR);
141         }
142  
143         void Implements(char* List)
144         {
145                 List[I_OnPreCommand] List[I_OnCleanup] = List[I_OnUserQuit] = List[I_On005Numeric] = 1;
146         }
147
148         /*
149          * OnPreCommand()
150          *   Intercept the LIST command.
151          */ 
152         virtual int OnPreCommand(std::string command, char **parameters, int pcnt, userrec *user, bool validated)
153         {
154                 /* If the command doesnt appear to be valid, we dont want to mess with it. */
155                 if (!validated)
156                         return 0;
157  
158                 if (command == "LIST")
159                 {
160                         return this->HandleList(parameters, pcnt, user);
161                 }
162                 return 0;
163         }
164         
165         /*
166          * HandleList()
167          *   Handle (override) the LIST command.
168          */
169         int HandleList(char** parameters, int pcnt, userrec* user)
170         {
171                 /* First, let's check if the user is currently /list'ing */
172                 ListData *ld = (ListData*)user->GetExt("safelist_cache");
173  
174                 if (ld)
175                 {
176                         /* user is already /list'ing, we don't want to do shit. */
177                         return 1;
178                 }
179
180                 time_t* last_list_time = (time_t*)user->GetExt("safelist_last");
181                 if (last_list_time)
182                 {
183                         if (TIME < (*last_list_time)+60)
184                         {
185                                 WriteServ(user->fd,"NOTICE %s :*** Woah there, slow down a little, you can't /LIST so often!",user->nick);
186                                 return 1;
187                         }
188
189                         delete last_list_time;
190                         user->Shrink("safelist_last");
191                 }
192  
193                 /*
194                  * start at channel 0! ;)
195                  */
196                 ld = new ListData(0,TIME);
197                 user->Extend("safelist_cache", (char*)ld);
198                 listusers.push_back(user);
199
200                 time_t* llt = new time_t;
201                 *llt = TIME;
202                 user->Extend("safelist_last",(char*)llt);
203                 
204                 WriteServ(user->fd,"321 %s Channel :Users Name",user->nick);
205                 /*
206                  * If we can, we try and fill up the user's sendq right now with the first batch of channels,
207                  * which on a small net, may be ALL of them.
208                  */
209                 this->OnBackgroundTimer(TIME);
210                 return 1;
211         }
212
213         virtual void OnCleanup(int target_type, void* item)
214         {
215                 if(target_type == TYPE_USER)
216                 {
217                         userrec* u = (userrec*)item;
218                         ListData* ld = (ListData*)u->GetExt("safelist_cache");
219                         if (ld)
220                         {
221                                 u->Shrink("safelist_cache");
222                                 delete ld;
223                         }
224                         for (UserList::iterator iter = listusers.begin(); iter != listusers.end(); iter++)
225                         {
226                                 userrec* u2 = (userrec*)(*iter);
227                                 if (u2 == u)
228                                 {
229                                         listusers.erase(iter);
230                                         break;
231                                 }
232                         }
233                         time_t* last_list_time = (time_t*)u->GetExt("safelist_last");
234                         if (last_list_time)
235                         {
236                                 delete last_list_time;
237                                 u->Shrink("safelist_last");
238                         }
239                 }
240         }
241
242         virtual void On005Numeric(std::string &output)
243         {
244                 output.append(" SAFELIST");
245         }
246
247         virtual void OnUserQuit(userrec* user, std::string message)
248         {
249                 this->OnCleanup(TYPE_USER,user);
250         }
251
252 };
253  
254  
255  
256 /******************************************************************************************************/
257  
258 class ModuleSafeListFactory : public ModuleFactory
259 {
260  public:
261         ModuleSafeListFactory()
262         {
263         }
264  
265         ~ModuleSafeListFactory()
266         {
267         }
268  
269         virtual Module * CreateModule(Server* Me)
270         {
271                 return new ModuleSafeList(Me);
272         }
273  
274 };
275  
276 extern "C" void * init_module( void )
277 {
278         return new ModuleSafeListFactory;
279 }