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