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