]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_ojoin.cpp
Always set the topic in Channel::SetTopic(), move access checks into cmd_topic
[user/henk/code/inspircd.git] / src / modules / m_ojoin.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5  *
6  * This file is part of InspIRCd.  InspIRCd is free software: you can
7  * redistribute it and/or modify it under the terms of the GNU General Public
8  * License as published by the Free Software Foundation, version 2.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19
20 /*
21  * Written for InspIRCd-1.2 by Taros on the Tel'Laerad M&D Team
22  * <http://tellaerad.net>
23  */
24
25 #include "inspircd.h"
26
27 /* $ModConfig: <ojoin prefix="!" notice="yes" op="yes">
28  *  Specify the prefix that +Y will grant here, it should be unused.
29  *  Leave prefix empty if you do not wish +Y to grant a prefix
30  *  If notice is set to on, upon ojoin, the server will notice
31  *  the channel saying that the oper is joining on network business
32  *  If op is set to on, it will give them +o along with +Y */
33 /* $ModDesc: Provides the /ojoin command, which joins a user to a channel on network business, and gives them +Y, which makes them immune to kick / deop and so on. */
34 /* $ModAuthor: Taros */
35 /* $ModAuthorMail: taros34@hotmail.com */
36
37 /* A note: This will not protect against kicks from services,
38  * ulines, or operoverride. */
39
40 #define NETWORK_VALUE 9000000
41
42 char NPrefix;
43 bool notice;
44 bool op;
45
46 /** Handle /OJOIN
47  */
48 class CommandOjoin : public SplitCommand
49 {
50  public:
51         bool active;
52         CommandOjoin(Module* parent) :
53                 SplitCommand(parent, "OJOIN", 1)
54         {
55                 flags_needed = 'o'; Penalty = 0; syntax = "<channel>";
56                 active = false;
57         }
58
59         CmdResult HandleLocal(const std::vector<std::string>& parameters, LocalUser* user)
60         {
61                 // Make sure the channel name is allowable.
62                 if (!ServerInstance->IsChannel(parameters[0]))
63                 {
64                         user->WriteNotice("*** Invalid characters in channel name or name too long");
65                         return CMD_FAILURE;
66                 }
67
68                 active = true;
69                 // override is false because we want OnUserPreJoin to run
70                 Channel* channel = Channel::JoinUser(user, parameters[0], false);
71                 active = false;
72
73                 if (channel)
74                 {
75                         ServerInstance->SNO->WriteGlobalSno('a', user->nick+" used OJOIN to join "+channel->name);
76
77                         if (notice)
78                         {
79                                 channel = ServerInstance->FindChan(parameters[0]);
80                                 channel->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s joined on official network business.",
81                                         parameters[0].c_str(), user->nick.c_str());
82                                 ServerInstance->PI->SendChannelNotice(channel, 0, user->nick + " joined on official network business.");
83                         }
84                 }
85                 else
86                 {
87                         ServerInstance->SNO->WriteGlobalSno('a', user->nick+" used OJOIN in "+parameters[0]);
88                         // they're already in the channel
89                         std::vector<std::string> modes;
90                         modes.push_back(parameters[0]);
91                         modes.push_back(op ? "+Yo" : "+Y");
92                         modes.push_back(user->nick);
93                         if (op)
94                                 modes.push_back(user->nick);
95                         ServerInstance->Modes->Process(modes, ServerInstance->FakeClient);
96                 }
97                 return CMD_SUCCESS;
98         }
99 };
100
101 /** channel mode +Y
102  */
103 class NetworkPrefix : public ModeHandler
104 {
105  public:
106         NetworkPrefix(Module* parent) : ModeHandler(parent, "official-join", 'Y', PARAM_ALWAYS, MODETYPE_CHANNEL)
107         {
108                 list = true;
109                 prefix = NPrefix;
110                 levelrequired = INT_MAX;
111                 m_paramtype = TR_NICK;
112                 prefixrank = NETWORK_VALUE;
113         }
114
115         ModResult AccessCheck(User* source, Channel* channel, std::string &parameter, bool adding)
116         {
117                 User* theuser = ServerInstance->FindNick(parameter);
118                 // remove own privs?
119                 if (source == theuser && !adding)
120                         return MOD_RES_ALLOW;
121
122                 return MOD_RES_PASSTHRU;
123         }
124
125         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
126         {
127                 return MODEACTION_ALLOW;
128         }
129
130 };
131
132 class ModuleOjoin : public Module
133 {
134         NetworkPrefix* np;
135         CommandOjoin mycommand;
136
137  public:
138
139         ModuleOjoin()
140                 : np(NULL), mycommand(this)
141         {
142         }
143
144         void init() CXX11_OVERRIDE
145         {
146                 /* Load config stuff */
147                 OnRehash(NULL);
148
149                 /* Initialise module variables */
150                 np = new NetworkPrefix(this);
151
152                 ServerInstance->Modules->AddService(*np);
153                 ServerInstance->Modules->AddService(mycommand);
154
155                 Implementation eventlist[] = { I_OnUserPreJoin, I_OnUserPreKick, I_OnRehash };
156                 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
157         }
158
159         ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
160         {
161                 if (mycommand.active)
162                 {
163                         privs += 'Y';
164                         if (op)
165                                 privs += 'o';
166                         return MOD_RES_ALLOW;
167                 }
168
169                 return MOD_RES_PASSTHRU;
170         }
171
172         void OnRehash(User* user) CXX11_OVERRIDE
173         {
174                 ConfigTag* Conf = ServerInstance->Config->ConfValue("ojoin");
175
176                 if (!np)
177                 {
178                         // This is done on module load only
179                         std::string npre = Conf->getString("prefix");
180                         NPrefix = npre.empty() ? 0 : npre[0];
181
182                         if (NPrefix && ServerInstance->Modes->FindPrefix(NPrefix))
183                                 throw ModuleException("Looks like the +Y prefix you picked for m_ojoin is already in use. Pick another.");
184                 }
185
186                 notice = Conf->getBool("notice", true);
187                 op = Conf->getBool("op", true);
188         }
189
190         ModResult OnUserPreKick(User* source, Membership* memb, const std::string &reason) CXX11_OVERRIDE
191         {
192                 // Don't do anything if they're not +Y
193                 if (!memb->hasMode('Y'))
194                         return MOD_RES_PASSTHRU;
195
196                 // Let them do whatever they want to themselves.
197                 if (source == memb->user)
198                         return MOD_RES_PASSTHRU;
199
200                 source->WriteNumeric(484, source->nick+" "+memb->chan->name+" :Can't kick "+memb->user->nick+" as they're on official network business.");
201                 return MOD_RES_DENY;
202         }
203
204         ~ModuleOjoin()
205         {
206                 delete np;
207         }
208
209         void Prioritize()
210         {
211                 ServerInstance->Modules->SetPriority(this, I_OnUserPreJoin, PRIORITY_FIRST);
212         }
213
214         Version GetVersion() CXX11_OVERRIDE
215         {
216                 return Version("Network Business Join", VF_VENDOR);
217         }
218 };
219
220 MODULE_INIT(ModuleOjoin)