]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_operprefix.cpp
9f1f6cc5e8f3f25863a940abde3513d133767d46
[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 /* $ModDesc: Gives opers cmode +y which provides a staff prefix. */
26
27 #include "inspircd.h"
28
29 #define OPERPREFIX_VALUE 1000000
30
31 class OperPrefixMode : public ModeHandler
32 {
33         public:
34                 OperPrefixMode(Module* Creator) : ModeHandler(Creator, "operprefix", 'y', PARAM_ALWAYS, MODETYPE_CHANNEL)
35                 {
36                         std::string pfx = ServerInstance->Config->ConfValue("operprefix")->getString("prefix", "!");
37                         list = true;
38                         prefix = pfx.empty() ? '!' : pfx[0];
39                         levelrequired = OPERPREFIX_VALUE;
40                         m_paramtype = TR_NICK;
41                 }
42
43                 unsigned int GetPrefixRank()
44                 {
45                         return OPERPREFIX_VALUE;
46                 }
47
48                 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
49                 {
50                         if (IS_SERVER(source) || ServerInstance->ULine(source->server))
51                                 return MODEACTION_ALLOW;
52                         else
53                         {
54                                 if (channel)
55                                         source->WriteNumeric(ERR_CHANOPRIVSNEEDED, "%s %s :Only servers are permitted to change channel mode '%c'", source->nick.c_str(), channel->name.c_str(), 'y');
56                                 return MODEACTION_DENY;
57                         }
58                 }
59
60                 bool NeedsOper() { return true; }
61 };
62
63 class ModuleOperPrefixMode;
64 class HideOperWatcher : public ModeWatcher
65 {
66         ModuleOperPrefixMode* parentmod;
67  public:
68         HideOperWatcher(ModuleOperPrefixMode* parent) : ModeWatcher((Module*) parent, 'H', MODETYPE_USER), parentmod(parent) {}
69         void AfterMode(User* source, User* dest, Channel* channel, const std::string &parameter, bool adding, ModeType type);
70 };
71
72 class ModuleOperPrefixMode : public Module
73 {
74  private:
75         OperPrefixMode opm;
76         bool mw_added;
77         HideOperWatcher hideoperwatcher;
78  public:
79         ModuleOperPrefixMode()
80                 : opm(this), mw_added(false), hideoperwatcher(this)
81         {
82         }
83
84         void init()
85         {
86                 ServerInstance->Modules->AddService(opm);
87
88                 Implementation eventlist[] = { I_OnUserPreJoin, I_OnPostOper, I_OnLoadModule, I_OnUnloadModule, I_OnPostJoin };
89                 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
90
91                 /* To give clients a chance to learn about the new prefix we don't give +y to opers
92                  * right now. That means if the module was loaded after opers have joined channels
93                  * they need to rejoin them in order to get the oper prefix.
94                  */
95
96                 if (ServerInstance->Modules->Find("m_hideoper.so"))
97                         mw_added = ServerInstance->Modes->AddModeWatcher(&hideoperwatcher);
98         }
99
100         ModResult OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string& privs, const std::string& keygiven)
101         {
102                 /* The user may have the +H umode on himself, but +H does not necessarily correspond
103                  * to the +H of m_hideoper.
104                  * However we only add the modewatcher when m_hideoper is loaded, so these
105                  * conditions (mw_added and the user being +H) together mean the user is a hidden oper.
106                  */
107
108                 if (IS_OPER(user) && (!mw_added || !user->IsModeSet('H')))
109                         privs.push_back('y');
110                 return MOD_RES_PASSTHRU;
111         }
112
113         void OnPostJoin(Membership* memb)
114         {
115                 if ((!IS_LOCAL(memb->user)) || (!IS_OPER(memb->user)) || (((mw_added) && (memb->user->IsModeSet('H')))))
116                         return;
117
118                 if (memb->hasMode(opm.GetModeChar()))
119                         return;
120
121                 // The user was force joined and OnUserPreJoin() did not run. Set the operprefix now.
122                 std::vector<std::string> modechange;
123                 modechange.push_back(memb->chan->name);
124                 modechange.push_back("+y");
125                 modechange.push_back(memb->user->nick);
126                 ServerInstance->SendGlobalMode(modechange, ServerInstance->FakeClient);
127         }
128
129         void SetOperPrefix(User* user, bool add)
130         {
131                 std::vector<std::string> modechange;
132                 modechange.push_back("");
133                 modechange.push_back(add ? "+y" : "-y");
134                 modechange.push_back(user->nick);
135                 for (UCListIter v = user->chans.begin(); v != user->chans.end(); v++)
136                 {
137                         modechange[0] = (*v)->name;
138                         ServerInstance->SendGlobalMode(modechange, ServerInstance->FakeClient);
139                 }
140         }
141
142         void OnPostOper(User* user, const std::string& opername, const std::string& opertype)
143         {
144                 if (IS_LOCAL(user) && (!mw_added || !user->IsModeSet('H')))
145                         SetOperPrefix(user, true);
146         }
147
148         void OnLoadModule(Module* mod)
149         {
150                 if ((!mw_added) && (mod->ModuleSourceFile == "m_hideoper.so"))
151                         mw_added = ServerInstance->Modes->AddModeWatcher(&hideoperwatcher);
152         }
153
154         void OnUnloadModule(Module* mod)
155         {
156                 if ((mw_added) && (mod->ModuleSourceFile == "m_hideoper.so") && (ServerInstance->Modes->DelModeWatcher(&hideoperwatcher)))
157                         mw_added = false;
158         }
159
160         ~ModuleOperPrefixMode()
161         {
162                 if (mw_added)
163                         ServerInstance->Modes->DelModeWatcher(&hideoperwatcher);
164         }
165
166         Version GetVersion()
167         {
168                 return Version("Gives opers cmode +y which provides a staff prefix.", VF_VENDOR);
169         }
170
171         void Prioritize()
172         {
173                 // m_opermodes may set +H on the oper to hide him, we don't want to set the oper prefix in that case
174                 Module* opermodes = ServerInstance->Modules->Find("m_opermodes.so");
175                 ServerInstance->Modules->SetPriority(this, I_OnPostOper, PRIORITY_AFTER, opermodes);
176         }
177 };
178
179 void HideOperWatcher::AfterMode(User* source, User* dest, Channel* channel, const std::string& parameter, bool adding, ModeType type)
180 {
181         // If hideoper is being unset because the user is deopering, don't set +y
182         if (IS_LOCAL(dest) && IS_OPER(dest))
183                 parentmod->SetOperPrefix(dest, !adding);
184 }
185
186 MODULE_INIT(ModuleOperPrefixMode)