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