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