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,1,0,0,VF_VENDOR,API_VERSION);
70 * Intercept the LIST command.
72 virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, 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, pcnt, user);
87 * Handle (override) the LIST command.
89 int HandleList(const char** parameters, int pcnt, User* user)
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);
96 user->WriteServ("321 %s Channel :Users Name",user->nick);
97 user->WriteServ("323 %s :End of channel list.",user->nick);
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] == '<')
116 maxusers = atoi(parameters[0]+1);
117 ServerInstance->Log(DEBUG,"Max users: %d", maxusers);
120 else if (*parameters[0] == '>')
122 minusers = atoi(parameters[0]+1);
123 ServerInstance->Log(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);
135 user->WriteServ("321 %s Channel :Users Name",user->nick);
136 user->WriteServ("323 %s :End of channel list.",user->nick);
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] : "*", 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->WriteServ("321 %s Channel :Users Name",user->nick);
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 has_user = (chan && chan->HasUser(user));
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))
185 if ((chan) && (chan->modes[CM_PRIVATE]))
187 bool display = (match(chan->name, ld->glob.c_str()) || (*chan->topic && match(chan->topic, ld->glob.c_str())));
188 if ((users) && (display))
190 int counter = snprintf(buffer, MAXBUF, "322 %s *", user->nick);
191 amount_sent += counter + ServerNameSize;
192 user->WriteServ(std::string(buffer));
195 else if ((chan) && (((!(chan->modes[CM_PRIVATE])) && (!(chan->modes[CM_SECRET]))) || (has_user)))
197 bool display = (match(chan->name, ld->glob.c_str()) || (*chan->topic && match(chan->topic, ld->glob.c_str())));
198 if ((users) && (display))
200 int counter = snprintf(buffer, MAXBUF, "322 %s %s %ld :[+%s] %s",user->nick, chan->name, users, chan->ChanModes(has_user), chan->topic);
201 amount_sent += counter + ServerNameSize;
202 user->WriteServ(std::string(buffer));
211 ld->list_ended = true;
212 user->WriteServ("323 %s :End of channel list.",user->nick);
218 while ((chan != NULL) && (amount_sent < (user->MyClass->GetSendqMax() / 4)));
221 user->Shrink("safelist_cache");
228 virtual void OnCleanup(int target_type, void* item)
230 if(target_type == TYPE_USER)
232 User* u = (User*)item;
234 u->GetExt("safelist_cache", ld);
237 u->Shrink("safelist_cache");
241 time_t* last_list_time;
242 u->GetExt("safelist_last", last_list_time);
245 delete last_list_time;
246 u->Shrink("safelist_last");
251 virtual void On005Numeric(std::string &output)
253 output.append(" SAFELIST");
256 virtual void OnUserQuit(User* user, const std::string &message, const std::string &oper_message)
258 this->OnCleanup(TYPE_USER,user);
263 MODULE_INIT(ModuleSafeList)