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 * ---------------------------------------------------
20 /** Holds a users m_safelist state
22 class ListData : public classbase
28 const std::string glob;
32 ListData() : list_start(0), list_position(0), list_ended(false) {};
33 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) {};
36 /* $ModDesc: A module overriding /list, and making it safe - stop those sendq problems. */
38 class ModuleSafeList : public Module
41 size_t ServerNameSize;
45 ModuleSafeList(InspIRCd* Me) : Module(Me)
50 virtual ~ModuleSafeList()
54 virtual void OnRehash(userrec* user, const std::string ¶meter)
56 ConfigReader MyConf(ServerInstance);
57 ThrottleSecs = MyConf.ReadInteger("safelist", "throttle", "60", 0, true);
58 LimitList = MyConf.ReadInteger("safelist", "maxlisters", "50", 0, true);
59 ServerNameSize = strlen(ServerInstance->Config->ServerName) + 4;
63 virtual Version GetVersion()
65 return Version(1,1,0,0,VF_VENDOR,API_VERSION);
68 void Implements(char* List)
70 List[I_OnBufferFlushed] = List[I_OnPreCommand] = List[I_OnCleanup] = List[I_OnUserQuit] = List[I_On005Numeric] = List[I_OnRehash] = 1;
75 * Intercept the LIST command.
77 virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated, const std::string &original_line)
79 /* If the command doesnt appear to be valid, we dont want to mess with it. */
83 if (command == "LIST")
85 return this->HandleList(parameters, pcnt, user);
92 * Handle (override) the LIST command.
94 int HandleList(const char** parameters, int pcnt, userrec* user)
96 int minusers = 0, maxusers = 0;
98 if (global_listing >= LimitList)
100 user->WriteServ("NOTICE %s :*** Server load is currently too heavy. Please try again later.", user->nick);
101 user->WriteServ("321 %s Channel :Users Name",user->nick);
102 user->WriteServ("323 %s :End of channel list.",user->nick);
106 /* First, let's check if the user is currently /list'ing */
108 user->GetExt("safelist_cache", ld);
112 /* user is already /list'ing, we don't want to do shit. */
116 /* Work around mIRC suckyness. YOU SUCK, KHALED! */
119 if (*parameters[0] == '<')
121 maxusers = atoi(parameters[0]+1);
122 ServerInstance->Log(DEBUG,"Max users: %d", maxusers);
125 else if (*parameters[0] == '>')
127 minusers = atoi(parameters[0]+1);
128 ServerInstance->Log(DEBUG,"Min users: %d", minusers);
133 time_t* last_list_time;
134 user->GetExt("safelist_last", last_list_time);
137 if (ServerInstance->Time() < (*last_list_time)+ThrottleSecs)
139 user->WriteServ("NOTICE %s :*** Woah there, slow down a little, you can't /LIST so often!",user->nick);
140 user->WriteServ("321 %s Channel :Users Name",user->nick);
141 user->WriteServ("323 %s :End of channel list.",user->nick);
145 DELETE(last_list_time);
146 user->Shrink("safelist_last");
151 * start at channel 0! ;)
153 ld = new ListData(0,ServerInstance->Time(), pcnt ? parameters[0] : "*", minusers, maxusers);
154 user->Extend("safelist_cache", ld);
156 time_t* llt = new time_t;
157 *llt = ServerInstance->Time();
158 user->Extend("safelist_last", llt);
160 user->WriteServ("321 %s Channel :Users Name",user->nick);
167 virtual void OnBufferFlushed(userrec* user)
171 if (user->GetExt("safelist_cache", ld))
173 chanrec* chan = NULL;
174 long amount_sent = 0;
177 chan = ServerInstance->GetChannelIndex(ld->list_position);
178 bool has_user = (chan && chan->HasUser(user));
179 long users = chan ? chan->GetUserCounter() : 0;
181 bool too_few = (ld->minusers && (users <= ld->minusers));
182 bool too_many = (ld->maxusers && (users >= ld->maxusers));
184 if (chan && (too_many || too_few))
190 if ((chan) && (chan->modes[CM_PRIVATE]))
192 bool display = (match(chan->name, ld->glob.c_str()) || (*chan->topic && match(chan->topic, ld->glob.c_str())));
193 if ((users) && (display))
195 int counter = snprintf(buffer, MAXBUF, "322 %s *", user->nick);
196 amount_sent += counter + ServerNameSize;
197 user->WriteServ(std::string(buffer));
200 else if ((chan) && (((!(chan->modes[CM_PRIVATE])) && (!(chan->modes[CM_SECRET]))) || (has_user)))
202 bool display = (match(chan->name, ld->glob.c_str()) || (*chan->topic && match(chan->topic, ld->glob.c_str())));
203 if ((users) && (display))
205 int counter = snprintf(buffer, MAXBUF, "322 %s %s %ld :[+%s] %s",user->nick, chan->name, users, chan->ChanModes(has_user), chan->topic);
206 amount_sent += counter + ServerNameSize;
207 user->WriteServ(std::string(buffer));
216 ld->list_ended = true;
217 user->WriteServ("323 %s :End of channel list.",user->nick);
223 while ((chan != NULL) && (amount_sent < (user->sendqmax / 4)));
226 user->Shrink("safelist_cache");
233 virtual void OnCleanup(int target_type, void* item)
235 if(target_type == TYPE_USER)
237 userrec* u = (userrec*)item;
239 u->GetExt("safelist_cache", ld);
242 u->Shrink("safelist_cache");
246 time_t* last_list_time;
247 u->GetExt("safelist_last", last_list_time);
250 DELETE(last_list_time);
251 u->Shrink("safelist_last");
256 virtual void On005Numeric(std::string &output)
258 output.append(" SAFELIST");
261 virtual void OnUserQuit(userrec* user, const std::string &message, const std::string &oper_message)
263 this->OnCleanup(TYPE_USER,user);
268 MODULE_INIT(ModuleSafeList)