1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 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 by Chernov-Phoenix Alexey (Phoenix@RusNet) mailto:phoenix /email address separator/ pravmail.ru */
19 static char prefixchar;
21 std::set<std::string>* SetupExt(User* user)
23 std::set<std::string>* ext;
24 if (!user->GetExt("m_operprefix",ext))
26 ext=new std::set<std::string>;
28 user->Extend("m_operprefix",ext);
34 void DelPrefixChan(User* user, Channel* channel)
36 std::set<std::string>* chans = SetupExt(user);
37 chans->erase(channel->name);
41 void AddPrefixChan(User* user, Channel* channel)
43 std::set<std::string>* chans = SetupExt(user);
44 chans->insert(channel->name);
48 class OperPrefixMode : public ModeHandler
51 OperPrefixMode(InspIRCd* Instance) : ModeHandler(Instance, 'y', 1, 1, true, MODETYPE_CHANNEL, false, prefixchar) { }
53 unsigned int GetPrefixRank()
58 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding, bool servermode)
60 if (servermode || (source && ServerInstance->ULine(source->server)))
61 return MODEACTION_ALLOW;
64 if (source && channel)
65 source->WriteNumeric(ERR_CHANOPRIVSNEEDED, "%s %s :Only servers are permitted to change channel mode '%c'", source->nick.c_str(), channel->name.c_str(), 'y');
66 return MODEACTION_DENY;
70 ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string ¶meter)
72 User* x = ServerInstance->FindNick(parameter);
75 if (!channel->HasUser(x))
77 return std::make_pair(false, parameter);
81 std::set<std::string>* ext;
82 if (x->GetExt("m_operprefix",ext))
84 if (ext->find(channel->name)!=ext->end())
86 return std::make_pair(true, x->nick);
89 return std::make_pair(false, parameter);
93 return std::make_pair(false, parameter);
97 return std::make_pair(false, parameter);
100 bool NeedsOper() { return true; }
103 class ModuleOperPrefixMode : public Module
108 ModuleOperPrefixMode(InspIRCd* Me) : Module(Me)
110 ConfigReader Conf(ServerInstance);
112 tmp = Conf.ReadValue("operprefix", "prefix", "!", 0, false);
113 strlcpy(&prefixchar,tmp.c_str(),2);
115 opm = new OperPrefixMode(ServerInstance);
116 if ((!ServerInstance->Modes->AddMode(opm)))
117 throw ModuleException("Could not add a new mode!");
119 Implementation eventlist[] = { I_OnPostJoin, I_OnCleanup, I_OnUserQuit, I_OnUserKick, I_OnUserPart };
120 ServerInstance->Modules->Attach(eventlist, this, 5);
123 virtual void PushChanMode(Channel* channel, User* user, bool negate = false)
126 DelPrefixChan(user, channel);
128 AddPrefixChan(user, channel);
129 char modeline[]="+y";
132 std::vector<std::string> modechange;
133 modechange.push_back(channel->name);
134 modechange.push_back(modeline);
135 modechange.push_back(user->nick);
136 ServerInstance->SendMode(modechange,this->ServerInstance->FakeClient);
139 virtual void OnPostJoin(User *user, Channel *channel)
141 // This may look wrong, but I don't think it is.. PushChanMode will send FMODE which should sort it all out.
145 if (user && IS_OPER(user))
147 if (user->IsModeSet('H'))
149 /* we respect your wish to be invisible */
152 PushChanMode(channel, user);
156 // XXX: is there a better way to do this?
157 virtual int OnRawMode(User* user, Channel* chan, const char mode, const std::string ¶m, bool adding, int pcnt, bool servermode)
159 /* force event propagation to its ModeHandler */
160 if (!servermode && chan && (mode == 'y'))
165 virtual ~ModuleOperPrefixMode()
167 ServerInstance->Modes->DelMode(opm);
171 virtual void CleanUser(User* user, bool quitting=false)
176 std::set<std::string>* ext;
177 if (user->GetExt("m_operprefix",ext))
179 // Don't want to announce -mode when they're quitting anyway..
182 for (UCListIter v = user->chans.begin(); v != user->chans.end(); v++)
184 ModePair ms=opm->ModeSet(NULL, NULL , v->first, user->nick);
187 PushChanMode(v->first, user, true);
193 user->Shrink("m_operprefix");
197 virtual void OnCleanup(int target_type, void* item)
199 if (target_type == TYPE_USER)
201 User* user = (User*)item;
206 virtual void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
208 CleanUser(user,true);
211 virtual void OnUserKick(User* source, User* user, Channel* chan, const std::string &reason, bool &silent)
213 DelPrefixChan(user, chan);
216 virtual void OnUserPart(User* user, Channel* channel, std::string &partreason, bool &silent)
218 DelPrefixChan(user, channel);
221 virtual Version GetVersion()
223 return Version("$Id$", VF_COMMON, API_VERSION);
227 MODULE_INIT(ModuleOperPrefixMode)