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