]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_operjoin.cpp
e421958b3693f5a7cf738b1cc70c48e94909b531
[user/henk/code/inspircd.git] / src / modules / m_operjoin.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
6  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
7  *   Copyright (C) 2005-2007 Robin Burchell <robin+git@viroteck.net>
8  *   Copyright (C) 2006-2007 Craig Edwards <craigedwards@brainbox.cc>
9  *   Copyright (C) 2004 Christopher Hall <typobox43@gmail.com>
10  *
11  * This file is part of InspIRCd.  InspIRCd is free software: you can
12  * redistribute it and/or modify it under the terms of the GNU General Public
13  * License as published by the Free Software Foundation, version 2.
14  *
15  * This program is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  */
23
24
25 #include "inspircd.h"
26
27 class ModuleOperjoin : public Module
28 {
29                 std::string operChan;
30                 std::vector<std::string> operChans;
31                 bool override;
32
33                 int tokenize(const std::string &str, std::vector<std::string> &tokens)
34                 {
35                         // skip delimiters at beginning.
36                         std::string::size_type lastPos = str.find_first_not_of(",", 0);
37                         // find first "non-delimiter".
38                         std::string::size_type pos = str.find_first_of(",", lastPos);
39
40                         while (std::string::npos != pos || std::string::npos != lastPos)
41                         {
42                                 // found a token, add it to the vector.
43                                 tokens.push_back(str.substr(lastPos, pos - lastPos));
44                                 // skip delimiters. Note the "not_of"
45                                 lastPos = str.find_first_not_of(",", pos);
46                                 // find next "non-delimiter"
47                                 pos = str.find_first_of(",", lastPos);
48                         }
49                         return tokens.size();
50                 }
51
52         public:
53                 void init() CXX11_OVERRIDE
54                 {
55                         OnRehash(NULL);
56                         Implementation eventlist[] = { I_OnPostOper, I_OnRehash };
57                         ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
58                 }
59
60
61                 void OnRehash(User* user) CXX11_OVERRIDE
62                 {
63                         ConfigTag* tag = ServerInstance->Config->ConfValue("operjoin");
64
65                         operChan = tag->getString("channel");
66                         override = tag->getBool("override", false);
67                         operChans.clear();
68                         if (!operChan.empty())
69                                 tokenize(operChan,operChans);
70                 }
71
72                 Version GetVersion() CXX11_OVERRIDE
73                 {
74                         return Version("Forces opers to join the specified channel(s) on oper-up", VF_VENDOR);
75                 }
76
77                 void OnPostOper(User* user, const std::string &opertype, const std::string &opername) CXX11_OVERRIDE
78                 {
79                         LocalUser* localuser = IS_LOCAL(user);
80                         if (!localuser)
81                                 return;
82
83                         for (std::vector<std::string>::const_iterator i = operChans.begin(); i != operChans.end(); ++i)
84                                 if (ServerInstance->IsChannel(*i))
85                                         Channel::JoinUser(localuser, *i, override);
86
87                         std::string chanList = localuser->oper->getConfig("autojoin");
88                         if (!chanList.empty())
89                         {
90                                 std::vector<std::string> typechans;
91                                 tokenize(chanList, typechans);
92                                 for (std::vector<std::string>::const_iterator it = typechans.begin(); it != typechans.end(); ++it)
93                                 {
94                                         if (ServerInstance->IsChannel(*it))
95                                         {
96                                                 Channel::JoinUser(localuser, *it, override);
97                                         }
98                                 }
99                         }
100                 }
101 };
102
103 MODULE_INIT(ModuleOperjoin)