]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_ojoin.cpp
c3ceb38d98f9dd5b11e5763d15f777b62a6013f8
[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 char NPrefix;
25 bool notice;
26 bool op;
27
28 /** Handle /OJOIN
29  */
30 class CommandOjoin : public SplitCommand
31 {
32  public:
33         bool active;
34         CommandOjoin(Module* parent) :
35                 SplitCommand(parent, "OJOIN", 1)
36         {
37                 flags_needed = 'o'; Penalty = 0; syntax = "<channel>";
38                 active = false;
39         }
40
41         CmdResult HandleLocal(const std::vector<std::string>& parameters, LocalUser* user)
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                         {
61                                 channel = ServerInstance->FindChan(parameters[0]);
62                                 channel->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s joined on official network business.",
63                                         parameters[0].c_str(), user->nick.c_str());
64                                 ServerInstance->PI->SendChannelNotice(channel, 0, user->nick + " joined on official network business.");
65                         }
66                 }
67                 else
68                 {
69                         ServerInstance->SNO->WriteGlobalSno('a', user->nick+" used OJOIN in "+parameters[0]);
70                         // they're already in the channel
71                         std::vector<std::string> modes;
72                         modes.push_back(parameters[0]);
73                         modes.push_back(op ? "+Yo" : "+Y");
74                         modes.push_back(user->nick);
75                         if (op)
76                                 modes.push_back(user->nick);
77                         ServerInstance->Modes->Process(modes, ServerInstance->FakeClient);
78                 }
79                 return CMD_SUCCESS;
80         }
81 };
82
83 /** channel mode +Y
84  */
85 class NetworkPrefix : public ModeHandler
86 {
87  public:
88         NetworkPrefix(Module* parent) : ModeHandler(parent, "official-join", 'Y', PARAM_ALWAYS, MODETYPE_CHANNEL)
89         {
90                 list = true;
91                 prefix = NPrefix;
92                 levelrequired = INT_MAX;
93                 m_paramtype = TR_NICK;
94                 prefixrank = NETWORK_VALUE;
95         }
96
97         ModResult AccessCheck(User* source, Channel* channel, std::string &parameter, bool adding)
98         {
99                 User* theuser = ServerInstance->FindNick(parameter);
100                 // remove own privs?
101                 if (source == theuser && !adding)
102                         return MOD_RES_ALLOW;
103
104                 return MOD_RES_PASSTHRU;
105         }
106
107         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
108         {
109                 return MODEACTION_ALLOW;
110         }
111
112 };
113
114 class ModuleOjoin : public Module
115 {
116         NetworkPrefix* np;
117         CommandOjoin mycommand;
118
119  public:
120
121         ModuleOjoin()
122                 : np(NULL), mycommand(this)
123         {
124         }
125
126         void init() CXX11_OVERRIDE
127         {
128                 /* Load config stuff */
129                 OnRehash(NULL);
130
131                 /* Initialise module variables */
132                 np = new NetworkPrefix(this);
133
134                 ServerInstance->Modules->AddService(*np);
135                 ServerInstance->Modules->AddService(mycommand);
136
137                 Implementation eventlist[] = { I_OnUserPreJoin, I_OnUserPreKick, I_OnRehash };
138                 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
139         }
140
141         ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
142         {
143                 if (mycommand.active)
144                 {
145                         privs += 'Y';
146                         if (op)
147                                 privs += 'o';
148                         return MOD_RES_ALLOW;
149                 }
150
151                 return MOD_RES_PASSTHRU;
152         }
153
154         void OnRehash(User* user) CXX11_OVERRIDE
155         {
156                 ConfigTag* Conf = ServerInstance->Config->ConfValue("ojoin");
157
158                 if (!np)
159                 {
160                         // This is done on module load only
161                         std::string npre = Conf->getString("prefix");
162                         NPrefix = npre.empty() ? 0 : npre[0];
163
164                         if (NPrefix && ServerInstance->Modes->FindPrefix(NPrefix))
165                                 throw ModuleException("Looks like the +Y prefix you picked for m_ojoin is already in use. Pick another.");
166                 }
167
168                 notice = Conf->getBool("notice", true);
169                 op = Conf->getBool("op", true);
170         }
171
172         ModResult OnUserPreKick(User* source, Membership* memb, const std::string &reason) CXX11_OVERRIDE
173         {
174                 // Don't do anything if they're not +Y
175                 if (!memb->hasMode('Y'))
176                         return MOD_RES_PASSTHRU;
177
178                 // Let them do whatever they want to themselves.
179                 if (source == memb->user)
180                         return MOD_RES_PASSTHRU;
181
182                 source->WriteNumeric(484, source->nick+" "+memb->chan->name+" :Can't kick "+memb->user->nick+" as they're on official network business.");
183                 return MOD_RES_DENY;
184         }
185
186         ~ModuleOjoin()
187         {
188                 delete np;
189         }
190
191         void Prioritize()
192         {
193                 ServerInstance->Modules->SetPriority(this, I_OnUserPreJoin, PRIORITY_FIRST);
194         }
195
196         Version GetVersion() CXX11_OVERRIDE
197         {
198                 return Version("Network Business Join", VF_VENDOR);
199         }
200 };
201
202 MODULE_INIT(ModuleOjoin)