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