]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_conn_join.cpp
bd8d89dc9b54365e895da7f7ad626b0e7778e8e9
[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, 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         std::string defchans;
66         unsigned int defdelay;
67
68  public:
69         ModuleConnJoin()
70                 : ext("join_timer", ExtensionItem::EXT_USER, this)
71         {
72         }
73
74         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
75         {
76                 ConfigTag* tag = ServerInstance->Config->ConfValue("autojoin");
77                 defchans = tag->getString("channel");
78                 defdelay = tag->getInt("delay", 0, 0, 60);
79         }
80
81         void Prioritize() CXX11_OVERRIDE
82         {
83                 ServerInstance->Modules->SetPriority(this, I_OnPostConnect, PRIORITY_LAST);
84         }
85
86         Version GetVersion() CXX11_OVERRIDE
87         {
88                 return Version("Forces users to join the specified channel(s) on connect", VF_VENDOR);
89         }
90
91         void OnPostConnect(User* user) CXX11_OVERRIDE
92         {
93                 LocalUser* localuser = IS_LOCAL(user);
94                 if (!localuser)
95                         return;
96
97                 std::string chanlist = localuser->GetClass()->config->getString("autojoin");
98                 unsigned int chandelay = localuser->GetClass()->config->getInt("autojoindelay", 0, 0, 60);
99
100                 if (chanlist.empty())
101                 {
102                         if (defchans.empty())
103                                 return;
104                         chanlist = defchans;
105                         chandelay = defdelay;
106                 }
107
108                 if (!chandelay)
109                         JoinChannels(localuser, chanlist);
110                 else
111                         ext.set(localuser, new JoinTimer(localuser, ext, chanlist, chandelay));
112         }
113 };
114
115 MODULE_INIT(ModuleConnJoin)