]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_ojoin.cpp
b8c12f00a3b9ae07561d98c1cac5a6954481b245
[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(Module* parent) : Command(parent,"OJOIN", 1)
47         {
48                 flags_needed = 'o'; Penalty = 0; 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 (notice)
71                         {
72                                 channel = ServerInstance->FindChan(parameters[0]);
73                                 channel->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s joined on official network business.",
74                                         parameters[0].c_str(), user->nick.c_str());
75                                 ServerInstance->PI->SendChannelNotice(channel, 0, std::string(user->nick) + " joined on official network business.");
76                         }
77                 }
78                 else
79                 {
80                         ServerInstance->SNO->WriteGlobalSno('a', std::string(user->nick)+" used OJOIN in "+parameters[0]);
81                         // they're already in the channel
82                         std::vector<std::string> modes;
83                         modes.push_back(parameters[0]);
84                         modes.push_back("+Y");
85                         modes.push_back(user->nick);
86                         ServerInstance->SendMode(modes, ServerInstance->FakeClient);
87                         ServerInstance->PI->SendMode(parameters[0], ServerInstance->Modes->GetLastParseParams(), ServerInstance->Modes->GetLastParseTranslate());
88                 }
89                 return CMD_SUCCESS;
90         }
91 };
92
93 /** channel mode +Y
94  */
95 class NetworkPrefix : public ModeHandler
96 {
97  public:
98         NetworkPrefix(Module* parent) : ModeHandler(parent, 'Y', PARAM_ALWAYS, MODETYPE_CHANNEL)
99         {
100                 list = true;
101                 prefix = NPrefix;
102                 levelrequired = 0xFFFFFFFF;
103                 m_paramtype = TR_NICK;
104         }
105
106         ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string &parameter)
107         {
108                 User* x = ServerInstance->FindNick(parameter);
109                 if (x)
110                 {
111                         Membership* m = channel->GetUser(x);
112                         if (!m)
113                         {
114                                 return std::make_pair(false, parameter);
115                         }
116                         else
117                         {
118                                 if (m->hasMode('Y'))
119                                 {
120                                         return std::make_pair(true, x->nick);
121                                 }
122                                 else
123                                 {
124                                         return std::make_pair(false, parameter);
125                                 }
126                         }
127                 }
128                 return std::make_pair(false, parameter);
129         }
130
131         void RemoveMode(Channel* channel, irc::modestacker* stack)
132         {
133                 const UserMembList* cl = channel->GetUsers();
134                 std::vector<std::string> mode_junk;
135                 mode_junk.push_back(channel->name);
136                 irc::modestacker modestack(ServerInstance, false);
137                 std::deque<std::string> stackresult;
138
139                 for (UserMembCIter i = cl->begin(); i != cl->end(); i++)
140                 {
141                         if (i->second->hasMode('Y'))
142                         {
143                                 if (stack)
144                                         stack->Push(this->GetModeChar(), i->first->nick);
145                                 else
146                                         modestack.Push(this->GetModeChar(), i->first->nick);
147                         }
148                 }
149
150                 if (stack)
151                         return;
152
153                 while (modestack.GetStackedLine(stackresult))
154                 {
155                         mode_junk.insert(mode_junk.end(), stackresult.begin(), stackresult.end());
156                         ServerInstance->SendMode(mode_junk, ServerInstance->FakeClient);
157                         mode_junk.erase(mode_junk.begin() + 1, mode_junk.end());
158                 }
159         }
160
161         User* FindAndVerify(std::string &parameter, Channel* channel)
162         {
163                 User* theuser = ServerInstance->FindNick(parameter);
164                 if ((!theuser) || (!channel->HasUser(theuser)))
165                 {
166                         parameter.clear();
167                         return NULL;
168                 }
169                 return theuser;
170         }
171
172         ModeAction HandleChange(User* source, User* theuser, bool adding, Channel* channel, std::string &parameter)
173         {
174                 Membership* m = channel->GetUser(theuser);
175                 if (m && adding)
176                 {
177                         if (!m->hasMode('Y'))
178                         {
179                                 parameter = theuser->nick;
180                                 return MODEACTION_ALLOW;
181                         }
182                 }
183                 else if (m && !adding)
184                 {
185                         if (m->hasMode('Y'))
186                         {
187                                 parameter = theuser->nick;
188                                 return MODEACTION_ALLOW;
189                         }
190                 }
191                 return MODEACTION_DENY;
192         }
193
194         unsigned int GetPrefixRank()
195         {
196                 return NETWORK_VALUE;
197         }
198
199         void RemoveMode(User* user, irc::modestacker* stack)
200         {
201         }
202
203         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
204         {
205                 User* theuser = FindAndVerify(parameter, channel);
206
207                 if (!theuser)
208                         return MODEACTION_DENY;
209
210                  // source is a server, or ulined, we'll let them +-Y the user.
211                 if (source == ServerInstance->FakeClient ||
212                         ((source == theuser) && (!adding)) ||
213                         (ServerInstance->ULine(source->nick.c_str())) ||
214                         (ServerInstance->ULine(source->server)) ||
215                         (!*source->server) ||
216                         (!IS_LOCAL(source))
217                         )
218                 {
219                         return HandleChange(source, theuser, adding, channel, parameter);
220                 }
221                 else
222                 {
223                         // bzzzt, wrong answer!
224                         source->WriteNumeric(482, "%s %s :Only servers may change this mode.", source->nick.c_str(), channel->name.c_str());
225                         return MODEACTION_DENY;
226                 }
227         }
228
229 };
230
231 class ModuleOjoin : public Module
232 {
233         NetworkPrefix* np;
234         CommandOjoin mycommand;
235
236  public:
237
238         ModuleOjoin(InspIRCd* Me)
239                 : Module(Me), np(NULL), mycommand(this)
240         {
241                 /* Load config stuff */
242                 OnRehash(NULL);
243
244                 /* Initialise module variables */
245                 np = new NetworkPrefix(this);
246
247                 if (!ServerInstance->Modes->AddMode(np))
248                 {
249                         delete np;
250                         throw ModuleException("Could not add new mode!");
251                 }
252
253                 ServerInstance->AddCommand(&mycommand);
254
255                 Implementation eventlist[] = { I_OnUserPreJoin, I_OnUserKick, I_OnUserPart, I_OnUserPreKick, I_OnRehash };
256                 ServerInstance->Modules->Attach(eventlist, this, 5);
257         }
258
259         ModResult OnUserPreJoin(User *user, Channel *chan, const char *cname, std::string &privs, const std::string &keygiven)
260         {
261                 if (mycommand.active)
262                 {
263                         privs += 'Y';
264                         if (op)
265                                 privs += 'o';
266                         return MOD_RES_ALLOW;
267                 }
268
269                 return MOD_RES_PASSTHRU;
270         }
271
272         void OnRehash(User* user)
273         {
274                 ConfigReader Conf(ServerInstance);
275
276                 if (!np)
277                 {
278                         // This is done on module load only
279                         std::string npre = Conf.ReadValue("ojoin", "prefix", 0);
280                         NPrefix = npre.empty() ? 0 : npre[0];
281
282                         if (NPrefix && ServerInstance->Modes->FindPrefix(NPrefix))
283                                 throw ModuleException("Looks like the +Y prefix you picked for m_ojoin is already in use. Pick another.");
284                 }
285
286                 notice = Conf.ReadFlag("ojoin", "notice", "yes", 0);
287                 op = Conf.ReadFlag("ojoin", "op", "yes", 0);
288         }
289
290         ModResult OnUserPreKick(User* source, Membership* memb, const std::string &reason)
291         {
292                 if ((ServerInstance->ULine(source->nick.c_str())) || (ServerInstance->ULine(source->server)) || (!*source->server))
293                         return MOD_RES_PASSTHRU;
294
295                 // Don't do anything if they're not +Y
296                 if (!memb->hasMode('Y'))
297                         return MOD_RES_PASSTHRU;
298
299                 // Let them do whatever they want to themselves.
300                 if (source == memb->user)
301                         return MOD_RES_PASSTHRU;
302
303                 source->WriteNumeric(484, source->nick+" "+memb->chan->name+" :Can't kick "+memb->user->nick+" as they're on official network business.");
304                 return MOD_RES_DENY;
305         }
306
307         ~ModuleOjoin()
308         {
309                 ServerInstance->Modes->DelMode(np);
310                 delete np;
311         }
312
313         void Prioritize()
314         {
315                 ServerInstance->Modules->SetPriority(this, I_OnUserPreJoin, PRIORITY_FIRST);
316         }
317
318         Version GetVersion()
319         {
320                 return Version("Network Buisness Join", VF_COMMON | VF_VENDOR);
321         }
322 };
323
324 MODULE_INIT(ModuleOjoin)
325