]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_safelist.cpp
Different way of displaying squit server group
[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                                         if ((chan) && (((!(chan->binarymodes & CM_PRIVATE)) && (!(chan->binarymodes & CM_SECRET))) || (has_channel(u,chan))))
101                                         {
102                                                 /* Increment total plus linefeed */
103                                                 int counter = snprintf(buffer,MAXBUF,"322 %s %s %d :[+%s] %s",u->nick,chan->name,usercount_i(chan),chanmodes(chan,has_channel(u,chan)),chan->topic);
104                                                 amount_sent += counter + 4 + Srv->GetServerName().length();
105                                                 log(DEBUG,"m_safelist.so: Sent %ld of safe %ld / 4",amount_sent,u->sendqmax);
106                                                 WriteServ_NoFormat(u->fd,buffer);
107                                         }
108                                         else
109                                         {
110                                                 if (!chan)
111                                                 {
112                                                         if (!ld->list_ended)
113                                                         {
114                                                                 ld->list_ended = true;
115                                                                 WriteServ(u->fd,"323 %s :End of channel list.",u->nick);
116                                                         }
117                                                 }
118                                         }
119
120                                         ld->list_position++;
121                                 }
122                                 while ((chan != NULL) && (amount_sent < (u->sendqmax / 4)));
123                         }
124                 }
125
126                 ListTimer* MyTimer = new ListTimer(1,Srv);
127                 Srv->AddTimer(MyTimer);
128         }
129 };
130
131 class ModuleSafeList : public Module
132 {
133  private:
134          Server *Srv;
135          ListTimer* MyTimer;
136  public:
137         ModuleSafeList(Server* Me) : Module::Module(Me)
138         {
139                 Srv = Me;
140
141                 MyTimer = new ListTimer(1,Srv);
142                 Srv->AddTimer(MyTimer);
143         }
144  
145         virtual ~ModuleSafeList()
146         {
147         }
148  
149         virtual Version GetVersion()
150         {
151                 return Version(1,0,0,0,VF_VENDOR);
152         }
153  
154         void Implements(char* List)
155         {
156                 List[I_OnPreCommand] = List[I_OnCleanup] = List[I_OnUserQuit] = List[I_On005Numeric] = 1;
157         }
158
159         /*
160          * OnPreCommand()
161          *   Intercept the LIST command.
162          */ 
163         virtual int OnPreCommand(std::string command, char **parameters, int pcnt, userrec *user, bool validated)
164         {
165                 /* If the command doesnt appear to be valid, we dont want to mess with it. */
166                 if (!validated)
167                         return 0;
168  
169                 if (command == "LIST")
170                 {
171                         return this->HandleList(parameters, pcnt, user);
172                 }
173                 return 0;
174         }
175         
176         /*
177          * HandleList()
178          *   Handle (override) the LIST command.
179          */
180         int HandleList(char** parameters, int pcnt, userrec* user)
181         {
182                 /* First, let's check if the user is currently /list'ing */
183                 ListData *ld = (ListData*)user->GetExt("safelist_cache");
184  
185                 if (ld)
186                 {
187                         /* user is already /list'ing, we don't want to do shit. */
188                         return 1;
189                 }
190
191                 time_t* last_list_time = (time_t*)user->GetExt("safelist_last");
192                 if (last_list_time)
193                 {
194                         if (TIME < (*last_list_time)+60)
195                         {
196                                 WriteServ(user->fd,"NOTICE %s :*** Woah there, slow down a little, you can't /LIST so often!",user->nick);
197                                 return 1;
198                         }
199
200                         delete last_list_time;
201                         user->Shrink("safelist_last");
202                 }
203  
204                 /*
205                  * start at channel 0! ;)
206                  */
207                 ld = new ListData(0,TIME);
208                 user->Extend("safelist_cache", (char*)ld);
209                 listusers.push_back(user);
210
211                 time_t* llt = new time_t;
212                 *llt = TIME;
213                 user->Extend("safelist_last",(char*)llt);
214         
215                 return 1;
216         }
217
218         virtual void OnCleanup(int target_type, void* item)
219         {
220                 if(target_type == TYPE_USER)
221                 {
222                         userrec* u = (userrec*)item;
223                         ListData* ld = (ListData*)u->GetExt("safelist_cache");
224                         if (ld)
225                         {
226                                 u->Shrink("safelist_cache");
227                                 delete ld;
228                         }
229                         for (UserList::iterator iter = listusers.begin(); iter != listusers.end(); iter++)
230                         {
231                                 userrec* u2 = (userrec*)(*iter);
232                                 if (u2 == u)
233                                 {
234                                         listusers.erase(iter);
235                                         break;
236                                 }
237                         }
238                         time_t* last_list_time = (time_t*)u->GetExt("safelist_last");
239                         if (last_list_time)
240                         {
241                                 delete last_list_time;
242                                 u->Shrink("safelist_last");
243                         }
244                 }
245         }
246
247         virtual void On005Numeric(std::string &output)
248         {
249                 output.append(" SAFELIST");
250         }
251
252         virtual void OnUserQuit(userrec* user, std::string message)
253         {
254                 this->OnCleanup(TYPE_USER,user);
255         }
256
257 };
258  
259  
260  
261 /******************************************************************************************************/
262  
263 class ModuleSafeListFactory : public ModuleFactory
264 {
265  public:
266         ModuleSafeListFactory()
267         {
268         }
269  
270         ~ModuleSafeListFactory()
271         {
272         }
273  
274         virtual Module * CreateModule(Server* Me)
275         {
276                 return new ModuleSafeList(Me);
277         }
278  
279 };
280  
281 extern "C" void * init_module( void )
282 {
283         return new ModuleSafeListFactory;
284 }