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