1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
24 #include "helperfuncs.h"
27 /* $ModDesc: Provides support for user parking/unparking */
46 typedef std::vector<awaymsg> awaylog;
47 typedef std::vector<parkedinfo> parkinfo;
54 class cmd_park : public command_t
57 cmd_park () : command_t("PARK", 0, 0)
59 this->source = "m_park.so";
62 void Handle (char **parameters, int pcnt, userrec *user)
64 /** Parking. easy stuff.
66 * We duplicate and switch the users file descriptor, so that they can remain forever as a 'ghost'
67 * We then disconnect the real user leaving a controlled ghost in their place :)
69 int othersessions = 0;
70 /* XXX - why can't just use pinfo.size() like we do below, rather than iterating over the whole vector? -- w00t */
72 for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
73 if (j->host == std::string(user->host))
75 if (othersessions >= ConcurrentParks)
77 Srv->SendServ(user->fd,"927 "+std::string(user->nick)+" :You are already parked up to the maximum number of allowed times.");
83 unsigned long key = abs(random() * 12345);
84 snprintf(msg,MAXBUF,"You are now parked. To unpark use /UNPARK %s %lu",user->nick,key);
85 Srv->UserToPseudo(user,std::string(msg));
87 user->Extend("park_awaylog",(char*)aw);
88 user->Extend("park_key",(char*)key);
91 pi.parktime = time(NULL);
98 class cmd_parkstats : public command_t
101 cmd_parkstats () : command_t("PARKSTATS", 'o', 0)
103 this->source = "m_park.so";
106 void Handle (char **parameters, int pcnt, userrec *user)
109 snprintf(status,MAXBUF,"NOTICE %s :There are a total of %lu parked clients on this server, with a maximum of %lu parked sessions allowed per user.",user->nick,(unsigned long)pinfo.size(),(unsigned long)ConcurrentParks);
110 Srv->SendServ(user->fd,status);
114 class cmd_unpark : public command_t
117 cmd_unpark () : command_t("UNPARK", 0, 2)
119 this->source = "m_park.so";
122 void Handle (char **parameters, int pcnt, userrec *user)
124 /** Unparking. complicated stuff.
126 * Unparking is done in several steps:
128 * (1) Check if the user is parked
129 * (2) Check the key of the user against the one provided
130 * (3) Part the user who issued the command from all of their channels so as not to confuse clients
131 * (4) Remove all the users UMODEs
132 * (5) Duplicate and switch file descriptors on the two users, and disconnect the dead one
133 * (6) Force a nickchange onto the user who issued the command forcing them to change to the parked nick
134 * (7) Force join the user into all the channels the parked nick is currently in (send them localized join and namelist)
135 * (8) Send the user the umodes of their new 'persona'
136 * (9) Spool any awaylog messages to the user
138 * And there you have it, easy huh (NOT)...
140 userrec* unpark = Srv->FindNick(std::string(parameters[0]));
143 WriteServ(user->fd,"942 %s %s :Invalid user specified.",user->nick, parameters[0]);
146 awaylog* awy = (awaylog*)unpark->GetExt("park_awaylog");
147 long key = (long)unpark->GetExt("park_key");
150 WriteServ(user->fd,"943 %s %s :This user is not parked.",user->nick, unpark->nick);
153 if (key == atoi(parameters[1]))
155 // first part the user from all chans theyre on, so things dont get messy
156 for (unsigned int i = 0; i < user->chans.size(); i++)
158 if (user->chans[i].channel != NULL)
160 if (user->chans[i].channel->name)
162 Srv->PartUserFromChannel(user,user->chans[i].channel->name,"Unparking");
166 // remove all their old modes
167 WriteServ(user->fd,"MODE %s -%s",user->nick,user->modes);
168 // now, map them to the parked user, while nobody can see :p
169 Srv->PseudoToUser(user,unpark,"Unparked to "+std::string(parameters[0]));
170 // set all their new modes
171 WriteServ(unpark->fd,"MODE %s +%s",unpark->nick,unpark->modes);
172 // spool their away log to them
173 WriteServ(unpark->fd,"NOTICE %s :*** You are now unparked. You have successfully taken back the nickname and privilages of %s.",unpark->nick,unpark->nick);
174 for (awaylog::iterator i = awy->begin(); i != awy->end(); i++)
176 char timebuf[MAXBUF];
177 tm *timeinfo = localtime(&i->tm);
178 strlcpy(timebuf,asctime(timeinfo),MAXBUF);
179 timebuf[strlen(timebuf)-1] = '\0';
180 WriteServ(unpark->fd,"NOTICE %s :From %s at %s: \2%s\2",unpark->nick,i->from.c_str(),timebuf,i->text.c_str());
183 unpark->Shrink("park_awaylog");
184 unpark->Shrink("park_key");
185 for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
187 if (j->nick == std::string(unpark->nick))
196 Srv->SendServ(user->fd,"928 "+std::string(user->nick)+" :Incorrect park key.");
201 class ModulePark : public Module
209 virtual void ReadSettings()
211 Conf = new ConfigReader;
212 ParkMaxTime = Conf->ReadInteger("park","maxtime",0,true);
213 ConcurrentParks = Conf->ReadInteger("park","maxperip",0,true);
214 ParkMaxMsgs = Conf->ReadInteger("park","maxmessages",0,true);
218 ModulePark(Server* Me)
223 this->ReadSettings();
224 cmd1 = new cmd_park();
225 cmd2 = new cmd_unpark();
226 cmd3 = new cmd_parkstats();
227 Srv->AddCommand(cmd1);
228 Srv->AddCommand(cmd2);
229 Srv->AddCommand(cmd3);
232 virtual ~ModulePark()
236 void Implements(char* List)
238 List[I_On005Numeric] = List[I_OnRehash] = List[I_OnUserQuit] = List[I_OnUserPreMessage] = List[I_OnUserPreNick] = List[I_OnBackgroundTimer] = List[I_OnWhois] = 1;
241 virtual void OnRehash(std::string parameter)
243 this->ReadSettings();
246 virtual void On005Numeric(std::string &output)
248 output = output + std::string(" PARK");
251 virtual void OnUserQuit(userrec* user, std::string reason)
253 std::string nick = user->nick;
254 // track quits in our parked user list
255 for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
266 virtual void OnPrePrivmsg(userrec* user, userrec* dest, std::string text)
268 awaylog* awy = (awaylog*)dest->GetExt("park_awaylog");
271 if (awy->size() <= (unsigned)ParkMaxMsgs)
275 am.from = user->GetFullHost();
278 Srv->SendServ(user->fd,"930 "+std::string(user->nick)+" :User "+std::string(dest->nick)+" is parked. Your message has been stored.");
280 else Srv->SendServ(user->fd,"929 "+std::string(user->nick)+" :User "+std::string(dest->nick)+" is parked, but their message queue is full. Message not saved.");
284 virtual int OnUserPreNick(userrec* user, std::string newnick)
286 // track nickchanges in our parked user list
287 // (this isnt too efficient, i'll tidy it up some time)
288 /* XXX - perhaps extend the user record, or, that wouldn't work - perhaps use a map? -- w00t */
289 for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
291 if (j->nick == std::string(user->nick))
300 virtual void OnBackgroundTimer(time_t curtime)
302 // look for parked clients which have timed out (this needs tidying)
305 bool go_again = true;
309 for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
311 if (time(NULL) >= (j->parktime+ParkMaxTime))
313 userrec* thisnick = Srv->FindNick(j->nick);
314 // THIS MUST COME BEFORE THE QuitUser - QuitUser can
315 // create a recursive call to OnUserQuit in this module
316 // and then corrupt the pointer!
319 Srv->QuitUser(thisnick,"PARK timeout");
327 /* XXX - why is OnPrePrivmsg seperated here, I assume there is reason for the extra function call? --w00t */
328 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status)
330 if (target_type == TYPE_USER)
332 userrec* u = (userrec*)dest;
333 OnPrePrivmsg(user,u,text);
338 virtual void OnWhois(userrec* src, userrec* dst)
340 if (dst->GetExt("park_awaylog"))
341 Srv->SendTo(NULL,src,"335 "+std::string(src->nick)+" "+std::string(dst->nick)+" :is a parked client");
344 virtual Version GetVersion()
346 return Version(1,0,0,1,VF_VENDOR);
351 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
353 class ModuleParkFactory : public ModuleFactory
364 virtual Module * CreateModule(Server* Me)
366 return new ModulePark(Me);
372 extern "C" void * init_module( void )
374 return new ModuleParkFactory;