]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_safelist.cpp
Fix for bug #205 reported by nenolod (modules that erroneously check remote users...
[user/henk/code/inspircd.git] / src / modules / m_safelist.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13  
14 #include "users.h"
15 #include "channels.h"
16 #include "modules.h"
17 #include "configreader.h"
18 #include "inspircd.h"
19 #include "wildcard.h"
20
21 /** Holds a users m_safelist state
22  */
23 class ListData : public classbase
24 {
25  public:
26         long list_start;
27         long list_position;
28         bool list_ended;
29         const std::string glob;
30
31         ListData() : list_start(0), list_position(0), list_ended(false) {};
32         ListData(long pos, time_t t, const std::string &pattern) : list_start(t), list_position(pos), list_ended(false), glob(pattern) {};
33 };
34
35 /* $ModDesc: A module overriding /list, and making it safe - stop those sendq problems. */
36  
37 typedef std::vector<userrec *> UserList;
38 UserList listusers;    /* vector of people doing a /list */
39 class ListTimer *timer;
40
41 /** To create a timer which recurs every second, we inherit from InspTimer.
42  * InspTimer is only one-shot however, so at the end of each Tick() we simply
43  * insert another of ourselves into the pending queue :)
44  */
45 class ListTimer : public InspTimer
46 {
47  private:
48
49         char buffer[MAXBUF];
50         chanrec *chan;
51         InspIRCd* ServerInstance;
52         const std::string glob;
53         size_t ServerNameSize;
54
55  public:
56
57         ListTimer(InspIRCd* Instance, long interval) : InspTimer(interval,Instance->Time()), ServerInstance(Instance)
58         {
59                 ServerNameSize = 4 + strlen(ServerInstance->Config->ServerName);
60         }
61
62         virtual void Tick(time_t TIME)
63         {
64                 bool go_again = true;
65
66                 while (go_again)
67                 {
68                         go_again = false;
69                         for (UserList::iterator iter = listusers.begin(); iter != listusers.end(); iter++)
70                         {
71                                 /*
72                                  * What we do here:
73                                  *  - Get where they are up to
74                                  *  - If it's more than total number of channels, erase
75                                  *    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 ((size_t)ld->list_position > ServerInstance->chanlist->size())
82                                 {
83                                         u->Shrink("safelist_cache");
84                                         DELETE(ld);
85                                         listusers.erase(iter);
86                                         go_again = true;
87                                         break;
88                                 }
89                                 chan = NULL;
90                                 /* Attempt to fill up to 25% the user's sendq with /LIST output */
91                                 long amount_sent = 0;
92                                 do
93                                 {
94                                         if (!ld->list_position)
95                                                 u->WriteServ("321 %s Channel :Users Name",u->nick);
96                                         chan = ServerInstance->GetChannelIndex(ld->list_position);
97                                         /* spool details */
98                                         bool has_user = (chan && chan->HasUser(u));
99                                         if ((chan) && (chan->modes[CM_PRIVATE]))
100                                         {
101                                                 bool display = match(chan->name, ld->glob.c_str());
102                                                 long users = chan->GetUserCounter();
103                                                 if ((users) && (display))
104                                                 {
105                                                         int counter = snprintf(buffer, MAXBUF, "322 %s *", u->nick);
106                                                         amount_sent += counter + ServerNameSize;
107                                                         u->WriteServ(std::string(buffer));
108                                                 }
109                                         }
110                                         else if ((chan) && (((!(chan->modes[CM_PRIVATE])) && (!(chan->modes[CM_SECRET]))) || (has_user)))
111                                         {
112                                                 bool display = match(chan->name, ld->glob.c_str());
113                                                 long users = chan->GetUserCounter();
114
115                                                 if ((users) && (display))
116                                                 {
117                                                         int counter = snprintf(buffer, MAXBUF, "322 %s %s %ld :[+%s] %s",u->nick, chan->name, users, chan->ChanModes(has_user), chan->topic);
118                                                         /* Increment total plus linefeed */
119                                                         amount_sent += counter + ServerNameSize;
120                                                         u->WriteServ(std::string(buffer));
121                                                 }
122                                         }
123                                         else
124                                         {
125                                                 if (!chan)
126                                                 {
127                                                         if (!ld->list_ended)
128                                                         {
129                                                                 ld->list_ended = true;
130                                                                 u->WriteServ("323 %s :End of channel list.",u->nick);
131                                                         }
132                                                 }
133                                         }
134
135                                         ld->list_position++;
136                                 }
137                                 while ((chan != NULL) && (amount_sent < (u->sendqmax / 4)));
138                         }
139                 }
140
141                 if (listusers.size())
142                 {
143                         timer = new ListTimer(ServerInstance,1);
144                         ServerInstance->Timers->AddTimer(timer);
145                 }
146                 else
147                 {
148                         timer = NULL;
149                 }
150         }
151 };
152
153 class ModuleSafeList : public Module
154 {
155  public:
156         ModuleSafeList(InspIRCd* Me) : Module::Module(Me)
157         {
158                 timer = NULL;
159         }
160  
161         virtual ~ModuleSafeList()
162         {
163                 if (timer)
164                         ServerInstance->Timers->DelTimer(timer);
165         }
166  
167         virtual Version GetVersion()
168         {
169                 return Version(1,1,0,0,VF_VENDOR,API_VERSION);
170         }
171  
172         void Implements(char* List)
173         {
174                 List[I_OnPreCommand] = List[I_OnCleanup] = List[I_OnUserQuit] = List[I_On005Numeric] = 1;
175         }
176
177         /*
178          * OnPreCommand()
179          *   Intercept the LIST command.
180          */ 
181         virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated, const std::string &original_line)
182         {
183                 /* If the command doesnt appear to be valid, we dont want to mess with it. */
184                 if (!validated)
185                         return 0;
186  
187                 if (command == "LIST")
188                 {
189                         return this->HandleList(parameters, pcnt, user);
190                 }
191                 return 0;
192         }
193         
194         /*
195          * HandleList()
196          *   Handle (override) the LIST command.
197          */
198         int HandleList(const char** parameters, int pcnt, userrec* user)
199         {
200                 /* First, let's check if the user is currently /list'ing */
201                 ListData *ld;
202                 user->GetExt("safelist_cache", ld);
203  
204                 if (ld)
205                 {
206                         /* user is already /list'ing, we don't want to do shit. */
207                         return 1;
208                 }
209
210                 /* Work around mIRC suckyness. YOU SUCK, KHALED! */
211                 if ((pcnt == 1) && (*parameters[0] == '<'))
212                         pcnt = 0;
213
214                 time_t* last_list_time;
215                 user->GetExt("safelist_last", last_list_time);
216                 if (last_list_time)
217                 {
218                         if (ServerInstance->Time() < (*last_list_time)+60)
219                         {
220                                 user->WriteServ("NOTICE %s :*** Woah there, slow down a little, you can't /LIST so often!",user->nick);
221                                 user->WriteServ("321 %s Channel :Users Name",user->nick);
222                                 user->WriteServ("323 %s :End of channel list.",user->nick);
223                                 return 1;
224                         }
225
226                         DELETE(last_list_time);
227                         user->Shrink("safelist_last");
228                 }
229  
230                 /*
231                  * start at channel 0! ;)
232                  */
233                 ld = new ListData(0,ServerInstance->Time(), pcnt ? parameters[0] : "*");
234                 user->Extend("safelist_cache", ld);
235                 listusers.push_back(user);
236
237                 time_t* llt = new time_t;
238                 *llt = ServerInstance->Time();
239                 user->Extend("safelist_last", llt);
240
241                 if (!timer)
242                 {
243                         timer = new ListTimer(ServerInstance,1);
244                         ServerInstance->Timers->AddTimer(timer);
245                 }
246
247                 return 1;
248         }
249
250         virtual void OnCleanup(int target_type, void* item)
251         {
252                 if(target_type == TYPE_USER)
253                 {
254                         userrec* u = (userrec*)item;
255                         ListData* ld;
256                         u->GetExt("safelist_cache", ld);
257                         if (ld)
258                         {
259                                 u->Shrink("safelist_cache");
260                                 DELETE(ld);
261                         }
262                         for (UserList::iterator iter = listusers.begin(); iter != listusers.end(); iter++)
263                         {
264                                 userrec* u2 = (userrec*)(*iter);
265                                 if (u2 == u)
266                                 {
267                                         listusers.erase(iter);
268                                         break;
269                                 }
270                         }
271                         time_t* last_list_time;
272                         u->GetExt("safelist_last", last_list_time);
273                         if (last_list_time)
274                         {
275                                 DELETE(last_list_time);
276                                 u->Shrink("safelist_last");
277                         }
278                 }
279         }
280
281         virtual void On005Numeric(std::string &output)
282         {
283                 output.append(" SAFELIST");
284         }
285
286         virtual void OnUserQuit(userrec* user, const std::string &message)
287         {
288                 this->OnCleanup(TYPE_USER,user);
289         }
290
291 };
292
293
294 class ModuleSafeListFactory : public ModuleFactory
295 {
296  public:
297         ModuleSafeListFactory()
298         {
299         }
300  
301         ~ModuleSafeListFactory()
302         {
303         }
304  
305         virtual Module * CreateModule(InspIRCd* Me)
306         {
307                 return new ModuleSafeList(Me);
308         }
309  
310 };
311  
312 extern "C" void * init_module( void )
313 {
314         return new ModuleSafeListFactory;
315 }