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;
71 for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
72 if (j->host == std::string(user->host))
74 if (othersessions >= ConcurrentParks)
76 Srv->SendServ(user->fd,"927 "+std::string(user->nick)+" :You are already parked up to the maximum number of allowed times.");
82 unsigned long key = abs(random() * 12345);
83 snprintf(msg,MAXBUF,"You are now parked. To unpark use /UNPARK %s %lu",user->nick,key);
84 Srv->UserToPseudo(user,std::string(msg));
86 user->Extend("park_awaylog",(char*)aw);
87 user->Extend("park_key",(char*)key);
90 pi.parktime = time(NULL);
97 class cmd_parkstats : public command_t
100 cmd_parkstats () : command_t("PARKSTATS", 'o', 0)
102 this->source = "m_park.so";
105 void Handle (char **parameters, int pcnt, userrec *user)
108 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);
109 Srv->SendServ(user->fd,status);
113 class cmd_unpark : public command_t
116 cmd_unpark () : command_t("UNPARK", 0, 2)
118 this->source = "m_park.so";
121 void Handle (char **parameters, int pcnt, userrec *user)
123 /** Unparking. complicated stuff.
125 * Unparking is done in several steps:
127 * (1) Check if the user is parked
128 * (2) Check the key of the user against the one provided
129 * (3) Part the user who issued the command from all of their channels so as not to confuse clients
130 * (4) Remove all the users UMODEs
131 * (5) Duplicate and switch file descriptors on the two users, and disconnect the dead one
132 * (6) Force a nickchange onto the user who issued the command forcing them to change to the parked nick
133 * (7) Force join the user into all the channels the parked nick is currently in (send them localized join and namelist)
134 * (8) Send the user the umodes of their new 'persona'
135 * (9) Spool any awaylog messages to the user
137 * And there you have it, easy huh (NOT)...
139 userrec* unpark = Srv->FindNick(std::string(parameters[0]));
142 WriteServ(user->fd,"942 %s %s :Invalid user specified.",user->nick, parameters[0]);
145 awaylog* awy = (awaylog*)unpark->GetExt("park_awaylog");
146 long key = (long)unpark->GetExt("park_key");
149 WriteServ(user->fd,"943 %s %s :This user is not parked.",user->nick, unpark->nick);
152 if (key == atoi(parameters[1]))
154 // first part the user from all chans theyre on, so things dont get messy
155 for (unsigned int i = 0; i < user->chans.size(); i++)
157 if (user->chans[i].channel != NULL)
159 if (user->chans[i].channel->name)
161 Srv->PartUserFromChannel(user,user->chans[i].channel->name,"Unparking");
165 // remove all their old modes
166 WriteServ(user->fd,"MODE %s -%s",user->nick,user->modes);
167 // now, map them to the parked user, while nobody can see :p
168 Srv->PseudoToUser(user,unpark,"Unparked to "+std::string(parameters[0]));
169 // set all their new modes
170 WriteServ(unpark->fd,"MODE %s +%s",unpark->nick,unpark->modes);
171 // spool their away log to them
172 WriteServ(unpark->fd,"NOTICE %s :*** You are now unparked. You have successfully taken back the nickname and privilages of %s.",unpark->nick,unpark->nick);
173 for (awaylog::iterator i = awy->begin(); i != awy->end(); i++)
175 char timebuf[MAXBUF];
176 tm *timeinfo = localtime(&i->tm);
177 strlcpy(timebuf,asctime(timeinfo),MAXBUF);
178 timebuf[strlen(timebuf)-1] = '\0';
179 WriteServ(unpark->fd,"NOTICE %s :From %s at %s: \2%s\2",unpark->nick,i->from.c_str(),timebuf,i->text.c_str());
182 unpark->Shrink("park_awaylog");
183 unpark->Shrink("park_key");
184 for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
186 if (j->nick == std::string(unpark->nick))
195 Srv->SendServ(user->fd,"928 "+std::string(user->nick)+" :Incorrect park key.");
200 class ModulePark : public Module
208 virtual void ReadSettings()
210 Conf = new ConfigReader;
211 ParkMaxTime = Conf->ReadInteger("park","maxtime",0,true);
212 ConcurrentParks = Conf->ReadInteger("park","maxperip",0,true);
213 ParkMaxMsgs = Conf->ReadInteger("park","maxmessages",0,true);
217 ModulePark(Server* Me)
222 this->ReadSettings();
223 cmd1 = new cmd_park();
224 cmd2 = new cmd_unpark();
225 cmd3 = new cmd_parkstats();
226 Srv->AddCommand(cmd1);
227 Srv->AddCommand(cmd2);
228 Srv->AddCommand(cmd3);
231 virtual ~ModulePark()
235 void Implements(char* List)
237 List[I_On005Numeric] = List[I_OnRehash] = List[I_OnUserQuit] = List[I_OnUserPreMessage] = List[I_OnUserPreNick] = List[I_OnBackgroundTimer] = List[I_OnWhois] = 1;
240 virtual void OnRehash(std::string parameter)
242 this->ReadSettings();
245 virtual void On005Numeric(std::string &output)
247 output = output + std::string(" PARK");
250 virtual void OnUserQuit(userrec* user, std::string reason)
252 std::string nick = user->nick;
253 // track quits in our parked user list
254 for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
265 virtual void OnPrePrivmsg(userrec* user, userrec* dest, std::string text)
267 awaylog* awy = (awaylog*)dest->GetExt("park_awaylog");
270 if (awy->size() <= (unsigned)ParkMaxMsgs)
274 am.from = user->GetFullHost();
277 Srv->SendServ(user->fd,"930 "+std::string(user->nick)+" :User "+std::string(dest->nick)+" is parked. Your message has been stored.");
279 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.");
283 virtual int OnUserPreNick(userrec* user, std::string newnick)
285 // track nickchanges in our parked user list
286 // (this isnt too efficient, i'll tidy it up some time)
287 for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
289 if (j->nick == std::string(user->nick))
298 virtual void OnBackgroundTimer(time_t curtime)
300 // look for parked clients which have timed out (this needs tidying)
303 bool go_again = true;
307 for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
309 if (time(NULL) >= (j->parktime+ParkMaxTime))
311 userrec* thisnick = Srv->FindNick(j->nick);
312 // THIS MUST COME BEFORE THE QuitUser - QuitUser can
313 // create a recursive call to OnUserQuit in this module
314 // and then corrupt the pointer!
317 Srv->QuitUser(thisnick,"PARK timeout");
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;