]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_operprefix.cpp
Nuke trailing spaces
[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         virtual 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                 // This may look wrong, but I don't think it is.. PushChanMode will send FMODE which should sort it all out.
145                 if (!IS_LOCAL(user))
146                         return;
147
148                 if (user && IS_OPER(user))
149                 {
150                         if (user->IsModeSet('H'))
151                         {
152                                 /* we respect your wish to be invisible */
153                                 return;
154                         }
155                         PushChanMode(channel, user);
156                 }
157         }
158
159         // XXX: is there a better way to do this?
160         virtual int OnRawMode(User* user, Channel* chan, const char mode, const std::string &param, bool adding, int pcnt, bool servermode)
161         {
162                 /* force event propagation to its ModeHandler */
163                 if (!servermode && chan && (mode == 'y'))
164                         return ACR_ALLOW;
165                 return 0;
166         }
167
168         virtual void OnOper(User *user, const std::string&)
169         {
170                 if (user && IS_LOCAL(user) && !user->IsModeSet('H'))
171                 {
172                         for (UCListIter v = user->chans.begin(); v != user->chans.end(); v++)
173                         {
174                                 PushChanMode(v->first, user);
175                         }
176                 }
177         }
178
179         virtual ~ModuleOperPrefixMode()
180         {
181                 ServerInstance->Modes->DelMode(opm);
182                 delete opm;
183         }
184
185         virtual void CleanUser(User* user, bool quitting=false)
186         {
187                 if (!IS_LOCAL(user))
188                         return;
189
190                 std::set<std::string>* ext;
191                 if (user->GetExt("m_operprefix",ext))
192                 {
193                         // Don't want to announce -mode when they're quitting anyway..
194                         if (!quitting)
195                         {
196                                 for (UCListIter v = user->chans.begin(); v != user->chans.end(); v++)
197                                 {
198                                         ModePair ms=opm->ModeSet(NULL, NULL , v->first, user->nick);
199                                         if (ms.first)
200                                         {
201                                                 PushChanMode(v->first, user, true);
202                                         }
203                                 }
204                         }
205                         ext->clear();
206                         delete ext;
207                         user->Shrink("m_operprefix");
208                 }
209         }
210
211         virtual void OnCleanup(int target_type, void* item)
212         {
213                 if (target_type == TYPE_USER)
214                 {
215                         User* user = (User*)item;
216                         CleanUser(user);
217                 }
218         }
219
220         virtual void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
221         {
222                 CleanUser(user,true);
223         }
224
225         virtual void OnUserKick(User* source, User* user, Channel* chan, const std::string &reason, bool &silent)
226         {
227                 DelPrefixChan(user, chan);
228         }
229
230         virtual void OnUserPart(User* user, Channel* channel, std::string &partreason, bool &silent)
231         {
232                 DelPrefixChan(user, channel);
233         }
234
235         virtual Version GetVersion()
236         {
237                 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
238         }
239 };
240
241 MODULE_INIT(ModuleOperPrefixMode)