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