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