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"
29 /** Channel mode +r - mark a channel as identified
31 class Channel_r : public ModeHandler
34 Channel_r(Module* Creator) : ModeHandler(Creator, "c_registered", 'r', PARAM_NONE, MODETYPE_CHANNEL) { }
36 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
38 // only a u-lined server may add or remove the +r mode.
39 if (!IS_LOCAL(source))
41 // Only change the mode if it's not redundant
42 if ((adding != channel->IsModeSet(this)))
44 channel->SetMode(this, adding);
45 return MODEACTION_ALLOW;
50 source->WriteNumeric(500, "Only a server may modify the +r channel mode");
52 return MODEACTION_DENY;
56 /** User mode +r - mark a user as identified
58 class User_r : public ModeHandler
62 User_r(Module* Creator) : ModeHandler(Creator, "u_registered", 'r', PARAM_NONE, MODETYPE_USER) { }
64 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
66 if (!IS_LOCAL(source))
68 if ((adding != dest->IsModeSet(this)))
70 dest->SetMode(this, adding);
71 return MODEACTION_ALLOW;
76 source->WriteNumeric(500, "Only a server may modify the +r user mode");
78 return MODEACTION_DENY;
82 /** Channel mode +R - unidentified users cannot join
84 class AChannel_R : public SimpleChannelModeHandler
87 AChannel_R(Module* Creator) : SimpleChannelModeHandler(Creator, "reginvite", 'R') { }
90 /** User mode +R - unidentified users cannot message
92 class AUser_R : public SimpleUserModeHandler
95 AUser_R(Module* Creator) : SimpleUserModeHandler(Creator, "regdeaf", 'R') { }
98 /** Channel mode +M - unidentified users cannot message channel
100 class AChannel_M : public SimpleChannelModeHandler
103 AChannel_M(Module* Creator) : SimpleChannelModeHandler(Creator, "regmoderated", 'M') { }
106 class AccountExtItemImpl : public AccountExtItem
108 Events::ModuleEventProvider eventprov;
111 AccountExtItemImpl(Module* mod)
112 : AccountExtItem("accountname", ExtensionItem::EXT_USER, mod)
113 , eventprov(mod, "event/account")
117 void unserialize(SerializeFormat format, Extensible* container, const std::string& value)
119 User* user = static_cast<User*>(container);
121 StringExtItem::unserialize(format, container, value);
123 // If we are being reloaded then don't send the numeric or run the event
124 if (format == FORMAT_INTERNAL)
132 user->WriteNumeric(900, user->GetFullHost(), value, InspIRCd::Format("You are now logged in as %s", value.c_str()));
135 // If value is empty then logged out
137 FOREACH_MOD_CUSTOM(eventprov, AccountEventListener, OnAccountChange, (user, value));
141 class ModuleServicesAccount : public Module, public Whois::EventListener
143 CheckExemption::EventProvider exemptionprov;
149 AccountExtItemImpl accountname;
152 ModuleServicesAccount()
153 : Whois::EventListener(this)
154 , exemptionprov(this)
155 , m1(this), m2(this), m3(this), m4(this), m5(this)
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(330, *account, "is logged in as");
177 if (whois.GetTarget()->IsModeSet(m5))
179 /* user is registered */
180 whois.SendLine(307, "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;
203 FIRST_MOD_RESULT_CUSTOM(exemptionprov, CheckExemption::EventListener, OnCheckExemption, res, (user, c, "regmoderated"));
205 if (c->IsModeSet(m2) && !is_registered && res != MOD_RES_ALLOW)
207 // user messaging a +M channel and is not registered
208 user->WriteNumeric(477, c->name, "You need to be identified to a registered account to message this channel");
212 else if (target_type == TYPE_USER)
214 User* u = (User*)dest;
216 if (u->IsModeSet(m3) && !is_registered)
218 // user messaging a +R user and is not registered
219 user->WriteNumeric(477, u->nick, "You need to be identified to a registered account to message this user");
223 return MOD_RES_PASSTHRU;
226 ModResult OnCheckBan(User* user, Channel* chan, const std::string& mask) CXX11_OVERRIDE
229 return MOD_RES_PASSTHRU;
231 if ((mask.length() > 2) && (mask[1] == ':'))
235 std::string *account = accountname.get(user);
236 if (account && InspIRCd::Match(*account, mask.substr(2)))
239 else if (mask[0] == 'U')
241 std::string *account = accountname.get(user);
242 /* If the user is registered we don't care. */
244 return MOD_RES_PASSTHRU;
246 /* If we made it this far we know the user isn't registered
247 so just deny if it matches */
249 bool result = chan->CheckBan(user, mask.substr(2));
250 checking_ban = false;
257 /* If we made it this far then the ban wasn't an ExtBan
258 or the user we were checking for didn't match either ExtBan */
259 return MOD_RES_PASSTHRU;
262 ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
264 std::string *account = accountname.get(user);
265 bool is_registered = account && !account->empty();
269 if (chan->IsModeSet(m1))
273 // joining a +R channel and not identified
274 user->WriteNumeric(477, chan->name, "You need to be identified to a registered account to join this channel");
279 return MOD_RES_PASSTHRU;
282 ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass) CXX11_OVERRIDE
284 if (myclass->config->getBool("requireaccount") && !accountname.get(user))
286 return MOD_RES_PASSTHRU;
289 Version GetVersion() CXX11_OVERRIDE
291 return Version("Provides support for ircu-style services accounts, including chmode +R, etc.",VF_OPTCOMMON|VF_VENDOR);
295 MODULE_INIT(ModuleServicesAccount)