]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_operprefix.cpp
Textual improvements and fixes such as typos, casing, etc. (#1612)
[user/henk/code/inspircd.git] / src / modules / m_operprefix.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
6  *
7  * This file is part of InspIRCd.  InspIRCd is free software: you can
8  * redistribute it and/or modify it under the terms of the GNU General Public
9  * License as published by the Free Software Foundation, version 2.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20
21 /*
22  * Originally by Chernov-Phoenix Alexey (Phoenix@RusNet) mailto:phoenix /email address separator/ pravmail.ru
23  */
24
25 #include "inspircd.h"
26
27 #define OPERPREFIX_VALUE 1000000
28
29 class OperPrefixMode : public PrefixMode
30 {
31         public:
32                 OperPrefixMode(Module* Creator)
33                         : PrefixMode(Creator, "operprefix", 'y', OPERPREFIX_VALUE)
34                 {
35                         prefix = ServerInstance->Config->ConfValue("operprefix")->getString("prefix", "!", 1, 1)[0];
36                         ranktoset = ranktounset = UINT_MAX;
37                 }
38 };
39
40 class ModuleOperPrefixMode;
41 class HideOperWatcher : public ModeWatcher
42 {
43         ModuleOperPrefixMode* parentmod;
44
45  public:
46         HideOperWatcher(ModuleOperPrefixMode* parent);
47         void AfterMode(User* source, User* dest, Channel* channel, const std::string &parameter, bool adding) CXX11_OVERRIDE;
48 };
49
50 class ModuleOperPrefixMode : public Module
51 {
52         OperPrefixMode opm;
53         HideOperWatcher hideoperwatcher;
54         UserModeReference hideopermode;
55
56  public:
57         ModuleOperPrefixMode()
58                 : opm(this), hideoperwatcher(this)
59                 , hideopermode(this, "hideoper")
60         {
61                 /* To give clients a chance to learn about the new prefix we don't give +y to opers
62                  * right now. That means if the module was loaded after opers have joined channels
63                  * they need to rejoin them in order to get the oper prefix.
64                  */
65         }
66
67         ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
68         {
69                 if ((user->IsOper()) && (!user->IsModeSet(hideopermode)))
70                         privs.push_back('y');
71                 return MOD_RES_PASSTHRU;
72         }
73
74         void OnPostJoin(Membership* memb) CXX11_OVERRIDE
75         {
76                 if ((!IS_LOCAL(memb->user)) || (!memb->user->IsOper()) || (memb->user->IsModeSet(hideopermode)))
77                         return;
78
79                 if (memb->HasMode(&opm))
80                         return;
81
82                 // The user was force joined and OnUserPreJoin() did not run. Set the operprefix now.
83                 Modes::ChangeList changelist;
84                 changelist.push_add(&opm, memb->user->nick);
85                 ServerInstance->Modes.Process(ServerInstance->FakeClient, memb->chan, NULL, changelist);
86         }
87
88         void SetOperPrefix(User* user, bool add)
89         {
90                 Modes::ChangeList changelist;
91                 changelist.push(&opm, add, user->nick);
92                 for (User::ChanList::iterator v = user->chans.begin(); v != user->chans.end(); v++)
93                         ServerInstance->Modes->Process(ServerInstance->FakeClient, (*v)->chan, NULL, changelist);
94         }
95
96         void OnPostOper(User* user, const std::string& opername, const std::string& opertype) CXX11_OVERRIDE
97         {
98                 if (IS_LOCAL(user) && (!user->IsModeSet(hideopermode)))
99                         SetOperPrefix(user, true);
100         }
101
102         Version GetVersion() CXX11_OVERRIDE
103         {
104                 return Version("Gives opers channel mode +y which provides a staff prefix", VF_VENDOR);
105         }
106
107         void Prioritize() CXX11_OVERRIDE
108         {
109                 // m_opermodes may set +H on the oper to hide him, we don't want to set the oper prefix in that case
110                 Module* opermodes = ServerInstance->Modules->Find("m_opermodes.so");
111                 ServerInstance->Modules->SetPriority(this, I_OnPostOper, PRIORITY_AFTER, opermodes);
112         }
113 };
114
115 HideOperWatcher::HideOperWatcher(ModuleOperPrefixMode* parent)
116         : ModeWatcher(parent, "hideoper", MODETYPE_USER)
117         , parentmod(parent)
118 {
119 }
120
121 void HideOperWatcher::AfterMode(User* source, User* dest, Channel* channel, const std::string& parameter, bool adding)
122 {
123         // If hideoper is being unset because the user is deopering, don't set +y
124         if (IS_LOCAL(dest) && dest->IsOper())
125                 parentmod->SetOperPrefix(dest, !adding);
126 }
127
128 MODULE_INIT(ModuleOperPrefixMode)