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