]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_safelist.cpp
More debug
[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 "configreader.h"
23 #include "inspircd.h"
24 #include "wildcard.h"
25
26 /** Holds a users m_safelist state
27  */
28 class ListData : public classbase
29 {
30  public:
31         long list_start;
32         long list_position;
33         bool list_ended;
34         const std::string glob;
35
36         ListData() : list_start(0), list_position(0), list_ended(false) {};
37         ListData(long pos, time_t t, const std::string &pattern) : list_start(t), list_position(pos), list_ended(false), glob(pattern) {};
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 /** 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         char buffer[MAXBUF];
54         chanrec *chan;
55         InspIRCd* ServerInstance;
56         const std::string glob;
57         size_t ServerNameSize;
58
59  public:
60
61         ListTimer(InspIRCd* Instance, long interval) : InspTimer(interval,Instance->Time()), ServerInstance(Instance)
62         {
63                 ServerNameSize = 4 + strlen(ServerInstance->Config->ServerName);
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 more than total number of channels, erase
79                                  *    them from the iterator, set go_again to true
80                                  *  - If not, spool more channels
81                                  */
82                                 userrec* u = (userrec*)(*iter);
83                                 ListData* ld;
84                                 u->GetExt("safelist_cache", ld);
85                                 if ((size_t)ld->list_position > ServerInstance->chanlist.size())
86                                 {
87                                         u->Shrink("safelist_cache");
88                                         DELETE(ld);
89                                         listusers.erase(iter);
90                                         go_again = true;
91                                         break;
92                                 }
93
94                                 ServerInstance->Log(DEBUG, "m_safelist.so: resuming spool of list to client %s at channel %ld", u->nick, ld->list_position);
95                                 chan = NULL;
96                                 /* Attempt to fill up to 25% the user's sendq with /LIST output */
97                                 long amount_sent = 0;
98                                 do
99                                 {
100                                         ServerInstance->Log(DEBUG,"Channel %ld",ld->list_position);
101                                         if (!ld->list_position)
102                                                 u->WriteServ("321 %s Channel :Users Name",u->nick);
103                                         chan = ServerInstance->GetChannelIndex(ld->list_position);
104                                         /* spool details */
105                                         bool has_user = (chan && chan->HasUser(u));
106                                         if ((chan) && (((!(chan->modes[CM_PRIVATE])) && (!(chan->modes[CM_SECRET]))) || (has_user)))
107                                         {
108                                                 bool display = match(chan->name, ld->glob.c_str());
109                                                 long users = chan->GetUserCounter();
110
111                                                 if ((users) && (display))
112                                                 {
113                                                         int counter = snprintf(buffer, MAXBUF, "322 %s %s %ld :[+%s] %s",u->nick, chan->name, users, chan->ChanModes(has_user), chan->topic);
114                                                         /* Increment total plus linefeed */
115                                                         amount_sent += counter + ServerNameSize;
116                                                         ServerInstance->Log(DEBUG, "m_safelist.so: Sent %ld of safe %ld / 4", amount_sent, u->sendqmax);
117                                                         u->WriteServ(std::string(buffer));
118                                                 }
119                                         }
120                                         else
121                                         {
122                                                 if (!chan)
123                                                 {
124                                                         if (!ld->list_ended)
125                                                         {
126                                                                 ld->list_ended = true;
127                                                                 u->WriteServ("323 %s :End of channel list.",u->nick);
128                                                         }
129                                                 }
130                                         }
131
132                                         ld->list_position++;
133                                 }
134                                 while ((chan != NULL) && (amount_sent < (u->sendqmax / 4)));
135                         }
136                 }
137
138                 ListTimer* MyTimer = new ListTimer(ServerInstance,1);
139                 ServerInstance->Timers->AddTimer(MyTimer);
140         }
141 };
142
143 class ModuleSafeList : public Module
144 {
145  private:
146          
147          ListTimer* MyTimer;
148  public:
149         ModuleSafeList(InspIRCd* Me) : Module::Module(Me)
150         {
151                 MyTimer = new ListTimer(ServerInstance,1);
152                 ServerInstance->Timers->AddTimer(MyTimer);
153         }
154  
155         virtual ~ModuleSafeList()
156         {
157                 ServerInstance->Timers->DelTimer(MyTimer);
158         }
159  
160         virtual Version GetVersion()
161         {
162                 return Version(1,0,0,0,VF_VENDOR,API_VERSION);
163         }
164  
165         void Implements(char* List)
166         {
167                 List[I_OnPreCommand] = List[I_OnCleanup] = List[I_OnUserQuit] = List[I_On005Numeric] = 1;
168         }
169
170         /*
171          * OnPreCommand()
172          *   Intercept the LIST command.
173          */ 
174         virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated, const std::string &original_line)
175         {
176                 /* If the command doesnt appear to be valid, we dont want to mess with it. */
177                 if (!validated)
178                         return 0;
179  
180                 if (command == "LIST")
181                 {
182                         return this->HandleList(parameters, pcnt, user);
183                 }
184                 return 0;
185         }
186         
187         /*
188          * HandleList()
189          *   Handle (override) the LIST command.
190          */
191         int HandleList(const char** parameters, int pcnt, userrec* user)
192         {
193                 /* First, let's check if the user is currently /list'ing */
194                 ListData *ld;
195                 user->GetExt("safelist_cache", ld);
196  
197                 if (ld)
198                 {
199                         /* user is already /list'ing, we don't want to do shit. */
200                         return 1;
201                 }
202
203                 time_t* last_list_time;
204                 user->GetExt("safelist_last", last_list_time);
205                 if (last_list_time)
206                 {
207                         if (ServerInstance->Time() < (*last_list_time)+60)
208                         {
209                                 user->WriteServ("NOTICE %s :*** Woah there, slow down a little, you can't /LIST so often!",user->nick);
210                                 return 1;
211                         }
212
213                         DELETE(last_list_time);
214                         user->Shrink("safelist_last");
215                 }
216  
217                 /*
218                  * start at channel 0! ;)
219                  */
220                 ld = new ListData(0,ServerInstance->Time(), pcnt ? parameters[0] : "*");
221                 user->Extend("safelist_cache", ld);
222                 listusers.push_back(user);
223
224                 time_t* llt = new time_t;
225                 *llt = ServerInstance->Time();
226                 user->Extend("safelist_last", llt);
227         
228                 return 1;
229         }
230
231         virtual void OnCleanup(int target_type, void* item)
232         {
233                 if(target_type == TYPE_USER)
234                 {
235                         userrec* u = (userrec*)item;
236                         ListData* ld;
237                         u->GetExt("safelist_cache", ld);
238                         if (ld)
239                         {
240                                 u->Shrink("safelist_cache");
241                                 DELETE(ld);
242                         }
243                         for (UserList::iterator iter = listusers.begin(); iter != listusers.end(); iter++)
244                         {
245                                 userrec* u2 = (userrec*)(*iter);
246                                 if (u2 == u)
247                                 {
248                                         listusers.erase(iter);
249                                         break;
250                                 }
251                         }
252                         time_t* last_list_time;
253                         u->GetExt("safelist_last", last_list_time);
254                         if (last_list_time)
255                         {
256                                 DELETE(last_list_time);
257                                 u->Shrink("safelist_last");
258                         }
259                 }
260         }
261
262         virtual void On005Numeric(std::string &output)
263         {
264                 output.append(" SAFELIST");
265         }
266
267         virtual void OnUserQuit(userrec* user, const std::string &message)
268         {
269                 this->OnCleanup(TYPE_USER,user);
270         }
271
272 };
273
274
275 class ModuleSafeListFactory : public ModuleFactory
276 {
277  public:
278         ModuleSafeListFactory()
279         {
280         }
281  
282         ~ModuleSafeListFactory()
283         {
284         }
285  
286         virtual Module * CreateModule(InspIRCd* Me)
287         {
288                 return new ModuleSafeList(Me);
289         }
290  
291 };
292  
293 extern "C" void * init_module( void )
294 {
295         return new ModuleSafeListFactory;
296 }