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/callerid.h"
28 #include "modules/exemption.h"
29 #include "modules/whois.h"
34 RPL_WHOISREGNICK = 307,
37 RPL_WHOISACCOUNT = 330,
40 ERR_NEEDREGGEDNICK = 477,
42 // From IRCv3 sasl-3.1.
47 /** Channel mode +r - mark a channel as identified
49 class Channel_r : public ModeHandler
52 Channel_r(Module* Creator) : ModeHandler(Creator, "c_registered", 'r', PARAM_NONE, MODETYPE_CHANNEL) { }
54 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE
56 // only a u-lined server may add or remove the +r mode.
57 if (!IS_LOCAL(source))
59 // Only change the mode if it's not redundant
60 if ((adding != channel->IsModeSet(this)))
62 channel->SetMode(this, adding);
63 return MODEACTION_ALLOW;
68 source->WriteNumeric(ERR_NOPRIVILEGES, "Only a server may modify the +r channel mode");
70 return MODEACTION_DENY;
74 /** User mode +r - mark a user as identified
76 class User_r : public ModeHandler
80 User_r(Module* Creator) : ModeHandler(Creator, "u_registered", 'r', PARAM_NONE, MODETYPE_USER) { }
82 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE
84 if (!IS_LOCAL(source))
86 if ((adding != dest->IsModeSet(this)))
88 dest->SetMode(this, adding);
89 return MODEACTION_ALLOW;
94 source->WriteNumeric(ERR_NOPRIVILEGES, "Only a server may modify the +r user mode");
96 return MODEACTION_DENY;
100 class AccountExtItemImpl : public AccountExtItem
102 Events::ModuleEventProvider eventprov;
105 AccountExtItemImpl(Module* mod)
106 : AccountExtItem("accountname", ExtensionItem::EXT_USER, mod)
107 , eventprov(mod, "event/account")
111 void unserialize(SerializeFormat format, Extensible* container, const std::string& value) CXX11_OVERRIDE
113 User* user = static_cast<User*>(container);
115 StringExtItem::unserialize(format, container, value);
117 // If we are being reloaded then don't send the numeric or run the event
118 if (format == FORMAT_INTERNAL)
126 user->WriteNumeric(RPL_LOGGEDOUT, user->GetFullHost(), "You are now logged out");
131 user->WriteNumeric(RPL_LOGGEDIN, user->GetFullHost(), value, InspIRCd::Format("You are now logged in as %s", value.c_str()));
135 FOREACH_MOD_CUSTOM(eventprov, AccountEventListener, OnAccountChange, (user, value));
139 class ModuleServicesAccount : public Module, public Whois::EventListener
142 CallerID::API calleridapi;
143 CheckExemption::EventProvider exemptionprov;
144 SimpleChannelModeHandler m1;
145 SimpleChannelModeHandler m2;
146 SimpleUserModeHandler m3;
149 AccountExtItemImpl accountname;
153 ModuleServicesAccount()
154 : Whois::EventListener(this)
156 , exemptionprov(this)
157 , m1(this, "reginvite", 'R')
158 , m2(this, "regmoderated", 'M')
159 , m3(this, "regdeaf", 'R')
163 , checking_ban(false)
167 void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
169 tokens["EXTBAN"].push_back('R');
170 tokens["EXTBAN"].push_back('U');
173 /* <- :twisted.oscnet.org 330 w00t2 w00t2 w00t :is logged in as */
174 void OnWhois(Whois::Context& whois) CXX11_OVERRIDE
176 std::string* account = accountname.get(whois.GetTarget());
180 whois.SendLine(RPL_WHOISACCOUNT, *account, "is logged in as");
183 if (whois.GetTarget()->IsModeSet(m5))
185 /* user is registered */
186 whois.SendLine(RPL_WHOISREGNICK, "is a registered nick");
190 void OnUserPostNick(User* user, const std::string &oldnick) CXX11_OVERRIDE
192 /* On nickchange, if they have +r, remove it */
193 if ((user->IsModeSet(m5)) && (ServerInstance->FindNickOnly(oldnick) != user))
197 ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
200 return MOD_RES_PASSTHRU;
202 std::string *account = accountname.get(user);
203 bool is_registered = account && !account->empty();
205 if (target.type == MessageTarget::TYPE_CHANNEL)
207 Channel* targchan = target.Get<Channel>();
209 if (!targchan->IsModeSet(m2) || is_registered)
210 return MOD_RES_PASSTHRU;
212 if (CheckExemption::Call(exemptionprov, user, targchan, "regmoderated") == MOD_RES_ALLOW)
213 return MOD_RES_PASSTHRU;
215 // User is messaging a +M channel and is not registered or exempt.
216 user->WriteNumeric(ERR_NEEDREGGEDNICK, targchan->name, "You need to be identified to a registered account to message this channel");
219 else if (target.type == MessageTarget::TYPE_USER)
221 User* targuser = target.Get<User>();
222 if (!targuser->IsModeSet(m3) || is_registered)
223 return MOD_RES_PASSTHRU;
225 if (calleridapi && calleridapi->IsOnAcceptList(user, targuser))
226 return MOD_RES_PASSTHRU;
228 // User is messaging a +R user and is not registered or on an accept list.
229 user->WriteNumeric(ERR_NEEDREGGEDNICK, targuser->nick, "You need to be identified to a registered account to message this user");
232 return MOD_RES_PASSTHRU;
235 ModResult OnCheckBan(User* user, Channel* chan, const std::string& mask) CXX11_OVERRIDE
238 return MOD_RES_PASSTHRU;
240 if ((mask.length() > 2) && (mask[1] == ':'))
244 std::string *account = accountname.get(user);
245 if (account && InspIRCd::Match(*account, mask.substr(2)))
248 else if (mask[0] == 'U')
250 std::string *account = accountname.get(user);
251 /* If the user is registered we don't care. */
253 return MOD_RES_PASSTHRU;
255 /* If we made it this far we know the user isn't registered
256 so just deny if it matches */
258 bool result = chan->CheckBan(user, mask.substr(2));
259 checking_ban = false;
266 /* If we made it this far then the ban wasn't an ExtBan
267 or the user we were checking for didn't match either ExtBan */
268 return MOD_RES_PASSTHRU;
271 ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
273 std::string *account = accountname.get(user);
274 bool is_registered = account && !account->empty();
278 if (chan->IsModeSet(m1))
282 // joining a +R channel and not identified
283 user->WriteNumeric(ERR_NEEDREGGEDNICK, chan->name, "You need to be identified to a registered account to join this channel");
288 return MOD_RES_PASSTHRU;
291 ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass) CXX11_OVERRIDE
293 if (myclass->config->getBool("requireaccount") && !accountname.get(user))
295 return MOD_RES_PASSTHRU;
298 Version GetVersion() CXX11_OVERRIDE
300 return Version("Provides support for ircu-style services accounts, including chmode +R, etc.",VF_OPTCOMMON|VF_VENDOR);
304 MODULE_INIT(ModuleServicesAccount)