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