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