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