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/ctctags.h"
29 #include "modules/exemption.h"
30 #include "modules/whois.h"
35 RPL_WHOISREGNICK = 307,
38 RPL_WHOISACCOUNT = 330,
41 ERR_NEEDREGGEDNICK = 477,
43 // From IRCv3 sasl-3.1.
48 /** Channel mode +r - mark a channel as identified
50 class Channel_r : public ModeHandler
53 Channel_r(Module* Creator) : ModeHandler(Creator, "c_registered", 'r', PARAM_NONE, MODETYPE_CHANNEL) { }
55 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE
57 // Only a U-lined server may add or remove the +r mode.
58 if (!IS_LOCAL(source))
60 // Only change the mode if it's not redundant
61 if ((adding != channel->IsModeSet(this)))
63 channel->SetMode(this, adding);
64 return MODEACTION_ALLOW;
69 source->WriteNumeric(ERR_NOPRIVILEGES, "Only a server may modify the +r channel mode");
71 return MODEACTION_DENY;
75 /** User mode +r - mark a user as identified
77 class User_r : public ModeHandler
81 User_r(Module* Creator) : ModeHandler(Creator, "u_registered", 'r', PARAM_NONE, MODETYPE_USER) { }
83 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE
85 if (!IS_LOCAL(source))
87 if ((adding != dest->IsModeSet(this)))
89 dest->SetMode(this, adding);
90 return MODEACTION_ALLOW;
95 source->WriteNumeric(ERR_NOPRIVILEGES, "Only a server may modify the +r user mode");
97 return MODEACTION_DENY;
101 class AccountExtItemImpl : public AccountExtItem
103 Events::ModuleEventProvider eventprov;
106 AccountExtItemImpl(Module* mod)
107 : AccountExtItem("accountname", ExtensionItem::EXT_USER, mod)
108 , eventprov(mod, "event/account")
112 void unserialize(SerializeFormat format, Extensible* container, const std::string& value) CXX11_OVERRIDE
114 User* user = static_cast<User*>(container);
116 StringExtItem::unserialize(format, container, value);
118 // If we are being reloaded then don't send the numeric or run the event
119 if (format == FORMAT_INTERNAL)
127 user->WriteNumeric(RPL_LOGGEDOUT, user->GetFullHost(), "You are now logged out");
132 user->WriteNumeric(RPL_LOGGEDIN, user->GetFullHost(), value, InspIRCd::Format("You are now logged in as %s", value.c_str()));
136 FOREACH_MOD_CUSTOM(eventprov, AccountEventListener, OnAccountChange, (user, value));
140 class ModuleServicesAccount
142 , public Whois::EventListener
143 , public CTCTags::EventListener
146 CallerID::API calleridapi;
147 CheckExemption::EventProvider exemptionprov;
148 SimpleChannelModeHandler m1;
149 SimpleChannelModeHandler m2;
150 SimpleUserModeHandler m3;
153 AccountExtItemImpl accountname;
157 ModuleServicesAccount()
158 : Whois::EventListener(this)
159 , CTCTags::EventListener(this)
161 , exemptionprov(this)
162 , m1(this, "reginvite", 'R')
163 , m2(this, "regmoderated", 'M')
164 , m3(this, "regdeaf", 'R')
168 , checking_ban(false)
172 void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
174 tokens["EXTBAN"].push_back('R');
175 tokens["EXTBAN"].push_back('U');
178 /* <- :twisted.oscnet.org 330 w00t2 w00t2 w00t :is logged in as */
179 void OnWhois(Whois::Context& whois) CXX11_OVERRIDE
181 std::string* account = accountname.get(whois.GetTarget());
185 whois.SendLine(RPL_WHOISACCOUNT, *account, "is logged in as");
188 if (whois.GetTarget()->IsModeSet(m5))
190 /* user is registered */
191 whois.SendLine(RPL_WHOISREGNICK, "is a registered nick");
195 void OnUserPostNick(User* user, const std::string &oldnick) CXX11_OVERRIDE
197 /* On nickchange, if they have +r, remove it */
198 if ((user->IsModeSet(m5)) && (ServerInstance->FindNickOnly(oldnick) != user))
202 ModResult HandleMessage(User* user, const MessageTarget& target)
205 return MOD_RES_PASSTHRU;
207 std::string *account = accountname.get(user);
208 bool is_registered = account && !account->empty();
212 case MessageTarget::TYPE_CHANNEL:
214 Channel* targchan = target.Get<Channel>();
216 if (!targchan->IsModeSet(m2) || is_registered)
217 return MOD_RES_PASSTHRU;
219 if (CheckExemption::Call(exemptionprov, user, targchan, "regmoderated") == MOD_RES_ALLOW)
220 return MOD_RES_PASSTHRU;
222 // User is messaging a +M channel and is not registered or exempt.
223 user->WriteNumeric(ERR_NEEDREGGEDNICK, targchan->name, "You need to be identified to a registered account to message this channel");
227 case MessageTarget::TYPE_USER:
229 User* targuser = target.Get<User>();
230 if (!targuser->IsModeSet(m3) || is_registered)
231 return MOD_RES_PASSTHRU;
233 if (calleridapi && calleridapi->IsOnAcceptList(user, targuser))
234 return MOD_RES_PASSTHRU;
236 // User is messaging a +R user and is not registered or on an accept list.
237 user->WriteNumeric(ERR_NEEDREGGEDNICK, targuser->nick, "You need to be identified to a registered account to message this user");
241 case MessageTarget::TYPE_SERVER:
244 return MOD_RES_PASSTHRU;
247 ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
249 return HandleMessage(user, target);
252 ModResult OnUserPreTagMessage(User* user, const MessageTarget& target, CTCTags::TagMessageDetails& details) CXX11_OVERRIDE
254 return HandleMessage(user, target);
257 ModResult OnCheckBan(User* user, Channel* chan, const std::string& mask) CXX11_OVERRIDE
260 return MOD_RES_PASSTHRU;
262 if ((mask.length() > 2) && (mask[1] == ':'))
266 std::string *account = accountname.get(user);
267 if (account && InspIRCd::Match(*account, mask.substr(2)))
270 else if (mask[0] == 'U')
272 std::string *account = accountname.get(user);
273 /* If the user is registered we don't care. */
275 return MOD_RES_PASSTHRU;
277 /* If we made it this far we know the user isn't registered
278 so just deny if it matches */
280 bool result = chan->CheckBan(user, mask.substr(2));
281 checking_ban = false;
288 /* If we made it this far then the ban wasn't an ExtBan
289 or the user we were checking for didn't match either ExtBan */
290 return MOD_RES_PASSTHRU;
293 ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
295 std::string *account = accountname.get(user);
296 bool is_registered = account && !account->empty();
300 if (chan->IsModeSet(m1))
304 // joining a +R channel and not identified
305 user->WriteNumeric(ERR_NEEDREGGEDNICK, chan->name, "You need to be identified to a registered account to join this channel");
310 return MOD_RES_PASSTHRU;
313 ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass) CXX11_OVERRIDE
315 if (myclass->config->getBool("requireaccount") && !accountname.get(user))
317 return MOD_RES_PASSTHRU;
320 Version GetVersion() CXX11_OVERRIDE
322 return Version("Provides support for ircu-style services accounts, including channel mode +R, etc", VF_OPTCOMMON|VF_VENDOR);
326 MODULE_INIT(ModuleServicesAccount)