2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2019 linuxdaemon <linuxdaemon.irc@gmail.com>
5 * Copyright (C) 2013, 2017-2020 Sadie Powell <sadie@witchery.services>
6 * Copyright (C) 2012-2015 Attila Molnar <attilamolnar@hush.com>
7 * Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
8 * Copyright (C) 2012 Shawn Smith <ShawnSmith0828@gmail.com>
9 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
10 * Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
11 * Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
12 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
13 * Copyright (C) 2006, 2008 Craig Edwards <brain@inspircd.org>
15 * This file is part of InspIRCd. InspIRCd is free software: you can
16 * redistribute it and/or modify it under the terms of the GNU General Public
17 * License as published by the Free Software Foundation, version 2.
19 * This program is distributed in the hope that it will be useful, but WITHOUT
20 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
21 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
24 * You should have received a copy of the GNU General Public License
25 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 #include "modules/account.h"
31 #include "modules/callerid.h"
32 #include "modules/ctctags.h"
33 #include "modules/exemption.h"
34 #include "modules/whois.h"
39 RPL_WHOISREGNICK = 307,
42 RPL_WHOISACCOUNT = 330,
45 ERR_NEEDREGGEDNICK = 477,
47 // From IRCv3 sasl-3.1.
52 /** Channel mode +r - mark a channel as identified
54 class Channel_r : public ModeHandler
57 Channel_r(Module* Creator) : ModeHandler(Creator, "c_registered", 'r', PARAM_NONE, MODETYPE_CHANNEL) { }
59 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE
61 // Only a U-lined server may add or remove the +r mode.
62 if (!IS_LOCAL(source))
64 // Only change the mode if it's not redundant
65 if ((adding != channel->IsModeSet(this)))
67 channel->SetMode(this, adding);
68 return MODEACTION_ALLOW;
73 source->WriteNumeric(ERR_NOPRIVILEGES, "Only a server may modify the +r channel mode");
75 return MODEACTION_DENY;
79 /** User mode +r - mark a user as identified
81 class User_r : public ModeHandler
85 User_r(Module* Creator) : ModeHandler(Creator, "u_registered", 'r', PARAM_NONE, MODETYPE_USER) { }
87 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE
89 if (!IS_LOCAL(source))
91 if ((adding != dest->IsModeSet(this)))
93 dest->SetMode(this, adding);
94 return MODEACTION_ALLOW;
99 source->WriteNumeric(ERR_NOPRIVILEGES, "Only a server may modify the +r user mode");
101 return MODEACTION_DENY;
105 class AccountExtItemImpl : public AccountExtItem
107 Events::ModuleEventProvider eventprov;
110 AccountExtItemImpl(Module* mod)
111 : AccountExtItem("accountname", ExtensionItem::EXT_USER, mod)
112 , eventprov(mod, "event/account")
116 void FromInternal(Extensible* container, const std::string& value) CXX11_OVERRIDE
118 StringExtItem::FromInternal(container, value);
121 void FromNetwork(Extensible* container, const std::string& value) CXX11_OVERRIDE
123 StringExtItem::FromNetwork(container, value);
125 User* user = static_cast<User*>(container);
131 user->WriteNumeric(RPL_LOGGEDOUT, user->GetFullHost(), "You are now logged out");
136 user->WriteNumeric(RPL_LOGGEDIN, user->GetFullHost(), value, InspIRCd::Format("You are now logged in as %s", value.c_str()));
140 FOREACH_MOD_CUSTOM(eventprov, AccountEventListener, OnAccountChange, (user, value));
144 class ModuleServicesAccount
146 , public Whois::EventListener
147 , public CTCTags::EventListener
150 CallerID::API calleridapi;
151 CheckExemption::EventProvider exemptionprov;
152 SimpleChannelModeHandler reginvitemode;
153 SimpleChannelModeHandler regmoderatedmode;
154 SimpleUserModeHandler regdeafmode;
155 Channel_r chanregmode;
157 AccountExtItemImpl accountname;
161 ModuleServicesAccount()
162 : Whois::EventListener(this)
163 , CTCTags::EventListener(this)
165 , exemptionprov(this)
166 , reginvitemode(this, "reginvite", 'R')
167 , regmoderatedmode(this, "regmoderated", 'M')
168 , regdeafmode(this, "regdeaf", 'R')
172 , checking_ban(false)
176 void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
178 tokens["EXTBAN"].push_back('R');
179 tokens["EXTBAN"].push_back('U');
182 /* <- :twisted.oscnet.org 330 w00t2 w00t2 w00t :is logged in as */
183 void OnWhois(Whois::Context& whois) CXX11_OVERRIDE
185 std::string* account = accountname.get(whois.GetTarget());
189 whois.SendLine(RPL_WHOISACCOUNT, *account, "is logged in as");
192 if (whois.GetTarget()->IsModeSet(userregmode))
194 /* user is registered */
195 whois.SendLine(RPL_WHOISREGNICK, "is a registered nick");
199 void OnUserPostNick(User* user, const std::string &oldnick) CXX11_OVERRIDE
201 /* On nickchange, if they have +r, remove it */
202 if ((user->IsModeSet(userregmode)) && (ServerInstance->FindNickOnly(oldnick) != user))
203 userregmode.RemoveMode(user);
206 ModResult HandleMessage(User* user, const MessageTarget& target)
209 return MOD_RES_PASSTHRU;
211 std::string *account = accountname.get(user);
212 bool is_registered = account && !account->empty();
216 case MessageTarget::TYPE_CHANNEL:
218 Channel* targchan = target.Get<Channel>();
220 if (!targchan->IsModeSet(regmoderatedmode) || is_registered)
221 return MOD_RES_PASSTHRU;
223 if (CheckExemption::Call(exemptionprov, user, targchan, "regmoderated") == MOD_RES_ALLOW)
224 return MOD_RES_PASSTHRU;
226 // User is messaging a +M channel and is not registered or exempt.
227 user->WriteNumeric(ERR_NEEDREGGEDNICK, targchan->name, "You need to be identified to a registered account to message this channel");
231 case MessageTarget::TYPE_USER:
233 User* targuser = target.Get<User>();
234 if (!targuser->IsModeSet(regdeafmode) || is_registered)
235 return MOD_RES_PASSTHRU;
237 if (calleridapi && calleridapi->IsOnAcceptList(user, targuser))
238 return MOD_RES_PASSTHRU;
240 // User is messaging a +R user and is not registered or on an accept list.
241 user->WriteNumeric(ERR_NEEDREGGEDNICK, targuser->nick, "You need to be identified to a registered account to message this user");
245 case MessageTarget::TYPE_SERVER:
248 return MOD_RES_PASSTHRU;
251 ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
253 return HandleMessage(user, target);
256 ModResult OnUserPreTagMessage(User* user, const MessageTarget& target, CTCTags::TagMessageDetails& details) CXX11_OVERRIDE
258 return HandleMessage(user, target);
261 ModResult OnCheckBan(User* user, Channel* chan, const std::string& mask) CXX11_OVERRIDE
264 return MOD_RES_PASSTHRU;
266 if ((mask.length() > 2) && (mask[1] == ':'))
270 std::string *account = accountname.get(user);
271 if (account && InspIRCd::Match(*account, mask.substr(2)))
274 else if (mask[0] == 'U')
276 std::string *account = accountname.get(user);
277 /* If the user is registered we don't care. */
279 return MOD_RES_PASSTHRU;
281 /* If we made it this far we know the user isn't registered
282 so just deny if it matches */
284 bool result = chan->CheckBan(user, mask.substr(2));
285 checking_ban = false;
292 /* If we made it this far then the ban wasn't an ExtBan
293 or the user we were checking for didn't match either ExtBan */
294 return MOD_RES_PASSTHRU;
297 ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
299 std::string *account = accountname.get(user);
300 bool is_registered = account && !account->empty();
304 if (chan->IsModeSet(reginvitemode))
308 // joining a +R channel and not identified
309 user->WriteNumeric(ERR_NEEDREGGEDNICK, chan->name, "You need to be identified to a registered account to join this channel");
314 return MOD_RES_PASSTHRU;
317 ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass) CXX11_OVERRIDE
319 if (myclass->config->getBool("requireaccount") && !accountname.get(user))
321 ServerInstance->Logs->Log("CONNECTCLASS", LOG_DEBUG, "The %s connect class is not suitable as it requires the user to be logged into an account",
322 myclass->GetName().c_str());
325 return MOD_RES_PASSTHRU;
328 Version GetVersion() CXX11_OVERRIDE
330 return Version("Adds various channel and user modes relating to services accounts.", VF_OPTCOMMON|VF_VENDOR);
334 MODULE_INIT(ModuleServicesAccount)