]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_operprefix.cpp
Merge pull request #1157 from SaberUK/insp20+fix-cron-restart
[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                 void RemoveMode(Channel* chan, irc::modestacker* stack)
63                 {
64                         irc::modestacker modestack(false);
65                         const UserMembList* users = chan->GetUsers();
66                         for (UserMembCIter i = users->begin(); i != users->end(); ++i)
67                         {
68                                 if (i->second->hasMode(mode))
69                                 {
70                                         if (stack)
71                                                 stack->Push(this->GetModeChar(), i->first->nick);
72                                         else
73                                                 modestack.Push(this->GetModeChar(), i->first->nick);
74                                 }
75                         }
76
77                         if (stack)
78                                 return;
79
80                         std::deque<std::string> stackresult;
81                         std::vector<std::string> mode_junk;
82                         mode_junk.push_back(chan->name);
83                         while (modestack.GetStackedLine(stackresult))
84                         {
85                                 mode_junk.insert(mode_junk.end(), stackresult.begin(), stackresult.end());
86                                 ServerInstance->SendMode(mode_junk, ServerInstance->FakeClient);
87                                 mode_junk.erase(mode_junk.begin() + 1, mode_junk.end());
88                         }
89                 }
90
91                 void RemoveMode(User* user, irc::modestacker* stack)
92                 {
93                 }
94 };
95
96 class ModuleOperPrefixMode;
97 class HideOperWatcher : public ModeWatcher
98 {
99         ModuleOperPrefixMode* parentmod;
100  public:
101         HideOperWatcher(ModuleOperPrefixMode* parent) : ModeWatcher((Module*) parent, 'H', MODETYPE_USER), parentmod(parent) {}
102         void AfterMode(User* source, User* dest, Channel* channel, const std::string &parameter, bool adding, ModeType type);
103 };
104
105 class ModuleOperPrefixMode : public Module
106 {
107  private:
108         OperPrefixMode opm;
109         bool mw_added;
110         HideOperWatcher hideoperwatcher;
111  public:
112         ModuleOperPrefixMode()
113                 : opm(this), mw_added(false), hideoperwatcher(this)
114         {
115         }
116
117         void init()
118         {
119                 ServerInstance->Modules->AddService(opm);
120
121                 Implementation eventlist[] = { I_OnUserPreJoin, I_OnPostOper, I_OnLoadModule, I_OnUnloadModule, I_OnPostJoin };
122                 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
123
124                 /* To give clients a chance to learn about the new prefix we don't give +y to opers
125                  * right now. That means if the module was loaded after opers have joined channels
126                  * they need to rejoin them in order to get the oper prefix.
127                  */
128
129                 if (ServerInstance->Modules->Find("m_hideoper.so"))
130                         mw_added = ServerInstance->Modes->AddModeWatcher(&hideoperwatcher);
131         }
132
133         ModResult OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string& privs, const std::string& keygiven)
134         {
135                 /* The user may have the +H umode on himself, but +H does not necessarily correspond
136                  * to the +H of m_hideoper.
137                  * However we only add the modewatcher when m_hideoper is loaded, so these
138                  * conditions (mw_added and the user being +H) together mean the user is a hidden oper.
139                  */
140
141                 if (IS_OPER(user) && (!mw_added || !user->IsModeSet('H')))
142                         privs.push_back('y');
143                 return MOD_RES_PASSTHRU;
144         }
145
146         void OnPostJoin(Membership* memb)
147         {
148                 if ((!IS_LOCAL(memb->user)) || (!IS_OPER(memb->user)) || (((mw_added) && (memb->user->IsModeSet('H')))))
149                         return;
150
151                 if (memb->hasMode(opm.GetModeChar()))
152                         return;
153
154                 // The user was force joined and OnUserPreJoin() did not run. Set the operprefix now.
155                 std::vector<std::string> modechange;
156                 modechange.push_back(memb->chan->name);
157                 modechange.push_back("+y");
158                 modechange.push_back(memb->user->nick);
159                 ServerInstance->SendGlobalMode(modechange, ServerInstance->FakeClient);
160         }
161
162         void SetOperPrefix(User* user, bool add)
163         {
164                 std::vector<std::string> modechange;
165                 modechange.push_back("");
166                 modechange.push_back(add ? "+y" : "-y");
167                 modechange.push_back(user->nick);
168                 for (UCListIter v = user->chans.begin(); v != user->chans.end(); v++)
169                 {
170                         modechange[0] = (*v)->name;
171                         ServerInstance->SendGlobalMode(modechange, ServerInstance->FakeClient);
172                 }
173         }
174
175         void OnPostOper(User* user, const std::string& opername, const std::string& opertype)
176         {
177                 if (IS_LOCAL(user) && (!mw_added || !user->IsModeSet('H')))
178                         SetOperPrefix(user, true);
179         }
180
181         void OnLoadModule(Module* mod)
182         {
183                 if ((!mw_added) && (mod->ModuleSourceFile == "m_hideoper.so"))
184                         mw_added = ServerInstance->Modes->AddModeWatcher(&hideoperwatcher);
185         }
186
187         void OnUnloadModule(Module* mod)
188         {
189                 if ((mw_added) && (mod->ModuleSourceFile == "m_hideoper.so") && (ServerInstance->Modes->DelModeWatcher(&hideoperwatcher)))
190                         mw_added = false;
191         }
192
193         ~ModuleOperPrefixMode()
194         {
195                 if (mw_added)
196                         ServerInstance->Modes->DelModeWatcher(&hideoperwatcher);
197         }
198
199         Version GetVersion()
200         {
201                 return Version("Gives opers cmode +y which provides a staff prefix.", VF_VENDOR);
202         }
203
204         void Prioritize()
205         {
206                 // m_opermodes may set +H on the oper to hide him, we don't want to set the oper prefix in that case
207                 Module* opermodes = ServerInstance->Modules->Find("m_opermodes.so");
208                 ServerInstance->Modules->SetPriority(this, I_OnPostOper, PRIORITY_AFTER, opermodes);
209         }
210 };
211
212 void HideOperWatcher::AfterMode(User* source, User* dest, Channel* channel, const std::string& parameter, bool adding, ModeType type)
213 {
214         // If hideoper is being unset because the user is deopering, don't set +y
215         if (IS_LOCAL(dest) && IS_OPER(dest))
216                 parentmod->SetOperPrefix(dest, !adding);
217 }
218
219 MODULE_INIT(ModuleOperPrefixMode)