]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_safelist.cpp
Fix these to use new hook system (u_listmode wasnt fixed yet)
[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 "wildcard.h"
16
17 /** Holds a users m_safelist state
18  */
19 class ListData : public classbase
20 {
21  public:
22         long list_start;
23         long list_position;
24         bool list_ended;
25         const std::string glob;
26         int minusers;
27         int maxusers;
28
29         ListData() : list_start(0), list_position(0), list_ended(false) {};
30         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) {};
31 };
32
33 /* $ModDesc: A module overriding /list, and making it safe - stop those sendq problems. */
34
35 class ModuleSafeList : public Module
36 {
37         time_t ThrottleSecs;
38         size_t ServerNameSize;
39         int global_listing;
40         int LimitList;
41  public:
42         ModuleSafeList(InspIRCd* Me) : Module(Me)
43         {
44                 OnRehash(NULL, "");
45                 Implementation eventlist[] = { I_OnBufferFlushed, I_OnPreCommand, I_OnCleanup, I_OnUserQuit, I_On005Numeric, I_OnRehash };
46                 ServerInstance->Modules->Attach(eventlist, this, 6);
47         }
48  
49         virtual ~ModuleSafeList()
50         {
51         }
52
53         virtual void OnRehash(User* 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, User *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, User* user)
94         {
95                 int minusers = 0, maxusers = 0;
96
97                 if (global_listing >= LimitList && !IS_OPER(user))
98                 {
99                         user->WriteServ("NOTICE %s :*** Server load is currently too heavy. Please try again later.", user->nick);
100                         user->WriteServ("321 %s Channel :Users Name",user->nick);
101                         user->WriteServ("323 %s :End of channel list.",user->nick);
102                         return 1;
103                 }
104
105                 /* First, let's check if the user is currently /list'ing */
106                 ListData *ld;
107                 user->GetExt("safelist_cache", ld);
108  
109                 if (ld)
110                 {
111                         /* user is already /list'ing, we don't want to do shit. */
112                         return 1;
113                 }
114
115                 /* Work around mIRC suckyness. YOU SUCK, KHALED! */
116                 if (pcnt == 1)
117                 {
118                         if (*parameters[0] == '<')
119                         {
120                                 maxusers = atoi(parameters[0]+1);
121                                 ServerInstance->Log(DEBUG,"Max users: %d", maxusers);
122                                 pcnt = 0;
123                         }
124                         else if (*parameters[0] == '>')
125                         {
126                                 minusers = atoi(parameters[0]+1);
127                                 ServerInstance->Log(DEBUG,"Min users: %d", minusers);
128                                 pcnt = 0;
129                         }
130                 }
131
132                 time_t* last_list_time;
133                 user->GetExt("safelist_last", last_list_time);
134                 if (last_list_time)
135                 {
136                         if (ServerInstance->Time() < (*last_list_time)+ThrottleSecs)
137                         {
138                                 user->WriteServ("NOTICE %s :*** Woah there, slow down a little, you can't /LIST so often!",user->nick);
139                                 user->WriteServ("321 %s Channel :Users Name",user->nick);
140                                 user->WriteServ("323 %s :End of channel list.",user->nick);
141                                 return 1;
142                         }
143
144                         delete last_list_time;
145                         user->Shrink("safelist_last");
146                 }
147
148  
149                 /*
150                  * start at channel 0! ;)
151                  */
152                 ld = new ListData(0,ServerInstance->Time(), pcnt ? parameters[0] : "*", minusers, maxusers);
153                 user->Extend("safelist_cache", ld);
154
155                 time_t* llt = new time_t;
156                 *llt = ServerInstance->Time();
157                 user->Extend("safelist_last", llt);
158
159                 user->WriteServ("321 %s Channel :Users Name",user->nick);
160
161                 global_listing++;
162
163                 return 1;
164         }
165
166         virtual void OnBufferFlushed(User* user)
167         {
168                 char buffer[MAXBUF];
169                 ListData* ld;
170                 if (user->GetExt("safelist_cache", ld))
171                 {
172                         Channel* chan = NULL;
173                         unsigned long amount_sent = 0;
174                         do
175                         {
176                                 chan = ServerInstance->GetChannelIndex(ld->list_position);
177                                 bool has_user = (chan && chan->HasUser(user));
178                                 long users = chan ? chan->GetUserCounter() : 0;
179
180                                 bool too_few = (ld->minusers && (users <= ld->minusers));
181                                 bool too_many = (ld->maxusers && (users >= ld->maxusers));
182
183                                 if (chan && (too_many || too_few))
184                                 {
185                                         ld->list_position++;
186                                         continue;
187                                 }
188
189                                 if ((chan) && (chan->modes[CM_PRIVATE]))
190                                 {
191                                         bool display = (match(chan->name, ld->glob.c_str()) || (*chan->topic && match(chan->topic, ld->glob.c_str())));
192                                         if ((users) && (display))
193                                         {
194                                                 int counter = snprintf(buffer, MAXBUF, "322 %s *", user->nick);
195                                                 amount_sent += counter + ServerNameSize;
196                                                 user->WriteServ(std::string(buffer));
197                                         }
198                                 }
199                                 else if ((chan) && (((!(chan->modes[CM_PRIVATE])) && (!(chan->modes[CM_SECRET]))) || (has_user)))
200                                 {
201                                         bool display = (match(chan->name, ld->glob.c_str()) || (*chan->topic && match(chan->topic, ld->glob.c_str())));
202                                         if ((users) && (display))
203                                         {
204                                                 int counter = snprintf(buffer, MAXBUF, "322 %s %s %ld :[+%s] %s",user->nick, chan->name, users, chan->ChanModes(has_user), chan->topic);
205                                                 amount_sent += counter + ServerNameSize;
206                                                 user->WriteServ(std::string(buffer));
207                                         }
208                                 }
209                                 else
210                                 {
211                                         if (!chan)
212                                         {
213                                                 if (!ld->list_ended)
214                                                 {
215                                                         ld->list_ended = true;
216                                                         user->WriteServ("323 %s :End of channel list.",user->nick);
217                                                 }
218                                         }
219                                 }
220                                 ld->list_position++;
221                         }
222                         while ((chan != NULL) && (amount_sent < (user->MyClass->GetSendqMax() / 4)));
223                         if (ld->list_ended)
224                         {
225                                 user->Shrink("safelist_cache");
226                                 delete ld;
227                                 global_listing--;
228                         }
229                 }
230         }
231
232         virtual void OnCleanup(int target_type, void* item)
233         {
234                 if(target_type == TYPE_USER)
235                 {
236                         User* u = (User*)item;
237                         ListData* ld;
238                         u->GetExt("safelist_cache", ld);
239                         if (ld)
240                         {
241                                 u->Shrink("safelist_cache");
242                                 delete ld;
243                                 global_listing--;
244                         }
245                         time_t* last_list_time;
246                         u->GetExt("safelist_last", last_list_time);
247                         if (last_list_time)
248                         {
249                                 delete last_list_time;
250                                 u->Shrink("safelist_last");
251                         }
252                 }
253         }
254
255         virtual void On005Numeric(std::string &output)
256         {
257                 output.append(" SAFELIST");
258         }
259
260         virtual void OnUserQuit(User* user, const std::string &message, const std::string &oper_message)
261         {
262                 this->OnCleanup(TYPE_USER,user);
263         }
264
265 };
266
267 MODULE_INIT(ModuleSafeList)