]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_conn_join.cpp
cbe8b0be3bea99fff1e2dd73bcb23cba904a85bd
[user/henk/code/inspircd.git] / src / modules / m_conn_join.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2010 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
6  *   Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
7  *   Copyright (C) 2007 Craig Edwards <craigedwards@brainbox.cc>
8  *
9  * This file is part of InspIRCd.  InspIRCd is free software: you can
10  * redistribute it and/or modify it under the terms of the GNU General Public
11  * License as published by the Free Software Foundation, version 2.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22
23 #include "inspircd.h"
24
25 static void JoinChannels(LocalUser* u, const std::string& chanlist)
26 {
27         irc::commasepstream chans(chanlist);
28         std::string chan;
29
30         while (chans.GetToken(chan))
31         {
32                 if (ServerInstance->IsChannel(chan))
33                         Channel::JoinUser(u, chan);
34         }
35 }
36
37 class JoinTimer : public Timer
38 {
39  private:
40         LocalUser* const user;
41         const std::string channels;
42         SimpleExtItem<JoinTimer>& ext;
43
44  public:
45         JoinTimer(LocalUser* u, SimpleExtItem<JoinTimer>& ex, const std::string& chans, unsigned int delay)
46                 : Timer(delay, ServerInstance->Time(), false)
47                 , user(u), channels(chans), ext(ex)
48         {
49                 ServerInstance->Timers->AddTimer(this);
50         }
51
52         bool Tick(time_t time) CXX11_OVERRIDE
53         {
54                 if (user->chans.empty())
55                         JoinChannels(user, channels);
56
57                 ext.unset(user);
58                 return false;
59         }
60 };
61
62 class ModuleConnJoin : public Module
63 {
64         SimpleExtItem<JoinTimer> ext;
65
66  public:
67         ModuleConnJoin() : ext("join_timer", this)
68         {
69         }
70
71         void Prioritize()
72         {
73                 ServerInstance->Modules->SetPriority(this, I_OnPostConnect, PRIORITY_LAST);
74         }
75
76         Version GetVersion() CXX11_OVERRIDE
77         {
78                 return Version("Forces users to join the specified channel(s) on connect", VF_VENDOR);
79         }
80
81         void OnPostConnect(User* user) CXX11_OVERRIDE
82         {
83                 LocalUser* localuser = IS_LOCAL(user);
84                 if (!localuser)
85                         return;
86
87                 std::string chanlist = localuser->GetClass()->config->getString("autojoin");
88                 unsigned int chandelay = localuser->GetClass()->config->getInt("autojoindelay", 0, 0, 60);
89
90                 if (chanlist.empty())
91                 {
92                         ConfigTag* tag = ServerInstance->Config->ConfValue("autojoin");
93                         chanlist = tag->getString("channel");
94                         chandelay = tag->getInt("delay", 0, 0, 60);
95                 }
96
97                 if (chanlist.empty())
98                         return;
99
100                 if (!chandelay)
101                         JoinChannels(localuser, chanlist);
102                 else
103                         ext.set(localuser, new JoinTimer(localuser, ext, chanlist, chandelay));
104         }
105 };
106
107 MODULE_INIT(ModuleConnJoin)