]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_ojoin.cpp
fcb26775017ef391a5d80117a92820d129f4b1f0
[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 Command
49 {
50  public:
51         bool active;
52         CommandOjoin(Module* parent) : Command(parent,"OJOIN", 1)
53         {
54                 flags_needed = 'o'; Penalty = 0; syntax = "<channel>";
55                 active = false;
56                 TRANSLATE3(TR_NICK, TR_TEXT, TR_END);
57         }
58
59         CmdResult Handle (const std::vector<std::string>& parameters, User *user)
60         {
61                 // Make sure the channel name is allowable.
62                 if (!ServerInstance->IsChannel(parameters[0].c_str(), ServerInstance->Config->Limits.ChanMax))
63                 {
64                         user->WriteServ("NOTICE "+user->nick+" :*** Invalid characters in channel name or name too long");
65                         return CMD_FAILURE;
66                 }
67
68                 active = true;
69                 Channel* channel = Channel::JoinUser(user, parameters[0].c_str(), false, "", false);
70                 active = false;
71
72                 if (channel)
73                 {
74                         ServerInstance->SNO->WriteGlobalSno('a', user->nick+" used OJOIN to join "+channel->name);
75
76                         if (notice)
77                         {
78                                 channel = ServerInstance->FindChan(parameters[0]);
79                                 channel->WriteChannelWithServ(ServerInstance->Config->ServerName.c_str(), "NOTICE %s :%s joined on official network business.",
80                                         parameters[0].c_str(), user->nick.c_str());
81                                 ServerInstance->PI->SendChannelNotice(channel, 0, user->nick + " joined on official network business.");
82                         }
83                 }
84                 else
85                 {
86                         ServerInstance->SNO->WriteGlobalSno('a', user->nick+" used OJOIN in "+parameters[0]);
87                         // they're already in the channel
88                         std::vector<std::string> modes;
89                         modes.push_back(parameters[0]);
90                         modes.push_back("+Y");
91                         modes.push_back(user->nick);
92                         ServerInstance->SendGlobalMode(modes, ServerInstance->FakeClient);
93                 }
94                 return CMD_SUCCESS;
95         }
96 };
97
98 /** channel mode +Y
99  */
100 class NetworkPrefix : public ModeHandler
101 {
102  public:
103         NetworkPrefix(Module* parent) : ModeHandler(parent, "official-join", 'Y', PARAM_ALWAYS, MODETYPE_CHANNEL)
104         {
105                 list = true;
106                 prefix = NPrefix;
107                 levelrequired = INT_MAX;
108                 m_paramtype = TR_NICK;
109         }
110
111         void RemoveMode(Channel* channel, irc::modestacker* stack)
112         {
113                 const UserMembList* cl = channel->GetUsers();
114                 std::vector<std::string> mode_junk;
115                 mode_junk.push_back(channel->name);
116                 irc::modestacker modestack(false);
117                 std::deque<std::string> stackresult;
118
119                 for (UserMembCIter i = cl->begin(); i != cl->end(); i++)
120                 {
121                         if (i->second->hasMode('Y'))
122                         {
123                                 if (stack)
124                                         stack->Push(this->GetModeChar(), i->first->nick);
125                                 else
126                                         modestack.Push(this->GetModeChar(), i->first->nick);
127                         }
128                 }
129
130                 if (stack)
131                         return;
132
133                 while (modestack.GetStackedLine(stackresult))
134                 {
135                         mode_junk.insert(mode_junk.end(), stackresult.begin(), stackresult.end());
136                         ServerInstance->SendMode(mode_junk, ServerInstance->FakeClient);
137                         mode_junk.erase(mode_junk.begin() + 1, mode_junk.end());
138                 }
139         }
140
141         unsigned int GetPrefixRank()
142         {
143                 return NETWORK_VALUE;
144         }
145
146         void RemoveMode(User* user, irc::modestacker* stack)
147         {
148         }
149
150         ModResult AccessCheck(User* source, Channel* channel, std::string &parameter, bool adding)
151         {
152                 User* theuser = ServerInstance->FindNick(parameter);
153                 // remove own privs?
154                 if (source == theuser && !adding)
155                         return MOD_RES_ALLOW;
156
157                 return MOD_RES_PASSTHRU;
158         }
159
160         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
161         {
162                 return MODEACTION_ALLOW;
163         }
164
165 };
166
167 class ModuleOjoin : public Module
168 {
169         NetworkPrefix* np;
170         CommandOjoin mycommand;
171
172  public:
173
174         ModuleOjoin()
175                 : np(NULL), mycommand(this)
176         {
177         }
178
179         void init()
180         {
181                 /* Load config stuff */
182                 OnRehash(NULL);
183
184                 /* Initialise module variables */
185                 np = new NetworkPrefix(this);
186
187                 ServerInstance->Modules->AddService(*np);
188                 ServerInstance->Modules->AddService(mycommand);
189
190                 Implementation eventlist[] = { I_OnUserPreJoin, I_OnUserPreKick, I_OnRehash };
191                 ServerInstance->Modules->Attach(eventlist, this, 3);
192         }
193
194         ModResult OnUserPreJoin(User *user, Channel *chan, const char *cname, std::string &privs, const std::string &keygiven)
195         {
196                 if (mycommand.active)
197                 {
198                         privs += 'Y';
199                         if (op)
200                                 privs += 'o';
201                         return MOD_RES_ALLOW;
202                 }
203
204                 return MOD_RES_PASSTHRU;
205         }
206
207         void OnRehash(User* user)
208         {
209                 ConfigTag* Conf = ServerInstance->Config->ConfValue("ojoin");
210
211                 if (!np)
212                 {
213                         // This is done on module load only
214                         std::string npre = Conf->getString("prefix");
215                         NPrefix = npre.empty() ? 0 : npre[0];
216
217                         if (NPrefix && ServerInstance->Modes->FindPrefix(NPrefix))
218                                 throw ModuleException("Looks like the +Y prefix you picked for m_ojoin is already in use. Pick another.");
219                 }
220
221                 notice = Conf->getBool("notice", true);
222                 op = Conf->getBool("op", true);
223         }
224
225         ModResult OnUserPreKick(User* source, Membership* memb, const std::string &reason)
226         {
227                 // Don't do anything if they're not +Y
228                 if (!memb->hasMode('Y'))
229                         return MOD_RES_PASSTHRU;
230
231                 // Let them do whatever they want to themselves.
232                 if (source == memb->user)
233                         return MOD_RES_PASSTHRU;
234
235                 source->WriteNumeric(484, source->nick+" "+memb->chan->name+" :Can't kick "+memb->user->nick+" as they're on official network business.");
236                 return MOD_RES_DENY;
237         }
238
239         ~ModuleOjoin()
240         {
241                 delete np;
242         }
243
244         void Prioritize()
245         {
246                 ServerInstance->Modules->SetPriority(this, I_OnUserPreJoin, PRIORITY_FIRST);
247         }
248
249         Version GetVersion()
250         {
251                 return Version("Network Business Join", VF_VENDOR);
252         }
253 };
254
255 MODULE_INIT(ModuleOjoin)
256