]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_ojoin.cpp
6267726ef8114bd43ae7e8c32cbc8081d7eb4f88
[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                         return CMD_SUCCESS;
89                 }
90                 else
91                 {
92                         user->WriteServ("NOTICE "+std::string(user->nick)+" :*** Could not join "+parameters[0]);
93                         return CMD_FAILURE;
94                 }
95         }
96 };
97
98 /** Abstraction of NetworkPrefixBase for channel mode +Y
99  */
100 class NetworkPrefix : public ModeHandler
101 {
102  public:
103         NetworkPrefix(InspIRCd* Instance, Module* parent)
104                 : ModeHandler(Instance, parent, 'Y', 1, 1, true, MODETYPE_CHANNEL, false, NPrefix, 0, TR_NICK)
105 // NetworkPrefixBase(Instance,"cm_network_","official user", 388, 389) { }
106 // NetworkPrefixBase(InspIRCd* Instance, const std::string &ext, const std::string &mtype, int l, int e) :
107 //  extend(ext), type(mtype), list(l), end(e)
108         {
109         }
110
111         ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string &parameter)
112         {
113                 User* x = ServerInstance->FindNick(parameter);
114                 if (x)
115                 {
116                         if (!channel->HasUser(x))
117                         {
118                                 return std::make_pair(false, parameter);
119                         }
120                         else
121                         {
122                                 std::string item = "cm_network_"+std::string(channel->name);
123                                 if (x->GetExt(item))
124                                 {
125                                         return std::make_pair(true, x->nick);
126                                 }
127                                 else
128                                 {
129                                         return std::make_pair(false, parameter);
130                                 }
131                         }
132                 }
133                 return std::make_pair(false, parameter);
134         }
135
136         void RemoveMode(Channel* channel, irc::modestacker* stack)
137         {
138                 CUList* cl = channel->GetUsers();
139                 std::string item = "cm_network_" + std::string(channel->name);
140                 std::vector<std::string> mode_junk;
141                 mode_junk.push_back(channel->name);
142                 irc::modestacker modestack(ServerInstance, false);
143                 std::deque<std::string> stackresult;
144
145                 for (CUList::iterator i = cl->begin(); i != cl->end(); i++)
146                 {
147                         if (i->first->GetExt(item))
148                         {
149                                 if (stack)
150                                         stack->Push(this->GetModeChar(), i->first->nick);
151                                 else
152                                         modestack.Push(this->GetModeChar(), i->first->nick);
153                         }
154                 }
155
156                 if (stack)
157                         return;
158
159                 while (modestack.GetStackedLine(stackresult))
160                 {
161                         mode_junk.insert(mode_junk.end(), stackresult.begin(), stackresult.end());
162                         ServerInstance->SendMode(mode_junk, ServerInstance->FakeClient);
163                         mode_junk.erase(mode_junk.begin() + 1, mode_junk.end());
164                 }
165         }
166
167         User* FindAndVerify(std::string &parameter, Channel* channel)
168         {
169                 User* theuser = ServerInstance->FindNick(parameter);
170                 if ((!theuser) || (!channel->HasUser(theuser)))
171                 {
172                         parameter.clear();
173                         return NULL;
174                 }
175                 return theuser;
176         }
177
178         ModeAction HandleChange(User* source, User* theuser, bool adding, Channel* channel, std::string &parameter)
179         {
180                 std::string item = "cm_network_" + std::string(channel->name);
181
182                 if (adding)
183                 {
184                         if (!theuser->GetExt(item))
185                         {
186                                 theuser->Extend(item);
187                                 parameter = theuser->nick;
188                                 return MODEACTION_ALLOW;
189                         }
190                 }
191                 else
192                 {
193                         if (theuser->GetExt(item))
194                         {
195                                 theuser->Shrink(item);
196                                 parameter = theuser->nick;
197                                 return MODEACTION_ALLOW;
198                         }
199                 }
200                 return MODEACTION_DENY;
201         }
202
203         unsigned int GetPrefixRank()
204         {
205                 return NETWORK_VALUE;
206         }
207
208         void RemoveMode(User* user, irc::modestacker* stack)
209         {
210         }
211
212         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
213         {
214                 User* theuser = FindAndVerify(parameter, channel);
215
216                 if (!theuser)
217                         return MODEACTION_DENY;
218
219                  // source is a server, or ulined, we'll let them +-Y the user.
220                 if (source == ServerInstance->FakeClient ||
221                         ((source == theuser) && (!adding)) ||
222                         (ServerInstance->ULine(source->nick.c_str())) ||
223                         (ServerInstance->ULine(source->server)) ||
224                         (!*source->server) ||
225                         (!IS_LOCAL(source))
226                         )
227                 {
228                         return HandleChange(source, theuser, adding, channel, parameter);
229                 }
230                 else
231                 {
232                         // bzzzt, wrong answer!
233                         source->WriteNumeric(482, "%s %s :Only servers may change this mode.", source->nick.c_str(), channel->name.c_str());
234                         return MODEACTION_DENY;
235                 }
236         }
237
238 };
239
240 class ModuleOjoin : public Module
241 {
242         NetworkPrefix* np;
243         CommandOjoin mycommand;
244
245  public:
246
247         ModuleOjoin(InspIRCd* Me)
248                 : Module(Me), np(NULL), mycommand(Me, this)
249         {
250                 /* Load config stuff */
251                 OnRehash(NULL);
252
253                 /* Initialise module variables */
254                 np = new NetworkPrefix(ServerInstance, this);
255
256                 if (!ServerInstance->Modes->AddMode(np))
257                 {
258                         delete np;
259                         throw ModuleException("Could not add new mode!");
260                 }
261
262                 ServerInstance->AddCommand(&mycommand);
263
264                 Implementation eventlist[] = { I_OnUserPreJoin, I_OnUserKick, I_OnUserPart, I_OnAccessCheck, I_OnRehash };
265                 ServerInstance->Modules->Attach(eventlist, this, 5);
266         }
267
268         ModResult OnUserPreJoin(User *user, Channel *chan, const char *cname, std::string &privs, const std::string &keygiven)
269         {
270                 if (mycommand.active)
271                 {
272                         if (NPrefix)
273                                 privs += NPrefix;
274                         if (op && privs.find('@') == std::string::npos)
275                                 privs += '@';
276                         return MOD_RES_ALLOW;
277                 }
278
279                 return MOD_RES_PASSTHRU;
280         }
281
282         void OnUserKick(User* source, User* user, Channel* chan, const std::string &reason, bool &silent)
283         {
284                 user->Shrink("cm_network_"+std::string(chan->name));
285         }
286
287         void OnUserPart(User* user, Channel* channel, std::string &partreason, bool &silent)
288         {
289                 user->Shrink("cm_network_"+std::string(channel->name));
290         }
291
292         void OnRehash(User* user)
293         {
294                 ConfigReader Conf(ServerInstance);
295
296                 if (!np)
297                 {
298                         // This is done on module load only
299                         std::string npre = Conf.ReadValue("ojoin", "prefix", 0);
300                         NPrefix = npre.empty() ? 0 : npre[0];
301
302                         if (NPrefix && ServerInstance->Modes->FindPrefix(NPrefix))
303                                 throw ModuleException("Looks like the +Y prefix you picked for m_ojoin is already in use. Pick another.");
304                 }
305
306                 notice = Conf.ReadFlag("ojoin", "notice", "yes", 0);
307                 op = Conf.ReadFlag("ojoin", "op", "yes", 0);
308         }
309
310         ModResult OnAccessCheck(User* source,User* dest,Channel* channel,int access_type)
311         {
312                 // Here's where we preform access checks, and disallow any kicking/deopping
313                 // of +Y users. 
314
315                 // If there's no dest, it's not for us.
316                 if (!dest || !channel)
317                         return MOD_RES_PASSTHRU;
318
319                 // If a ulined nickname, or a server is setting the mode, let it
320                 // do whatever it wants.
321                 if ((ServerInstance->ULine(source->nick.c_str())) || (ServerInstance->ULine(source->server)) || (!*source->server))
322                         return MOD_RES_ALLOW;
323
324                 std::string network("cm_network_"+channel->name);
325
326                 // Don't do anything if they're not +Y
327                 if (!dest->GetExt(network))
328                         return MOD_RES_PASSTHRU;
329
330                 // Let them do whatever they want to themselves.
331                 if (source == dest)
332                         return MOD_RES_PASSTHRU;
333
334                 switch (access_type)
335                 {
336                         // Disallow deopping of +Y users.
337                         case AC_DEOP:
338                                 source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't deop "+dest->nick+" as they're on official network business.");
339                                 return MOD_RES_DENY;
340                         break;
341
342                         // Kicking people who are here on network business is a no no.
343                         case AC_KICK:
344                                 source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't kick "+dest->nick+" as they're on official network business.");
345                                 return MOD_RES_DENY;
346                         break;
347
348                         // Yes, they're immune to dehalfopping too.
349                         case AC_DEHALFOP:
350                                 source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't de-halfop "+dest->nick+" as they're on official network business.");
351                                 return MOD_RES_DENY;
352                         break;
353
354                         // same with devoice.
355                         case AC_DEVOICE:
356                                 source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't devoice "+dest->nick+" as they're on official network business.");
357                                 return MOD_RES_DENY;
358                         break;
359                 }
360
361                 // Some other access check that doesn't fall into the above. Let it through.
362                 return MOD_RES_PASSTHRU;
363         }
364
365         ~ModuleOjoin()
366         {
367                 ServerInstance->Modes->DelMode(np);
368                 delete np;
369         }
370
371         void Prioritize()
372         {
373                 ServerInstance->Modules->SetPriority(this, I_OnUserPreJoin, PRIORITY_FIRST);
374         }
375
376         Version GetVersion()
377         {
378                 return Version("Network Buisness Join", VF_COMMON | VF_VENDOR);
379         }
380 };
381
382 MODULE_INIT(ModuleOjoin)
383