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 (std::vector<ucrec*>::iterator i = user->chans.begin(); i != user->chans.end(); i++)
158 if (((ucrec*)(*i))->channel != NULL)
160 Srv->PartUserFromChannel(user,((ucrec*)(*i))->channel->name,"Unparking");
163 // remove all their old modes
164 WriteServ(user->fd,"MODE %s -%s",user->nick,user->modes);
165 // now, map them to the parked user, while nobody can see :p
166 Srv->PseudoToUser(user,unpark,"Unparked to "+std::string(parameters[0]));
167 // set all their new modes
168 WriteServ(unpark->fd,"MODE %s +%s",unpark->nick,unpark->modes);
169 // spool their away log to them
170 WriteServ(unpark->fd,"NOTICE %s :*** You are now unparked. You have successfully taken back the nickname and privilages of %s.",unpark->nick,unpark->nick);
171 for (awaylog::iterator i = awy->begin(); i != awy->end(); i++)
173 char timebuf[MAXBUF];
174 tm *timeinfo = localtime(&i->tm);
175 strlcpy(timebuf,asctime(timeinfo),MAXBUF);
176 timebuf[strlen(timebuf)-1] = '\0';
177 WriteServ(unpark->fd,"NOTICE %s :From %s at %s: \2%s\2",unpark->nick,i->from.c_str(),timebuf,i->text.c_str());
180 unpark->Shrink("park_awaylog");
181 unpark->Shrink("park_key");
182 for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
184 if (j->nick == std::string(unpark->nick))
193 Srv->SendServ(user->fd,"928 "+std::string(user->nick)+" :Incorrect park key.");
198 class ModulePark : public Module
206 virtual void ReadSettings()
208 Conf = new ConfigReader;
209 ParkMaxTime = Conf->ReadInteger("park","maxtime",0,true);
210 ConcurrentParks = Conf->ReadInteger("park","maxperip",0,true);
211 ParkMaxMsgs = Conf->ReadInteger("park","maxmessages",0,true);
215 ModulePark(Server* Me)
220 this->ReadSettings();
221 cmd1 = new cmd_park();
222 cmd2 = new cmd_unpark();
223 cmd3 = new cmd_parkstats();
224 Srv->AddCommand(cmd1);
225 Srv->AddCommand(cmd2);
226 Srv->AddCommand(cmd3);
229 virtual ~ModulePark()
233 void Implements(char* List)
235 List[I_On005Numeric] = List[I_OnRehash] = List[I_OnUserQuit] = List[I_OnUserPreMessage] = List[I_OnUserPreNick] = List[I_OnBackgroundTimer] = List[I_OnWhois] = 1;
238 virtual void OnRehash(const std::string ¶meter)
240 this->ReadSettings();
243 virtual void On005Numeric(std::string &output)
245 output = output + std::string(" PARK");
248 virtual void OnUserQuit(userrec* user, const std::string &reason)
250 std::string nick = user->nick;
251 // track quits in our parked user list
252 for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
263 virtual void OnPrePrivmsg(userrec* user, userrec* dest, const std::string &text)
265 awaylog* awy = (awaylog*)dest->GetExt("park_awaylog");
268 if (awy->size() <= (unsigned)ParkMaxMsgs)
272 am.from = user->GetFullHost();
275 Srv->SendServ(user->fd,"930 "+std::string(user->nick)+" :User "+std::string(dest->nick)+" is parked. Your message has been stored.");
277 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.");
281 virtual int OnUserPreNick(userrec* user, const std::string &newnick)
283 // track nickchanges in our parked user list
284 // (this isnt too efficient, i'll tidy it up some time)
285 /* XXX - perhaps extend the user record, or, that wouldn't work - perhaps use a map? -- w00t */
286 for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
288 if (j->nick == std::string(user->nick))
297 virtual void OnBackgroundTimer(time_t curtime)
299 // look for parked clients which have timed out (this needs tidying)
302 bool go_again = true;
306 for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
308 if (time(NULL) >= (j->parktime+ParkMaxTime))
310 userrec* thisnick = Srv->FindNick(j->nick);
311 // THIS MUST COME BEFORE THE QuitUser - QuitUser can
312 // create a recursive call to OnUserQuit in this module
313 // and then corrupt the pointer!
316 Srv->QuitUser(thisnick,"PARK timeout");
324 /* XXX - why is OnPrePrivmsg seperated here, I assume there is reason for the extra function call? --w00t */
325 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status)
327 if (target_type == TYPE_USER)
329 userrec* u = (userrec*)dest;
330 OnPrePrivmsg(user,u,text);
335 virtual void OnWhois(userrec* src, userrec* dst)
337 if (dst->GetExt("park_awaylog"))
338 Srv->SendTo(NULL,src,"335 "+std::string(src->nick)+" "+std::string(dst->nick)+" :is a parked client");
341 virtual Version GetVersion()
343 return Version(1,0,0,1,VF_VENDOR);
348 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
350 class ModuleParkFactory : public ModuleFactory
361 virtual Module * CreateModule(Server* Me)
363 return new ModulePark(Me);
369 extern "C" void * init_module( void )
371 return new ModuleParkFactory;