]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_park.cpp
WHEEEEE!!!!!
[user/henk/code/inspircd.git] / src / modules / m_park.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
19 #include <stdio.h>
20 #include <string>
21 #include <vector>
22 #include "users.h"
23 #include "channels.h"
24 #include "helperfuncs.h"
25 #include "modules.h"
26
27 /* $ModDesc: Provides support for user parking/unparking */
28
29 class parking : public classbase
30 {
31 };
32
33 class awaymsg : public parking
34 {
35  public:
36         std::string from;
37         std::string text;
38         time_t tm;
39 };
40
41 class parkedinfo : public parking
42 {
43  public:
44         std::string nick;
45         std::string host;
46         time_t parktime;
47 };
48
49 static Server *Srv;
50 typedef std::vector<awaymsg> awaylog;
51 typedef std::vector<parkedinfo> parkinfo;
52 parkinfo pinfo;
53 long ParkMaxTime;
54 long ConcurrentParks;
55 long ParkMaxMsgs;
56 parkedinfo pi;
57
58 class cmd_park : public command_t
59 {
60  public:
61         cmd_park () : command_t("PARK", 0, 0)
62         {
63                 this->source = "m_park.so";
64         }
65
66         void Handle (const char** parameters, int pcnt, userrec *user)
67         {
68                 /** Parking. easy stuff.
69                  *
70                  * We duplicate and switch the users file descriptor, so that they can remain forever as a 'ghost'
71                  * We then disconnect the real user leaving a controlled ghost in their place :)
72                  */
73                 int othersessions = 0;
74                 /* XXX - why can't just use pinfo.size() like we do below, rather than iterating over the whole vector? -- w00t */
75                 if (pinfo.size())
76                         for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
77                                 if (j->host == std::string(user->host))
78                                         othersessions++;
79                 if (othersessions >= ConcurrentParks)
80                 {
81                         user->WriteServ("927 "+std::string(user->nick)+" :You are already parked up to the maximum number of allowed times.");
82                 }
83                 else
84                 {
85                         awaylog* aw;
86                         char msg[MAXBUF];
87                         unsigned long* key = new unsigned long;
88                         *key = abs(random() * 12345);
89                         snprintf(msg,MAXBUF,"You are now parked. To unpark use /UNPARK %s %lu",user->nick, *key);
90                         Srv->UserToPseudo(user,std::string(msg));
91                         aw = new awaylog;
92                         user->Extend("park_awaylog", aw);
93                         user->Extend("park_key", key);
94                         pi.nick = user->nick;
95                         pi.host = user->host;
96                         pi.parktime = time(NULL);
97                         pinfo.push_back(pi);
98                 }
99         }
100
101 };
102
103 class cmd_parkstats : public command_t
104 {
105  public:
106         cmd_parkstats () : command_t("PARKSTATS", 'o', 0)
107         {
108                 this->source = "m_park.so";
109         }
110
111         void Handle (const char** parameters, int pcnt, userrec *user)
112         {
113                 char status[MAXBUF];
114                 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);
115                 user->WriteServ(std::string(status));
116         }
117 };
118
119 class cmd_unpark : public command_t
120 {
121  public:
122         cmd_unpark () : command_t("UNPARK", 0, 2)
123         {
124                 this->source = "m_park.so";
125                 syntax = "<nick> <key>";
126         }
127
128         void Handle (const char** parameters, int pcnt, userrec *user)
129         {
130                 /** Unparking. complicated stuff.
131                  *
132                  * Unparking is done in several steps:
133                  *
134                  * (1) Check if the user is parked
135                  * (2) Check the key of the user against the one provided
136                  * (3) Part the user who issued the command from all of their channels so as not to confuse clients
137                  * (4) Remove all the users UMODEs
138                  * (5) Duplicate and switch file descriptors on the two users, and disconnect the dead one
139                  * (6) Force a nickchange onto the user who issued the command forcing them to change to the parked nick
140                  * (7) Force join the user into all the channels the parked nick is currently in (send them localized join and namelist)
141                  * (8) Send the user the umodes of their new 'persona'
142                  * (9) Spool any awaylog messages to the user
143                  *
144                  * And there you have it, easy huh (NOT)...
145                  */
146                 userrec* unpark = Srv->FindNick(std::string(parameters[0]));
147                 if (!unpark)
148                 {
149                         user->WriteServ("942 %s %s :Invalid user specified.",user->nick, parameters[0]);
150                         return;
151                 }
152                 awaylog* awy;
153                 unpark->GetExt("park_awaylog", awy);
154                 long* key;
155                 unpark->GetExt("park_key", key);
156                 if (!awy)
157                 {
158                         user->WriteServ("943 %s %s :This user is not parked.",user->nick, unpark->nick);
159                         return;
160                 }
161                 if (*key == atoi(parameters[1]))
162                 {
163                         // first part the user from all chans theyre on, so things dont get messy
164                         for (std::vector<ucrec*>::iterator i = user->chans.begin(); i != user->chans.end(); i++)
165                         {
166                                 chanrec* chan = (*i)->channel;
167                                 if (chan != NULL)
168                                 {
169                                         if (!chan->PartUser(user, "Unparking"))
170                                                 delete chan;
171                                 }
172                         }
173                         // remove all their old modes
174                         user->WriteServ("MODE %s -%s",user->nick,user->FormatModes());
175                         // now, map them to the parked user, while nobody can see :p
176                         Srv->PseudoToUser(user,unpark,"Unparked to "+std::string(parameters[0]));
177                         // set all their new modes
178                         unpark->WriteServ("MODE %s +%s",unpark->nick,unpark->FormatModes());
179                         // spool their away log to them
180                         unpark->WriteServ("NOTICE %s :*** You are now unparked. You have successfully taken back the nickname and privilages of %s.",unpark->nick,unpark->nick);
181                         for (awaylog::iterator i = awy->begin(); i != awy->end(); i++)
182                         {
183                                 char timebuf[MAXBUF];
184                                 tm *timeinfo = localtime(&i->tm);
185                                 strlcpy(timebuf,asctime(timeinfo),MAXBUF);
186                                 timebuf[strlen(timebuf)-1] = '\0';
187                                 unpark->WriteServ("NOTICE %s :From %s at %s: \2%s\2",unpark->nick,i->from.c_str(),timebuf,i->text.c_str());
188                         }
189                         DELETE(awy);
190                         DELETE(key);
191                         unpark->Shrink("park_awaylog");
192                         unpark->Shrink("park_key");
193                         for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
194                         {
195                                 if (j->nick == std::string(unpark->nick))
196                                 {
197                                         pinfo.erase(j);
198                                         break;
199                                 }
200                         }
201                 }
202                 else
203                 {
204                         user->WriteServ("928 "+std::string(user->nick)+" :Incorrect park key.");
205                 }
206         }
207 };
208
209 class ModulePark : public Module
210 {
211  protected:
212         ConfigReader* Conf;
213         cmd_park*       cmd1;
214         cmd_unpark*     cmd2;
215         cmd_parkstats*  cmd3;
216  public:
217         virtual void ReadSettings()
218         {
219                 Conf = new ConfigReader;
220                 ParkMaxTime = Conf->ReadInteger("park","maxtime",0,true);
221                 ConcurrentParks = Conf->ReadInteger("park","maxperip",0,true);
222                 ParkMaxMsgs = Conf->ReadInteger("park","maxmessages",0,true);
223                 DELETE(Conf);
224         }
225
226         ModulePark(Server* Me)
227                 : Module::Module(Me)
228         {
229                 Srv = Me;
230                 pinfo.clear();
231                 this->ReadSettings();
232                 cmd1 = new cmd_park();
233                 cmd2 = new cmd_unpark();
234                 cmd3 = new cmd_parkstats();
235                 Srv->AddCommand(cmd1);
236                 Srv->AddCommand(cmd2);
237                 Srv->AddCommand(cmd3);
238         }
239
240         virtual ~ModulePark()
241         {
242         }
243
244         void Implements(char* List)
245         {
246                 List[I_On005Numeric] = List[I_OnRehash] = List[I_OnUserQuit] = List[I_OnUserPreMessage] = List[I_OnUserPreNick] = List[I_OnBackgroundTimer] = List[I_OnWhois] = 1;
247         }
248
249         virtual void OnRehash(const std::string &parameter)
250         {
251                 this->ReadSettings();
252         }
253
254         virtual void On005Numeric(std::string &output)
255         {
256                 output = output + std::string(" PARK");
257         }
258
259         virtual void OnUserQuit(userrec* user, const std::string &reason)
260         {
261                 std::string nick = user->nick;
262                 // track quits in our parked user list
263                 for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
264                 {
265                         if (j->nick == nick)
266                         {
267                                 pinfo.erase(j);
268                                 break;
269                         }
270                 }
271         }
272
273
274         virtual void OnPrePrivmsg(userrec* user, userrec* dest, const std::string &text)
275         {
276                 awaylog* awy;
277                 dest->GetExt("park_awaylog", awy);
278                 if (awy)
279                 {
280                         if (awy->size() <= (unsigned)ParkMaxMsgs)
281                         {
282                                 awaymsg am;
283                                 am.text = text;
284                                 am.from = user->GetFullHost();
285                                 am.tm = time(NULL);
286                                 awy->push_back(am);
287                                 user->WriteServ("930 "+std::string(user->nick)+" :User "+std::string(dest->nick)+" is parked. Your message has been stored.");
288                         }
289                         else user->WriteServ("929 "+std::string(user->nick)+" :User "+std::string(dest->nick)+" is parked, but their message queue is full. Message not saved.");
290                 }
291         }
292
293         virtual int OnUserPreNick(userrec* user, const std::string &newnick)
294         {
295                 // track nickchanges in our parked user list
296                 // (this isnt too efficient, i'll tidy it up some time)
297                 /* XXX - perhaps extend the user record, or, that wouldn't work - perhaps use a map? -- w00t */
298                 for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
299                 {
300                         if (j->nick == std::string(user->nick))
301                         {
302                                 j->nick = newnick;
303                                 break;
304                         }
305                 }
306                 return 0;
307         }
308
309         virtual void OnBackgroundTimer(time_t curtime)
310         {
311                 // look for parked clients which have timed out (this needs tidying)
312                 if (pinfo.empty())
313                         return;
314                 bool go_again = true;
315                 while (go_again)
316                 {
317                         go_again = false;
318                         for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
319                         {
320                                 if (time(NULL) >= (j->parktime+ParkMaxTime))
321                                 {
322                                         userrec* thisnick = Srv->FindNick(j->nick);
323                                         // THIS MUST COME BEFORE THE QuitUser - QuitUser can
324                                         // create a recursive call to OnUserQuit in this module
325                                         // and then corrupt the pointer!
326                                         pinfo.erase(j);
327                                         if (thisnick)
328                                                 userrec::QuitUser(thisnick,"PARK timeout");
329                                         go_again = true;
330                                         break;
331                                 }
332                         }
333                 }
334         }
335
336         /* XXX - why is OnPrePrivmsg seperated here, I assume there is reason for the extra function call? --w00t */
337         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status)
338         {
339                 if (target_type == TYPE_USER)
340                 {
341                         userrec* u = (userrec*)dest;
342                         OnPrePrivmsg(user,u,text);
343                 }
344                 return 0;
345         }
346
347         virtual void OnWhois(userrec* src, userrec* dst)
348         {
349                 char* dummy;
350                 if (dst->GetExt("park_awaylog", dummy))
351                         src->WriteServ("335 "+std::string(src->nick)+" "+std::string(dst->nick)+" :is a parked client");
352         }
353         
354         virtual Version GetVersion()
355         {
356                 return Version(1,0,0,1,VF_VENDOR);
357         }
358         
359 };
360
361 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
362
363 class ModuleParkFactory : public ModuleFactory
364 {
365  public:
366         ModuleParkFactory()
367         {
368         }
369         
370         ~ModuleParkFactory()
371         {
372         }
373         
374         virtual Module * CreateModule(Server* Me)
375         {
376                 return new ModulePark(Me);
377         }
378         
379 };
380
381
382 extern "C" void * init_module( void )
383 {
384         return new ModuleParkFactory;
385 }
386