]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_operprefix.cpp
Convert UserChanList to an intrusively linked list
[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) : PrefixMode(Creator, "operprefix", 'y')
33                 {
34                         std::string pfx = ServerInstance->Config->ConfValue("operprefix")->getString("prefix", "!");
35                         prefix = pfx.empty() ? '!' : pfx[0];
36                         levelrequired = INT_MAX;
37                         prefixrank = OPERPREFIX_VALUE;
38                 }
39 };
40
41 class ModuleOperPrefixMode;
42 class HideOperWatcher : public ModeWatcher
43 {
44         ModuleOperPrefixMode* parentmod;
45
46  public:
47         HideOperWatcher(ModuleOperPrefixMode* parent);
48         void AfterMode(User* source, User* dest, Channel* channel, const std::string &parameter, bool adding);
49 };
50
51 class ModuleOperPrefixMode : public Module
52 {
53         OperPrefixMode opm;
54         HideOperWatcher hideoperwatcher;
55         UserModeReference hideopermode;
56
57  public:
58         ModuleOperPrefixMode()
59                 : opm(this), hideoperwatcher(this)
60                 , hideopermode(this, "hideoper")
61         {
62                 /* To give clients a chance to learn about the new prefix we don't give +y to opers
63                  * right now. That means if the module was loaded after opers have joined channels
64                  * they need to rejoin them in order to get the oper prefix.
65                  */
66         }
67
68         ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
69         {
70                 if ((user->IsOper()) && (!user->IsModeSet(hideopermode)))
71                         privs.push_back('y');
72                 return MOD_RES_PASSTHRU;
73         }
74
75         void SetOperPrefix(User* user, bool add)
76         {
77                 std::vector<std::string> modechange;
78                 modechange.push_back("");
79                 modechange.push_back(add ? "+" : "-");
80                 modechange[1].push_back(opm.GetModeChar());
81                 modechange.push_back(user->nick);
82                 for (UCListIter v = user->chans.begin(); v != user->chans.end(); v++)
83                 {
84                         modechange[0] = (*v)->chan->name;
85                         ServerInstance->Modes->Process(modechange, ServerInstance->FakeClient);
86                 }
87         }
88
89         void OnPostOper(User* user, const std::string& opername, const std::string& opertype) CXX11_OVERRIDE
90         {
91                 if (IS_LOCAL(user) && (!user->IsModeSet(hideopermode)))
92                         SetOperPrefix(user, true);
93         }
94
95         Version GetVersion() CXX11_OVERRIDE
96         {
97                 return Version("Gives opers cmode +y which provides a staff prefix.", VF_VENDOR);
98         }
99
100         void Prioritize()
101         {
102                 // m_opermodes may set +H on the oper to hide him, we don't want to set the oper prefix in that case
103                 Module* opermodes = ServerInstance->Modules->Find("m_opermodes.so");
104                 ServerInstance->Modules->SetPriority(this, I_OnPostOper, PRIORITY_AFTER, opermodes);
105         }
106 };
107
108 HideOperWatcher::HideOperWatcher(ModuleOperPrefixMode* parent)
109         : ModeWatcher(parent, "hideoper", MODETYPE_USER)
110         , parentmod(parent)
111 {
112 }
113
114 void HideOperWatcher::AfterMode(User* source, User* dest, Channel* channel, const std::string& parameter, bool adding)
115 {
116         if (IS_LOCAL(dest))
117                 parentmod->SetOperPrefix(dest, !adding);
118 }
119
120 MODULE_INIT(ModuleOperPrefixMode)