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 class ModuleSafeList : public Module
40 size_t ServerNameSize;
42 ModuleSafeList(InspIRCd* Me) : Module::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 ServerNameSize = strlen(ServerInstance->Config->ServerName) + 4;
58 virtual Version GetVersion()
60 return Version(1,1,0,0,VF_VENDOR,API_VERSION);
63 void Implements(char* List)
65 List[I_OnBufferFlushed] = List[I_OnPreCommand] = List[I_OnCleanup] = List[I_OnUserQuit] = List[I_On005Numeric] = List[I_OnRehash] = 1;
70 * Intercept the LIST command.
72 virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *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, userrec* user)
91 /* First, let's check if the user is currently /list'ing */
93 user->GetExt("safelist_cache", ld);
97 /* user is already /list'ing, we don't want to do shit. */
101 /* Work around mIRC suckyness. YOU SUCK, KHALED! */
102 if ((pcnt == 1) && (*parameters[0] == '<'))
105 time_t* last_list_time;
106 user->GetExt("safelist_last", last_list_time);
109 if (ServerInstance->Time() < (*last_list_time)+ThrottleSecs)
111 user->WriteServ("NOTICE %s :*** Woah there, slow down a little, you can't /LIST so often!",user->nick);
112 user->WriteServ("321 %s Channel :Users Name",user->nick);
113 user->WriteServ("323 %s :End of channel list.",user->nick);
117 DELETE(last_list_time);
118 user->Shrink("safelist_last");
122 * start at channel 0! ;)
124 ld = new ListData(0,ServerInstance->Time(), pcnt ? parameters[0] : "*");
125 user->Extend("safelist_cache", ld);
127 time_t* llt = new time_t;
128 *llt = ServerInstance->Time();
129 user->Extend("safelist_last", llt);
131 user->WriteServ("321 %s Channel :Users Name",user->nick);
136 virtual void OnBufferFlushed(userrec* user)
140 if (user->GetExt("safelist_cache", ld))
142 chanrec* chan = NULL;
143 long amount_sent = 0;
146 chan = ServerInstance->GetChannelIndex(ld->list_position);
147 bool has_user = (chan && chan->HasUser(user));
148 if ((chan) && (chan->modes[CM_PRIVATE]))
150 bool display = match(chan->name, ld->glob.c_str());
151 long users = chan->GetUserCounter();
152 if ((users) && (display))
154 int counter = snprintf(buffer, MAXBUF, "322 %s *", user->nick);
155 amount_sent += counter + ServerNameSize;
156 user->WriteServ(std::string(buffer));
159 else if ((chan) && (((!(chan->modes[CM_PRIVATE])) && (!(chan->modes[CM_SECRET]))) || (has_user)))
161 bool display = match(chan->name, ld->glob.c_str());
162 long users = chan->GetUserCounter();
163 if ((users) && (display))
165 int counter = snprintf(buffer, MAXBUF, "322 %s %s %ld :[+%s] %s",user->nick, chan->name, users, chan->ChanModes(has_user), chan->topic);
166 amount_sent += counter + ServerNameSize;
167 user->WriteServ(std::string(buffer));
176 ld->list_ended = true;
177 user->WriteServ("323 %s :End of channel list.",user->nick);
183 while ((chan != NULL) && (amount_sent < (user->sendqmax / 4)));
186 user->Shrink("safelist_cache");
192 virtual void OnCleanup(int target_type, void* item)
194 if(target_type == TYPE_USER)
196 userrec* u = (userrec*)item;
198 u->GetExt("safelist_cache", ld);
201 u->Shrink("safelist_cache");
204 time_t* last_list_time;
205 u->GetExt("safelist_last", last_list_time);
208 DELETE(last_list_time);
209 u->Shrink("safelist_last");
214 virtual void On005Numeric(std::string &output)
216 output.append(" SAFELIST");
219 virtual void OnUserQuit(userrec* user, const std::string &message)
221 this->OnCleanup(TYPE_USER,user);
227 class ModuleSafeListFactory : public ModuleFactory
230 ModuleSafeListFactory()
234 ~ModuleSafeListFactory()
238 virtual Module * CreateModule(InspIRCd* Me)
240 return new ModuleSafeList(Me);
245 extern "C" void * init_module( void )
247 return new ModuleSafeListFactory;