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 */
29 class parking : public classbase
33 class awaymsg : public parking
41 class parkedinfo : public parking
50 typedef std::vector<awaymsg> awaylog;
51 typedef std::vector<parkedinfo> parkinfo;
58 class cmd_park : public command_t
61 cmd_park () : command_t("PARK", 0, 0)
63 this->source = "m_park.so";
66 void Handle (const char** parameters, int pcnt, userrec *user)
68 /** Parking. easy stuff.
70 * We duplicate and switch the users file descriptor, so that they can remain forever as a 'ghost'
71 * We then disconnect the real user leaving a controlled ghost in their place :)
73 int othersessions = 0;
74 /* XXX - why can't just use pinfo.size() like we do below, rather than iterating over the whole vector? -- w00t */
76 for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
77 if (j->host == std::string(user->host))
79 if (othersessions >= ConcurrentParks)
81 Srv->SendServ(user->fd,"927 "+std::string(user->nick)+" :You are already parked up to the maximum number of allowed times.");
87 unsigned long* key = new unsigned long;
88 *key = abs(random() * 12345);
89 snprintf(msg,MAXBUF,"You are now parked. To unpark use /UNPARK %s %lu",user->nick, *key);
90 Srv->UserToPseudo(user,std::string(msg));
92 user->Extend("park_awaylog", aw);
93 user->Extend("park_key", key);
96 pi.parktime = time(NULL);
103 class cmd_parkstats : public command_t
106 cmd_parkstats () : command_t("PARKSTATS", 'o', 0)
108 this->source = "m_park.so";
111 void Handle (const char** parameters, int pcnt, userrec *user)
114 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);
115 Srv->SendServ(user->fd,status);
119 class cmd_unpark : public command_t
122 cmd_unpark () : command_t("UNPARK", 0, 2)
124 this->source = "m_park.so";
127 void Handle (const char** parameters, int pcnt, userrec *user)
129 /** Unparking. complicated stuff.
131 * Unparking is done in several steps:
133 * (1) Check if the user is parked
134 * (2) Check the key of the user against the one provided
135 * (3) Part the user who issued the command from all of their channels so as not to confuse clients
136 * (4) Remove all the users UMODEs
137 * (5) Duplicate and switch file descriptors on the two users, and disconnect the dead one
138 * (6) Force a nickchange onto the user who issued the command forcing them to change to the parked nick
139 * (7) Force join the user into all the channels the parked nick is currently in (send them localized join and namelist)
140 * (8) Send the user the umodes of their new 'persona'
141 * (9) Spool any awaylog messages to the user
143 * And there you have it, easy huh (NOT)...
145 userrec* unpark = Srv->FindNick(std::string(parameters[0]));
148 WriteServ(user->fd,"942 %s %s :Invalid user specified.",user->nick, parameters[0]);
152 unpark->GetExt("park_awaylog", awy);
154 unpark->GetExt("park_key", key);
157 WriteServ(user->fd,"943 %s %s :This user is not parked.",user->nick, unpark->nick);
160 if (*key == atoi(parameters[1]))
162 // first part the user from all chans theyre on, so things dont get messy
163 for (std::vector<ucrec*>::iterator i = user->chans.begin(); i != user->chans.end(); i++)
165 if (((ucrec*)(*i))->channel != NULL)
167 Srv->PartUserFromChannel(user,((ucrec*)(*i))->channel->name,"Unparking");
170 // remove all their old modes
171 WriteServ(user->fd,"MODE %s -%s",user->nick,user->FormatModes());
172 // now, map them to the parked user, while nobody can see :p
173 Srv->PseudoToUser(user,unpark,"Unparked to "+std::string(parameters[0]));
174 // set all their new modes
175 WriteServ(unpark->fd,"MODE %s +%s",unpark->nick,unpark->FormatModes());
176 // spool their away log to them
177 WriteServ(unpark->fd,"NOTICE %s :*** You are now unparked. You have successfully taken back the nickname and privilages of %s.",unpark->nick,unpark->nick);
178 for (awaylog::iterator i = awy->begin(); i != awy->end(); i++)
180 char timebuf[MAXBUF];
181 tm *timeinfo = localtime(&i->tm);
182 strlcpy(timebuf,asctime(timeinfo),MAXBUF);
183 timebuf[strlen(timebuf)-1] = '\0';
184 WriteServ(unpark->fd,"NOTICE %s :From %s at %s: \2%s\2",unpark->nick,i->from.c_str(),timebuf,i->text.c_str());
188 unpark->Shrink("park_awaylog");
189 unpark->Shrink("park_key");
190 for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
192 if (j->nick == std::string(unpark->nick))
201 Srv->SendServ(user->fd,"928 "+std::string(user->nick)+" :Incorrect park key.");
206 class ModulePark : public Module
214 virtual void ReadSettings()
216 Conf = new ConfigReader;
217 ParkMaxTime = Conf->ReadInteger("park","maxtime",0,true);
218 ConcurrentParks = Conf->ReadInteger("park","maxperip",0,true);
219 ParkMaxMsgs = Conf->ReadInteger("park","maxmessages",0,true);
223 ModulePark(Server* Me)
228 this->ReadSettings();
229 cmd1 = new cmd_park();
230 cmd2 = new cmd_unpark();
231 cmd3 = new cmd_parkstats();
232 Srv->AddCommand(cmd1);
233 Srv->AddCommand(cmd2);
234 Srv->AddCommand(cmd3);
237 virtual ~ModulePark()
241 void Implements(char* List)
243 List[I_On005Numeric] = List[I_OnRehash] = List[I_OnUserQuit] = List[I_OnUserPreMessage] = List[I_OnUserPreNick] = List[I_OnBackgroundTimer] = List[I_OnWhois] = 1;
246 virtual void OnRehash(const std::string ¶meter)
248 this->ReadSettings();
251 virtual void On005Numeric(std::string &output)
253 output = output + std::string(" PARK");
256 virtual void OnUserQuit(userrec* user, const std::string &reason)
258 std::string nick = user->nick;
259 // track quits in our parked user list
260 for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
271 virtual void OnPrePrivmsg(userrec* user, userrec* dest, const std::string &text)
274 dest->GetExt("park_awaylog", awy);
277 if (awy->size() <= (unsigned)ParkMaxMsgs)
281 am.from = user->GetFullHost();
284 Srv->SendServ(user->fd,"930 "+std::string(user->nick)+" :User "+std::string(dest->nick)+" is parked. Your message has been stored.");
286 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.");
290 virtual int OnUserPreNick(userrec* user, const std::string &newnick)
292 // track nickchanges in our parked user list
293 // (this isnt too efficient, i'll tidy it up some time)
294 /* XXX - perhaps extend the user record, or, that wouldn't work - perhaps use a map? -- w00t */
295 for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
297 if (j->nick == std::string(user->nick))
306 virtual void OnBackgroundTimer(time_t curtime)
308 // look for parked clients which have timed out (this needs tidying)
311 bool go_again = true;
315 for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
317 if (time(NULL) >= (j->parktime+ParkMaxTime))
319 userrec* thisnick = Srv->FindNick(j->nick);
320 // THIS MUST COME BEFORE THE QuitUser - QuitUser can
321 // create a recursive call to OnUserQuit in this module
322 // and then corrupt the pointer!
325 Srv->QuitUser(thisnick,"PARK timeout");
333 /* XXX - why is OnPrePrivmsg seperated here, I assume there is reason for the extra function call? --w00t */
334 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status)
336 if (target_type == TYPE_USER)
338 userrec* u = (userrec*)dest;
339 OnPrePrivmsg(user,u,text);
344 virtual void OnWhois(userrec* src, userrec* dst)
347 if (dst->GetExt("park_awaylog", dummy))
348 Srv->SendTo(NULL,src,"335 "+std::string(src->nick)+" "+std::string(dst->nick)+" :is a parked client");
351 virtual Version GetVersion()
353 return Version(1,0,0,1,VF_VENDOR);
358 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
360 class ModuleParkFactory : public ModuleFactory
371 virtual Module * CreateModule(Server* Me)
373 return new ModulePark(Me);
379 extern "C" void * init_module( void )
381 return new ModuleParkFactory;