]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_operprefix.cpp
Construct explicit parameter type list for MODE parameters
[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 #define OPERPREFIX_VALUE 1000000
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, char pfx) : ModeHandler(Instance, 'y', 1, 1, true, MODETYPE_CHANNEL, false, pfx, pfx, TR_NICK) { }
55
56                 unsigned int GetPrefixRank()
57                 {
58                         return OPERPREFIX_VALUE;
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 pfx = Conf.ReadValue("operprefix", "prefix", "!", 0, false);
115
116                 opm = new OperPrefixMode(ServerInstance, pfx[0]);
117                 if ((!ServerInstance->Modes->AddMode(opm)))
118                         throw ModuleException("Could not add a new mode!");
119
120                 Implementation eventlist[] = { I_OnPostJoin, I_OnCleanup, I_OnUserQuit, I_OnUserKick, I_OnUserPart, I_OnOper };
121                 ServerInstance->Modules->Attach(eventlist, this, 6);
122         }
123
124         void PushChanMode(Channel* channel, User* user, bool negate = false)
125         {
126                 if (negate)
127                         DelPrefixChan(user, channel);
128                 else
129                         AddPrefixChan(user, channel);
130                 char modeline[] = "+y";
131                 if (negate)
132                         modeline[0] = '-';
133                 std::vector<std::string> modechange;
134                 modechange.push_back(channel->name);
135                 modechange.push_back(modeline);
136                 modechange.push_back(user->nick);
137                 ServerInstance->SendMode(modechange,this->ServerInstance->FakeClient);
138         }
139
140         virtual void OnPostJoin(User *user, Channel *channel)
141         {
142                 if (user && IS_OPER(user))
143                 {
144                         if (user->IsModeSet('H'))
145                         {
146                                 /* we respect your wish to be invisible */
147                                 return;
148                         }
149                         PushChanMode(channel, user);
150                 }
151         }
152
153         // XXX: is there a better way to do this?
154         virtual int OnRawMode(User* user, Channel* chan, const char mode, const std::string &param, bool adding, int pcnt, bool servermode)
155         {
156                 /* force event propagation to its ModeHandler */
157                 if (!servermode && chan && (mode == 'y'))
158                         return ACR_ALLOW;
159                 return 0;
160         }
161
162         virtual void OnOper(User *user, const std::string&)
163         {
164                 if (user && !user->IsModeSet('H'))
165                 {
166                         for (UCListIter v = user->chans.begin(); v != user->chans.end(); v++)
167                         {
168                                 PushChanMode(v->first, user);
169                         }
170                 }
171         }
172
173         virtual ~ModuleOperPrefixMode()
174         {
175                 ServerInstance->Modes->DelMode(opm);
176                 delete opm;
177         }
178
179         void CleanUser(User* user, bool quitting)
180         {
181
182                 std::set<std::string>* ext;
183                 if (user->GetExt("m_operprefix",ext))
184                 {
185                         // Don't want to announce -mode when they're quitting anyway..
186                         if (!quitting)
187                         {
188                                 for (UCListIter v = user->chans.begin(); v != user->chans.end(); v++)
189                                 {
190                                         ModePair ms = opm->ModeSet(NULL, NULL , v->first, user->nick);
191                                         if (ms.first)
192                                         {
193                                                 PushChanMode(v->first, user, true);
194                                         }
195                                 }
196                         }
197                         ext->clear();
198                         delete ext;
199                         user->Shrink("m_operprefix");
200                 }
201         }
202
203         virtual void OnCleanup(int target_type, void* item)
204         {
205                 if (target_type == TYPE_USER)
206                 {
207                         User* user = (User*)item;
208                         CleanUser(user, false);
209                 }
210         }
211
212         virtual void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
213         {
214                 CleanUser(user,true);
215         }
216
217         virtual void OnUserKick(User* source, User* user, Channel* chan, const std::string &reason, bool &silent)
218         {
219                 DelPrefixChan(user, chan);
220         }
221
222         virtual void OnUserPart(User* user, Channel* channel, std::string &partreason, bool &silent)
223         {
224                 DelPrefixChan(user, channel);
225         }
226
227         virtual Version GetVersion()
228         {
229                 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
230         }
231 };
232
233 MODULE_INIT(ModuleOperPrefixMode)