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 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding, bool servermode)
55 if (servermode || (source && ServerInstance->ULine(source->server)))
56 return MODEACTION_ALLOW;
59 if (source && channel)
60 source->WriteNumeric(ERR_CHANOPRIVSNEEDED, "%s %s :Only servers are permitted to change channel mode '%c'", source->nick.c_str(), channel->name.c_str(), 'y');
61 return MODEACTION_DENY;
65 ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string ¶meter)
67 User* x = ServerInstance->FindNick(parameter);
70 if (!channel->HasUser(x))
72 return std::make_pair(false, parameter);
76 std::set<std::string>* ext;
77 if (x->GetExt("m_operprefix",ext))
79 if (ext->find(channel->name)!=ext->end())
81 return std::make_pair(true, x->nick);
84 return std::make_pair(false, parameter);
88 return std::make_pair(false, parameter);
92 return std::make_pair(false, parameter);
95 bool NeedsOper() { return true; }
98 class ModuleOperPrefixMode : public Module
103 ModuleOperPrefixMode(InspIRCd* Me) : Module(Me)
105 ConfigReader Conf(ServerInstance);
107 tmp = Conf.ReadValue("operprefix", "prefix", "!", 0, false);
108 strlcpy(&prefixchar,tmp.c_str(),2);
110 opm = new OperPrefixMode(ServerInstance);
111 if ((!ServerInstance->Modes->AddMode(opm)))
112 throw ModuleException("Could not add a new mode!");
114 Implementation eventlist[] = { I_OnPostJoin, I_OnCleanup, I_OnUserQuit, I_OnUserKick, I_OnUserPart };
115 ServerInstance->Modules->Attach(eventlist, this, 6);
118 virtual void PushChanMode(Channel* channel, User* user, bool negate = false)
121 DelPrefixChan(user, channel);
123 AddPrefixChan(user, channel);
124 char modeline[]="+y";
127 std::vector<std::string> modechange;
128 modechange.push_back(channel->name);
129 modechange.push_back(modeline);
130 modechange.push_back(user->nick);
131 ServerInstance->SendMode(modechange,this->ServerInstance->FakeClient);
134 virtual void OnPostJoin(User *user, Channel *channel)
136 // This may look wrong, but I don't think it is.. PushChanMode will send FMODE which should sort it all out.
140 if (user && IS_OPER(user))
142 if (user->IsModeSet('H'))
144 /* we respect your wish to be invisible */
147 PushChanMode(channel, user);
151 // XXX: is there a better way to do this?
152 virtual int OnRawMode(User* user, Channel* chan, const char mode, const std::string ¶m, bool adding, int pcnt, bool servermode)
154 /* force event propagation to its ModeHandler */
155 if (!servermode && chan && (mode == 'y'))
160 virtual ~ModuleOperPrefixMode()
162 ServerInstance->Modes->DelMode(opm);
166 virtual void CleanUser(User* user, bool quitting=false)
171 std::set<std::string>* ext;
172 if (user->GetExt("m_operprefix",ext))
174 // Don't want to announce -mode when they're quitting anyway..
177 for (UCListIter v = user->chans.begin(); v != user->chans.end(); v++)
179 ModePair ms=opm->ModeSet(NULL, NULL , v->first, user->nick);
182 PushChanMode(v->first, user, true);
188 user->Shrink("m_operprefix");
192 virtual void OnCleanup(int target_type, void* item)
194 if (target_type == TYPE_USER)
196 User* user = (User*)item;
201 virtual void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
203 CleanUser(user,true);
206 virtual void OnUserKick(User* source, User* user, Channel* chan, const std::string &reason, bool &silent)
208 DelPrefixChan(user, chan);
211 virtual void OnUserPart(User* user, Channel* channel, std::string &partreason, bool &silent)
213 DelPrefixChan(user, channel);
216 virtual Version GetVersion()
218 return Version("$Id$", VF_COMMON, API_VERSION);
222 MODULE_INIT(ModuleOperPrefixMode)