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