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