1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
17 #include "configreader.h"
21 /** Holds a users m_safelist state
23 class ListData : public classbase
29 const std::string glob;
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) {};
35 /* $ModDesc: A module overriding /list, and making it safe - stop those sendq problems. */
37 class ModuleSafeList : public Module
40 size_t ServerNameSize;
44 ModuleSafeList(InspIRCd* Me) : Module::Module(Me)
49 virtual ~ModuleSafeList()
53 virtual void OnRehash(userrec* user, const std::string ¶meter)
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;
62 virtual Version GetVersion()
64 return Version(1,1,0,0,VF_VENDOR,API_VERSION);
67 void Implements(char* List)
69 List[I_OnBufferFlushed] = List[I_OnPreCommand] = List[I_OnCleanup] = List[I_OnUserQuit] = List[I_On005Numeric] = List[I_OnRehash] = 1;
74 * Intercept the LIST command.
76 virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated, const std::string &original_line)
78 /* If the command doesnt appear to be valid, we dont want to mess with it. */
82 if (command == "LIST")
84 return this->HandleList(parameters, pcnt, user);
91 * Handle (override) the LIST command.
93 int HandleList(const char** parameters, int pcnt, userrec* user)
95 if (global_listing >= LimitList)
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);
103 /* First, let's check if the user is currently /list'ing */
105 user->GetExt("safelist_cache", ld);
109 /* user is already /list'ing, we don't want to do shit. */
113 /* Work around mIRC suckyness. YOU SUCK, KHALED! */
114 if ((pcnt == 1) && (*parameters[0] == '<'))
117 time_t* last_list_time;
118 user->GetExt("safelist_last", last_list_time);
121 if (ServerInstance->Time() < (*last_list_time)+ThrottleSecs)
123 user->WriteServ("NOTICE %s :*** Woah there, slow down a little, you can't /LIST so often!",user->nick);
124 user->WriteServ("321 %s Channel :Users Name",user->nick);
125 user->WriteServ("323 %s :End of channel list.",user->nick);
129 DELETE(last_list_time);
130 user->Shrink("safelist_last");
135 * start at channel 0! ;)
137 ld = new ListData(0,ServerInstance->Time(), pcnt ? parameters[0] : "*");
138 user->Extend("safelist_cache", ld);
140 time_t* llt = new time_t;
141 *llt = ServerInstance->Time();
142 user->Extend("safelist_last", llt);
144 user->WriteServ("321 %s Channel :Users Name",user->nick);
151 virtual void OnBufferFlushed(userrec* user)
155 if (user->GetExt("safelist_cache", ld))
157 chanrec* chan = NULL;
158 long amount_sent = 0;
161 chan = ServerInstance->GetChannelIndex(ld->list_position);
162 bool has_user = (chan && chan->HasUser(user));
163 if ((chan) && (chan->modes[CM_PRIVATE]))
165 bool display = match(chan->name, ld->glob.c_str());
166 long users = chan->GetUserCounter();
167 if ((users) && (display))
169 int counter = snprintf(buffer, MAXBUF, "322 %s *", user->nick);
170 amount_sent += counter + ServerNameSize;
171 user->WriteServ(std::string(buffer));
174 else if ((chan) && (((!(chan->modes[CM_PRIVATE])) && (!(chan->modes[CM_SECRET]))) || (has_user)))
176 bool display = match(chan->name, ld->glob.c_str());
177 long users = chan->GetUserCounter();
178 if ((users) && (display))
180 int counter = snprintf(buffer, MAXBUF, "322 %s %s %ld :[+%s] %s",user->nick, chan->name, users, chan->ChanModes(has_user), chan->topic);
181 amount_sent += counter + ServerNameSize;
182 user->WriteServ(std::string(buffer));
191 ld->list_ended = true;
192 user->WriteServ("323 %s :End of channel list.",user->nick);
198 while ((chan != NULL) && (amount_sent < (user->sendqmax / 4)));
201 user->Shrink("safelist_cache");
208 virtual void OnCleanup(int target_type, void* item)
210 if(target_type == TYPE_USER)
212 userrec* u = (userrec*)item;
214 u->GetExt("safelist_cache", ld);
217 u->Shrink("safelist_cache");
221 time_t* last_list_time;
222 u->GetExt("safelist_last", last_list_time);
225 DELETE(last_list_time);
226 u->Shrink("safelist_last");
231 virtual void On005Numeric(std::string &output)
233 output.append(" SAFELIST");
236 virtual void OnUserQuit(userrec* user, const std::string &message, const std::string &oper_message)
238 this->OnCleanup(TYPE_USER,user);
244 class ModuleSafeListFactory : public ModuleFactory
247 ModuleSafeListFactory()
251 ~ModuleSafeListFactory()
255 virtual Module * CreateModule(InspIRCd* Me)
257 return new ModuleSafeList(Me);
262 extern "C" void * init_module( void )
264 return new ModuleSafeListFactory;