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"
28 /** Channel mode +r - mark a channel as identified
30 class Channel_r : public ModeHandler
33 Channel_r(Module* Creator) : ModeHandler(Creator, "c_registered", 'r', PARAM_NONE, MODETYPE_CHANNEL) { }
35 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
37 // only a u-lined server may add or remove the +r mode.
38 if (!IS_LOCAL(source))
40 // Only change the mode if it's not redundant
41 if ((adding != channel->IsModeSet(this)))
43 channel->SetMode(this, adding);
44 return MODEACTION_ALLOW;
49 source->WriteNumeric(500, ":Only a server may modify the +r channel mode");
51 return MODEACTION_DENY;
55 /** User mode +r - mark a user as identified
57 class User_r : public ModeHandler
61 User_r(Module* Creator) : ModeHandler(Creator, "u_registered", 'r', PARAM_NONE, MODETYPE_USER) { }
63 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
65 if (!IS_LOCAL(source))
67 if ((adding != dest->IsModeSet(this)))
69 dest->SetMode(this, adding);
70 return MODEACTION_ALLOW;
75 source->WriteNumeric(500, ":Only a server may modify the +r user mode");
77 return MODEACTION_DENY;
81 /** Channel mode +R - unidentified users cannot join
83 class AChannel_R : public SimpleChannelModeHandler
86 AChannel_R(Module* Creator) : SimpleChannelModeHandler(Creator, "reginvite", 'R') { }
89 /** User mode +R - unidentified users cannot message
91 class AUser_R : public SimpleUserModeHandler
94 AUser_R(Module* Creator) : SimpleUserModeHandler(Creator, "regdeaf", 'R') { }
97 /** Channel mode +M - unidentified users cannot message channel
99 class AChannel_M : public SimpleChannelModeHandler
102 AChannel_M(Module* Creator) : SimpleChannelModeHandler(Creator, "regmoderated", 'M') { }
105 static bool ReadCGIIRCExt(const char* extname, User* user, const std::string*& out)
107 ExtensionItem* wiext = ServerInstance->Extensions.GetItem(extname);
111 if (wiext->creator->ModuleSourceFile != "m_cgiirc.so")
114 StringExtItem* stringext = static_cast<StringExtItem*>(wiext);
115 std::string* addr = stringext->get(user);
123 class AccountExtItemImpl : public AccountExtItem
126 AccountExtItemImpl(Module* mod)
127 : AccountExtItem("accountname", mod)
131 void unserialize(SerializeFormat format, Extensible* container, const std::string& value)
133 User* user = dynamic_cast<User*>(container);
137 StringExtItem::unserialize(format, container, value);
143 const std::string* host = &user->dhost;
144 if (user->registered != REG_ALL)
146 if (!ReadCGIIRCExt("cgiirc_webirc_hostname", user, host))
148 ReadCGIIRCExt("cgiirc_webirc_ip", user, host);
152 user->WriteNumeric(900, "%s!%s@%s %s :You are now logged in as %s",
153 user->nick.c_str(), user->ident.c_str(), host->c_str(), value.c_str(), value.c_str());
156 AccountEvent(creator, user, value).Send();
161 AccountEvent(creator, user, "").Send();
166 class ModuleServicesAccount : public Module
173 AccountExtItemImpl accountname;
176 ModuleServicesAccount() : m1(this), m2(this), m3(this), m4(this), m5(this),
181 void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
183 tokens["EXTBAN"].push_back('R');
184 tokens["EXTBAN"].push_back('U');
187 /* <- :twisted.oscnet.org 330 w00t2 w00t2 w00t :is logged in as */
188 void OnWhois(User* source, User* dest) CXX11_OVERRIDE
190 std::string *account = accountname.get(dest);
194 ServerInstance->SendWhoisLine(source, dest, 330, "%s %s :is logged in as", dest->nick.c_str(), account->c_str());
197 if (dest->IsModeSet(m5))
199 /* user is registered */
200 ServerInstance->SendWhoisLine(source, dest, 307, "%s :is a registered nick", dest->nick.c_str());
204 void OnUserPostNick(User* user, const std::string &oldnick) CXX11_OVERRIDE
206 /* On nickchange, if they have +r, remove it */
207 if (user->IsModeSet(m5) && assign(user->nick) != oldnick)
209 std::vector<std::string> modechange;
210 modechange.push_back(user->nick);
211 modechange.push_back("-r");
212 ServerInstance->Modes->Process(modechange, ServerInstance->FakeClient, ModeParser::MODE_LOCALONLY);
216 ModResult OnUserPreMessage(User* user, void* dest, int target_type, std::string& text, char status, CUList& exempt_list, MessageType msgtype) CXX11_OVERRIDE
219 return MOD_RES_PASSTHRU;
221 std::string *account = accountname.get(user);
222 bool is_registered = account && !account->empty();
224 if (target_type == TYPE_CHANNEL)
226 Channel* c = (Channel*)dest;
227 ModResult res = ServerInstance->OnCheckExemption(user,c,"regmoderated");
229 if (c->IsModeSet(m2) && !is_registered && res != MOD_RES_ALLOW)
231 // user messaging a +M channel and is not registered
232 user->WriteNumeric(477, c->name+" :You need to be identified to a registered account to message this channel");
236 else if (target_type == TYPE_USER)
238 User* u = (User*)dest;
240 if (u->IsModeSet(m3) && !is_registered)
242 // user messaging a +R user and is not registered
243 user->WriteNumeric(477, u->nick +" :You need to be identified to a registered account to message this user");
247 return MOD_RES_PASSTHRU;
250 ModResult OnCheckBan(User* user, Channel* chan, const std::string& mask) CXX11_OVERRIDE
253 return MOD_RES_PASSTHRU;
255 if ((mask.length() > 2) && (mask[1] == ':'))
259 std::string *account = accountname.get(user);
260 if (account && InspIRCd::Match(*account, mask.substr(2)))
263 else if (mask[0] == 'U')
265 std::string *account = accountname.get(user);
266 /* If the user is registered we don't care. */
268 return MOD_RES_PASSTHRU;
270 /* If we made it this far we know the user isn't registered
271 so just deny if it matches */
273 bool result = chan->CheckBan(user, mask.substr(2));
274 checking_ban = false;
281 /* If we made it this far then the ban wasn't an ExtBan
282 or the user we were checking for didn't match either ExtBan */
283 return MOD_RES_PASSTHRU;
286 ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
288 std::string *account = accountname.get(user);
289 bool is_registered = account && !account->empty();
293 if (chan->IsModeSet(m1))
297 // joining a +R channel and not identified
298 user->WriteNumeric(477, chan->name + " :You need to be identified to a registered account to join this channel");
303 return MOD_RES_PASSTHRU;
306 ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass) CXX11_OVERRIDE
308 if (myclass->config->getBool("requireaccount") && !accountname.get(user))
310 return MOD_RES_PASSTHRU;
313 Version GetVersion() CXX11_OVERRIDE
315 return Version("Provides support for ircu-style services accounts, including chmode +R, etc.",VF_OPTCOMMON|VF_VENDOR);
319 MODULE_INIT(ModuleServicesAccount)