]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_ojoin.cpp
Replace OnAccessCheck with OnPreMode to remove a number of redundant checks
[user/henk/code/inspircd.git] / src / modules / m_ojoin.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 /*
15  * Written for InspIRCd-1.2 by Taros on the Tel'Laerad M&D Team
16  * <http://tellaerad.net>
17  */
18
19 #include "inspircd.h"
20
21 /* $ModConfig: <ojoin prefix="!" notice="yes" op="yes">
22  *  Specify the prefix that +Y will grant here, it should be unused.
23  *  Leave prefix empty if you do not wish +Y to grant a prefix
24  *  If notice is set to on, upon ojoin, the server will notice
25  *  the channel saying that the oper is joining on network business
26  *  If op is set to on, it will give them +o along with +Y */
27 /* $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. */
28 /* $ModAuthor: Taros */
29 /* $ModAuthorMail: taros34@hotmail.com */
30
31 /* A note: This will not protect against kicks from services,
32  * ulines, or operoverride. */
33
34 #define NETWORK_VALUE 9000000
35
36 char NPrefix;
37 bool notice;
38 bool op;
39
40 /** Handle /OJOIN
41  */
42 class CommandOjoin : public Command
43 {
44  public:
45         bool active;
46         CommandOjoin (InspIRCd* Instance, Module* parent) : Command(Instance,parent,"OJOIN", "o", 1, false, 0)
47         {
48                 syntax = "<channel>";
49                 active = false;
50                 TRANSLATE3(TR_NICK, TR_TEXT, TR_END);
51         }
52
53         CmdResult Handle (const std::vector<std::string>& parameters, User *user)
54         {
55                 // Make sure the channel name is allowable.
56                 if (!ServerInstance->IsChannel(parameters[0].c_str(), ServerInstance->Config->Limits.ChanMax))
57                 {
58                         user->WriteServ("NOTICE "+std::string(user->nick)+" :*** Invalid characters in channel name or name too long");
59                         return CMD_FAILURE;
60                 }
61
62                 active = true;
63                 Channel* channel = Channel::JoinUser(ServerInstance, user, parameters[0].c_str(), false, "", false);
64                 active = false;
65
66                 if (channel)
67                 {
68                         ServerInstance->SNO->WriteGlobalSno('a', std::string(user->nick)+" used OJOIN to join "+channel->name);
69
70                         if (!NPrefix)
71                         {
72                                 std::vector<std::string> modes;
73                                 modes.push_back(parameters[0]);
74                                 modes.push_back("+Y");
75                                 modes.push_back(user->nick);
76                                 ServerInstance->SendMode(modes, ServerInstance->FakeClient);
77                                 ServerInstance->PI->SendMode(channel->name, ServerInstance->Modes->GetLastParseParams(), ServerInstance->Modes->GetLastParseTranslate());
78                         }
79
80                         if (notice)
81                         {
82                                 channel = ServerInstance->FindChan(parameters[0]);
83                                 channel->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s joined on official network business.",
84                                         parameters[0].c_str(), user->nick.c_str());
85                                 ServerInstance->PI->SendChannelNotice(channel, 0, std::string(user->nick) + " joined on official network business.");
86                         }
87                 }
88                 else
89                 {
90                         ServerInstance->SNO->WriteGlobalSno('a', std::string(user->nick)+" used OJOIN in "+parameters[0]);
91                         // they're already in the channel
92                         std::vector<std::string> modes;
93                         modes.push_back(parameters[0]);
94                         modes.push_back("+Y");
95                         modes.push_back(user->nick);
96                         ServerInstance->SendMode(modes, ServerInstance->FakeClient);
97                         ServerInstance->PI->SendMode(parameters[0], ServerInstance->Modes->GetLastParseParams(), ServerInstance->Modes->GetLastParseTranslate());
98                 }
99                 return CMD_SUCCESS;
100         }
101 };
102
103 /** channel mode +Y
104  */
105 class NetworkPrefix : public ModeHandler
106 {
107  public:
108         NetworkPrefix(InspIRCd* Instance, Module* parent)
109                 : ModeHandler(Instance, parent, 'Y', 1, 1, true, MODETYPE_CHANNEL, false, NPrefix, 0, TR_NICK)
110         {
111         }
112
113         ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string &parameter)
114         {
115                 User* x = ServerInstance->FindNick(parameter);
116                 if (x)
117                 {
118                         Membership* m = channel->GetUser(x);
119                         if (!m)
120                         {
121                                 return std::make_pair(false, parameter);
122                         }
123                         else
124                         {
125                                 if (m->hasMode('Y'))
126                                 {
127                                         return std::make_pair(true, x->nick);
128                                 }
129                                 else
130                                 {
131                                         return std::make_pair(false, parameter);
132                                 }
133                         }
134                 }
135                 return std::make_pair(false, parameter);
136         }
137
138         void RemoveMode(Channel* channel, irc::modestacker* stack)
139         {
140                 const UserMembList* cl = channel->GetUsers();
141                 std::vector<std::string> mode_junk;
142                 mode_junk.push_back(channel->name);
143                 irc::modestacker modestack(ServerInstance, false);
144                 std::deque<std::string> stackresult;
145
146                 for (UserMembCIter i = cl->begin(); i != cl->end(); i++)
147                 {
148                         if (i->second->hasMode('Y'))
149                         {
150                                 if (stack)
151                                         stack->Push(this->GetModeChar(), i->first->nick);
152                                 else
153                                         modestack.Push(this->GetModeChar(), i->first->nick);
154                         }
155                 }
156
157                 if (stack)
158                         return;
159
160                 while (modestack.GetStackedLine(stackresult))
161                 {
162                         mode_junk.insert(mode_junk.end(), stackresult.begin(), stackresult.end());
163                         ServerInstance->SendMode(mode_junk, ServerInstance->FakeClient);
164                         mode_junk.erase(mode_junk.begin() + 1, mode_junk.end());
165                 }
166         }
167
168         User* FindAndVerify(std::string &parameter, Channel* channel)
169         {
170                 User* theuser = ServerInstance->FindNick(parameter);
171                 if ((!theuser) || (!channel->HasUser(theuser)))
172                 {
173                         parameter.clear();
174                         return NULL;
175                 }
176                 return theuser;
177         }
178
179         ModeAction HandleChange(User* source, User* theuser, bool adding, Channel* channel, std::string &parameter)
180         {
181                 Membership* m = channel->GetUser(theuser);
182                 if (m && adding)
183                 {
184                         if (!m->hasMode('Y'))
185                         {
186                                 parameter = theuser->nick;
187                                 return MODEACTION_ALLOW;
188                         }
189                 }
190                 else if (m && !adding)
191                 {
192                         if (m->hasMode('Y'))
193                         {
194                                 parameter = theuser->nick;
195                                 return MODEACTION_ALLOW;
196                         }
197                 }
198                 return MODEACTION_DENY;
199         }
200
201         unsigned int GetPrefixRank()
202         {
203                 return NETWORK_VALUE;
204         }
205
206         void RemoveMode(User* user, irc::modestacker* stack)
207         {
208         }
209
210         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
211         {
212                 User* theuser = FindAndVerify(parameter, channel);
213
214                 if (!theuser)
215                         return MODEACTION_DENY;
216
217                  // source is a server, or ulined, we'll let them +-Y the user.
218                 if (source == ServerInstance->FakeClient ||
219                         ((source == theuser) && (!adding)) ||
220                         (ServerInstance->ULine(source->nick.c_str())) ||
221                         (ServerInstance->ULine(source->server)) ||
222                         (!*source->server) ||
223                         (!IS_LOCAL(source))
224                         )
225                 {
226                         return HandleChange(source, theuser, adding, channel, parameter);
227                 }
228                 else
229                 {
230                         // bzzzt, wrong answer!
231                         source->WriteNumeric(482, "%s %s :Only servers may change this mode.", source->nick.c_str(), channel->name.c_str());
232                         return MODEACTION_DENY;
233                 }
234         }
235
236 };
237
238 class ModuleOjoin : public Module
239 {
240         NetworkPrefix* np;
241         CommandOjoin mycommand;
242
243  public:
244
245         ModuleOjoin(InspIRCd* Me)
246                 : Module(Me), np(NULL), mycommand(Me, this)
247         {
248                 /* Load config stuff */
249                 OnRehash(NULL);
250
251                 /* Initialise module variables */
252                 np = new NetworkPrefix(Me, this);
253
254                 if (!ServerInstance->Modes->AddMode(np))
255                 {
256                         delete np;
257                         throw ModuleException("Could not add new mode!");
258                 }
259
260                 ServerInstance->AddCommand(&mycommand);
261
262                 Implementation eventlist[] = { I_OnUserPreJoin, I_OnUserKick, I_OnUserPart, I_OnUserPreKick, I_OnRehash };
263                 ServerInstance->Modules->Attach(eventlist, this, 5);
264         }
265
266         ModResult OnUserPreJoin(User *user, Channel *chan, const char *cname, std::string &privs, const std::string &keygiven)
267         {
268                 if (mycommand.active)
269                 {
270                         privs += 'Y';
271                         if (op)
272                                 privs += 'o';
273                         return MOD_RES_ALLOW;
274                 }
275
276                 return MOD_RES_PASSTHRU;
277         }
278
279         void OnRehash(User* user)
280         {
281                 ConfigReader Conf(ServerInstance);
282
283                 if (!np)
284                 {
285                         // This is done on module load only
286                         std::string npre = Conf.ReadValue("ojoin", "prefix", 0);
287                         NPrefix = npre.empty() ? 0 : npre[0];
288
289                         if (NPrefix && ServerInstance->Modes->FindPrefix(NPrefix))
290                                 throw ModuleException("Looks like the +Y prefix you picked for m_ojoin is already in use. Pick another.");
291                 }
292
293                 notice = Conf.ReadFlag("ojoin", "notice", "yes", 0);
294                 op = Conf.ReadFlag("ojoin", "op", "yes", 0);
295         }
296
297         ModResult OnUserPreKick(User* source, Membership* memb, const std::string &reason)
298         {
299                 if ((ServerInstance->ULine(source->nick.c_str())) || (ServerInstance->ULine(source->server)) || (!*source->server))
300                         return MOD_RES_PASSTHRU;
301
302                 // Don't do anything if they're not +Y
303                 if (!memb->hasMode('Y'))
304                         return MOD_RES_PASSTHRU;
305
306                 // Let them do whatever they want to themselves.
307                 if (source == memb->user)
308                         return MOD_RES_PASSTHRU;
309
310                 source->WriteNumeric(484, source->nick+" "+memb->chan->name+" :Can't kick "+memb->user->nick+" as they're on official network business.");
311                 return MOD_RES_DENY;
312         }
313
314         ~ModuleOjoin()
315         {
316                 ServerInstance->Modes->DelMode(np);
317                 delete np;
318         }
319
320         void Prioritize()
321         {
322                 ServerInstance->Modules->SetPriority(this, I_OnUserPreJoin, PRIORITY_FIRST);
323         }
324
325         Version GetVersion()
326         {
327                 return Version("Network Buisness Join", VF_COMMON | VF_VENDOR);
328         }
329 };
330
331 MODULE_INIT(ModuleOjoin)
332