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