1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
16 /** Holds a users m_safelist state
18 class ListData : public classbase
24 const std::string glob;
28 ListData() : list_start(0), list_position(0), list_ended(false) {};
29 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) {};
32 /* $ModDesc: A module overriding /list, and making it safe - stop those sendq problems. */
34 class ModuleSafeList : public Module
37 size_t ServerNameSize;
41 ModuleSafeList(InspIRCd* Me) : Module(Me)
44 Implementation eventlist[] = { I_OnBufferFlushed, I_OnPreCommand, I_OnCleanup, I_OnUserQuit, I_On005Numeric, I_OnRehash };
45 ServerInstance->Modules->Attach(eventlist, this, 6);
48 virtual ~ModuleSafeList()
52 virtual void OnRehash(User* user)
54 ConfigReader MyConf(ServerInstance);
55 ThrottleSecs = MyConf.ReadInteger("safelist", "throttle", "60", 0, true);
56 LimitList = MyConf.ReadInteger("safelist", "maxlisters", "50", 0, true);
57 ServerNameSize = strlen(ServerInstance->Config->ServerName) + 4;
61 virtual Version GetVersion()
63 return Version("$Id$",VF_VENDOR,API_VERSION);
69 * Intercept the LIST command.
71 virtual int OnPreCommand(std::string &command, std::vector<std::string> ¶meters, User *user, bool validated, const std::string &original_line)
73 /* If the command doesnt appear to be valid, we dont want to mess with it. */
77 if (command == "LIST")
79 return this->HandleList(parameters, user);
86 * Handle (override) the LIST command.
88 int HandleList(const std::vector<std::string> ¶meters, User* user)
90 int pcnt = parameters.size();
91 int minusers = 0, maxusers = 0;
93 if (global_listing >= LimitList && !IS_OPER(user))
95 user->WriteServ("NOTICE %s :*** Server load is currently too heavy. Please try again later.", user->nick.c_str());
96 user->WriteNumeric(321, "%s Channel :Users Name",user->nick.c_str());
97 user->WriteNumeric(323, "%s :End of channel list.",user->nick.c_str());
101 /* First, let's check if the user is currently /list'ing */
103 user->GetExt("safelist_cache", ld);
107 /* user is already /list'ing, we don't want to do shit. */
111 /* Work around mIRC suckyness. YOU SUCK, KHALED! */
114 if (parameters[0][0] == '<')
116 maxusers = atoi(parameters[0].c_str()+1);
117 ServerInstance->Logs->Log("m_safelist",DEBUG,"Max users: %d", maxusers);
120 else if (parameters[0][0] == '>')
122 minusers = atoi(parameters[0].c_str()+1);
123 ServerInstance->Logs->Log("m_safelist",DEBUG,"Min users: %d", minusers);
128 time_t* last_list_time;
129 user->GetExt("safelist_last", last_list_time);
132 if (ServerInstance->Time() < (*last_list_time)+ThrottleSecs)
134 user->WriteServ("NOTICE %s :*** Woah there, slow down a little, you can't /LIST so often!",user->nick.c_str());
135 user->WriteNumeric(321, "%s Channel :Users Name",user->nick.c_str());
136 user->WriteNumeric(323, "%s :End of channel list.",user->nick.c_str());
140 delete last_list_time;
141 user->Shrink("safelist_last");
146 * start at channel 0! ;)
148 ld = new ListData(0,ServerInstance->Time(), (pcnt && (parameters[0][0] != '<' && parameters[0][0] != '>')) ? parameters[0] : "*", minusers, maxusers);
149 user->Extend("safelist_cache", ld);
151 time_t* llt = new time_t;
152 *llt = ServerInstance->Time();
153 user->Extend("safelist_last", llt);
155 user->WriteNumeric(321, "%s Channel :Users Name",user->nick.c_str());
162 virtual void OnBufferFlushed(User* user)
166 if (user->GetExt("safelist_cache", ld))
168 Channel* chan = NULL;
169 unsigned long amount_sent = 0;
172 chan = ServerInstance->GetChannelIndex(ld->list_position);
173 bool is_special = (chan && (chan->HasUser(user) || user->HasPrivPermission("channels/auspex")));
174 long users = chan ? chan->GetUserCounter() : 0;
176 bool too_few = (ld->minusers && (users <= ld->minusers));
177 bool too_many = (ld->maxusers && (users >= ld->maxusers));
179 if (chan && (too_many || too_few))
187 bool display = (InspIRCd::Match(chan->name, ld->glob) || (!chan->topic.empty() && InspIRCd::Match(chan->topic, ld->glob)));
189 if (!users || !display)
195 /* +s, not in chan / not got channels/auspex */
196 if (chan->IsModeSet('s') && !is_special)
202 if (chan->IsModeSet('p') && !is_special)
204 /* Channel is +p and user is outside/not privileged */
205 int counter = snprintf(buffer, MAXBUF, "322 %s * %ld :", user->nick.c_str(), users);
206 amount_sent += counter + ServerNameSize;
207 user->WriteServ(std::string(buffer));
211 /* User is in the channel/privileged, channel is not +s */
212 int counter = snprintf(buffer, MAXBUF, "322 %s %s %ld :[+%s] %s", user->nick.c_str(), chan->name.c_str(), users, chan->ChanModes(is_special), chan->topic.c_str());
213 amount_sent += counter + ServerNameSize;
214 user->WriteServ(std::string(buffer));
221 ld->list_ended = true;
222 user->WriteNumeric(323, "%s :End of channel list.",user->nick.c_str());
228 while ((chan != NULL) && (amount_sent < (user->MyClass->GetSendqMax() / 4)));
231 user->Shrink("safelist_cache");
238 virtual void OnCleanup(int target_type, void* item)
240 if(target_type == TYPE_USER)
242 User* u = (User*)item;
244 u->GetExt("safelist_cache", ld);
247 u->Shrink("safelist_cache");
251 time_t* last_list_time;
252 u->GetExt("safelist_last", last_list_time);
255 delete last_list_time;
256 u->Shrink("safelist_last");
261 virtual void On005Numeric(std::string &output)
263 output.append(" SAFELIST");
266 virtual void OnUserQuit(User* user, const std::string &message, const std::string &oper_message)
268 this->OnCleanup(TYPE_USER,user);
273 MODULE_INIT(ModuleSafeList)