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)
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);
67 void Implements(char* List)
69 List[I_OnBufferFlushed] = List[I_OnPreCommand] = List[I_OnCleanup] = List[I_OnUserQuit] = List[I_On005Numeric] = List[I_OnRehash] = 1;
74 * Intercept the LIST command.
76 virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, User *user, bool validated, const std::string &original_line)
78 /* If the command doesnt appear to be valid, we dont want to mess with it. */
82 if (command == "LIST")
84 return this->HandleList(parameters, pcnt, user);
91 * Handle (override) the LIST command.
93 int HandleList(const char** parameters, int pcnt, User* user)
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);
100 user->WriteServ("321 %s Channel :Users Name",user->nick);
101 user->WriteServ("323 %s :End of channel list.",user->nick);
105 /* First, let's check if the user is currently /list'ing */
107 user->GetExt("safelist_cache", ld);
111 /* user is already /list'ing, we don't want to do shit. */
115 /* Work around mIRC suckyness. YOU SUCK, KHALED! */
118 if (*parameters[0] == '<')
120 maxusers = atoi(parameters[0]+1);
121 ServerInstance->Log(DEBUG,"Max users: %d", maxusers);
124 else if (*parameters[0] == '>')
126 minusers = atoi(parameters[0]+1);
127 ServerInstance->Log(DEBUG,"Min users: %d", minusers);
132 time_t* last_list_time;
133 user->GetExt("safelist_last", last_list_time);
136 if (ServerInstance->Time() < (*last_list_time)+ThrottleSecs)
138 user->WriteServ("NOTICE %s :*** Woah there, slow down a little, you can't /LIST so often!",user->nick);
139 user->WriteServ("321 %s Channel :Users Name",user->nick);
140 user->WriteServ("323 %s :End of channel list.",user->nick);
144 delete last_list_time;
145 user->Shrink("safelist_last");
150 * start at channel 0! ;)
152 ld = new ListData(0,ServerInstance->Time(), pcnt ? parameters[0] : "*", minusers, maxusers);
153 user->Extend("safelist_cache", ld);
155 time_t* llt = new time_t;
156 *llt = ServerInstance->Time();
157 user->Extend("safelist_last", llt);
159 user->WriteServ("321 %s Channel :Users Name",user->nick);
166 virtual void OnBufferFlushed(User* user)
170 if (user->GetExt("safelist_cache", ld))
172 Channel* chan = NULL;
173 unsigned long amount_sent = 0;
176 chan = ServerInstance->GetChannelIndex(ld->list_position);
177 bool has_user = (chan && chan->HasUser(user));
178 long users = chan ? chan->GetUserCounter() : 0;
180 bool too_few = (ld->minusers && (users <= ld->minusers));
181 bool too_many = (ld->maxusers && (users >= ld->maxusers));
183 if (chan && (too_many || too_few))
189 if ((chan) && (chan->modes[CM_PRIVATE]))
191 bool display = (match(chan->name, ld->glob.c_str()) || (*chan->topic && match(chan->topic, ld->glob.c_str())));
192 if ((users) && (display))
194 int counter = snprintf(buffer, MAXBUF, "322 %s *", user->nick);
195 amount_sent += counter + ServerNameSize;
196 user->WriteServ(std::string(buffer));
199 else if ((chan) && (((!(chan->modes[CM_PRIVATE])) && (!(chan->modes[CM_SECRET]))) || (has_user)))
201 bool display = (match(chan->name, ld->glob.c_str()) || (*chan->topic && match(chan->topic, ld->glob.c_str())));
202 if ((users) && (display))
204 int counter = snprintf(buffer, MAXBUF, "322 %s %s %ld :[+%s] %s",user->nick, chan->name, users, chan->ChanModes(has_user), chan->topic);
205 amount_sent += counter + ServerNameSize;
206 user->WriteServ(std::string(buffer));
215 ld->list_ended = true;
216 user->WriteServ("323 %s :End of channel list.",user->nick);
222 while ((chan != NULL) && (amount_sent < (user->MyClass->GetSendqMax() / 4)));
225 user->Shrink("safelist_cache");
232 virtual void OnCleanup(int target_type, void* item)
234 if(target_type == TYPE_USER)
236 User* u = (User*)item;
238 u->GetExt("safelist_cache", ld);
241 u->Shrink("safelist_cache");
245 time_t* last_list_time;
246 u->GetExt("safelist_last", last_list_time);
249 delete last_list_time;
250 u->Shrink("safelist_last");
255 virtual void On005Numeric(std::string &output)
257 output.append(" SAFELIST");
260 virtual void OnUserQuit(User* user, const std::string &message, const std::string &oper_message)
262 this->OnCleanup(TYPE_USER,user);
267 MODULE_INIT(ModuleSafeList)