]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_ojoin.cpp
Update copyright headers.
[user/henk/code/inspircd.git] / src / modules / m_ojoin.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2019 Robby <robby@chatbelgie.be>
5  *   Copyright (C) 2013, 2017-2019 Sadie Powell <sadie@witchery.services>
6  *   Copyright (C) 2012-2014, 2016 Attila Molnar <attilamolnar@hush.com>
7  *   Copyright (C) 2010 Craig Edwards <brain@inspircd.org>
8  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
9  *
10  * This file is part of InspIRCd.  InspIRCd is free software: you can
11  * redistribute it and/or modify it under the terms of the GNU General Public
12  * License as published by the Free Software Foundation, version 2.
13  *
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
17  * details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  */
22
23 #include "inspircd.h"
24
25 #define NETWORK_VALUE 9000000
26
27 /** Handle /OJOIN
28  */
29 class CommandOjoin : public SplitCommand
30 {
31  public:
32         bool active;
33         bool notice;
34         bool op;
35         ModeHandler* npmh;
36         CommandOjoin(Module* parent, ModeHandler& mode)
37                 : SplitCommand(parent, "OJOIN", 1)
38                 , npmh(&mode)
39         {
40                 flags_needed = 'o'; syntax = "<channel>";
41                 active = false;
42         }
43
44         CmdResult HandleLocal(LocalUser* user, const Params& parameters) CXX11_OVERRIDE
45         {
46                 // Make sure the channel name is allowable.
47                 if (!ServerInstance->IsChannel(parameters[0]))
48                 {
49                         user->WriteNotice("*** Invalid characters in channel name or name too long");
50                         return CMD_FAILURE;
51                 }
52
53                 active = true;
54                 // override is false because we want OnUserPreJoin to run
55                 Channel* channel = Channel::JoinUser(user, parameters[0], false);
56                 active = false;
57
58                 if (channel)
59                 {
60                         ServerInstance->SNO->WriteGlobalSno('a', user->nick+" used OJOIN to join "+channel->name);
61
62                         if (notice)
63                                 channel->WriteNotice(user->nick + " joined on official network business.");
64                 }
65                 else
66                 {
67                         channel = ServerInstance->FindChan(parameters[0]);
68                         if (!channel)
69                                 return CMD_FAILURE;
70
71                         ServerInstance->SNO->WriteGlobalSno('a', user->nick+" used OJOIN in "+parameters[0]);
72                         // they're already in the channel
73                         Modes::ChangeList changelist;
74                         changelist.push_add(npmh, user->nick);
75                         if (op)
76                                 changelist.push_add(ServerInstance->Modes->FindMode('o', MODETYPE_CHANNEL), user->nick);
77                         ServerInstance->Modes->Process(ServerInstance->FakeClient, channel, NULL, changelist);
78                 }
79                 return CMD_SUCCESS;
80         }
81 };
82
83 /** channel mode +Y
84  */
85 class NetworkPrefix : public PrefixMode
86 {
87  public:
88         NetworkPrefix(Module* parent, char NPrefix)
89                 : PrefixMode(parent, "official-join", 'Y', NETWORK_VALUE, NPrefix)
90         {
91                 ranktoset = ranktounset = UINT_MAX;
92         }
93
94         ModResult AccessCheck(User* source, Channel* channel, std::string &parameter, bool adding) CXX11_OVERRIDE
95         {
96                 User* theuser = ServerInstance->FindNick(parameter);
97                 // remove own privs?
98                 if (source == theuser && !adding)
99                         return MOD_RES_ALLOW;
100
101                 return MOD_RES_PASSTHRU;
102         }
103 };
104
105 class ModuleOjoin : public Module
106 {
107         NetworkPrefix np;
108         CommandOjoin mycommand;
109
110  public:
111
112         ModuleOjoin()
113                 : np(this, ServerInstance->Config->ConfValue("ojoin")->getString("prefix").c_str()[0])
114                 , mycommand(this, np)
115         {
116         }
117
118         ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
119         {
120                 if (mycommand.active)
121                 {
122                         privs += np.GetModeChar();
123                         if (mycommand.op)
124                                 privs += 'o';
125                         return MOD_RES_ALLOW;
126                 }
127
128                 return MOD_RES_PASSTHRU;
129         }
130
131         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
132         {
133                 ConfigTag* Conf = ServerInstance->Config->ConfValue("ojoin");
134                 mycommand.notice = Conf->getBool("notice", true);
135                 mycommand.op = Conf->getBool("op", true);
136         }
137
138         ModResult OnUserPreKick(User* source, Membership* memb, const std::string &reason) CXX11_OVERRIDE
139         {
140                 // Don't do anything if they're not +Y
141                 if (!memb->HasMode(&np))
142                         return MOD_RES_PASSTHRU;
143
144                 // Let them do whatever they want to themselves.
145                 if (source == memb->user)
146                         return MOD_RES_PASSTHRU;
147
148                 source->WriteNumeric(ERR_RESTRICTED, memb->chan->name, "Can't kick "+memb->user->nick+" as they're on official network business.");
149                 return MOD_RES_DENY;
150         }
151
152         void Prioritize() CXX11_OVERRIDE
153         {
154                 ServerInstance->Modules->SetPriority(this, I_OnUserPreJoin, PRIORITY_FIRST);
155         }
156
157         Version GetVersion() CXX11_OVERRIDE
158         {
159                 return Version("Provides the OJOIN command, allows an oper to join a channel and be immune to kicks", VF_VENDOR);
160         }
161 };
162
163 MODULE_INIT(ModuleOjoin)