]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_safelist.cpp
Someone is getting slapped for this; the new hidesplits/hidebans behavior doesn't...
[user/henk/code/inspircd.git] / src / modules / m_safelist.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *          the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13  
14 #include "users.h"
15 #include "channels.h"
16 #include "modules.h"
17 #include "configreader.h"
18 #include "inspircd.h"
19 #include "wildcard.h"
20
21 /** Holds a users m_safelist state
22  */
23 class ListData : public classbase
24 {
25  public:
26         long list_start;
27         long list_position;
28         bool list_ended;
29         const std::string glob;
30
31         ListData() : list_start(0), list_position(0), list_ended(false) {};
32         ListData(long pos, time_t t, const std::string &pattern) : list_start(t), list_position(pos), list_ended(false), glob(pattern) {};
33 };
34
35 /* $ModDesc: A module overriding /list, and making it safe - stop those sendq problems. */
36
37 class ModuleSafeList : public Module
38 {
39         time_t ThrottleSecs;
40         size_t ServerNameSize;
41         int global_listing;
42         int LimitList;
43  public:
44         ModuleSafeList(InspIRCd* Me) : Module::Module(Me)
45         {
46                 OnRehash(NULL, "");
47         }
48  
49         virtual ~ModuleSafeList()
50         {
51         }
52
53         virtual void OnRehash(userrec* user, const std::string &parameter)
54         {
55                 ConfigReader MyConf(ServerInstance);
56                 ThrottleSecs = MyConf.ReadInteger("safelist", "throttle", "60", 0, true);
57                 LimitList = MyConf.ReadInteger("safelist", "maxlisters", "50", 0, true);
58                 ServerNameSize = strlen(ServerInstance->Config->ServerName) + 4;
59                 global_listing = 0;
60         }
61  
62         virtual Version GetVersion()
63         {
64                 return Version(1,1,0,0,VF_VENDOR,API_VERSION);
65         }
66  
67         void Implements(char* List)
68         {
69                 List[I_OnBufferFlushed] = List[I_OnPreCommand] = List[I_OnCleanup] = List[I_OnUserQuit] = List[I_On005Numeric] = List[I_OnRehash] = 1;
70         }
71
72         /*
73          * OnPreCommand()
74          *   Intercept the LIST command.
75          */ 
76         virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated, const std::string &original_line)
77         {
78                 /* If the command doesnt appear to be valid, we dont want to mess with it. */
79                 if (!validated)
80                         return 0;
81  
82                 if (command == "LIST")
83                 {
84                         return this->HandleList(parameters, pcnt, user);
85                 }
86                 return 0;
87         }
88         
89         /*
90          * HandleList()
91          *   Handle (override) the LIST command.
92          */
93         int HandleList(const char** parameters, int pcnt, userrec* user)
94         {
95                 if (global_listing >= LimitList)
96                 {
97                         user->WriteServ("NOTICE %s :*** Server load is currently too heavy. Please try again later.", user->nick);
98                         user->WriteServ("321 %s Channel :Users Name",user->nick);
99                         user->WriteServ("323 %s :End of channel list.",user->nick);
100                         return 1;
101                 }
102
103                 /* First, let's check if the user is currently /list'ing */
104                 ListData *ld;
105                 user->GetExt("safelist_cache", ld);
106  
107                 if (ld)
108                 {
109                         /* user is already /list'ing, we don't want to do shit. */
110                         return 1;
111                 }
112
113                 /* Work around mIRC suckyness. YOU SUCK, KHALED! */
114                 if ((pcnt == 1) && (*parameters[0] == '<'))
115                         pcnt = 0;
116
117                 time_t* last_list_time;
118                 user->GetExt("safelist_last", last_list_time);
119                 if (last_list_time)
120                 {
121                         if (ServerInstance->Time() < (*last_list_time)+ThrottleSecs)
122                         {
123                                 user->WriteServ("NOTICE %s :*** Woah there, slow down a little, you can't /LIST so often!",user->nick);
124                                 user->WriteServ("321 %s Channel :Users Name",user->nick);
125                                 user->WriteServ("323 %s :End of channel list.",user->nick);
126                                 return 1;
127                         }
128
129                         DELETE(last_list_time);
130                         user->Shrink("safelist_last");
131                 }
132
133  
134                 /*
135                  * start at channel 0! ;)
136                  */
137                 ld = new ListData(0,ServerInstance->Time(), pcnt ? parameters[0] : "*");
138                 user->Extend("safelist_cache", ld);
139
140                 time_t* llt = new time_t;
141                 *llt = ServerInstance->Time();
142                 user->Extend("safelist_last", llt);
143
144                 user->WriteServ("321 %s Channel :Users Name",user->nick);
145
146                 global_listing++;
147
148                 return 1;
149         }
150
151         virtual void OnBufferFlushed(userrec* user)
152         {
153                 char buffer[MAXBUF];
154                 ListData* ld;
155                 if (user->GetExt("safelist_cache", ld))
156                 {
157                         chanrec* chan = NULL;
158                         long amount_sent = 0;
159                         do
160                         {
161                                 chan = ServerInstance->GetChannelIndex(ld->list_position);
162                                 bool has_user = (chan && chan->HasUser(user));
163                                 if ((chan) && (chan->modes[CM_PRIVATE]))
164                                 {
165                                         bool display = match(chan->name, ld->glob.c_str());
166                                         long users = chan->GetUserCounter();
167                                         if ((users) && (display))
168                                         {
169                                                 int counter = snprintf(buffer, MAXBUF, "322 %s *", user->nick);
170                                                 amount_sent += counter + ServerNameSize;
171                                                 user->WriteServ(std::string(buffer));
172                                         }
173                                 }
174                                 else if ((chan) && (((!(chan->modes[CM_PRIVATE])) && (!(chan->modes[CM_SECRET]))) || (has_user)))
175                                 {
176                                         bool display = match(chan->name, ld->glob.c_str());
177                                         long users = chan->GetUserCounter();
178                                         if ((users) && (display))
179                                         {
180                                                 int counter = snprintf(buffer, MAXBUF, "322 %s %s %ld :[+%s] %s",user->nick, chan->name, users, chan->ChanModes(has_user), chan->topic);
181                                                 amount_sent += counter + ServerNameSize;
182                                                 user->WriteServ(std::string(buffer));
183                                         }
184                                 }
185                                 else
186                                 {
187                                         if (!chan)
188                                         {
189                                                 if (!ld->list_ended)
190                                                 {
191                                                         ld->list_ended = true;
192                                                         user->WriteServ("323 %s :End of channel list.",user->nick);
193                                                 }
194                                         }
195                                 }
196                                 ld->list_position++;
197                         }
198                         while ((chan != NULL) && (amount_sent < (user->sendqmax / 4)));
199                         if (ld->list_ended)
200                         {
201                                 user->Shrink("safelist_cache");
202                                 DELETE(ld);
203                                 global_listing--;
204                         }
205                 }
206         }
207
208         virtual void OnCleanup(int target_type, void* item)
209         {
210                 if(target_type == TYPE_USER)
211                 {
212                         userrec* u = (userrec*)item;
213                         ListData* ld;
214                         u->GetExt("safelist_cache", ld);
215                         if (ld)
216                         {
217                                 u->Shrink("safelist_cache");
218                                 DELETE(ld);
219                                 global_listing--;
220                         }
221                         time_t* last_list_time;
222                         u->GetExt("safelist_last", last_list_time);
223                         if (last_list_time)
224                         {
225                                 DELETE(last_list_time);
226                                 u->Shrink("safelist_last");
227                         }
228                 }
229         }
230
231         virtual void On005Numeric(std::string &output)
232         {
233                 output.append(" SAFELIST");
234         }
235
236         virtual void OnUserQuit(userrec* user, const std::string &message)
237         {
238                 this->OnCleanup(TYPE_USER,user);
239         }
240
241 };
242
243
244 class ModuleSafeListFactory : public ModuleFactory
245 {
246  public:
247         ModuleSafeListFactory()
248         {
249         }
250  
251         ~ModuleSafeListFactory()
252         {
253         }
254  
255         virtual Module * CreateModule(InspIRCd* Me)
256         {
257                 return new ModuleSafeList(Me);
258         }
259  
260 };
261  
262 extern "C" void * init_module( void )
263 {
264         return new ModuleSafeListFactory;
265 }