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