]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_safelist.cpp
In the grand tradition of huge fucking commits:
[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         }
46  
47         virtual ~ModuleSafeList()
48         {
49         }
50
51         virtual void OnRehash(User* user, const std::string &parameter)
52         {
53                 ConfigReader MyConf(ServerInstance);
54                 ThrottleSecs = MyConf.ReadInteger("safelist", "throttle", "60", 0, true);
55                 LimitList = MyConf.ReadInteger("safelist", "maxlisters", "50", 0, true);
56                 ServerNameSize = strlen(ServerInstance->Config->ServerName) + 4;
57                 global_listing = 0;
58         }
59  
60         virtual Version GetVersion()
61         {
62                 return Version(1,1,0,0,VF_VENDOR,API_VERSION);
63         }
64  
65         void Implements(char* List)
66         {
67                 List[I_OnBufferFlushed] = List[I_OnPreCommand] = List[I_OnCleanup] = List[I_OnUserQuit] = List[I_On005Numeric] = List[I_OnRehash] = 1;
68         }
69
70         /*
71          * OnPreCommand()
72          *   Intercept the LIST command.
73          */ 
74         virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, User *user, bool validated, const std::string &original_line)
75         {
76                 /* If the command doesnt appear to be valid, we dont want to mess with it. */
77                 if (!validated)
78                         return 0;
79  
80                 if (command == "LIST")
81                 {
82                         return this->HandleList(parameters, pcnt, user);
83                 }
84                 return 0;
85         }
86         
87         /*
88          * HandleList()
89          *   Handle (override) the LIST command.
90          */
91         int HandleList(const char** parameters, int pcnt, User* user)
92         {
93                 int minusers = 0, maxusers = 0;
94
95                 if (global_listing >= LimitList && !IS_OPER(user))
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)
115                 {
116                         if (*parameters[0] == '<')
117                         {
118                                 maxusers = atoi(parameters[0]+1);
119                                 ServerInstance->Log(DEBUG,"Max users: %d", maxusers);
120                                 pcnt = 0;
121                         }
122                         else if (*parameters[0] == '>')
123                         {
124                                 minusers = atoi(parameters[0]+1);
125                                 ServerInstance->Log(DEBUG,"Min users: %d", minusers);
126                                 pcnt = 0;
127                         }
128                 }
129
130                 time_t* last_list_time;
131                 user->GetExt("safelist_last", last_list_time);
132                 if (last_list_time)
133                 {
134                         if (ServerInstance->Time() < (*last_list_time)+ThrottleSecs)
135                         {
136                                 user->WriteServ("NOTICE %s :*** Woah there, slow down a little, you can't /LIST so often!",user->nick);
137                                 user->WriteServ("321 %s Channel :Users Name",user->nick);
138                                 user->WriteServ("323 %s :End of channel list.",user->nick);
139                                 return 1;
140                         }
141
142                         delete last_list_time;
143                         user->Shrink("safelist_last");
144                 }
145
146  
147                 /*
148                  * start at channel 0! ;)
149                  */
150                 ld = new ListData(0,ServerInstance->Time(), pcnt ? parameters[0] : "*", minusers, maxusers);
151                 user->Extend("safelist_cache", ld);
152
153                 time_t* llt = new time_t;
154                 *llt = ServerInstance->Time();
155                 user->Extend("safelist_last", llt);
156
157                 user->WriteServ("321 %s Channel :Users Name",user->nick);
158
159                 global_listing++;
160
161                 return 1;
162         }
163
164         virtual void OnBufferFlushed(User* user)
165         {
166                 char buffer[MAXBUF];
167                 ListData* ld;
168                 if (user->GetExt("safelist_cache", ld))
169                 {
170                         Channel* chan = NULL;
171                         long amount_sent = 0;
172                         do
173                         {
174                                 chan = ServerInstance->GetChannelIndex(ld->list_position);
175                                 bool has_user = (chan && chan->HasUser(user));
176                                 long users = chan ? chan->GetUserCounter() : 0;
177
178                                 bool too_few = (ld->minusers && (users <= ld->minusers));
179                                 bool too_many = (ld->maxusers && (users >= ld->maxusers));
180
181                                 if (chan && (too_many || too_few))
182                                 {
183                                         ld->list_position++;
184                                         continue;
185                                 }
186
187                                 if ((chan) && (chan->modes[CM_PRIVATE]))
188                                 {
189                                         bool display = (match(chan->name, ld->glob.c_str()) || (*chan->topic && match(chan->topic, ld->glob.c_str())));
190                                         if ((users) && (display))
191                                         {
192                                                 int counter = snprintf(buffer, MAXBUF, "322 %s *", user->nick);
193                                                 amount_sent += counter + ServerNameSize;
194                                                 user->WriteServ(std::string(buffer));
195                                         }
196                                 }
197                                 else if ((chan) && (((!(chan->modes[CM_PRIVATE])) && (!(chan->modes[CM_SECRET]))) || (has_user)))
198                                 {
199                                         bool display = (match(chan->name, ld->glob.c_str()) || (*chan->topic && match(chan->topic, ld->glob.c_str())));
200                                         if ((users) && (display))
201                                         {
202                                                 int counter = snprintf(buffer, MAXBUF, "322 %s %s %ld :[+%s] %s",user->nick, chan->name, users, chan->ChanModes(has_user), chan->topic);
203                                                 amount_sent += counter + ServerNameSize;
204                                                 user->WriteServ(std::string(buffer));
205                                         }
206                                 }
207                                 else
208                                 {
209                                         if (!chan)
210                                         {
211                                                 if (!ld->list_ended)
212                                                 {
213                                                         ld->list_ended = true;
214                                                         user->WriteServ("323 %s :End of channel list.",user->nick);
215                                                 }
216                                         }
217                                 }
218                                 ld->list_position++;
219                         }
220                         while ((chan != NULL) && (amount_sent < (user->sendqmax / 4)));
221                         if (ld->list_ended)
222                         {
223                                 user->Shrink("safelist_cache");
224                                 delete ld;
225                                 global_listing--;
226                         }
227                 }
228         }
229
230         virtual void OnCleanup(int target_type, void* item)
231         {
232                 if(target_type == TYPE_USER)
233                 {
234                         User* u = (User*)item;
235                         ListData* ld;
236                         u->GetExt("safelist_cache", ld);
237                         if (ld)
238                         {
239                                 u->Shrink("safelist_cache");
240                                 delete ld;
241                                 global_listing--;
242                         }
243                         time_t* last_list_time;
244                         u->GetExt("safelist_last", last_list_time);
245                         if (last_list_time)
246                         {
247                                 delete last_list_time;
248                                 u->Shrink("safelist_last");
249                         }
250                 }
251         }
252
253         virtual void On005Numeric(std::string &output)
254         {
255                 output.append(" SAFELIST");
256         }
257
258         virtual void OnUserQuit(User* user, const std::string &message, const std::string &oper_message)
259         {
260                 this->OnCleanup(TYPE_USER,user);
261         }
262
263 };
264
265 MODULE_INIT(ModuleSafeList)