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