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 * ---------------------------------------------------
24 /* $ModDesc: Provides support for user parking/unparking */
43 typedef std::vector<awaymsg> awaylog;
44 typedef std::vector<parkedinfo> parkinfo;
51 void handle_park(char **parameters, int pcnt, userrec *user)
53 /** Parking. easy stuff.
55 * We duplicate and switch the users file descriptor, so that they can remain forever as a 'ghost'
56 * We then disconnect the real user leaving a controlled ghost in their place :)
58 int othersessions = 0;
60 for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
61 if (j->host == std::string(user->host))
63 if (othersessions >= ConcurrentParks)
65 Srv->SendServ(user->fd,"927 "+std::string(user->nick)+" :You are already parked up to the maximum number of allowed times.");
71 long key = abs(random() * 12345);
72 snprintf(msg,MAXBUF,"You are now parked. To unpark use /UNPARK %s %d",user->nick,key);
73 Srv->UserToPseudo(user,std::string(msg));
75 user->Extend("park_awaylog",(char*)aw);
76 user->Extend("park_key",(char*)key);
79 pi.parktime = time(NULL);
84 void handle_parkstats(char **parameters, int pcnt, userrec *user)
87 snprintf(status,MAXBUF,"NOTICE %s :There are a total of %d parked clients on this server, with a maximum of %d parked sessions allowed per user.",user->nick,pinfo.size(),ConcurrentParks);
88 Srv->SendServ(user->fd,status);
91 void handle_unpark(char **parameters, int pcnt, userrec *user)
93 /** Unparking. complicated stuff.
95 * Unparking is done in several steps:
97 * (1) Check if the user is parked
98 * (2) Check the key of the user against the one provided
99 * (3) Part the user who issued the command from all of their channels so as not to confuse clients
100 * (4) Remove all the users UMODEs
101 * (5) Duplicate and switch file descriptors on the two users, and disconnect the dead one
102 * (6) Force a nickchange onto the user who issued the command forcing them to change to the parked nick
103 * (7) Force join the user into all the channels the parked nick is currently in (send them localized join and namelist)
104 * (8) Send the user the umodes of their new 'persona'
105 * (9) Spool any awaylog messages to the user
107 * And there you have it, easy huh (NOT)...
109 userrec* unpark = Srv->FindNick(std::string(parameters[0]));
112 WriteServ(user->fd,"942 %s %s :Invalid user specified.",user->nick, parameters[0]);
115 awaylog* awy = (awaylog*)unpark->GetExt("park_awaylog");
116 long key = (long)unpark->GetExt("park_key");
119 WriteServ(user->fd,"943 %s %s :This user is not parked.",user->nick, unpark->nick);
122 if (key == atoi(parameters[1]))
124 // first part the user from all chans theyre on, so things dont get messy
125 for (int i = 0; i != MAXCHANS; i++)
127 if (user->chans[i].channel != NULL)
129 if (user->chans[i].channel->name)
131 Srv->PartUserFromChannel(user,user->chans[i].channel->name,"Unparking");
135 // remove all their old modes
136 WriteServ(user->fd,"MODE %s -%s",user->nick,user->modes);
137 // now, map them to the parked user, while nobody can see :p
138 Srv->PseudoToUser(user,unpark,"Unparked to "+std::string(parameters[0]));
139 // set all their new modes
140 WriteServ(unpark->fd,"MODE %s +%s",unpark->nick,unpark->modes);
141 // spool their away log to them
142 WriteServ(unpark->fd,"NOTICE %s :*** You are now unparked. You have successfully taken back the nickname and privilages of %s.",unpark->nick,unpark->nick);
143 for (awaylog::iterator i = awy->begin(); i != awy->end(); i++)
145 char timebuf[MAXBUF];
146 tm *timeinfo = localtime(&i->tm);
147 strlcpy(timebuf,asctime(timeinfo),MAXBUF);
148 timebuf[strlen(timebuf)-1] = '\0';
149 WriteServ(unpark->fd,"NOTICE %s :From %s at %s: \2%s\2",unpark->nick,i->from.c_str(),timebuf,i->text.c_str());
152 unpark->Shrink("park_awaylog");
153 unpark->Shrink("park_key");
154 for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
156 if (j->nick == std::string(unpark->nick))
165 Srv->SendServ(user->fd,"928 "+std::string(user->nick)+" :Incorrect park key.");
170 class ModulePark : public Module
175 virtual void ReadSettings()
177 Conf = new ConfigReader;
178 ParkMaxTime = Conf->ReadInteger("park","maxtime",0,true);
179 ConcurrentParks = Conf->ReadInteger("park","maxperip",0,true);
180 ParkMaxMsgs = Conf->ReadInteger("park","maxmessages",0,true);
188 this->ReadSettings();
189 Srv->AddCommand("PARK",handle_park,0,0,"m_park.so");
190 Srv->AddCommand("UNPARK",handle_unpark,0,2,"m_park.so");
191 Srv->AddCommand("PARKSTATS",handle_parkstats,'o',0,"m_park.so");
194 virtual ~ModulePark()
199 virtual void OnRehash()
201 this->ReadSettings();
204 virtual void On005Numeric(std::string &output)
206 output = output + std::string(" PARK");
209 virtual void OnUserQuit(userrec* user)
211 std::string nick = user->nick;
212 // track quits in our parked user list
213 for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
224 virtual void OnPrePrivmsg(userrec* user, userrec* dest, std::string text)
226 awaylog* awy = (awaylog*)dest->GetExt("park_awaylog");
229 if (awy->size() <= ParkMaxMsgs)
233 am.from = user->GetFullHost();
236 Srv->SendServ(user->fd,"930 "+std::string(user->nick)+" :User "+std::string(dest->nick)+" is parked. Your message has been stored.");
238 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.");
242 virtual int OnUserPreNick(userrec* user, std::string newnick)
244 // track nickchanges in our parked user list
245 // (this isnt too efficient, i'll tidy it up some time)
246 for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
248 if (j->nick == std::string(user->nick))
257 virtual void OnBackgroundTimer(time_t curtime)
259 // look for parked clients which have timed out (this needs tidying)
262 bool go_again = true;
266 for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
268 if (time(NULL) >= (j->parktime+ParkMaxTime))
270 userrec* thisnick = Srv->FindNick(j->nick);
271 // THIS MUST COME BEFORE THE QuitUser - QuitUser can
272 // create a recursive call to OnUserQuit in this module
273 // and then corrupt the pointer!
276 Srv->QuitUser(thisnick,"PARK timeout");
284 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text)
286 if (target_type == TYPE_USER)
288 userrec* u = (userrec*)dest;
289 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;