]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_safelist.cpp
3a0ef37ddc36be3ba9ddf389af17e33679c76db6
[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 class ListTimer *timer;
45
46 /** To create a timer which recurs every second, we inherit from InspTimer.
47  * InspTimer is only one-shot however, so at the end of each Tick() we simply
48  * insert another of ourselves into the pending queue :)
49  */
50 class ListTimer : public InspTimer
51 {
52  private:
53
54         char buffer[MAXBUF];
55         chanrec *chan;
56         InspIRCd* ServerInstance;
57         const std::string glob;
58         size_t ServerNameSize;
59
60  public:
61
62         ListTimer(InspIRCd* Instance, long interval) : InspTimer(interval,Instance->Time()), ServerInstance(Instance)
63         {
64                 ServerNameSize = 4 + strlen(ServerInstance->Config->ServerName);
65         }
66
67         virtual void Tick(time_t TIME)
68         {
69                 bool go_again = true;
70
71                 while (go_again)
72                 {
73                         go_again = false;
74                         for (UserList::iterator iter = listusers.begin(); iter != listusers.end(); iter++)
75                         {
76                                 /*
77                                  * What we do here:
78                                  *  - Get where they are up to
79                                  *  - If it's more than total number of channels, erase
80                                  *    them from the iterator, set go_again to true
81                                  *  - If not, spool more channels
82                                  */
83                                 userrec* u = (userrec*)(*iter);
84                                 ListData* ld;
85                                 u->GetExt("safelist_cache", ld);
86                                 if ((size_t)ld->list_position > ServerInstance->chanlist.size())
87                                 {
88                                         u->Shrink("safelist_cache");
89                                         DELETE(ld);
90                                         listusers.erase(iter);
91                                         go_again = true;
92                                         break;
93                                 }
94
95                                 ServerInstance->Log(DEBUG, "m_safelist.so: resuming spool of list to client %s at channel %ld", u->nick, ld->list_position);
96                                 chan = NULL;
97                                 /* Attempt to fill up to 25% the user's sendq with /LIST output */
98                                 long amount_sent = 0;
99                                 do
100                                 {
101                                         ServerInstance->Log(DEBUG,"Channel %ld",ld->list_position);
102                                         if (!ld->list_position)
103                                                 u->WriteServ("321 %s Channel :Users Name",u->nick);
104                                         chan = ServerInstance->GetChannelIndex(ld->list_position);
105                                         /* spool details */
106                                         bool has_user = (chan && chan->HasUser(u));
107                                         if ((chan) && (((!(chan->modes[CM_PRIVATE])) && (!(chan->modes[CM_SECRET]))) || (has_user)))
108                                         {
109                                                 bool display = match(chan->name, ld->glob.c_str());
110                                                 long users = chan->GetUserCounter();
111
112                                                 if ((users) && (display))
113                                                 {
114                                                         int counter = snprintf(buffer, MAXBUF, "322 %s %s %ld :[+%s] %s",u->nick, chan->name, users, chan->ChanModes(has_user), chan->topic);
115                                                         /* Increment total plus linefeed */
116                                                         amount_sent += counter + ServerNameSize;
117                                                         ServerInstance->Log(DEBUG, "m_safelist.so: Sent %ld of safe %ld / 4", amount_sent, u->sendqmax);
118                                                         u->WriteServ(std::string(buffer));
119                                                 }
120                                         }
121                                         else
122                                         {
123                                                 if (!chan)
124                                                 {
125                                                         if (!ld->list_ended)
126                                                         {
127                                                                 ld->list_ended = true;
128                                                                 u->WriteServ("323 %s :End of channel list.",u->nick);
129                                                         }
130                                                 }
131                                         }
132
133                                         ld->list_position++;
134                                 }
135                                 while ((chan != NULL) && (amount_sent < (u->sendqmax / 4)));
136                         }
137                 }
138
139                 if (listusers.size())
140                 {
141                         timer = new ListTimer(ServerInstance,1);
142                         ServerInstance->Timers->AddTimer(timer);
143                 }
144                 else
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                                 return 1;
220                         }
221
222                         DELETE(last_list_time);
223                         user->Shrink("safelist_last");
224                 }
225  
226                 /*
227                  * start at channel 0! ;)
228                  */
229                 ld = new ListData(0,ServerInstance->Time(), pcnt ? parameters[0] : "*");
230                 user->Extend("safelist_cache", ld);
231                 listusers.push_back(user);
232
233                 time_t* llt = new time_t;
234                 *llt = ServerInstance->Time();
235                 user->Extend("safelist_last", llt);
236
237                 if (!timer)
238                 {
239                         timer = new ListTimer(ServerInstance,1);
240                         ServerInstance->Timers->AddTimer(timer);
241                 }
242
243                 return 1;
244         }
245
246         virtual void OnCleanup(int target_type, void* item)
247         {
248                 if(target_type == TYPE_USER)
249                 {
250                         userrec* u = (userrec*)item;
251                         ListData* ld;
252                         u->GetExt("safelist_cache", ld);
253                         if (ld)
254                         {
255                                 u->Shrink("safelist_cache");
256                                 DELETE(ld);
257                         }
258                         for (UserList::iterator iter = listusers.begin(); iter != listusers.end(); iter++)
259                         {
260                                 userrec* u2 = (userrec*)(*iter);
261                                 if (u2 == u)
262                                 {
263                                         listusers.erase(iter);
264                                         break;
265                                 }
266                         }
267                         time_t* last_list_time;
268                         u->GetExt("safelist_last", last_list_time);
269                         if (last_list_time)
270                         {
271                                 DELETE(last_list_time);
272                                 u->Shrink("safelist_last");
273                         }
274                 }
275         }
276
277         virtual void On005Numeric(std::string &output)
278         {
279                 output.append(" SAFELIST");
280         }
281
282         virtual void OnUserQuit(userrec* user, const std::string &message)
283         {
284                 this->OnCleanup(TYPE_USER,user);
285         }
286
287 };
288
289
290 class ModuleSafeListFactory : public ModuleFactory
291 {
292  public:
293         ModuleSafeListFactory()
294         {
295         }
296  
297         ~ModuleSafeListFactory()
298         {
299         }
300  
301         virtual Module * CreateModule(InspIRCd* Me)
302         {
303                 return new ModuleSafeList(Me);
304         }
305  
306 };
307  
308 extern "C" void * init_module( void )
309 {
310         return new ModuleSafeListFactory;
311 }