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