]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_park.cpp
798c4edd80e946d46b08a4b5d08593715783f9b0
[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 #include "inspircd.h"
27
28 extern InspIRCd* ServerInstance;
29
30 /* $ModDesc: Provides support for user parking/unparking */
31
32 class parking : public classbase
33 {
34 };
35
36 class awaymsg : public parking
37 {
38  public:
39         std::string from;
40         std::string text;
41         time_t tm;
42 };
43
44 class parkedinfo : public parking
45 {
46  public:
47         std::string nick;
48         std::string host;
49         time_t parktime;
50 };
51
52
53 typedef std::vector<awaymsg> awaylog;
54 typedef std::vector<parkedinfo> parkinfo;
55 parkinfo pinfo;
56 long ParkMaxTime;
57 long ConcurrentParks;
58 long ParkMaxMsgs;
59 parkedinfo pi;
60
61 class cmd_park : public command_t
62 {
63  public:
64  cmd_park (InspIRCd* Instance) : command_t(Instance,"PARK", 0, 0)
65         {
66                 this->source = "m_park.so";
67         }
68
69         void Handle (const char** parameters, int pcnt, userrec *user)
70         {
71                 /** Parking. easy stuff.
72                  *
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 :)
75                  */
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 */
78                 if (pinfo.size())
79                         for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
80                                 if (j->host == std::string(user->host))
81                                         othersessions++;
82                 if (othersessions >= ConcurrentParks)
83                 {
84                         user->WriteServ("927 "+std::string(user->nick)+" :You are already parked up to the maximum number of allowed times.");
85                 }
86                 else
87                 {
88                         awaylog* aw;
89                         char msg[MAXBUF];
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));
94                         aw = new awaylog;
95                         user->Extend("park_awaylog", aw);
96                         user->Extend("park_key", key);
97                         pi.nick = user->nick;
98                         pi.host = user->host;
99                         pi.parktime = time(NULL);
100                         pinfo.push_back(pi);
101                 }
102         }
103
104 };
105
106 class cmd_parkstats : public command_t
107 {
108  public:
109  cmd_parkstats (InspIRCd* Instance) : command_t(Instance,"PARKSTATS", 'o', 0)
110         {
111                 this->source = "m_park.so";
112         }
113
114         void Handle (const char** parameters, int pcnt, userrec *user)
115         {
116                 char status[MAXBUF];
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));
119         }
120 };
121
122 class cmd_unpark : public command_t
123 {
124  public:
125  cmd_unpark (InspIRCd* Instance) : command_t(Instance,"UNPARK", 0, 2)
126         {
127                 this->source = "m_park.so";
128                 syntax = "<nick> <key>";
129         }
130
131         void Handle (const char** parameters, int pcnt, userrec *user)
132         {
133                 /** Unparking. complicated stuff.
134                  *
135                  * Unparking is done in several steps:
136                  *
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
146                  *
147                  * And there you have it, easy huh (NOT)...
148                  */
149                 userrec* unpark = ServerInstance->FindNick(parameters[0]);
150                 if (!unpark)
151                 {
152                         user->WriteServ("942 %s %s :Invalid user specified.",user->nick, parameters[0]);
153                         return;
154                 }
155                 awaylog* awy;
156                 unpark->GetExt("park_awaylog", awy);
157                 long* key;
158                 unpark->GetExt("park_key", key);
159                 if (!awy)
160                 {
161                         user->WriteServ("943 %s %s :This user is not parked.",user->nick, unpark->nick);
162                         return;
163                 }
164                 if (*key == atoi(parameters[1]))
165                 {
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++)
168                         {
169                                 chanrec* chan = (*i)->channel;
170                                 if (chan != NULL)
171                                 {
172                                         if (!chan->PartUser(user, "Unparking"))
173                                                 delete chan;
174                                 }
175                         }
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++)
185                         {
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());
191                         }
192                         DELETE(awy);
193                         DELETE(key);
194                         unpark->Shrink("park_awaylog");
195                         unpark->Shrink("park_key");
196                         for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
197                         {
198                                 if (j->nick == std::string(unpark->nick))
199                                 {
200                                         pinfo.erase(j);
201                                         break;
202                                 }
203                         }
204                 }
205                 else
206                 {
207                         user->WriteServ("928 "+std::string(user->nick)+" :Incorrect park key.");
208                 }
209         }
210 };
211
212 class ModulePark : public Module
213 {
214  protected:
215         ConfigReader* Conf;
216         cmd_park*       cmd1;
217         cmd_unpark*     cmd2;
218         cmd_parkstats*  cmd3;
219  public:
220         virtual void ReadSettings()
221         {
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);
226                 DELETE(Conf);
227         }
228
229         ModulePark(InspIRCd* Me)
230                 : Module::Module(Me)
231         {
232                 
233                 pinfo.clear();
234                 this->ReadSettings();
235                 cmd1 = new cmd_park(ServerInstance);
236                 cmd2 = new cmd_unpark(ServerInstance);
237                 cmd3 = new cmd_parkstats(ServerInstance);
238                 ServerInstance->AddCommand(cmd1);
239                 ServerInstance->AddCommand(cmd2);
240                 ServerInstance->AddCommand(cmd3);
241         }
242
243         virtual ~ModulePark()
244         {
245         }
246
247         void Implements(char* List)
248         {
249                 List[I_On005Numeric] = List[I_OnRehash] = List[I_OnUserQuit] = List[I_OnUserPreMessage] = List[I_OnUserPreNick] = List[I_OnBackgroundTimer] = List[I_OnWhois] = 1;
250         }
251
252         virtual void OnRehash(const std::string &parameter)
253         {
254                 this->ReadSettings();
255         }
256
257         virtual void On005Numeric(std::string &output)
258         {
259                 output = output + std::string(" PARK");
260         }
261
262         virtual void OnUserQuit(userrec* user, const std::string &reason)
263         {
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++)
267                 {
268                         if (j->nick == nick)
269                         {
270                                 pinfo.erase(j);
271                                 break;
272                         }
273                 }
274         }
275
276
277         virtual void OnPrePrivmsg(userrec* user, userrec* dest, const std::string &text)
278         {
279                 awaylog* awy;
280                 dest->GetExt("park_awaylog", awy);
281                 if (awy)
282                 {
283                         if (awy->size() <= (unsigned)ParkMaxMsgs)
284                         {
285                                 awaymsg am;
286                                 am.text = text;
287                                 am.from = user->GetFullHost();
288                                 am.tm = time(NULL);
289                                 awy->push_back(am);
290                                 user->WriteServ("930 "+std::string(user->nick)+" :User "+std::string(dest->nick)+" is parked. Your message has been stored.");
291                         }
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.");
293                 }
294         }
295
296         virtual int OnUserPreNick(userrec* user, const std::string &newnick)
297         {
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++)
302                 {
303                         if (j->nick == std::string(user->nick))
304                         {
305                                 j->nick = newnick;
306                                 break;
307                         }
308                 }
309                 return 0;
310         }
311
312         virtual void OnBackgroundTimer(time_t curtime)
313         {
314                 // look for parked clients which have timed out (this needs tidying)
315                 if (pinfo.empty())
316                         return;
317                 bool go_again = true;
318                 while (go_again)
319                 {
320                         go_again = false;
321                         for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
322                         {
323                                 if (time(NULL) >= (j->parktime+ParkMaxTime))
324                                 {
325                                         userrec* thisnick = ServerInstance->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!
329                                         pinfo.erase(j);
330                                         if (thisnick)
331                                                 userrec::QuitUser(ServerInstance,thisnick,"PARK timeout");
332                                         go_again = true;
333                                         break;
334                                 }
335                         }
336                 }
337         }
338
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)
341         {
342                 if (target_type == TYPE_USER)
343                 {
344                         userrec* u = (userrec*)dest;
345                         OnPrePrivmsg(user,u,text);
346                 }
347                 return 0;
348         }
349
350         virtual void OnWhois(userrec* src, userrec* dst)
351         {
352                 char* dummy;
353                 if (dst->GetExt("park_awaylog", dummy))
354                         src->WriteServ("335 "+std::string(src->nick)+" "+std::string(dst->nick)+" :is a parked client");
355         }
356         
357         virtual Version GetVersion()
358         {
359                 return Version(1,0,0,1,VF_VENDOR);
360         }
361         
362 };
363
364 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
365
366 class ModuleParkFactory : public ModuleFactory
367 {
368  public:
369         ModuleParkFactory()
370         {
371         }
372         
373         ~ModuleParkFactory()
374         {
375         }
376         
377         virtual Module * CreateModule(InspIRCd* Me)
378         {
379                 return new ModulePark(Me);
380         }
381         
382 };
383
384
385 extern "C" void * init_module( void )
386 {
387         return new ModuleParkFactory;
388 }
389