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