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";
125 syntax = "<nick> <key>";
128 void Handle (const char** parameters, int pcnt, userrec *user)
130 /** Unparking. complicated stuff.
132 * Unparking is done in several steps:
134 * (1) Check if the user is parked
135 * (2) Check the key of the user against the one provided
136 * (3) Part the user who issued the command from all of their channels so as not to confuse clients
137 * (4) Remove all the users UMODEs
138 * (5) Duplicate and switch file descriptors on the two users, and disconnect the dead one
139 * (6) Force a nickchange onto the user who issued the command forcing them to change to the parked nick
140 * (7) Force join the user into all the channels the parked nick is currently in (send them localized join and namelist)
141 * (8) Send the user the umodes of their new 'persona'
142 * (9) Spool any awaylog messages to the user
144 * And there you have it, easy huh (NOT)...
146 userrec* unpark = Srv->FindNick(std::string(parameters[0]));
149 WriteServ(user->fd,"942 %s %s :Invalid user specified.",user->nick, parameters[0]);
153 unpark->GetExt("park_awaylog", awy);
155 unpark->GetExt("park_key", key);
158 WriteServ(user->fd,"943 %s %s :This user is not parked.",user->nick, unpark->nick);
161 if (*key == atoi(parameters[1]))
163 // first part the user from all chans theyre on, so things dont get messy
164 for (std::vector<ucrec*>::iterator i = user->chans.begin(); i != user->chans.end(); i++)
166 chanrec* chan = (*i)->channel;
169 if (!chan->PartUser(user, "Unparking"))
173 // remove all their old modes
174 WriteServ(user->fd,"MODE %s -%s",user->nick,user->FormatModes());
175 // now, map them to the parked user, while nobody can see :p
176 Srv->PseudoToUser(user,unpark,"Unparked to "+std::string(parameters[0]));
177 // set all their new modes
178 WriteServ(unpark->fd,"MODE %s +%s",unpark->nick,unpark->FormatModes());
179 // spool their away log to them
180 WriteServ(unpark->fd,"NOTICE %s :*** You are now unparked. You have successfully taken back the nickname and privilages of %s.",unpark->nick,unpark->nick);
181 for (awaylog::iterator i = awy->begin(); i != awy->end(); i++)
183 char timebuf[MAXBUF];
184 tm *timeinfo = localtime(&i->tm);
185 strlcpy(timebuf,asctime(timeinfo),MAXBUF);
186 timebuf[strlen(timebuf)-1] = '\0';
187 WriteServ(unpark->fd,"NOTICE %s :From %s at %s: \2%s\2",unpark->nick,i->from.c_str(),timebuf,i->text.c_str());
191 unpark->Shrink("park_awaylog");
192 unpark->Shrink("park_key");
193 for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
195 if (j->nick == std::string(unpark->nick))
204 Srv->SendServ(user->fd,"928 "+std::string(user->nick)+" :Incorrect park key.");
209 class ModulePark : public Module
217 virtual void ReadSettings()
219 Conf = new ConfigReader;
220 ParkMaxTime = Conf->ReadInteger("park","maxtime",0,true);
221 ConcurrentParks = Conf->ReadInteger("park","maxperip",0,true);
222 ParkMaxMsgs = Conf->ReadInteger("park","maxmessages",0,true);
226 ModulePark(Server* Me)
231 this->ReadSettings();
232 cmd1 = new cmd_park();
233 cmd2 = new cmd_unpark();
234 cmd3 = new cmd_parkstats();
235 Srv->AddCommand(cmd1);
236 Srv->AddCommand(cmd2);
237 Srv->AddCommand(cmd3);
240 virtual ~ModulePark()
244 void Implements(char* List)
246 List[I_On005Numeric] = List[I_OnRehash] = List[I_OnUserQuit] = List[I_OnUserPreMessage] = List[I_OnUserPreNick] = List[I_OnBackgroundTimer] = List[I_OnWhois] = 1;
249 virtual void OnRehash(const std::string ¶meter)
251 this->ReadSettings();
254 virtual void On005Numeric(std::string &output)
256 output = output + std::string(" PARK");
259 virtual void OnUserQuit(userrec* user, const std::string &reason)
261 std::string nick = user->nick;
262 // track quits in our parked user list
263 for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
274 virtual void OnPrePrivmsg(userrec* user, userrec* dest, const std::string &text)
277 dest->GetExt("park_awaylog", awy);
280 if (awy->size() <= (unsigned)ParkMaxMsgs)
284 am.from = user->GetFullHost();
287 Srv->SendServ(user->fd,"930 "+std::string(user->nick)+" :User "+std::string(dest->nick)+" is parked. Your message has been stored.");
289 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.");
293 virtual int OnUserPreNick(userrec* user, const std::string &newnick)
295 // track nickchanges in our parked user list
296 // (this isnt too efficient, i'll tidy it up some time)
297 /* XXX - perhaps extend the user record, or, that wouldn't work - perhaps use a map? -- w00t */
298 for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
300 if (j->nick == std::string(user->nick))
309 virtual void OnBackgroundTimer(time_t curtime)
311 // look for parked clients which have timed out (this needs tidying)
314 bool go_again = true;
318 for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
320 if (time(NULL) >= (j->parktime+ParkMaxTime))
322 userrec* thisnick = Srv->FindNick(j->nick);
323 // THIS MUST COME BEFORE THE QuitUser - QuitUser can
324 // create a recursive call to OnUserQuit in this module
325 // and then corrupt the pointer!
328 userrec::QuitUser(thisnick,"PARK timeout");
336 /* XXX - why is OnPrePrivmsg seperated here, I assume there is reason for the extra function call? --w00t */
337 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status)
339 if (target_type == TYPE_USER)
341 userrec* u = (userrec*)dest;
342 OnPrePrivmsg(user,u,text);
347 virtual void OnWhois(userrec* src, userrec* dst)
350 if (dst->GetExt("park_awaylog", dummy))
351 Srv->SendTo(NULL,src,"335 "+std::string(src->nick)+" "+std::string(dst->nick)+" :is a parked client");
354 virtual Version GetVersion()
356 return Version(1,0,0,1,VF_VENDOR);
361 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
363 class ModuleParkFactory : public ModuleFactory
374 virtual Module * CreateModule(Server* Me)
376 return new ModulePark(Me);
382 extern "C" void * init_module( void )
384 return new ModuleParkFactory;