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