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"
28 extern InspIRCd* ServerInstance;
30 /* $ModDesc: Provides support for user parking/unparking */
32 class parking : public classbase
36 class awaymsg : public parking
44 class parkedinfo : public parking
53 typedef std::vector<awaymsg> awaylog;
54 typedef std::vector<parkedinfo> parkinfo;
61 class cmd_park : public command_t
64 cmd_park () : command_t("PARK", 0, 0)
66 this->source = "m_park.so";
69 void Handle (const char** parameters, int pcnt, userrec *user)
71 /** Parking. easy stuff.
73 * We duplicate and switch the users file descriptor, so that they can remain forever as a 'ghost'
74 * We then disconnect the real user leaving a controlled ghost in their place :)
76 int othersessions = 0;
77 /* XXX - why can't just use pinfo.size() like we do below, rather than iterating over the whole vector? -- w00t */
79 for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
80 if (j->host == std::string(user->host))
82 if (othersessions >= ConcurrentParks)
84 user->WriteServ("927 "+std::string(user->nick)+" :You are already parked up to the maximum number of allowed times.");
90 unsigned long* key = new unsigned long;
91 *key = abs(random() * 12345);
92 snprintf(msg,MAXBUF,"You are now parked. To unpark use /UNPARK %s %lu",user->nick, *key);
93 ServerInstance->UserToPseudo(user,std::string(msg));
95 user->Extend("park_awaylog", aw);
96 user->Extend("park_key", key);
99 pi.parktime = time(NULL);
106 class cmd_parkstats : public command_t
109 cmd_parkstats () : command_t("PARKSTATS", 'o', 0)
111 this->source = "m_park.so";
114 void Handle (const char** parameters, int pcnt, userrec *user)
117 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);
118 user->WriteServ(std::string(status));
122 class cmd_unpark : public command_t
125 cmd_unpark () : command_t("UNPARK", 0, 2)
127 this->source = "m_park.so";
128 syntax = "<nick> <key>";
131 void Handle (const char** parameters, int pcnt, userrec *user)
133 /** Unparking. complicated stuff.
135 * Unparking is done in several steps:
137 * (1) Check if the user is parked
138 * (2) Check the key of the user against the one provided
139 * (3) Part the user who issued the command from all of their channels so as not to confuse clients
140 * (4) Remove all the users UMODEs
141 * (5) Duplicate and switch file descriptors on the two users, and disconnect the dead one
142 * (6) Force a nickchange onto the user who issued the command forcing them to change to the parked nick
143 * (7) Force join the user into all the channels the parked nick is currently in (send them localized join and namelist)
144 * (8) Send the user the umodes of their new 'persona'
145 * (9) Spool any awaylog messages to the user
147 * And there you have it, easy huh (NOT)...
149 userrec* unpark = Srv->FindNick(std::string(parameters[0]));
152 user->WriteServ("942 %s %s :Invalid user specified.",user->nick, parameters[0]);
156 unpark->GetExt("park_awaylog", awy);
158 unpark->GetExt("park_key", key);
161 user->WriteServ("943 %s %s :This user is not parked.",user->nick, unpark->nick);
164 if (*key == atoi(parameters[1]))
166 // first part the user from all chans theyre on, so things dont get messy
167 for (std::vector<ucrec*>::iterator i = user->chans.begin(); i != user->chans.end(); i++)
169 chanrec* chan = (*i)->channel;
172 if (!chan->PartUser(user, "Unparking"))
176 // remove all their old modes
177 user->WriteServ("MODE %s -%s",user->nick,user->FormatModes());
178 // now, map them to the parked user, while nobody can see :p
179 ServerInstance->PseudoToUser(user,unpark,"Unparked to "+std::string(parameters[0]));
180 // set all their new modes
181 unpark->WriteServ("MODE %s +%s",unpark->nick,unpark->FormatModes());
182 // spool their away log to them
183 unpark->WriteServ("NOTICE %s :*** You are now unparked. You have successfully taken back the nickname and privilages of %s.",unpark->nick,unpark->nick);
184 for (awaylog::iterator i = awy->begin(); i != awy->end(); i++)
186 char timebuf[MAXBUF];
187 tm *timeinfo = localtime(&i->tm);
188 strlcpy(timebuf,asctime(timeinfo),MAXBUF);
189 timebuf[strlen(timebuf)-1] = '\0';
190 unpark->WriteServ("NOTICE %s :From %s at %s: \2%s\2",unpark->nick,i->from.c_str(),timebuf,i->text.c_str());
194 unpark->Shrink("park_awaylog");
195 unpark->Shrink("park_key");
196 for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
198 if (j->nick == std::string(unpark->nick))
207 user->WriteServ("928 "+std::string(user->nick)+" :Incorrect park key.");
212 class ModulePark : public Module
220 virtual void ReadSettings()
222 Conf = new ConfigReader;
223 ParkMaxTime = Conf->ReadInteger("park","maxtime",0,true);
224 ConcurrentParks = Conf->ReadInteger("park","maxperip",0,true);
225 ParkMaxMsgs = Conf->ReadInteger("park","maxmessages",0,true);
229 ModulePark(Server* Me)
234 this->ReadSettings();
235 cmd1 = new cmd_park();
236 cmd2 = new cmd_unpark();
237 cmd3 = new cmd_parkstats();
238 Srv->AddCommand(cmd1);
239 Srv->AddCommand(cmd2);
240 Srv->AddCommand(cmd3);
243 virtual ~ModulePark()
247 void Implements(char* List)
249 List[I_On005Numeric] = List[I_OnRehash] = List[I_OnUserQuit] = List[I_OnUserPreMessage] = List[I_OnUserPreNick] = List[I_OnBackgroundTimer] = List[I_OnWhois] = 1;
252 virtual void OnRehash(const std::string ¶meter)
254 this->ReadSettings();
257 virtual void On005Numeric(std::string &output)
259 output = output + std::string(" PARK");
262 virtual void OnUserQuit(userrec* user, const std::string &reason)
264 std::string nick = user->nick;
265 // track quits in our parked user list
266 for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
277 virtual void OnPrePrivmsg(userrec* user, userrec* dest, const std::string &text)
280 dest->GetExt("park_awaylog", awy);
283 if (awy->size() <= (unsigned)ParkMaxMsgs)
287 am.from = user->GetFullHost();
290 user->WriteServ("930 "+std::string(user->nick)+" :User "+std::string(dest->nick)+" is parked. Your message has been stored.");
292 else user->WriteServ("929 "+std::string(user->nick)+" :User "+std::string(dest->nick)+" is parked, but their message queue is full. Message not saved.");
296 virtual int OnUserPreNick(userrec* user, const std::string &newnick)
298 // track nickchanges in our parked user list
299 // (this isnt too efficient, i'll tidy it up some time)
300 /* XXX - perhaps extend the user record, or, that wouldn't work - perhaps use a map? -- w00t */
301 for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
303 if (j->nick == std::string(user->nick))
312 virtual void OnBackgroundTimer(time_t curtime)
314 // look for parked clients which have timed out (this needs tidying)
317 bool go_again = true;
321 for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
323 if (time(NULL) >= (j->parktime+ParkMaxTime))
325 userrec* thisnick = Srv->FindNick(j->nick);
326 // THIS MUST COME BEFORE THE QuitUser - QuitUser can
327 // create a recursive call to OnUserQuit in this module
328 // and then corrupt the pointer!
331 userrec::QuitUser(ServerInstance,thisnick,"PARK timeout");
339 /* XXX - why is OnPrePrivmsg seperated here, I assume there is reason for the extra function call? --w00t */
340 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status)
342 if (target_type == TYPE_USER)
344 userrec* u = (userrec*)dest;
345 OnPrePrivmsg(user,u,text);
350 virtual void OnWhois(userrec* src, userrec* dst)
353 if (dst->GetExt("park_awaylog", dummy))
354 src->WriteServ("335 "+std::string(src->nick)+" "+std::string(dst->nick)+" :is a parked client");
357 virtual Version GetVersion()
359 return Version(1,0,0,1,VF_VENDOR);
364 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
366 class ModuleParkFactory : public ModuleFactory
377 virtual Module * CreateModule(Server* Me)
379 return new ModulePark(Me);
385 extern "C" void * init_module( void )
387 return new ModuleParkFactory;