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 * ---------------------------------------------------
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 (InspIRCd* Instance) : command_t(Instance,"PARK", 0, 0)
66 this->source = "m_park.so";
69 CmdResult 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.");
91 unsigned long* key = new unsigned long;
92 *key = abs(random() * 12345);
93 snprintf(msg,MAXBUF,"You are now parked. To unpark use /UNPARK %s %lu",user->nick, *key);
94 ServerInstance->UserToPseudo(user,std::string(msg));
96 user->Extend("park_awaylog", aw);
97 user->Extend("park_key", key);
100 pi.parktime = time(NULL);
108 class cmd_parkstats : public command_t
111 cmd_parkstats (InspIRCd* Instance) : command_t(Instance,"PARKSTATS", 'o', 0)
113 this->source = "m_park.so";
116 CmdResult Handle (const char** parameters, int pcnt, userrec *user)
119 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);
120 user->WriteServ(std::string(status));
125 class cmd_unpark : public command_t
128 cmd_unpark (InspIRCd* Instance) : command_t(Instance,"UNPARK", 0, 2)
130 this->source = "m_park.so";
131 syntax = "<nick> <key>";
134 CmdResult Handle (const char** parameters, int pcnt, userrec *user)
136 /** Unparking. complicated stuff.
138 * Unparking is done in several steps:
140 * (1) Check if the user is parked
141 * (2) Check the key of the user against the one provided
142 * (3) Part the user who issued the command from all of their channels so as not to confuse clients
143 * (4) Remove all the users UMODEs
144 * (5) Duplicate and switch file descriptors on the two users, and disconnect the dead one
145 * (6) Force a nickchange onto the user who issued the command forcing them to change to the parked nick
146 * (7) Force join the user into all the channels the parked nick is currently in (send them localized join and namelist)
147 * (8) Send the user the umodes of their new 'persona'
148 * (9) Spool any awaylog messages to the user
150 * And there you have it, easy huh (NOT)...
152 userrec* unpark = ServerInstance->FindNick(parameters[0]);
155 user->WriteServ("942 %s %s :Invalid user specified.",user->nick, parameters[0]);
159 unpark->GetExt("park_awaylog", awy);
161 unpark->GetExt("park_key", key);
164 user->WriteServ("943 %s %s :This user is not parked.",user->nick, unpark->nick);
167 if (*key == atoi(parameters[1]))
169 // first part the user from all chans theyre on, so things dont get messy
170 for (std::vector<ucrec*>::iterator i = user->chans.begin(); i != user->chans.end(); i++)
172 chanrec* chan = (*i)->channel;
175 if (!chan->PartUser(user, "Unparking"))
179 // remove all their old modes
180 user->WriteServ("MODE %s -%s",user->nick,user->FormatModes());
181 // now, map them to the parked user, while nobody can see :p
182 ServerInstance->PseudoToUser(user,unpark,"Unparked to "+std::string(parameters[0]));
183 // set all their new modes
184 unpark->WriteServ("MODE %s +%s",unpark->nick,unpark->FormatModes());
185 // spool their away log to them
186 unpark->WriteServ("NOTICE %s :*** You are now unparked. You have successfully taken back the nickname and privilages of %s.",unpark->nick,unpark->nick);
187 for (awaylog::iterator i = awy->begin(); i != awy->end(); i++)
189 char timebuf[MAXBUF];
190 tm *timeinfo = localtime(&i->tm);
191 strlcpy(timebuf,asctime(timeinfo),MAXBUF);
192 timebuf[strlen(timebuf)-1] = '\0';
193 unpark->WriteServ("NOTICE %s :From %s at %s: \2%s\2",unpark->nick,i->from.c_str(),timebuf,i->text.c_str());
197 unpark->Shrink("park_awaylog");
198 unpark->Shrink("park_key");
199 for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
201 if (j->nick == std::string(unpark->nick))
210 user->WriteServ("928 "+std::string(user->nick)+" :Incorrect park key.");
218 class ModulePark : public Module
226 virtual void ReadSettings()
228 Conf = new ConfigReader(ServerInstance);
229 ParkMaxTime = Conf->ReadInteger("park","maxtime",0,true);
230 ConcurrentParks = Conf->ReadInteger("park","maxperip",0,true);
231 ParkMaxMsgs = Conf->ReadInteger("park","maxmessages",0,true);
235 ModulePark(InspIRCd* Me)
240 this->ReadSettings();
241 cmd1 = new cmd_park(ServerInstance);
242 cmd2 = new cmd_unpark(ServerInstance);
243 cmd3 = new cmd_parkstats(ServerInstance);
244 ServerInstance->AddCommand(cmd1);
245 ServerInstance->AddCommand(cmd2);
246 ServerInstance->AddCommand(cmd3);
249 virtual ~ModulePark()
253 void Implements(char* List)
255 List[I_On005Numeric] = List[I_OnRehash] = List[I_OnUserQuit] = List[I_OnUserPreMessage] = List[I_OnUserPreNick] = List[I_OnBackgroundTimer] = List[I_OnWhois] = 1;
258 virtual void OnRehash(const std::string ¶meter)
260 this->ReadSettings();
263 virtual void On005Numeric(std::string &output)
265 output = output + std::string(" PARK");
268 virtual void OnUserQuit(userrec* user, const std::string &reason)
270 std::string nick = user->nick;
271 // track quits in our parked user list
272 for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
283 virtual void OnPrePrivmsg(userrec* user, userrec* dest, const std::string &text)
286 dest->GetExt("park_awaylog", awy);
289 if (awy->size() <= (unsigned)ParkMaxMsgs)
293 am.from = user->GetFullHost();
296 user->WriteServ("930 "+std::string(user->nick)+" :User "+std::string(dest->nick)+" is parked. Your message has been stored.");
298 else user->WriteServ("929 "+std::string(user->nick)+" :User "+std::string(dest->nick)+" is parked, but their message queue is full. Message not saved.");
302 virtual int OnUserPreNick(userrec* user, const std::string &newnick)
304 // track nickchanges in our parked user list
305 // (this isnt too efficient, i'll tidy it up some time)
306 /* XXX - perhaps extend the user record, or, that wouldn't work - perhaps use a map? -- w00t */
307 for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
309 if (j->nick == std::string(user->nick))
318 virtual void OnBackgroundTimer(time_t curtime)
320 // look for parked clients which have timed out (this needs tidying)
323 bool go_again = true;
327 for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
329 if (time(NULL) >= (j->parktime+ParkMaxTime))
331 userrec* thisnick = ServerInstance->FindNick(j->nick);
332 // THIS MUST COME BEFORE THE QuitUser - QuitUser can
333 // create a recursive call to OnUserQuit in this module
334 // and then corrupt the pointer!
337 userrec::QuitUser(ServerInstance,thisnick,"PARK timeout");
345 /* XXX - why is OnPrePrivmsg seperated here, I assume there is reason for the extra function call? --w00t */
346 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status)
348 if (target_type == TYPE_USER)
350 userrec* u = (userrec*)dest;
351 OnPrePrivmsg(user,u,text);
356 virtual void OnWhois(userrec* src, userrec* dst)
359 if (dst->GetExt("park_awaylog", dummy))
360 src->WriteServ("335 "+std::string(src->nick)+" "+std::string(dst->nick)+" :is a parked client");
363 virtual Version GetVersion()
365 return Version(1,0,0,1,VF_VENDOR);
370 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
372 class ModuleParkFactory : public ModuleFactory
383 virtual Module * CreateModule(InspIRCd* Me)
385 return new ModulePark(Me);
391 extern "C" void * init_module( void )
393 return new ModuleParkFactory;