1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/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 static char prefixchar;
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) : ModeHandler(Instance, 'y', 1, 1, true, MODETYPE_CHANNEL, false, prefixchar) { }
56 unsigned int GetPrefixRank()
61 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding, bool servermode)
63 if (servermode || (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);
115 tmp = Conf.ReadValue("operprefix", "prefix", "!", 0, false);
116 strlcpy(&prefixchar,tmp.c_str(),2);
118 opm = new OperPrefixMode(ServerInstance);
119 if ((!ServerInstance->Modes->AddMode(opm)))
120 throw ModuleException("Could not add a new mode!");
122 Implementation eventlist[] = { I_OnPostJoin, I_OnCleanup, I_OnUserQuit, I_OnUserKick, I_OnUserPart, I_OnOper };
123 ServerInstance->Modules->Attach(eventlist, this, 6);
126 virtual void PushChanMode(Channel* channel, User* user, bool negate = false)
129 DelPrefixChan(user, channel);
131 AddPrefixChan(user, channel);
132 char modeline[]="+y";
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);
142 virtual void OnPostJoin(User *user, Channel *channel)
144 // This may look wrong, but I don't think it is.. PushChanMode will send FMODE which should sort it all out.
148 if (user && IS_OPER(user))
150 if (user->IsModeSet('H'))
152 /* we respect your wish to be invisible */
155 PushChanMode(channel, user);
159 // XXX: is there a better way to do this?
160 virtual int OnRawMode(User* user, Channel* chan, const char mode, const std::string ¶m, bool adding, int pcnt, bool servermode)
162 /* force event propagation to its ModeHandler */
163 if (!servermode && chan && (mode == 'y'))
168 virtual void OnOper(User *user, const std::string&)
170 if (user && IS_LOCAL(user) && !user->IsModeSet('H'))
172 for (UCListIter v = user->chans.begin(); v != user->chans.end(); v++)
174 PushChanMode(v->first, user);
179 virtual ~ModuleOperPrefixMode()
181 ServerInstance->Modes->DelMode(opm);
185 virtual void CleanUser(User* user, bool quitting=false)
190 std::set<std::string>* ext;
191 if (user->GetExt("m_operprefix",ext))
193 // Don't want to announce -mode when they're quitting anyway..
196 for (UCListIter v = user->chans.begin(); v != user->chans.end(); v++)
198 ModePair ms=opm->ModeSet(NULL, NULL , v->first, user->nick);
201 PushChanMode(v->first, user, true);
207 user->Shrink("m_operprefix");
211 virtual void OnCleanup(int target_type, void* item)
213 if (target_type == TYPE_USER)
215 User* user = (User*)item;
220 virtual void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
222 CleanUser(user,true);
225 virtual void OnUserKick(User* source, User* user, Channel* chan, const std::string &reason, bool &silent)
227 DelPrefixChan(user, chan);
230 virtual void OnUserPart(User* user, Channel* channel, std::string &partreason, bool &silent)
232 DelPrefixChan(user, channel);
235 virtual Version GetVersion()
237 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
241 MODULE_INIT(ModuleOperPrefixMode)