1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 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 /** Holds a users m_safelist state
19 class ListData : public classbase
25 const std::string glob;
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) {};
33 /* $ModDesc: A module overriding /list, and making it safe - stop those sendq problems. */
35 class ModuleSafeList : public Module
38 size_t ServerNameSize;
42 ModuleSafeList(InspIRCd* Me) : Module(Me)
45 Implementation eventlist[] = { I_OnBufferFlushed, I_OnPreCommand, I_OnCleanup, I_OnUserQuit, I_On005Numeric, I_OnRehash };
46 ServerInstance->Modules->Attach(eventlist, this, 6);
49 virtual ~ModuleSafeList()
53 virtual void OnRehash(User* 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,2,0,0,VF_VENDOR,API_VERSION);
70 * Intercept the LIST command.
72 virtual int OnPreCommand(std::string &command, std::vector<std::string> ¶meters, User *user, bool validated, const std::string &original_line)
74 /* If the command doesnt appear to be valid, we dont want to mess with it. */
78 if (command == "LIST")
80 return this->HandleList(parameters, user);
87 * Handle (override) the LIST command.
89 int HandleList(const std::vector<std::string> ¶meters, User* user)
91 int pcnt = parameters.size();
92 int minusers = 0, maxusers = 0;
94 if (global_listing >= LimitList && !IS_OPER(user))
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());
102 /* First, let's check if the user is currently /list'ing */
104 user->GetExt("safelist_cache", ld);
108 /* user is already /list'ing, we don't want to do shit. */
112 /* Work around mIRC suckyness. YOU SUCK, KHALED! */
115 if (parameters[0][0] == '<')
117 maxusers = atoi(parameters[0].c_str()+1);
118 ServerInstance->Logs->Log("m_safelist",DEBUG,"Max users: %d", maxusers);
121 else if (parameters[0][0] == '>')
123 minusers = atoi(parameters[0].c_str()+1);
124 ServerInstance->Logs->Log("m_safelist",DEBUG,"Min users: %d", minusers);
129 time_t* last_list_time;
130 user->GetExt("safelist_last", last_list_time);
133 if (ServerInstance->Time() < (*last_list_time)+ThrottleSecs)
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());
141 delete last_list_time;
142 user->Shrink("safelist_last");
147 * start at channel 0! ;)
149 ld = new ListData(0,ServerInstance->Time(), pcnt ? parameters[0] : "*", minusers, maxusers);
150 user->Extend("safelist_cache", ld);
152 time_t* llt = new time_t;
153 *llt = ServerInstance->Time();
154 user->Extend("safelist_last", llt);
156 user->WriteNumeric(321, "%s Channel :Users Name",user->nick.c_str());
163 virtual void OnBufferFlushed(User* user)
167 if (user->GetExt("safelist_cache", ld))
169 Channel* chan = NULL;
170 unsigned long amount_sent = 0;
173 chan = ServerInstance->GetChannelIndex(ld->list_position);
174 bool has_user = (chan && chan->HasUser(user));
175 long users = chan ? chan->GetUserCounter() : 0;
177 bool too_few = (ld->minusers && (users <= ld->minusers));
178 bool too_many = (ld->maxusers && (users >= ld->maxusers));
180 if (chan && (too_many || too_few))
186 if ((chan) && (chan->modes[CM_PRIVATE]) && (!IS_OPER(user)))
188 bool display = (match(chan->name, ld->glob) || (!chan->topic.empty() && match(chan->topic, ld->glob)));
189 if ((users) && (display))
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));
196 else if ((chan) && ((((!(chan->IsModeSet('p'))) && (!(chan->IsModeSet('s'))))) || (has_user) || IS_OPER(user)))
198 bool display = (match(chan->name, ld->glob) || (!chan->topic.empty() && match(chan->topic, ld->glob)));
199 if ((users) && (display))
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));
212 ld->list_ended = true;
213 user->WriteNumeric(323, "%s :End of channel list.",user->nick.c_str());
219 while ((chan != NULL) && (amount_sent < (user->MyClass->GetSendqMax() / 4)));
222 user->Shrink("safelist_cache");
229 virtual void OnCleanup(int target_type, void* item)
231 if(target_type == TYPE_USER)
233 User* u = (User*)item;
235 u->GetExt("safelist_cache", ld);
238 u->Shrink("safelist_cache");
242 time_t* last_list_time;
243 u->GetExt("safelist_last", last_list_time);
246 delete last_list_time;
247 u->Shrink("safelist_last");
252 virtual void On005Numeric(std::string &output)
254 output.append(" SAFELIST");
257 virtual void OnUserQuit(User* user, const std::string &message, const std::string &oper_message)
259 this->OnCleanup(TYPE_USER,user);
264 MODULE_INIT(ModuleSafeList)