]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_park.cpp
Gah, im forgetting to SetMode!
[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 = 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 (std::vector<ucrec*>::iterator i = user->chans.begin(); i != user->chans.end(); i++)
157                         {
158                                 if (((ucrec*)(*i))->channel != NULL)
159                                 {
160                                         Srv->PartUserFromChannel(user,((ucrec*)(*i))->channel->name,"Unparking");
161                                 }
162                         }
163                         // remove all their old modes
164                         WriteServ(user->fd,"MODE %s -%s",user->nick,user->FormatModes());
165                         // now, map them to the parked user, while nobody can see :p
166                         Srv->PseudoToUser(user,unpark,"Unparked to "+std::string(parameters[0]));
167                         // set all their new modes
168                         WriteServ(unpark->fd,"MODE %s +%s",unpark->nick,unpark->FormatModes());
169                         // spool their away log to them
170                         WriteServ(unpark->fd,"NOTICE %s :*** You are now unparked. You have successfully taken back the nickname and privilages of %s.",unpark->nick,unpark->nick);
171                         for (awaylog::iterator i = awy->begin(); i != awy->end(); i++)
172                         {
173                                 char timebuf[MAXBUF];
174                                 tm *timeinfo = localtime(&i->tm);
175                                 strlcpy(timebuf,asctime(timeinfo),MAXBUF);
176                                 timebuf[strlen(timebuf)-1] = '\0';
177                                 WriteServ(unpark->fd,"NOTICE %s :From %s at %s: \2%s\2",unpark->nick,i->from.c_str(),timebuf,i->text.c_str());
178                         }
179                         DELETE(awy);
180                         unpark->Shrink("park_awaylog");
181                         unpark->Shrink("park_key");
182                         for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
183                         {
184                                 if (j->nick == std::string(unpark->nick))
185                                 {
186                                         pinfo.erase(j);
187                                         break;
188                                 }
189                         }
190                 }
191                 else
192                 {
193                         Srv->SendServ(user->fd,"928 "+std::string(user->nick)+" :Incorrect park key.");
194                 }
195         }
196 };
197
198 class ModulePark : public Module
199 {
200  protected:
201         ConfigReader* Conf;
202         cmd_park*       cmd1;
203         cmd_unpark*     cmd2;
204         cmd_parkstats*  cmd3;
205  public:
206         virtual void ReadSettings()
207         {
208                 Conf = new ConfigReader;
209                 ParkMaxTime = Conf->ReadInteger("park","maxtime",0,true);
210                 ConcurrentParks = Conf->ReadInteger("park","maxperip",0,true);
211                 ParkMaxMsgs = Conf->ReadInteger("park","maxmessages",0,true);
212                 DELETE(Conf);
213         }
214
215         ModulePark(Server* Me)
216                 : Module::Module(Me)
217         {
218                 Srv = Me;
219                 pinfo.clear();
220                 this->ReadSettings();
221                 cmd1 = new cmd_park();
222                 cmd2 = new cmd_unpark();
223                 cmd3 = new cmd_parkstats();
224                 Srv->AddCommand(cmd1);
225                 Srv->AddCommand(cmd2);
226                 Srv->AddCommand(cmd3);
227         }
228
229         virtual ~ModulePark()
230         {
231         }
232
233         void Implements(char* List)
234         {
235                 List[I_On005Numeric] = List[I_OnRehash] = List[I_OnUserQuit] = List[I_OnUserPreMessage] = List[I_OnUserPreNick] = List[I_OnBackgroundTimer] = List[I_OnWhois] = 1;
236         }
237
238         virtual void OnRehash(const std::string &parameter)
239         {
240                 this->ReadSettings();
241         }
242
243         virtual void On005Numeric(std::string &output)
244         {
245                 output = output + std::string(" PARK");
246         }
247
248         virtual void OnUserQuit(userrec* user, const std::string &reason)
249         {
250                 std::string nick = user->nick;
251                 // track quits in our parked user list
252                 for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
253                 {
254                         if (j->nick == nick)
255                         {
256                                 pinfo.erase(j);
257                                 break;
258                         }
259                 }
260         }
261
262
263         virtual void OnPrePrivmsg(userrec* user, userrec* dest, const std::string &text)
264         {
265                 awaylog* awy = (awaylog*)dest->GetExt("park_awaylog");
266                 if (awy)
267                 {
268                         if (awy->size() <= (unsigned)ParkMaxMsgs)
269                         {
270                                 awaymsg am;
271                                 am.text = text;
272                                 am.from = user->GetFullHost();
273                                 am.tm = time(NULL);
274                                 awy->push_back(am);
275                                 Srv->SendServ(user->fd,"930 "+std::string(user->nick)+" :User "+std::string(dest->nick)+" is parked. Your message has been stored.");
276                         }
277                         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.");
278                 }
279         }
280
281         virtual int OnUserPreNick(userrec* user, const std::string &newnick)
282         {
283                 // track nickchanges in our parked user list
284                 // (this isnt too efficient, i'll tidy it up some time)
285                 /* XXX - perhaps extend the user record, or, that wouldn't work - perhaps use a map? -- w00t */
286                 for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
287                 {
288                         if (j->nick == std::string(user->nick))
289                         {
290                                 j->nick = newnick;
291                                 break;
292                         }
293                 }
294                 return 0;
295         }
296
297         virtual void OnBackgroundTimer(time_t curtime)
298         {
299                 // look for parked clients which have timed out (this needs tidying)
300                 if (pinfo.empty())
301                         return;
302                 bool go_again = true;
303                 while (go_again)
304                 {
305                         go_again = false;
306                         for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
307                         {
308                                 if (time(NULL) >= (j->parktime+ParkMaxTime))
309                                 {
310                                         userrec* thisnick = Srv->FindNick(j->nick);
311                                         // THIS MUST COME BEFORE THE QuitUser - QuitUser can
312                                         // create a recursive call to OnUserQuit in this module
313                                         // and then corrupt the pointer!
314                                         pinfo.erase(j);
315                                         if (thisnick)
316                                                 Srv->QuitUser(thisnick,"PARK timeout");
317                                         go_again = true;
318                                         break;
319                                 }
320                         }
321                 }
322         }
323
324         /* XXX - why is OnPrePrivmsg seperated here, I assume there is reason for the extra function call? --w00t */
325         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status)
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