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