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