]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_safelist.cpp
fixed some indentation and spacing in modules
[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,2,0,0,VF_VENDOR,API_VERSION);
65         }
66
67
68         /*
69          * OnPreCommand()
70          *   Intercept the LIST command.
71          */
72         virtual int OnPreCommand(std::string &command, std::vector<std::string> &parameters, 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, user);
81                 }
82                 return 0;
83         }
84
85         /*
86          * HandleList()
87          *   Handle (override) the LIST command.
88          */
89         int HandleList(const std::vector<std::string> &parameters, User* user)
90         {
91                 int pcnt = parameters.size();
92                 int minusers = 0, maxusers = 0;
93
94                 if (global_listing >= LimitList && !IS_OPER(user))
95                 {
96                         user->WriteServ("NOTICE %s :*** Server load is currently too heavy. Please try again later.", user->nick.c_str());
97                         user->WriteNumeric(321, "%s Channel :Users Name",user->nick.c_str());
98                         user->WriteNumeric(323, "%s :End of channel list.",user->nick.c_str());
99                         return 1;
100                 }
101
102                 /* First, let's check if the user is currently /list'ing */
103                 ListData *ld;
104                 user->GetExt("safelist_cache", ld);
105
106                 if (ld)
107                 {
108                         /* user is already /list'ing, we don't want to do shit. */
109                         return 1;
110                 }
111
112                 /* Work around mIRC suckyness. YOU SUCK, KHALED! */
113                 if (pcnt == 1)
114                 {
115                         if (parameters[0][0] == '<')
116                         {
117                                 maxusers = atoi(parameters[0].c_str()+1);
118                                 ServerInstance->Logs->Log("m_safelist",DEBUG,"Max users: %d", maxusers);
119                                 pcnt = 0;
120                         }
121                         else if (parameters[0][0] == '>')
122                         {
123                                 minusers = atoi(parameters[0].c_str()+1);
124                                 ServerInstance->Logs->Log("m_safelist",DEBUG,"Min users: %d", minusers);
125                                 pcnt = 0;
126                         }
127                 }
128
129                 time_t* last_list_time;
130                 user->GetExt("safelist_last", last_list_time);
131                 if (last_list_time)
132                 {
133                         if (ServerInstance->Time() < (*last_list_time)+ThrottleSecs)
134                         {
135                                 user->WriteServ("NOTICE %s :*** Woah there, slow down a little, you can't /LIST so often!",user->nick.c_str());
136                                 user->WriteNumeric(321, "%s Channel :Users Name",user->nick.c_str());
137                                 user->WriteNumeric(323, "%s :End of channel list.",user->nick.c_str());
138                                 return 1;
139                         }
140
141                         delete last_list_time;
142                         user->Shrink("safelist_last");
143                 }
144
145
146                 /*
147                  * start at channel 0! ;)
148                  */
149                 ld = new ListData(0,ServerInstance->Time(), pcnt ? parameters[0] : "*", minusers, maxusers);
150                 user->Extend("safelist_cache", ld);
151
152                 time_t* llt = new time_t;
153                 *llt = ServerInstance->Time();
154                 user->Extend("safelist_last", llt);
155
156                 user->WriteNumeric(321, "%s Channel :Users Name",user->nick.c_str());
157
158                 global_listing++;
159
160                 return 1;
161         }
162
163         virtual void OnBufferFlushed(User* user)
164         {
165                 char buffer[MAXBUF];
166                 ListData* ld;
167                 if (user->GetExt("safelist_cache", ld))
168                 {
169                         Channel* chan = NULL;
170                         unsigned long amount_sent = 0;
171                         do
172                         {
173                                 chan = ServerInstance->GetChannelIndex(ld->list_position);
174                                 bool has_user = (chan && chan->HasUser(user));
175                                 long users = chan ? chan->GetUserCounter() : 0;
176
177                                 bool too_few = (ld->minusers && (users <= ld->minusers));
178                                 bool too_many = (ld->maxusers && (users >= ld->maxusers));
179
180                                 if (chan && (too_many || too_few))
181                                 {
182                                         ld->list_position++;
183                                         continue;
184                                 }
185
186                                 if ((chan) && (chan->modes[CM_PRIVATE]) && (!IS_OPER(user)))
187                                 {
188                                         bool display = (match(chan->name, ld->glob) || (!chan->topic.empty() && match(chan->topic, ld->glob)));
189                                         if ((users) && (display))
190                                         {
191                                                 int counter = snprintf(buffer, MAXBUF, "322 %s * %ld :", user->nick.c_str(), users);
192                                                 amount_sent += counter + ServerNameSize;
193                                                 user->WriteServ(std::string(buffer));
194                                         }
195                                 }
196                                 else if ((chan) && ((((!(chan->IsModeSet('p'))) && (!(chan->IsModeSet('s'))))) || (has_user) || IS_OPER(user)))
197                                 {
198                                         bool display = (match(chan->name, ld->glob) || (!chan->topic.empty() && match(chan->topic, ld->glob)));
199                                         if ((users) && (display))
200                                         {
201                                                 int counter = snprintf(buffer, MAXBUF, "322 %s %s %ld :[+%s] %s", user->nick.c_str(), chan->name.c_str(), users, chan->ChanModes(has_user || IS_OPER(user)), chan->topic.c_str());
202                                                 amount_sent += counter + ServerNameSize;
203                                                 user->WriteServ(std::string(buffer));
204                                         }
205                                 }
206                                 else
207                                 {
208                                         if (!chan)
209                                         {
210                                                 if (!ld->list_ended)
211                                                 {
212                                                         ld->list_ended = true;
213                                                         user->WriteNumeric(323, "%s :End of channel list.",user->nick.c_str());
214                                                 }
215                                         }
216                                 }
217                                 ld->list_position++;
218                         }
219                         while ((chan != NULL) && (amount_sent < (user->MyClass->GetSendqMax() / 4)));
220                         if (ld->list_ended)
221                         {
222                                 user->Shrink("safelist_cache");
223                                 delete ld;
224                                 global_listing--;
225                         }
226                 }
227         }
228
229         virtual void OnCleanup(int target_type, void* item)
230         {
231                 if(target_type == TYPE_USER)
232                 {
233                         User* u = (User*)item;
234                         ListData* ld;
235                         u->GetExt("safelist_cache", ld);
236                         if (ld)
237                         {
238                                 u->Shrink("safelist_cache");
239                                 delete ld;
240                                 global_listing--;
241                         }
242                         time_t* last_list_time;
243                         u->GetExt("safelist_last", last_list_time);
244                         if (last_list_time)
245                         {
246                                 delete last_list_time;
247                                 u->Shrink("safelist_last");
248                         }
249                 }
250         }
251
252         virtual void On005Numeric(std::string &output)
253         {
254                 output.append(" SAFELIST");
255         }
256
257         virtual void OnUserQuit(User* user, const std::string &message, const std::string &oper_message)
258         {
259                 this->OnCleanup(TYPE_USER,user);
260         }
261
262 };
263
264 MODULE_INIT(ModuleSafeList)