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