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