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 RPL_WHOISREGNICK = 307,
35 RPL_WHOISACCOUNT = 330,
38 ERR_NEEDREGGEDNICK = 477,
40 // From IRCv3 sasl-3.1.
45 /** Channel mode +r - mark a channel as identified
47 class Channel_r : public ModeHandler
50 Channel_r(Module* Creator) : ModeHandler(Creator, "c_registered", 'r', PARAM_NONE, MODETYPE_CHANNEL) { }
52 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE
54 // only a u-lined server may add or remove the +r mode.
55 if (!IS_LOCAL(source))
57 // Only change the mode if it's not redundant
58 if ((adding != channel->IsModeSet(this)))
60 channel->SetMode(this, adding);
61 return MODEACTION_ALLOW;
66 source->WriteNumeric(500, "Only a server may modify the +r channel mode");
68 return MODEACTION_DENY;
72 /** User mode +r - mark a user as identified
74 class User_r : public ModeHandler
78 User_r(Module* Creator) : ModeHandler(Creator, "u_registered", 'r', PARAM_NONE, MODETYPE_USER) { }
80 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE
82 if (!IS_LOCAL(source))
84 if ((adding != dest->IsModeSet(this)))
86 dest->SetMode(this, adding);
87 return MODEACTION_ALLOW;
92 source->WriteNumeric(500, "Only a server may modify the +r user mode");
94 return MODEACTION_DENY;
98 class AccountExtItemImpl : public AccountExtItem
100 Events::ModuleEventProvider eventprov;
103 AccountExtItemImpl(Module* mod)
104 : AccountExtItem("accountname", ExtensionItem::EXT_USER, mod)
105 , eventprov(mod, "event/account")
109 void unserialize(SerializeFormat format, Extensible* container, const std::string& value) CXX11_OVERRIDE
111 User* user = static_cast<User*>(container);
113 StringExtItem::unserialize(format, container, value);
115 // If we are being reloaded then don't send the numeric or run the event
116 if (format == FORMAT_INTERNAL)
124 user->WriteNumeric(RPL_LOGGEDOUT, user->GetFullHost(), "You are now logged out");
129 user->WriteNumeric(RPL_LOGGEDIN, user->GetFullHost(), value, InspIRCd::Format("You are now logged in as %s", value.c_str()));
133 FOREACH_MOD_CUSTOM(eventprov, AccountEventListener, OnAccountChange, (user, value));
137 class ModuleServicesAccount : public Module, public Whois::EventListener
139 CheckExemption::EventProvider exemptionprov;
140 SimpleChannelModeHandler m1;
141 SimpleChannelModeHandler m2;
142 SimpleUserModeHandler m3;
145 AccountExtItemImpl accountname;
148 ModuleServicesAccount()
149 : Whois::EventListener(this)
150 , exemptionprov(this)
151 , m1(this, "reginvite", 'R')
152 , m2(this, "regmoderated", 'M')
153 , m3(this, "regdeaf", 'R')
157 , checking_ban(false)
161 void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
163 tokens["EXTBAN"].push_back('R');
164 tokens["EXTBAN"].push_back('U');
167 /* <- :twisted.oscnet.org 330 w00t2 w00t2 w00t :is logged in as */
168 void OnWhois(Whois::Context& whois) CXX11_OVERRIDE
170 std::string* account = accountname.get(whois.GetTarget());
174 whois.SendLine(RPL_WHOISACCOUNT, *account, "is logged in as");
177 if (whois.GetTarget()->IsModeSet(m5))
179 /* user is registered */
180 whois.SendLine(RPL_WHOISREGNICK, "is a registered nick");
184 void OnUserPostNick(User* user, const std::string &oldnick) CXX11_OVERRIDE
186 /* On nickchange, if they have +r, remove it */
187 if ((user->IsModeSet(m5)) && (ServerInstance->FindNickOnly(oldnick) != user))
191 ModResult OnUserPreMessage(User* user, void* dest, int target_type, std::string& text, char status, CUList& exempt_list, MessageType msgtype) CXX11_OVERRIDE
194 return MOD_RES_PASSTHRU;
196 std::string *account = accountname.get(user);
197 bool is_registered = account && !account->empty();
199 if (target_type == TYPE_CHANNEL)
201 Channel* c = (Channel*)dest;
202 ModResult res = CheckExemption::Call(exemptionprov, user, c, "regmoderated");
204 if (c->IsModeSet(m2) && !is_registered && res != MOD_RES_ALLOW)
206 // user messaging a +M channel and is not registered
207 user->WriteNumeric(ERR_NEEDREGGEDNICK, c->name, "You need to be identified to a registered account to message this channel");
211 else if (target_type == TYPE_USER)
213 User* u = (User*)dest;
215 if (u->IsModeSet(m3) && !is_registered)
217 // user messaging a +R user and is not registered
218 user->WriteNumeric(ERR_NEEDREGGEDNICK, u->nick, "You need to be identified to a registered account to message this user");
222 return MOD_RES_PASSTHRU;
225 ModResult OnCheckBan(User* user, Channel* chan, const std::string& mask) CXX11_OVERRIDE
228 return MOD_RES_PASSTHRU;
230 if ((mask.length() > 2) && (mask[1] == ':'))
234 std::string *account = accountname.get(user);
235 if (account && InspIRCd::Match(*account, mask.substr(2)))
238 else if (mask[0] == 'U')
240 std::string *account = accountname.get(user);
241 /* If the user is registered we don't care. */
243 return MOD_RES_PASSTHRU;
245 /* If we made it this far we know the user isn't registered
246 so just deny if it matches */
248 bool result = chan->CheckBan(user, mask.substr(2));
249 checking_ban = false;
256 /* If we made it this far then the ban wasn't an ExtBan
257 or the user we were checking for didn't match either ExtBan */
258 return MOD_RES_PASSTHRU;
261 ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
263 std::string *account = accountname.get(user);
264 bool is_registered = account && !account->empty();
268 if (chan->IsModeSet(m1))
272 // joining a +R channel and not identified
273 user->WriteNumeric(ERR_NEEDREGGEDNICK, chan->name, "You need to be identified to a registered account to join this channel");
278 return MOD_RES_PASSTHRU;
281 ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass) CXX11_OVERRIDE
283 if (myclass->config->getBool("requireaccount") && !accountname.get(user))
285 return MOD_RES_PASSTHRU;
288 Version GetVersion() CXX11_OVERRIDE
290 return Version("Provides support for ircu-style services accounts, including chmode +R, etc.",VF_OPTCOMMON|VF_VENDOR);
294 MODULE_INIT(ModuleServicesAccount)