1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
15 * Originally by Chernov-Phoenix Alexey (Phoenix@RusNet) mailto:phoenix /email address separator/ pravmail.ru
18 /* $ModDesc: Gives opers cmode +y which provides a staff prefix. */
22 #define OPERPREFIX_VALUE 1000000
24 std::set<std::string>* SetupExt(User* user)
26 std::set<std::string>* ext;
27 if (!user->GetExt("m_operprefix",ext))
29 ext=new std::set<std::string>;
31 user->Extend("m_operprefix",ext);
37 void DelPrefixChan(User* user, Channel* channel)
39 std::set<std::string>* chans = SetupExt(user);
40 chans->erase(channel->name);
44 void AddPrefixChan(User* user, Channel* channel)
46 std::set<std::string>* chans = SetupExt(user);
47 chans->insert(channel->name);
51 class OperPrefixMode : public ModeHandler
54 OperPrefixMode(InspIRCd* Instance, Module* Creator, char pfx) : ModeHandler(Instance, Creator, 'y', 1, 1, true, MODETYPE_CHANNEL, false, pfx, pfx, TR_NICK) { }
56 unsigned int GetPrefixRank()
58 return OPERPREFIX_VALUE;
61 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
63 if (IS_FAKE(source) || (source && ServerInstance->ULine(source->server)))
64 return MODEACTION_ALLOW;
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;
73 ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string ¶meter)
75 User* x = ServerInstance->FindNick(parameter);
78 if (!channel->HasUser(x))
80 return std::make_pair(false, parameter);
84 std::set<std::string>* ext;
85 if (x->GetExt("m_operprefix",ext))
87 if (ext->find(channel->name)!=ext->end())
89 return std::make_pair(true, x->nick);
92 return std::make_pair(false, parameter);
96 return std::make_pair(false, parameter);
100 return std::make_pair(false, parameter);
103 bool NeedsOper() { return true; }
106 class ModuleOperPrefixMode : public Module
111 ModuleOperPrefixMode(InspIRCd* Me) : Module(Me)
113 ConfigReader Conf(ServerInstance);
114 std::string pfx = Conf.ReadValue("operprefix", "prefix", "!", 0, false);
116 opm = new OperPrefixMode(ServerInstance, this, pfx[0]);
117 if ((!ServerInstance->Modes->AddMode(opm)))
118 throw ModuleException("Could not add a new mode!");
120 Implementation eventlist[] = { I_OnPostJoin, I_OnCleanup, I_OnUserQuit, I_OnUserKick, I_OnUserPart, I_OnOper };
121 ServerInstance->Modules->Attach(eventlist, this, 6);
124 void PushChanMode(Channel* channel, User* user, bool negate = false)
127 DelPrefixChan(user, channel);
129 AddPrefixChan(user, channel);
130 char modeline[] = "+y";
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);
140 virtual void OnPostJoin(User *user, Channel *channel)
142 if (user && IS_OPER(user))
144 if (user->IsModeSet('H'))
146 /* we respect your wish to be invisible */
149 PushChanMode(channel, user);
153 // XXX: is there a better way to do this?
154 virtual ModResult OnRawMode(User* user, Channel* chan, const char mode, const std::string ¶m, bool adding, int pcnt)
156 /* force event propagation to its ModeHandler */
157 if (!IS_FAKE(user) && chan && (mode == 'y'))
158 return MOD_RES_ALLOW;
159 return MOD_RES_PASSTHRU;
162 virtual void OnOper(User *user, const std::string&)
164 if (user && !user->IsModeSet('H'))
166 for (UCListIter v = user->chans.begin(); v != user->chans.end(); v++)
168 PushChanMode(v->first, user);
173 virtual ~ModuleOperPrefixMode()
175 ServerInstance->Modes->DelMode(opm);
179 void CleanUser(User* user, bool quitting)
182 std::set<std::string>* ext;
183 if (user->GetExt("m_operprefix",ext))
185 // Don't want to announce -mode when they're quitting anyway..
188 for (UCListIter v = user->chans.begin(); v != user->chans.end(); v++)
190 ModePair ms = opm->ModeSet(NULL, NULL , v->first, user->nick);
193 PushChanMode(v->first, user, true);
199 user->Shrink("m_operprefix");
203 virtual void OnCleanup(int target_type, void* item)
205 if (target_type == TYPE_USER)
207 User* user = (User*)item;
208 CleanUser(user, false);
212 virtual void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
214 CleanUser(user,true);
217 virtual void OnUserKick(User* source, User* user, Channel* chan, const std::string &reason, bool &silent)
219 DelPrefixChan(user, chan);
222 virtual void OnUserPart(User* user, Channel* channel, std::string &partreason, bool &silent)
224 DelPrefixChan(user, channel);
227 virtual Version GetVersion()
229 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
233 MODULE_INIT(ModuleOperPrefixMode)