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 /** 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)
47 virtual ~ModuleSafeList()
51 virtual void OnRehash(userrec* user, const std::string ¶meter)
53 ConfigReader MyConf(ServerInstance);
54 ThrottleSecs = MyConf.ReadInteger("safelist", "throttle", "60", 0, true);
55 LimitList = MyConf.ReadInteger("safelist", "maxlisters", "50", 0, true);
56 ServerNameSize = strlen(ServerInstance->Config->ServerName) + 4;
60 virtual Version GetVersion()
62 return Version(1,1,0,0,VF_VENDOR,API_VERSION);
65 void Implements(char* List)
67 List[I_OnBufferFlushed] = List[I_OnPreCommand] = List[I_OnCleanup] = List[I_OnUserQuit] = List[I_On005Numeric] = List[I_OnRehash] = 1;
72 * Intercept the LIST command.
74 virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated, const std::string &original_line)
76 /* If the command doesnt appear to be valid, we dont want to mess with it. */
80 if (command == "LIST")
82 return this->HandleList(parameters, pcnt, user);
89 * Handle (override) the LIST command.
91 int HandleList(const char** parameters, int pcnt, userrec* user)
93 int minusers = 0, maxusers = 0;
95 if (global_listing >= LimitList && !IS_OPER(user))
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! */
116 if (*parameters[0] == '<')
118 maxusers = atoi(parameters[0]+1);
119 ServerInstance->Log(DEBUG,"Max users: %d", maxusers);
122 else if (*parameters[0] == '>')
124 minusers = atoi(parameters[0]+1);
125 ServerInstance->Log(DEBUG,"Min users: %d", minusers);
130 time_t* last_list_time;
131 user->GetExt("safelist_last", last_list_time);
134 if (ServerInstance->Time() < (*last_list_time)+ThrottleSecs)
136 user->WriteServ("NOTICE %s :*** Woah there, slow down a little, you can't /LIST so often!",user->nick);
137 user->WriteServ("321 %s Channel :Users Name",user->nick);
138 user->WriteServ("323 %s :End of channel list.",user->nick);
142 DELETE(last_list_time);
143 user->Shrink("safelist_last");
148 * start at channel 0! ;)
150 ld = new ListData(0,ServerInstance->Time(), pcnt ? parameters[0] : "*", minusers, maxusers);
151 user->Extend("safelist_cache", ld);
153 time_t* llt = new time_t;
154 *llt = ServerInstance->Time();
155 user->Extend("safelist_last", llt);
157 user->WriteServ("321 %s Channel :Users Name",user->nick);
164 virtual void OnBufferFlushed(userrec* user)
168 if (user->GetExt("safelist_cache", ld))
170 chanrec* chan = NULL;
171 long amount_sent = 0;
174 chan = ServerInstance->GetChannelIndex(ld->list_position);
175 bool has_user = (chan && chan->HasUser(user));
176 long users = chan ? chan->GetUserCounter() : 0;
178 bool too_few = (ld->minusers && (users <= ld->minusers));
179 bool too_many = (ld->maxusers && (users >= ld->maxusers));
181 if (chan && (too_many || too_few))
187 if ((chan) && (chan->modes[CM_PRIVATE]))
189 bool display = (match(chan->name, ld->glob.c_str()) || (*chan->topic && match(chan->topic, ld->glob.c_str())));
190 if ((users) && (display))
192 int counter = snprintf(buffer, MAXBUF, "322 %s *", user->nick);
193 amount_sent += counter + ServerNameSize;
194 user->WriteServ(std::string(buffer));
197 else if ((chan) && (((!(chan->modes[CM_PRIVATE])) && (!(chan->modes[CM_SECRET]))) || (has_user)))
199 bool display = (match(chan->name, ld->glob.c_str()) || (*chan->topic && match(chan->topic, ld->glob.c_str())));
200 if ((users) && (display))
202 int counter = snprintf(buffer, MAXBUF, "322 %s %s %ld :[+%s] %s",user->nick, chan->name, users, chan->ChanModes(has_user), chan->topic);
203 amount_sent += counter + ServerNameSize;
204 user->WriteServ(std::string(buffer));
213 ld->list_ended = true;
214 user->WriteServ("323 %s :End of channel list.",user->nick);
220 while ((chan != NULL) && (amount_sent < (user->sendqmax / 4)));
223 user->Shrink("safelist_cache");
230 virtual void OnCleanup(int target_type, void* item)
232 if(target_type == TYPE_USER)
234 userrec* u = (userrec*)item;
236 u->GetExt("safelist_cache", ld);
239 u->Shrink("safelist_cache");
243 time_t* last_list_time;
244 u->GetExt("safelist_last", last_list_time);
247 DELETE(last_list_time);
248 u->Shrink("safelist_last");
253 virtual void On005Numeric(std::string &output)
255 output.append(" SAFELIST");
258 virtual void OnUserQuit(userrec* user, const std::string &message, const std::string &oper_message)
260 this->OnCleanup(TYPE_USER,user);
265 MODULE_INIT(ModuleSafeList)