]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_safelist.cpp
b3dfc93b9bf8d5ae12b5867ca6e463d15badf93f
[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                                         if (!ld->list_position)
91                                                 WriteServ(u->fd,"321 %s Channel :Users Name",u->nick);
92                                         chan = Srv->GetChannelIndex(ld->list_position);
93                                         /* spool details */
94                                         if (chan)
95                                         {
96                                                 /* Increment total plus linefeed */
97                                                 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);
98                                                 amount_sent += counter + 4 + Srv->GetServerName().length();
99                                                 log(DEBUG,"m_safelist.so: Sent %ld of safe %ld / 2",amount_sent,u->sendqmax);
100                                                 WriteServ_NoFormat(u->fd,buffer);
101                                         }
102                                         else
103                                         {
104                                                 if (!ld->list_ended)
105                                                 {
106                                                         ld->list_ended = true;
107                                                         WriteServ(u->fd,"323 %s :End of channel list.",u->nick);
108                                                 }
109                                         }
110
111                                         ld->list_position++;
112                                 }
113                                 while ((chan != NULL) && (amount_sent < (u->sendqmax / 2)));
114                         }
115                 }
116
117                 ListTimer* MyTimer = new ListTimer(1,Srv);
118                 Srv->AddTimer(MyTimer);
119         }
120 };
121
122 class ModuleSafeList : public Module
123 {
124  private:
125          Server *Srv;
126          ListTimer* MyTimer;
127  public:
128         ModuleSafeList(Server* Me) : Module::Module(Me)
129         {
130                 Srv = Me;
131
132                 MyTimer = new ListTimer(1,Srv);
133                 Srv->AddTimer(MyTimer);
134         }
135  
136         virtual ~ModuleSafeList()
137         {
138         }
139  
140         virtual Version GetVersion()
141         {
142                 return Version(1,0,0,0,VF_VENDOR);
143         }
144  
145         void Implements(char* List)
146         {
147                 List[I_OnPreCommand] = List[I_OnCleanup] = List[I_OnUserQuit] = List[I_On005Numeric] = 1;
148         }
149
150         /*
151          * OnPreCommand()
152          *   Intercept the LIST command.
153          */ 
154         virtual int OnPreCommand(std::string command, char **parameters, int pcnt, userrec *user, bool validated)
155         {
156                 /* If the command doesnt appear to be valid, we dont want to mess with it. */
157                 if (!validated)
158                         return 0;
159  
160                 if (command == "LIST")
161                 {
162                         return this->HandleList(parameters, pcnt, user);
163                 }
164                 return 0;
165         }
166         
167         /*
168          * HandleList()
169          *   Handle (override) the LIST command.
170          */
171         int HandleList(char** parameters, int pcnt, userrec* user)
172         {
173                 /* First, let's check if the user is currently /list'ing */
174                 ListData *ld = (ListData*)user->GetExt("safelist_cache");
175  
176                 if (ld)
177                 {
178                         /* user is already /list'ing, we don't want to do shit. */
179                         return 1;
180                 }
181
182                 time_t* last_list_time = (time_t*)user->GetExt("safelist_last");
183                 if (last_list_time)
184                 {
185                         if (TIME < (*last_list_time)+60)
186                         {
187                                 WriteServ(user->fd,"NOTICE %s :*** Woah there, slow down a little, you can't /LIST so often!",user->nick);
188                                 return 1;
189                         }
190
191                         delete last_list_time;
192                         user->Shrink("safelist_last");
193                 }
194  
195                 /*
196                  * start at channel 0! ;)
197                  */
198                 ld = new ListData(0,TIME);
199                 user->Extend("safelist_cache", (char*)ld);
200                 listusers.push_back(user);
201
202                 time_t* llt = new time_t;
203                 *llt = TIME;
204                 user->Extend("safelist_last",(char*)llt);
205         
206                 return 1;
207         }
208
209         virtual void OnCleanup(int target_type, void* item)
210         {
211                 if(target_type == TYPE_USER)
212                 {
213                         userrec* u = (userrec*)item;
214                         ListData* ld = (ListData*)u->GetExt("safelist_cache");
215                         if (ld)
216                         {
217                                 u->Shrink("safelist_cache");
218                                 delete ld;
219                         }
220                         for (UserList::iterator iter = listusers.begin(); iter != listusers.end(); iter++)
221                         {
222                                 userrec* u2 = (userrec*)(*iter);
223                                 if (u2 == u)
224                                 {
225                                         listusers.erase(iter);
226                                         break;
227                                 }
228                         }
229                         time_t* last_list_time = (time_t*)u->GetExt("safelist_last");
230                         if (last_list_time)
231                         {
232                                 delete last_list_time;
233                                 u->Shrink("safelist_last");
234                         }
235                 }
236         }
237
238         virtual void On005Numeric(std::string &output)
239         {
240                 output.append(" SAFELIST");
241         }
242
243         virtual void OnUserQuit(userrec* user, std::string message)
244         {
245                 this->OnCleanup(TYPE_USER,user);
246         }
247
248 };
249  
250  
251  
252 /******************************************************************************************************/
253  
254 class ModuleSafeListFactory : public ModuleFactory
255 {
256  public:
257         ModuleSafeListFactory()
258         {
259         }
260  
261         ~ModuleSafeListFactory()
262         {
263         }
264  
265         virtual Module * CreateModule(Server* Me)
266         {
267                 return new ModuleSafeList(Me);
268         }
269  
270 };
271  
272 extern "C" void * init_module( void )
273 {
274         return new ModuleSafeListFactory;
275 }