2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2012 Shawn Smith <shawn@inspircd.org>
5 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
6 * Copyright (C) 2006-2008 Robin Burchell <robin+git@viroteck.net>
7 * Copyright (C) 2008 Pippijn van Steenhoven <pip88nl@gmail.com>
8 * Copyright (C) 2006, 2008 Craig Edwards <craigedwards@brainbox.cc>
9 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
11 * This file is part of InspIRCd. InspIRCd is free software: you can
12 * redistribute it and/or modify it under the terms of the GNU General Public
13 * License as published by the Free Software Foundation, version 2.
15 * This program is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "modules/account.h"
27 #include "modules/exemption.h"
32 ERR_NEEDREGGEDNICK = 477,
34 // From IRCv3 sasl-3.1.
39 /** Channel mode +r - mark a channel as identified
41 class Channel_r : public ModeHandler
44 Channel_r(Module* Creator) : ModeHandler(Creator, "c_registered", 'r', PARAM_NONE, MODETYPE_CHANNEL) { }
46 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
48 // only a u-lined server may add or remove the +r mode.
49 if (!IS_LOCAL(source))
51 // Only change the mode if it's not redundant
52 if ((adding != channel->IsModeSet(this)))
54 channel->SetMode(this, adding);
55 return MODEACTION_ALLOW;
60 source->WriteNumeric(500, "Only a server may modify the +r channel mode");
62 return MODEACTION_DENY;
66 /** User mode +r - mark a user as identified
68 class User_r : public ModeHandler
72 User_r(Module* Creator) : ModeHandler(Creator, "u_registered", 'r', PARAM_NONE, MODETYPE_USER) { }
74 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
76 if (!IS_LOCAL(source))
78 if ((adding != dest->IsModeSet(this)))
80 dest->SetMode(this, adding);
81 return MODEACTION_ALLOW;
86 source->WriteNumeric(500, "Only a server may modify the +r user mode");
88 return MODEACTION_DENY;
92 class AccountExtItemImpl : public AccountExtItem
94 Events::ModuleEventProvider eventprov;
97 AccountExtItemImpl(Module* mod)
98 : AccountExtItem("accountname", ExtensionItem::EXT_USER, mod)
99 , eventprov(mod, "event/account")
103 void unserialize(SerializeFormat format, Extensible* container, const std::string& value)
105 User* user = static_cast<User*>(container);
107 StringExtItem::unserialize(format, container, value);
109 // If we are being reloaded then don't send the numeric or run the event
110 if (format == FORMAT_INTERNAL)
118 user->WriteNumeric(RPL_LOGGEDOUT, user->GetFullHost(), "You are now logged out");
123 user->WriteNumeric(RPL_LOGGEDIN, user->GetFullHost(), value, InspIRCd::Format("You are now logged in as %s", value.c_str()));
127 FOREACH_MOD_CUSTOM(eventprov, AccountEventListener, OnAccountChange, (user, value));
131 class ModuleServicesAccount : public Module, public Whois::EventListener
133 CheckExemption::EventProvider exemptionprov;
134 SimpleChannelModeHandler m1;
135 SimpleChannelModeHandler m2;
136 SimpleUserModeHandler m3;
139 AccountExtItemImpl accountname;
142 ModuleServicesAccount()
143 : Whois::EventListener(this)
144 , exemptionprov(this)
145 , m1(this, "reginvite", 'R')
146 , m2(this, "regmoderated", 'M')
147 , m3(this, "regdeaf", 'R')
151 , checking_ban(false)
155 void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
157 tokens["EXTBAN"].push_back('R');
158 tokens["EXTBAN"].push_back('U');
161 /* <- :twisted.oscnet.org 330 w00t2 w00t2 w00t :is logged in as */
162 void OnWhois(Whois::Context& whois) CXX11_OVERRIDE
164 std::string* account = accountname.get(whois.GetTarget());
168 whois.SendLine(330, *account, "is logged in as");
171 if (whois.GetTarget()->IsModeSet(m5))
173 /* user is registered */
174 whois.SendLine(307, "is a registered nick");
178 void OnUserPostNick(User* user, const std::string &oldnick) CXX11_OVERRIDE
180 /* On nickchange, if they have +r, remove it */
181 if ((user->IsModeSet(m5)) && (ServerInstance->FindNickOnly(oldnick) != user))
185 ModResult OnUserPreMessage(User* user, void* dest, int target_type, std::string& text, char status, CUList& exempt_list, MessageType msgtype) CXX11_OVERRIDE
188 return MOD_RES_PASSTHRU;
190 std::string *account = accountname.get(user);
191 bool is_registered = account && !account->empty();
193 if (target_type == TYPE_CHANNEL)
195 Channel* c = (Channel*)dest;
196 ModResult res = CheckExemption::Call(exemptionprov, user, c, "regmoderated");
198 if (c->IsModeSet(m2) && !is_registered && res != MOD_RES_ALLOW)
200 // user messaging a +M channel and is not registered
201 user->WriteNumeric(ERR_NEEDREGGEDNICK, c->name, "You need to be identified to a registered account to message this channel");
205 else if (target_type == TYPE_USER)
207 User* u = (User*)dest;
209 if (u->IsModeSet(m3) && !is_registered)
211 // user messaging a +R user and is not registered
212 user->WriteNumeric(ERR_NEEDREGGEDNICK, u->nick, "You need to be identified to a registered account to message this user");
216 return MOD_RES_PASSTHRU;
219 ModResult OnCheckBan(User* user, Channel* chan, const std::string& mask) CXX11_OVERRIDE
222 return MOD_RES_PASSTHRU;
224 if ((mask.length() > 2) && (mask[1] == ':'))
228 std::string *account = accountname.get(user);
229 if (account && InspIRCd::Match(*account, mask.substr(2)))
232 else if (mask[0] == 'U')
234 std::string *account = accountname.get(user);
235 /* If the user is registered we don't care. */
237 return MOD_RES_PASSTHRU;
239 /* If we made it this far we know the user isn't registered
240 so just deny if it matches */
242 bool result = chan->CheckBan(user, mask.substr(2));
243 checking_ban = false;
250 /* If we made it this far then the ban wasn't an ExtBan
251 or the user we were checking for didn't match either ExtBan */
252 return MOD_RES_PASSTHRU;
255 ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
257 std::string *account = accountname.get(user);
258 bool is_registered = account && !account->empty();
262 if (chan->IsModeSet(m1))
266 // joining a +R channel and not identified
267 user->WriteNumeric(ERR_NEEDREGGEDNICK, chan->name, "You need to be identified to a registered account to join this channel");
272 return MOD_RES_PASSTHRU;
275 ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass) CXX11_OVERRIDE
277 if (myclass->config->getBool("requireaccount") && !accountname.get(user))
279 return MOD_RES_PASSTHRU;
282 Version GetVersion() CXX11_OVERRIDE
284 return Version("Provides support for ircu-style services accounts, including chmode +R, etc.",VF_OPTCOMMON|VF_VENDOR);
288 MODULE_INIT(ModuleServicesAccount)