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 #include "configreader.h"
21 /** Holds a users m_safelist state
23 class ListData : public classbase
29 const std::string glob;
31 ListData() : list_start(0), list_position(0), list_ended(false) {};
32 ListData(long pos, time_t t, const std::string &pattern) : list_start(t), list_position(pos), list_ended(false), glob(pattern) {};
35 /* $ModDesc: A module overriding /list, and making it safe - stop those sendq problems. */
37 typedef std::vector<userrec *> UserList;
38 UserList listusers; /* vector of people doing a /list */
39 class ListTimer *timer;
41 /** To create a timer which recurs every second, we inherit from InspTimer.
42 * InspTimer is only one-shot however, so at the end of each Tick() we simply
43 * insert another of ourselves into the pending queue :)
45 class ListTimer : public InspTimer
51 InspIRCd* ServerInstance;
52 const std::string glob;
53 size_t ServerNameSize;
57 ListTimer(InspIRCd* Instance, long interval) : InspTimer(interval,Instance->Time()), ServerInstance(Instance)
59 ServerNameSize = 4 + strlen(ServerInstance->Config->ServerName);
62 virtual void Tick(time_t TIME)
69 for (UserList::iterator iter = listusers.begin(); iter != listusers.end(); iter++)
73 * - Get where they are up to
74 * - If it's more than total number of channels, erase
75 * them from the iterator, set go_again to true
76 * - If not, spool more channels
78 userrec* u = (userrec*)(*iter);
80 u->GetExt("safelist_cache", ld);
81 if ((size_t)ld->list_position > ServerInstance->chanlist.size())
83 u->Shrink("safelist_cache");
85 listusers.erase(iter);
90 ServerInstance->Log(DEBUG, "m_safelist.so: resuming spool of list to client %s at channel %ld", u->nick, ld->list_position);
92 /* Attempt to fill up to 25% the user's sendq with /LIST output */
96 ServerInstance->Log(DEBUG,"Channel %ld",ld->list_position);
97 if (!ld->list_position)
98 u->WriteServ("321 %s Channel :Users Name",u->nick);
99 chan = ServerInstance->GetChannelIndex(ld->list_position);
101 bool has_user = (chan && chan->HasUser(u));
102 if ((chan) && (chan->modes[CM_PRIVATE]))
104 bool display = match(chan->name, ld->glob.c_str());
105 long users = chan->GetUserCounter();
106 if ((users) && (display))
108 int counter = snprintf(buffer, MAXBUF, "322 %s *", u->nick);
109 amount_sent += counter + ServerNameSize;
110 ServerInstance->Log(DEBUG, "m_safelist.so: Sent %ld of safe %ld / 4", amount_sent, u->sendqmax);
111 u->WriteServ(std::string(buffer));
114 else if ((chan) && (((!(chan->modes[CM_PRIVATE])) && (!(chan->modes[CM_SECRET]))) || (has_user)))
116 bool display = match(chan->name, ld->glob.c_str());
117 long users = chan->GetUserCounter();
119 if ((users) && (display))
121 int counter = snprintf(buffer, MAXBUF, "322 %s %s %ld :[+%s] %s",u->nick, chan->name, users, chan->ChanModes(has_user), chan->topic);
122 /* Increment total plus linefeed */
123 amount_sent += counter + ServerNameSize;
124 ServerInstance->Log(DEBUG, "m_safelist.so: Sent %ld of safe %ld / 4", amount_sent, u->sendqmax);
125 u->WriteServ(std::string(buffer));
134 ld->list_ended = true;
135 u->WriteServ("323 %s :End of channel list.",u->nick);
142 while ((chan != NULL) && (amount_sent < (u->sendqmax / 4)));
146 if (listusers.size())
148 timer = new ListTimer(ServerInstance,1);
149 ServerInstance->Timers->AddTimer(timer);
158 class ModuleSafeList : public Module
161 ModuleSafeList(InspIRCd* Me) : Module::Module(Me)
166 virtual ~ModuleSafeList()
169 ServerInstance->Timers->DelTimer(timer);
172 virtual Version GetVersion()
174 return Version(1,1,0,0,VF_VENDOR,API_VERSION);
177 void Implements(char* List)
179 List[I_OnPreCommand] = List[I_OnCleanup] = List[I_OnUserQuit] = List[I_On005Numeric] = 1;
184 * Intercept the LIST command.
186 virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated, const std::string &original_line)
188 /* If the command doesnt appear to be valid, we dont want to mess with it. */
192 if (command == "LIST")
194 return this->HandleList(parameters, pcnt, user);
201 * Handle (override) the LIST command.
203 int HandleList(const char** parameters, int pcnt, userrec* user)
205 /* First, let's check if the user is currently /list'ing */
207 user->GetExt("safelist_cache", ld);
211 /* user is already /list'ing, we don't want to do shit. */
215 /* Work around mIRC suckyness. YOU SUCK, KHALED! */
216 if ((pcnt == 1) && (*parameters[0] == '<'))
219 time_t* last_list_time;
220 user->GetExt("safelist_last", last_list_time);
223 if (ServerInstance->Time() < (*last_list_time)+60)
225 user->WriteServ("NOTICE %s :*** Woah there, slow down a little, you can't /LIST so often!",user->nick);
226 user->WriteServ("321 %s Channel :Users Name",user->nick);
227 user->WriteServ("323 %s :End of channel list.",user->nick);
231 DELETE(last_list_time);
232 user->Shrink("safelist_last");
236 * start at channel 0! ;)
238 ld = new ListData(0,ServerInstance->Time(), pcnt ? parameters[0] : "*");
239 user->Extend("safelist_cache", ld);
240 listusers.push_back(user);
242 time_t* llt = new time_t;
243 *llt = ServerInstance->Time();
244 user->Extend("safelist_last", llt);
248 timer = new ListTimer(ServerInstance,1);
249 ServerInstance->Timers->AddTimer(timer);
255 virtual void OnCleanup(int target_type, void* item)
257 if(target_type == TYPE_USER)
259 userrec* u = (userrec*)item;
261 u->GetExt("safelist_cache", ld);
264 u->Shrink("safelist_cache");
267 for (UserList::iterator iter = listusers.begin(); iter != listusers.end(); iter++)
269 userrec* u2 = (userrec*)(*iter);
272 listusers.erase(iter);
276 time_t* last_list_time;
277 u->GetExt("safelist_last", last_list_time);
280 DELETE(last_list_time);
281 u->Shrink("safelist_last");
286 virtual void On005Numeric(std::string &output)
288 output.append(" SAFELIST");
291 virtual void OnUserQuit(userrec* user, const std::string &message)
293 this->OnCleanup(TYPE_USER,user);
299 class ModuleSafeListFactory : public ModuleFactory
302 ModuleSafeListFactory()
306 ~ModuleSafeListFactory()
310 virtual Module * CreateModule(InspIRCd* Me)
312 return new ModuleSafeList(Me);
317 extern "C" void * init_module( void )
319 return new ModuleSafeListFactory;