]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_park.cpp
1cf69b46fea9a2dc9e72b9382ee693b9195483bc
[user/henk/code/inspircd.git] / src / modules / m_park.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  Inspire is copyright (C) 2002-2004 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 #include <stdio.h>
18 #include <string>
19 #include <vector>
20 #include "users.h"
21 #include "channels.h"
22 #include "helperfuncs.h"
23 #include "modules.h"
24
25 /* $ModDesc: Provides support for user parking/unparking */
26
27 class awaymsg
28 {
29  public:
30         std::string from;
31         std::string text;
32         time_t tm;
33 };
34
35 class parkedinfo
36 {
37  public:
38         std::string nick;
39         std::string host;
40         time_t parktime;
41 };
42
43 Server *Srv;
44 typedef std::vector<awaymsg> awaylog;
45 typedef std::vector<parkedinfo> parkinfo;
46 parkinfo pinfo;
47 long ParkMaxTime;
48 long ConcurrentParks;
49 long ParkMaxMsgs;
50 parkedinfo pi;
51
52 void handle_park(char **parameters, int pcnt, userrec *user)
53 {
54         /** Parking. easy stuff.
55          *
56          * We duplicate and switch the users file descriptor, so that they can remain forever as a 'ghost'
57          * We then disconnect the real user leaving a controlled ghost in their place :)
58          */
59         int othersessions = 0;
60         if (pinfo.size())
61                 for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
62                         if (j->host == std::string(user->host))
63                                 othersessions++;
64         if (othersessions >= ConcurrentParks)
65         {
66                 Srv->SendServ(user->fd,"927 "+std::string(user->nick)+" :You are already parked up to the maximum number of allowed times.");
67         }
68         else
69         {
70                 awaylog* aw;
71                 char msg[MAXBUF];
72                 unsigned long key = abs(random() * 12345);
73                 snprintf(msg,MAXBUF,"You are now parked. To unpark use /UNPARK %s %lu",user->nick,key);
74                 Srv->UserToPseudo(user,std::string(msg));
75                 aw = new awaylog;
76                 user->Extend("park_awaylog",(char*)aw);
77                 user->Extend("park_key",(char*)key);
78                 pi.nick = user->nick;
79                 pi.host = user->host;
80                 pi.parktime = time(NULL);
81                 pinfo.push_back(pi);
82         }
83 }
84
85 void handle_parkstats(char **parameters, int pcnt, userrec *user)
86 {
87         char status[MAXBUF];
88         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);
89         Srv->SendServ(user->fd,status);
90 }
91
92 void handle_unpark(char **parameters, int pcnt, userrec *user)
93 {
94         /** Unparking. complicated stuff.
95          *
96          * Unparking is done in several steps:
97          *
98          * (1) Check if the user is parked
99          * (2) Check the key of the user against the one provided
100          * (3) Part the user who issued the command from all of their channels so as not to confuse clients
101          * (4) Remove all the users UMODEs
102          * (5) Duplicate and switch file descriptors on the two users, and disconnect the dead one
103          * (6) Force a nickchange onto the user who issued the command forcing them to change to the parked nick
104          * (7) Force join the user into all the channels the parked nick is currently in (send them localized join and namelist)
105          * (8) Send the user the umodes of their new 'persona'
106          * (9) Spool any awaylog messages to the user
107          *
108          * And there you have it, easy huh (NOT)...
109          */
110         userrec* unpark = Srv->FindNick(std::string(parameters[0]));
111         if (!unpark)
112         {
113                 WriteServ(user->fd,"942 %s %s :Invalid user specified.",user->nick, parameters[0]);
114                 return;
115         }
116         awaylog* awy = (awaylog*)unpark->GetExt("park_awaylog");
117         long key = (long)unpark->GetExt("park_key");
118         if (!awy)
119         {
120                 WriteServ(user->fd,"943 %s %s :This user is not parked.",user->nick, unpark->nick);
121                 return;
122         }
123         if (key == atoi(parameters[1]))
124         {
125                 // first part the user from all chans theyre on, so things dont get messy
126                 for (int i = 0; i != MAXCHANS; i++)
127                 {
128                         if (user->chans[i].channel != NULL)
129                         {
130                                 if (user->chans[i].channel->name)
131                                 {
132                                         Srv->PartUserFromChannel(user,user->chans[i].channel->name,"Unparking");
133                                 }
134                         }
135                 }
136                 // remove all their old modes
137                 WriteServ(user->fd,"MODE %s -%s",user->nick,user->modes);
138                 // now, map them to the parked user, while nobody can see :p
139                 Srv->PseudoToUser(user,unpark,"Unparked to "+std::string(parameters[0]));
140                 // set all their new modes
141                 WriteServ(unpark->fd,"MODE %s +%s",unpark->nick,unpark->modes);
142                 // spool their away log to them
143                 WriteServ(unpark->fd,"NOTICE %s :*** You are now unparked. You have successfully taken back the nickname and privilages of %s.",unpark->nick,unpark->nick);
144                 for (awaylog::iterator i = awy->begin(); i != awy->end(); i++)
145                 {
146                         char timebuf[MAXBUF];
147                         tm *timeinfo = localtime(&i->tm);
148                         strlcpy(timebuf,asctime(timeinfo),MAXBUF);
149                         timebuf[strlen(timebuf)-1] = '\0';
150                         WriteServ(unpark->fd,"NOTICE %s :From %s at %s: \2%s\2",unpark->nick,i->from.c_str(),timebuf,i->text.c_str());
151                 }
152                 delete awy;
153                 unpark->Shrink("park_awaylog");
154                 unpark->Shrink("park_key");
155                 for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
156                 {
157                         if (j->nick == std::string(unpark->nick))
158                         {
159                                 pinfo.erase(j);
160                                 break;
161                         }
162                 }
163         }
164         else
165         {
166                 Srv->SendServ(user->fd,"928 "+std::string(user->nick)+" :Incorrect park key.");
167         }
168 }
169
170
171 class ModulePark : public Module
172 {
173  protected:
174         ConfigReader* Conf;
175  public:
176         virtual void ReadSettings()
177         {
178                 Conf = new ConfigReader;
179                 ParkMaxTime = Conf->ReadInteger("park","maxtime",0,true);
180                 ConcurrentParks = Conf->ReadInteger("park","maxperip",0,true);
181                 ParkMaxMsgs = Conf->ReadInteger("park","maxmessages",0,true);
182                 delete Conf;
183         }
184
185         ModulePark()
186         {
187                 Srv = new Server;
188                 pinfo.clear();
189                 this->ReadSettings();
190                 Srv->AddCommand("PARK",handle_park,0,0,"m_park.so");
191                 Srv->AddCommand("UNPARK",handle_unpark,0,2,"m_park.so");
192                 Srv->AddCommand("PARKSTATS",handle_parkstats,'o',0,"m_park.so");
193         }
194         
195         virtual ~ModulePark()
196         {
197                 delete Srv;
198         }
199
200         virtual void OnRehash()
201         {
202                 this->ReadSettings();
203         }
204
205         virtual void On005Numeric(std::string &output)
206         {
207                 output = output + std::string(" PARK");
208         }
209
210         virtual void OnUserQuit(userrec* user)
211         {
212                 std::string nick = user->nick;
213                 // track quits in our parked user list
214                 for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
215                 {
216                         if (j->nick == nick)
217                         {
218                                 pinfo.erase(j);
219                                 break;
220                         }
221                 }
222         }
223
224
225         virtual void OnPrePrivmsg(userrec* user, userrec* dest, std::string text)
226         {
227                 awaylog* awy = (awaylog*)dest->GetExt("park_awaylog");
228                 if (awy)
229                 {
230                         if (awy->size() <= ParkMaxMsgs)
231                         {
232                                 awaymsg am;
233                                 am.text = text;
234                                 am.from = user->GetFullHost();
235                                 am.tm = time(NULL);
236                                 awy->push_back(am);
237                                 Srv->SendServ(user->fd,"930 "+std::string(user->nick)+" :User "+std::string(dest->nick)+" is parked. Your message has been stored.");
238                         }
239                         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.");
240                 }
241         }
242
243         virtual int OnUserPreNick(userrec* user, std::string newnick)
244         {
245                 // track nickchanges in our parked user list
246                 // (this isnt too efficient, i'll tidy it up some time)
247                 for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
248                 {
249                         if (j->nick == std::string(user->nick))
250                         {
251                                 j->nick = newnick;
252                                 break;
253                         }
254                 }
255                 return 0;
256         }
257
258         virtual void OnBackgroundTimer(time_t curtime)
259         {
260                 // look for parked clients which have timed out (this needs tidying)
261                 if (pinfo.empty())
262                         return;
263                 bool go_again = true;
264                 while (go_again)
265                 {
266                         go_again = false;
267                         for (parkinfo::iterator j = pinfo.begin(); j != pinfo.end(); j++)
268                         {
269                                 if (time(NULL) >= (j->parktime+ParkMaxTime))
270                                 {
271                                         userrec* thisnick = Srv->FindNick(j->nick);
272                                         // THIS MUST COME BEFORE THE QuitUser - QuitUser can
273                                         // create a recursive call to OnUserQuit in this module
274                                         // and then corrupt the pointer!
275                                         pinfo.erase(j);
276                                         if (thisnick)
277                                                 Srv->QuitUser(thisnick,"PARK timeout");
278                                         go_again = true;
279                                         break;
280                                 }
281                         }
282                 }
283         }
284
285         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text)
286         {
287                 if (target_type == TYPE_USER)
288                 {
289                         userrec* u = (userrec*)dest;
290                         OnPrePrivmsg(user,u,text);
291                 }
292                 return 0;
293         }
294
295         virtual void OnWhois(userrec* src, userrec* dst)
296         {
297                 if (dst->GetExt("park_awaylog"))
298                         Srv->SendTo(NULL,src,"335 "+std::string(src->nick)+" "+std::string(dst->nick)+" :is a parked client");
299         }
300         
301         virtual Version GetVersion()
302         {
303                 return Version(1,0,0,1,VF_VENDOR);
304         }
305         
306 };
307
308 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
309
310 class ModuleParkFactory : public ModuleFactory
311 {
312  public:
313         ModuleParkFactory()
314         {
315         }
316         
317         ~ModuleParkFactory()
318         {
319         }
320         
321         virtual Module * CreateModule()
322         {
323                 return new ModulePark;
324         }
325         
326 };
327
328
329 extern "C" void * init_module( void )
330 {
331         return new ModuleParkFactory;
332 }
333