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