1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * Inspire is copyright (C) 2002-2004 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 * ---------------------------------------------------
22 #include "helperfuncs.h"
25 /* $ModDesc: Provides support for user parking/unparking */
44 typedef std::vector<awaymsg> awaylog;
45 typedef std::vector<parkedinfo> parkinfo;
52 void handle_park(char **parameters, int pcnt, userrec *user)
54 /** Parking. easy stuff.
56 * We duplicate and switch the users file descriptor, so that they can remain forever as a 'ghost'
57 * We then disconnect the real user leaving a controlled ghost in their place :)
59 int othersessions = 0;
61 for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
62 if (j->host == std::string(user->host))
64 if (othersessions >= ConcurrentParks)
66 Srv->SendServ(user->fd,"927 "+std::string(user->nick)+" :You are already parked up to the maximum number of allowed times.");
72 unsigned long key = abs(random() * 12345);
73 snprintf(msg,MAXBUF,"You are now parked. To unpark use /UNPARK %s %lu",user->nick,key);
74 Srv->UserToPseudo(user,std::string(msg));
76 user->Extend("park_awaylog",(char*)aw);
77 user->Extend("park_key",(char*)key);
80 pi.parktime = time(NULL);
85 void handle_parkstats(char **parameters, int pcnt, userrec *user)
88 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);
89 Srv->SendServ(user->fd,status);
92 void handle_unpark(char **parameters, int pcnt, userrec *user)
94 /** Unparking. complicated stuff.
96 * Unparking is done in several steps:
98 * (1) Check if the user is parked
99 * (2) Check the key of the user against the one provided
100 * (3) Part the user who issued the command from all of their channels so as not to confuse clients
101 * (4) Remove all the users UMODEs
102 * (5) Duplicate and switch file descriptors on the two users, and disconnect the dead one
103 * (6) Force a nickchange onto the user who issued the command forcing them to change to the parked nick
104 * (7) Force join the user into all the channels the parked nick is currently in (send them localized join and namelist)
105 * (8) Send the user the umodes of their new 'persona'
106 * (9) Spool any awaylog messages to the user
108 * And there you have it, easy huh (NOT)...
110 userrec* unpark = Srv->FindNick(std::string(parameters[0]));
113 WriteServ(user->fd,"942 %s %s :Invalid user specified.",user->nick, parameters[0]);
116 awaylog* awy = (awaylog*)unpark->GetExt("park_awaylog");
117 long key = (long)unpark->GetExt("park_key");
120 WriteServ(user->fd,"943 %s %s :This user is not parked.",user->nick, unpark->nick);
123 if (key == atoi(parameters[1]))
125 // first part the user from all chans theyre on, so things dont get messy
126 for (int i = 0; i != MAXCHANS; i++)
128 if (user->chans[i].channel != NULL)
130 if (user->chans[i].channel->name)
132 Srv->PartUserFromChannel(user,user->chans[i].channel->name,"Unparking");
136 // remove all their old modes
137 WriteServ(user->fd,"MODE %s -%s",user->nick,user->modes);
138 // now, map them to the parked user, while nobody can see :p
139 Srv->PseudoToUser(user,unpark,"Unparked to "+std::string(parameters[0]));
140 // set all their new modes
141 WriteServ(unpark->fd,"MODE %s +%s",unpark->nick,unpark->modes);
142 // spool their away log to them
143 WriteServ(unpark->fd,"NOTICE %s :*** You are now unparked. You have successfully taken back the nickname and privilages of %s.",unpark->nick,unpark->nick);
144 for (awaylog::iterator i = awy->begin(); i != awy->end(); i++)
146 char timebuf[MAXBUF];
147 tm *timeinfo = localtime(&i->tm);
148 strlcpy(timebuf,asctime(timeinfo),MAXBUF);
149 timebuf[strlen(timebuf)-1] = '\0';
150 WriteServ(unpark->fd,"NOTICE %s :From %s at %s: \2%s\2",unpark->nick,i->from.c_str(),timebuf,i->text.c_str());
153 unpark->Shrink("park_awaylog");
154 unpark->Shrink("park_key");
155 for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
157 if (j->nick == std::string(unpark->nick))
166 Srv->SendServ(user->fd,"928 "+std::string(user->nick)+" :Incorrect park key.");
171 class ModulePark : public Module
176 virtual void ReadSettings()
178 Conf = new ConfigReader;
179 ParkMaxTime = Conf->ReadInteger("park","maxtime",0,true);
180 ConcurrentParks = Conf->ReadInteger("park","maxperip",0,true);
181 ParkMaxMsgs = Conf->ReadInteger("park","maxmessages",0,true);
189 this->ReadSettings();
190 Srv->AddCommand("PARK",handle_park,0,0,"m_park.so");
191 Srv->AddCommand("UNPARK",handle_unpark,0,2,"m_park.so");
192 Srv->AddCommand("PARKSTATS",handle_parkstats,'o',0,"m_park.so");
195 virtual ~ModulePark()
200 virtual void OnRehash()
202 this->ReadSettings();
205 virtual void On005Numeric(std::string &output)
207 output = output + std::string(" PARK");
210 virtual void OnUserQuit(userrec* user)
212 std::string nick = user->nick;
213 // track quits in our parked user list
214 for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
225 virtual void OnPrePrivmsg(userrec* user, userrec* dest, std::string text)
227 awaylog* awy = (awaylog*)dest->GetExt("park_awaylog");
230 if (awy->size() <= ParkMaxMsgs)
234 am.from = user->GetFullHost();
237 Srv->SendServ(user->fd,"930 "+std::string(user->nick)+" :User "+std::string(dest->nick)+" is parked. Your message has been stored.");
239 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.");
243 virtual int OnUserPreNick(userrec* user, std::string newnick)
245 // track nickchanges in our parked user list
246 // (this isnt too efficient, i'll tidy it up some time)
247 for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
249 if (j->nick == std::string(user->nick))
258 virtual void OnBackgroundTimer(time_t curtime)
260 // look for parked clients which have timed out (this needs tidying)
263 bool go_again = true;
267 for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
269 if (time(NULL) >= (j->parktime+ParkMaxTime))
271 userrec* thisnick = Srv->FindNick(j->nick);
272 // THIS MUST COME BEFORE THE QuitUser - QuitUser can
273 // create a recursive call to OnUserQuit in this module
274 // and then corrupt the pointer!
277 Srv->QuitUser(thisnick,"PARK timeout");
285 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text)
287 if (target_type == TYPE_USER)
289 userrec* u = (userrec*)dest;
290 OnPrePrivmsg(user,u,text);
295 virtual void OnWhois(userrec* src, userrec* dst)
297 if (dst->GetExt("park_awaylog"))
298 Srv->SendTo(NULL,src,"335 "+std::string(src->nick)+" "+std::string(dst->nick)+" :is a parked client");
301 virtual Version GetVersion()
303 return Version(1,0,0,1,VF_VENDOR);
308 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
310 class ModuleParkFactory : public ModuleFactory
321 virtual Module * CreateModule()
323 return new ModulePark;
329 extern "C" void * init_module( void )
331 return new ModuleParkFactory;