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