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/>.
25 /* $ModDesc: Provides support for ircu-style services accounts, including chmode +R, etc. */
30 /** Channel mode +r - mark a channel as identified
32 class Channel_r : public ModeHandler
35 Channel_r(Module* Creator) : ModeHandler(Creator, "c_registered", 'r', PARAM_NONE, MODETYPE_CHANNEL) { }
37 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
39 // only a u-lined server may add or remove the +r mode.
40 if (!IS_LOCAL(source))
42 // Only change the mode if it's not redundant
43 if ((adding != channel->IsModeSet('r')))
45 channel->SetMode('r',adding);
46 return MODEACTION_ALLOW;
51 source->WriteNumeric(500, "%s :Only a server may modify the +r channel mode", source->nick.c_str());
53 return MODEACTION_DENY;
57 /** User mode +r - mark a user as identified
59 class User_r : public ModeHandler
63 User_r(Module* Creator) : ModeHandler(Creator, "u_registered", 'r', PARAM_NONE, MODETYPE_USER) { }
65 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
67 if (!IS_LOCAL(source))
69 if ((adding != dest->IsModeSet('r')))
71 dest->SetMode('r',adding);
72 return MODEACTION_ALLOW;
77 source->WriteNumeric(500, "%s :Only a server may modify the +r user mode", source->nick.c_str());
79 return MODEACTION_DENY;
83 /** Channel mode +R - unidentified users cannot join
85 class AChannel_R : public SimpleChannelModeHandler
88 AChannel_R(Module* Creator) : SimpleChannelModeHandler(Creator, "reginvite", 'R') { }
91 /** User mode +R - unidentified users cannot message
93 class AUser_R : public SimpleUserModeHandler
96 AUser_R(Module* Creator) : SimpleUserModeHandler(Creator, "regdeaf", 'R') { }
99 /** Channel mode +M - unidentified users cannot message channel
101 class AChannel_M : public SimpleChannelModeHandler
104 AChannel_M(Module* Creator) : SimpleChannelModeHandler(Creator, "regmoderated", 'M') { }
107 class ModuleServicesAccount : public Module
114 AccountExtItem accountname;
117 static bool ReadCGIIRCExt(const char* extname, User* user, const std::string*& out)
119 ExtensionItem* wiext = ServerInstance->Extensions.GetItem(extname);
123 if (wiext->creator->ModuleSourceFile != "m_cgiirc.so")
126 StringExtItem* stringext = static_cast<StringExtItem*>(wiext);
127 std::string* addr = stringext->get(user);
136 ModuleServicesAccount() : m1(this), m2(this), m3(this), m4(this), m5(this),
137 accountname("accountname", this), checking_ban(false)
143 ServiceProvider* providerlist[] = { &m1, &m2, &m3, &m4, &m5, &accountname };
144 ServerInstance->Modules->AddServices(providerlist, sizeof(providerlist)/sizeof(ServiceProvider*));
145 Implementation eventlist[] = { I_OnWhois, I_OnUserPreMessage, I_OnUserPreNotice, I_OnUserPreJoin, I_OnCheckBan,
146 I_OnDecodeMetaData, I_On005Numeric, I_OnUserPostNick, I_OnSetConnectClass };
148 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
151 void On005Numeric(std::string &t)
153 ServerInstance->AddExtBanChar('R');
154 ServerInstance->AddExtBanChar('U');
157 /* <- :twisted.oscnet.org 330 w00t2 w00t2 w00t :is logged in as */
158 void OnWhois(User* source, User* dest)
160 std::string *account = accountname.get(dest);
164 ServerInstance->SendWhoisLine(source, dest, 330, "%s %s %s :is logged in as", source->nick.c_str(), dest->nick.c_str(), account->c_str());
167 if (dest->IsModeSet('r'))
169 /* user is registered */
170 ServerInstance->SendWhoisLine(source, dest, 307, "%s %s :is a registered nick", source->nick.c_str(), dest->nick.c_str());
174 void OnUserPostNick(User* user, const std::string &oldnick)
176 /* On nickchange, if they have +r, remove it */
177 if (user->IsModeSet('r') && assign(user->nick) != oldnick)
179 std::vector<std::string> modechange;
180 modechange.push_back(user->nick);
181 modechange.push_back("-r");
182 ServerInstance->SendMode(modechange, ServerInstance->FakeClient);
186 ModResult OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
189 return MOD_RES_PASSTHRU;
191 std::string *account = accountname.get(user);
192 bool is_registered = account && !account->empty();
194 if (target_type == TYPE_CHANNEL)
196 Channel* c = (Channel*)dest;
197 ModResult res = ServerInstance->OnCheckExemption(user,c,"regmoderated");
199 if (c->IsModeSet('M') && !is_registered && res != MOD_RES_ALLOW)
201 // user messaging a +M channel and is not registered
202 user->WriteNumeric(477, user->nick+" "+c->name+" :You need to be identified to a registered account to message this channel");
206 else if (target_type == TYPE_USER)
208 User* u = (User*)dest;
210 if (u->IsModeSet('R') && !is_registered)
212 // user messaging a +R user and is not registered
213 user->WriteNumeric(477, ""+ user->nick +" "+ u->nick +" :You need to be identified to a registered account to message this user");
217 return MOD_RES_PASSTHRU;
220 ModResult OnCheckBan(User* user, Channel* chan, const std::string& mask)
223 return MOD_RES_PASSTHRU;
225 if ((mask.length() > 2) && (mask[1] == ':'))
229 std::string *account = accountname.get(user);
230 if (account && InspIRCd::Match(*account, mask.substr(2)))
233 else if (mask[0] == 'U')
235 std::string *account = accountname.get(user);
236 /* If the user is registered we don't care. */
238 return MOD_RES_PASSTHRU;
240 /* If we made it this far we know the user isn't registered
241 so just deny if it matches */
243 bool result = chan->CheckBan(user, mask.substr(2));
244 checking_ban = false;
251 /* If we made it this far then the ban wasn't an ExtBan
252 or the user we were checking for didn't match either ExtBan */
253 return MOD_RES_PASSTHRU;
256 ModResult OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
258 return OnUserPreMessage(user, dest, target_type, text, status, exempt_list);
261 ModResult OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs, const std::string &keygiven)
264 return MOD_RES_PASSTHRU;
266 std::string *account = accountname.get(user);
267 bool is_registered = account && !account->empty();
271 if (chan->IsModeSet('R'))
275 // joining a +R channel and not identified
276 user->WriteNumeric(477, user->nick + " " + chan->name + " :You need to be identified to a registered account to join this channel");
281 return MOD_RES_PASSTHRU;
284 // Whenever the linking module receives metadata from another server and doesnt know what
285 // to do with it (of course, hence the 'meta') it calls this method, and it is up to each
286 // module in turn to figure out if this metadata key belongs to them, and what they want
288 // In our case we're only sending a single string around, so we just construct a std::string.
289 // Some modules will probably get much more complex and format more detailed structs and classes
290 // in a textual way for sending over the link.
291 void OnDecodeMetaData(Extensible* target, const std::string &extname, const std::string &extdata)
293 User* dest = dynamic_cast<User*>(target);
294 // check if its our metadata key, and its associated with a user
295 if (dest && (extname == "accountname"))
297 std::string *account = accountname.get(dest);
298 if (account && !account->empty())
304 const std::string* host = &dest->dhost;
305 if (dest->registered != REG_ALL)
307 if (!ReadCGIIRCExt("cgiirc_webirc_hostname", dest, host))
309 ReadCGIIRCExt("cgiirc_webirc_ip", dest, host);
313 dest->WriteNumeric(900, "%s %s!%s@%s %s :You are now logged in as %s",
314 dest->nick.c_str(), dest->nick.c_str(), dest->ident.c_str(), host->c_str(), account->c_str(), account->c_str());
317 AccountEvent(this, dest, *account).Send();
321 AccountEvent(this, dest, "").Send();
326 ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass)
328 if (myclass->config->getBool("requireaccount") && !accountname.get(user))
330 return MOD_RES_PASSTHRU;
335 return Version("Provides support for ircu-style services accounts, including chmode +R, etc.",VF_OPTCOMMON|VF_VENDOR);
339 MODULE_INIT(ModuleServicesAccount)