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