]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_safelist.cpp
Header update: 2007 -> 2008
[user/henk/code/inspircd.git] / src / modules / m_safelist.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 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
68         /*
69          * OnPreCommand()
70          *   Intercept the LIST command.
71          */ 
72         virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, User *user, bool validated, const std::string &original_line)
73         {
74                 /* If the command doesnt appear to be valid, we dont want to mess with it. */
75                 if (!validated)
76                         return 0;
77  
78                 if (command == "LIST")
79                 {
80                         return this->HandleList(parameters, pcnt, user);
81                 }
82                 return 0;
83         }
84         
85         /*
86          * HandleList()
87          *   Handle (override) the LIST command.
88          */
89         int HandleList(const char** parameters, int pcnt, User* user)
90         {
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);
96                         user->WriteServ("321 %s Channel :Users Name",user->nick);
97                         user->WriteServ("323 %s :End of channel list.",user->nick);
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] == '<')
115                         {
116                                 maxusers = atoi(parameters[0]+1);
117                                 ServerInstance->Log(DEBUG,"Max users: %d", maxusers);
118                                 pcnt = 0;
119                         }
120                         else if (*parameters[0] == '>')
121                         {
122                                 minusers = atoi(parameters[0]+1);
123                                 ServerInstance->Log(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);
135                                 user->WriteServ("321 %s Channel :Users Name",user->nick);
136                                 user->WriteServ("323 %s :End of channel list.",user->nick);
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] : "*", 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->WriteServ("321 %s Channel :Users Name",user->nick);
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 has_user = (chan && chan->HasUser(user));
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) && (chan->modes[CM_PRIVATE]))
186                                 {
187                                         bool display = (match(chan->name, ld->glob.c_str()) || (*chan->topic && match(chan->topic, ld->glob.c_str())));
188                                         if ((users) && (display))
189                                         {
190                                                 int counter = snprintf(buffer, MAXBUF, "322 %s *", user->nick);
191                                                 amount_sent += counter + ServerNameSize;
192                                                 user->WriteServ(std::string(buffer));
193                                         }
194                                 }
195                                 else if ((chan) && (((!(chan->modes[CM_PRIVATE])) && (!(chan->modes[CM_SECRET]))) || (has_user)))
196                                 {
197                                         bool display = (match(chan->name, ld->glob.c_str()) || (*chan->topic && match(chan->topic, ld->glob.c_str())));
198                                         if ((users) && (display))
199                                         {
200                                                 int counter = snprintf(buffer, MAXBUF, "322 %s %s %ld :[+%s] %s",user->nick, chan->name, users, chan->ChanModes(has_user), chan->topic);
201                                                 amount_sent += counter + ServerNameSize;
202                                                 user->WriteServ(std::string(buffer));
203                                         }
204                                 }
205                                 else
206                                 {
207                                         if (!chan)
208                                         {
209                                                 if (!ld->list_ended)
210                                                 {
211                                                         ld->list_ended = true;
212                                                         user->WriteServ("323 %s :End of channel list.",user->nick);
213                                                 }
214                                         }
215                                 }
216                                 ld->list_position++;
217                         }
218                         while ((chan != NULL) && (amount_sent < (user->MyClass->GetSendqMax() / 4)));
219                         if (ld->list_ended)
220                         {
221                                 user->Shrink("safelist_cache");
222                                 delete ld;
223                                 global_listing--;
224                         }
225                 }
226         }
227
228         virtual void OnCleanup(int target_type, void* item)
229         {
230                 if(target_type == TYPE_USER)
231                 {
232                         User* u = (User*)item;
233                         ListData* ld;
234                         u->GetExt("safelist_cache", ld);
235                         if (ld)
236                         {
237                                 u->Shrink("safelist_cache");
238                                 delete ld;
239                                 global_listing--;
240                         }
241                         time_t* last_list_time;
242                         u->GetExt("safelist_last", last_list_time);
243                         if (last_list_time)
244                         {
245                                 delete last_list_time;
246                                 u->Shrink("safelist_last");
247                         }
248                 }
249         }
250
251         virtual void On005Numeric(std::string &output)
252         {
253                 output.append(" SAFELIST");
254         }
255
256         virtual void OnUserQuit(User* user, const std::string &message, const std::string &oper_message)
257         {
258                 this->OnCleanup(TYPE_USER,user);
259         }
260
261 };
262
263 MODULE_INIT(ModuleSafeList)