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;
40 SimpleExtItem<ListData> listData;
43 ModuleSafeList(InspIRCd* Me) : Module(Me), listData("safelist_data", this), listTime("safelist_last", this)
46 Extensible::Register(&listData);
47 Extensible::Register(&listTime);
48 Implementation eventlist[] = { I_OnBufferFlushed, I_OnPreCommand, I_On005Numeric, I_OnRehash };
49 ServerInstance->Modules->Attach(eventlist, this, 4);
56 void OnRehash(User* user)
58 ConfigReader MyConf(ServerInstance);
59 ThrottleSecs = MyConf.ReadInteger("safelist", "throttle", "60", 0, true);
60 LimitList = MyConf.ReadInteger("safelist", "maxlisters", "50", 0, true);
61 ServerNameSize = strlen(ServerInstance->Config->ServerName) + 4;
67 return Version("$Id$",VF_VENDOR,API_VERSION);
73 * Intercept the LIST command.
75 ModResult OnPreCommand(std::string &command, std::vector<std::string> ¶meters, User *user, bool validated, const std::string &original_line)
77 /* If the command doesnt appear to be valid, we dont want to mess with it. */
79 return MOD_RES_PASSTHRU;
81 if (command == "LIST")
83 return this->HandleList(parameters, user);
85 return MOD_RES_PASSTHRU;
90 * Handle (override) the LIST command.
92 ModResult HandleList(const std::vector<std::string> ¶meters, User* user)
94 int pcnt = parameters.size();
95 int minusers = 0, maxusers = 0;
97 if (global_listing >= LimitList && !IS_OPER(user))
99 user->WriteServ("NOTICE %s :*** Server load is currently too heavy. Please try again later.", user->nick.c_str());
100 user->WriteNumeric(321, "%s Channel :Users Name",user->nick.c_str());
101 user->WriteNumeric(323, "%s :End of channel list.",user->nick.c_str());
105 /* First, let's check if the user is currently /list'ing */
106 ListData *ld = listData.get(user);
110 /* user is already /list'ing, we don't want to do shit. */
114 /* Work around mIRC suckyness. YOU SUCK, KHALED! */
117 if (parameters[0][0] == '<')
119 maxusers = atoi(parameters[0].c_str()+1);
120 ServerInstance->Logs->Log("m_safelist",DEBUG,"Max users: %d", maxusers);
123 else if (parameters[0][0] == '>')
125 minusers = atoi(parameters[0].c_str()+1);
126 ServerInstance->Logs->Log("m_safelist",DEBUG,"Min users: %d", minusers);
131 time_t last_list_time = listTime.get(user);
132 if (last_list_time && 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());
141 * start at channel 0! ;)
143 ld = new ListData(0,ServerInstance->Time(), (pcnt && (parameters[0][0] != '<' && parameters[0][0] != '>')) ? parameters[0] : "*", minusers, maxusers);
144 listData.set(user, ld);
145 listTime.set(user, ServerInstance->Time());
147 user->WriteNumeric(321, "%s Channel :Users Name",user->nick.c_str());
154 void OnBufferFlushed(User* user)
157 ListData* ld = listData.get(user);
160 Channel* chan = NULL;
161 unsigned long amount_sent = 0;
164 chan = ServerInstance->GetChannelIndex(ld->list_position);
165 bool is_special = (chan && (chan->HasUser(user) || user->HasPrivPermission("channels/auspex")));
166 long users = chan ? chan->GetUserCounter() : 0;
168 bool too_few = (ld->minusers && (users <= ld->minusers));
169 bool too_many = (ld->maxusers && (users >= ld->maxusers));
171 if (chan && (too_many || too_few))
179 bool display = (InspIRCd::Match(chan->name, ld->glob) || (!chan->topic.empty() && InspIRCd::Match(chan->topic, ld->glob)));
181 if (!users || !display)
187 /* +s, not in chan / not got channels/auspex */
188 if (chan->IsModeSet('s') && !is_special)
194 if (chan->IsModeSet('p') && !is_special)
196 /* Channel is +p and user is outside/not privileged */
197 int counter = snprintf(buffer, MAXBUF, "322 %s * %ld :", user->nick.c_str(), users);
198 amount_sent += counter + ServerNameSize;
199 user->WriteServ(std::string(buffer));
203 /* User is in the channel/privileged, channel is not +s */
204 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());
205 amount_sent += counter + ServerNameSize;
206 user->WriteServ(std::string(buffer));
213 ld->list_ended = true;
214 user->WriteNumeric(323, "%s :End of channel list.",user->nick.c_str());
220 while ((chan != NULL) && (amount_sent < (user->MyClass->GetSendqMax() / 4)));
223 listData.unset(user);
229 void On005Numeric(std::string &output)
231 output.append(" SAFELIST");
236 MODULE_INIT(ModuleSafeList)