]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_operprefix.cpp
m_operprefix: Remove IS_LOCAL checks to also set/unset +y on remote opers
[user/henk/code/inspircd.git] / src / modules / m_operprefix.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 /*
15  * Originally by Chernov-Phoenix Alexey (Phoenix@RusNet) mailto:phoenix /email address separator/ pravmail.ru
16  */
17
18 /* $ModDesc: Gives opers cmode +y which provides a staff prefix. */
19
20 #include "inspircd.h"
21
22 static char prefixchar;
23
24 std::set<std::string>* SetupExt(User* user)
25 {
26         std::set<std::string>* ext;
27         if (!user->GetExt("m_operprefix",ext))
28         {
29                 ext=new std::set<std::string>;
30                 ext->clear();
31                 user->Extend("m_operprefix",ext);
32         }
33         return ext;
34 }
35
36
37 void DelPrefixChan(User* user, Channel* channel)
38 {
39         std::set<std::string>* chans = SetupExt(user);
40         chans->erase(channel->name);
41 }
42
43
44 void AddPrefixChan(User* user, Channel* channel)
45 {
46         std::set<std::string>* chans = SetupExt(user);
47         chans->insert(channel->name);
48 }
49
50
51 class OperPrefixMode : public ModeHandler
52 {
53         public:
54                 OperPrefixMode(InspIRCd* Instance) : ModeHandler(Instance, 'y', 1, 1, true, MODETYPE_CHANNEL, false, prefixchar) { }
55
56                 unsigned int GetPrefixRank()
57                 {
58                         return 40000;
59                 }
60
61                 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding, bool servermode)
62                 {
63                         if (servermode || (source && ServerInstance->ULine(source->server)))
64                                 return MODEACTION_ALLOW;
65                         else
66                         {
67                                 if (source && channel)
68                                         source->WriteNumeric(ERR_CHANOPRIVSNEEDED, "%s %s :Only servers are permitted to change channel mode '%c'", source->nick.c_str(), channel->name.c_str(), 'y');
69                                 return MODEACTION_DENY;
70                         }
71                 }
72
73                 ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string &parameter)
74                 {
75                         User* x = ServerInstance->FindNick(parameter);
76                         if (x)
77                         {
78                                 if (!channel->HasUser(x))
79                                 {
80                                         return std::make_pair(false, parameter);
81                                 }
82                                 else
83                                 {
84                                         std::set<std::string>* ext;
85                                         if (x->GetExt("m_operprefix",ext))
86                                         {
87                                                 if (ext->find(channel->name)!=ext->end())
88                                                 {
89                                                         return std::make_pair(true, x->nick);
90                                                 }
91                                                 else
92                                                         return std::make_pair(false, parameter);
93                                         }
94                                         else
95                                         {
96                                                 return std::make_pair(false, parameter);
97                                         }
98                                 }
99                         }
100                         return std::make_pair(false, parameter);
101                 }
102
103                 bool NeedsOper() { return true; }
104 };
105
106 class ModuleOperPrefixMode : public Module
107 {
108  private:
109         OperPrefixMode* opm;
110  public:
111         ModuleOperPrefixMode(InspIRCd* Me) : Module(Me)
112         {
113                 ConfigReader Conf(ServerInstance);
114                 std::string tmp;
115                 tmp = Conf.ReadValue("operprefix", "prefix", "!", 0, false);
116                 strlcpy(&prefixchar,tmp.c_str(),2);
117
118                 opm = new OperPrefixMode(ServerInstance);
119                 if ((!ServerInstance->Modes->AddMode(opm)))
120                         throw ModuleException("Could not add a new mode!");
121
122                 Implementation eventlist[] = { I_OnPostJoin, I_OnCleanup, I_OnUserQuit, I_OnUserKick, I_OnUserPart, I_OnOper };
123                 ServerInstance->Modules->Attach(eventlist, this, 6);
124         }
125
126         void PushChanMode(Channel* channel, User* user, bool negate = false)
127         {
128                 if (negate)
129                         DelPrefixChan(user, channel);
130                 else
131                         AddPrefixChan(user, channel);
132                 char modeline[]="+y";
133                 if (negate)
134                         modeline [0]='-';
135                 std::vector<std::string> modechange;
136                 modechange.push_back(channel->name);
137                 modechange.push_back(modeline);
138                 modechange.push_back(user->nick);
139                 ServerInstance->SendMode(modechange,this->ServerInstance->FakeClient);
140         }
141
142         virtual void OnPostJoin(User *user, Channel *channel)
143         {
144                 if (user && IS_OPER(user))
145                 {
146                         if (user->IsModeSet('H'))
147                         {
148                                 /* we respect your wish to be invisible */
149                                 return;
150                         }
151                         PushChanMode(channel, user);
152                 }
153         }
154
155         // XXX: is there a better way to do this?
156         virtual int OnRawMode(User* user, Channel* chan, const char mode, const std::string &param, bool adding, int pcnt, bool servermode)
157         {
158                 /* force event propagation to its ModeHandler */
159                 if (!servermode && chan && (mode == 'y'))
160                         return ACR_ALLOW;
161                 return 0;
162         }
163
164         virtual void OnOper(User *user, const std::string&)
165         {
166                 if (user && !user->IsModeSet('H'))
167                 {
168                         for (UCListIter v = user->chans.begin(); v != user->chans.end(); v++)
169                         {
170                                 PushChanMode(v->first, user);
171                         }
172                 }
173         }
174
175         virtual ~ModuleOperPrefixMode()
176         {
177                 ServerInstance->Modes->DelMode(opm);
178                 delete opm;
179         }
180
181         void CleanUser(User* user, bool quitting)
182         {
183
184                 std::set<std::string>* ext;
185                 if (user->GetExt("m_operprefix",ext))
186                 {
187                         // Don't want to announce -mode when they're quitting anyway..
188                         if (!quitting)
189                         {
190                                 for (UCListIter v = user->chans.begin(); v != user->chans.end(); v++)
191                                 {
192                                         ModePair ms=opm->ModeSet(NULL, NULL , v->first, user->nick);
193                                         if (ms.first)
194                                         {
195                                                 PushChanMode(v->first, user, true);
196                                         }
197                                 }
198                         }
199                         ext->clear();
200                         delete ext;
201                         user->Shrink("m_operprefix");
202                 }
203         }
204
205         virtual void OnCleanup(int target_type, void* item)
206         {
207                 if (target_type == TYPE_USER)
208                 {
209                         User* user = (User*)item;
210                         CleanUser(user, false);
211                 }
212         }
213
214         virtual void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
215         {
216                 CleanUser(user,true);
217         }
218
219         virtual void OnUserKick(User* source, User* user, Channel* chan, const std::string &reason, bool &silent)
220         {
221                 DelPrefixChan(user, chan);
222         }
223
224         virtual void OnUserPart(User* user, Channel* channel, std::string &partreason, bool &silent)
225         {
226                 DelPrefixChan(user, channel);
227         }
228
229         virtual Version GetVersion()
230         {
231                 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
232         }
233 };
234
235 MODULE_INIT(ModuleOperPrefixMode)