]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_services_account.cpp
Allow users on an accept list to bypass the +R user mode.
[user/henk/code/inspircd.git] / src / modules / m_services_account.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
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>
10  *
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.
14  *
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
18  * details.
19  *
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/>.
22  */
23
24
25 #include "inspircd.h"
26 #include "modules/account.h"
27 #include "modules/callerid.h"
28 #include "modules/exemption.h"
29 #include "modules/whois.h"
30
31 enum
32 {
33         // From UnrealIRCd.
34         RPL_WHOISREGNICK = 307,
35
36         // From ircu.
37         RPL_WHOISACCOUNT = 330,
38
39         // From ircd-hybrid?
40         ERR_NEEDREGGEDNICK = 477,
41
42         // From IRCv3 sasl-3.1.
43         RPL_LOGGEDIN = 900,
44         RPL_LOGGEDOUT = 901
45 };
46
47 /** Channel mode +r - mark a channel as identified
48  */
49 class Channel_r : public ModeHandler
50 {
51  public:
52         Channel_r(Module* Creator) : ModeHandler(Creator, "c_registered", 'r', PARAM_NONE, MODETYPE_CHANNEL) { }
53
54         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE
55         {
56                 // only a u-lined server may add or remove the +r mode.
57                 if (!IS_LOCAL(source))
58                 {
59                         // Only change the mode if it's not redundant
60                         if ((adding != channel->IsModeSet(this)))
61                         {
62                                 channel->SetMode(this, adding);
63                                 return MODEACTION_ALLOW;
64                         }
65                 }
66                 else
67                 {
68                         source->WriteNumeric(ERR_NOPRIVILEGES, "Only a server may modify the +r channel mode");
69                 }
70                 return MODEACTION_DENY;
71         }
72 };
73
74 /** User mode +r - mark a user as identified
75  */
76 class User_r : public ModeHandler
77 {
78
79  public:
80         User_r(Module* Creator) : ModeHandler(Creator, "u_registered", 'r', PARAM_NONE, MODETYPE_USER) { }
81
82         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE
83         {
84                 if (!IS_LOCAL(source))
85                 {
86                         if ((adding != dest->IsModeSet(this)))
87                         {
88                                 dest->SetMode(this, adding);
89                                 return MODEACTION_ALLOW;
90                         }
91                 }
92                 else
93                 {
94                         source->WriteNumeric(ERR_NOPRIVILEGES, "Only a server may modify the +r user mode");
95                 }
96                 return MODEACTION_DENY;
97         }
98 };
99
100 class AccountExtItemImpl : public AccountExtItem
101 {
102         Events::ModuleEventProvider eventprov;
103
104  public:
105         AccountExtItemImpl(Module* mod)
106                 : AccountExtItem("accountname", ExtensionItem::EXT_USER, mod)
107                 , eventprov(mod, "event/account")
108         {
109         }
110
111         void unserialize(SerializeFormat format, Extensible* container, const std::string& value) CXX11_OVERRIDE
112         {
113                 User* user = static_cast<User*>(container);
114
115                 StringExtItem::unserialize(format, container, value);
116
117                 // If we are being reloaded then don't send the numeric or run the event
118                 if (format == FORMAT_INTERNAL)
119                         return;
120
121                 if (IS_LOCAL(user))
122                 {
123                         if (value.empty())
124                         {
125                                 // Logged out.
126                                 user->WriteNumeric(RPL_LOGGEDOUT, user->GetFullHost(), "You are now logged out");
127                         }
128                         else
129                         {
130                                 // Logged in.
131                                 user->WriteNumeric(RPL_LOGGEDIN, user->GetFullHost(), value, InspIRCd::Format("You are now logged in as %s", value.c_str()));
132                         }
133                 }
134
135                 FOREACH_MOD_CUSTOM(eventprov, AccountEventListener, OnAccountChange, (user, value));
136         }
137 };
138
139 class ModuleServicesAccount : public Module, public Whois::EventListener
140 {
141  private:
142         CallerID::API calleridapi;
143         CheckExemption::EventProvider exemptionprov;
144         SimpleChannelModeHandler m1;
145         SimpleChannelModeHandler m2;
146         SimpleUserModeHandler m3;
147         Channel_r m4;
148         User_r m5;
149         AccountExtItemImpl accountname;
150         bool checking_ban;
151
152  public:
153         ModuleServicesAccount()
154                 : Whois::EventListener(this)
155                 , calleridapi(this)
156                 , exemptionprov(this)
157                 , m1(this, "reginvite", 'R')
158                 , m2(this, "regmoderated", 'M')
159                 , m3(this, "regdeaf", 'R')
160                 , m4(this)
161                 , m5(this)
162                 , accountname(this)
163                 , checking_ban(false)
164         {
165         }
166
167         void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
168         {
169                 tokens["EXTBAN"].push_back('R');
170                 tokens["EXTBAN"].push_back('U');
171         }
172
173         /* <- :twisted.oscnet.org 330 w00t2 w00t2 w00t :is logged in as */
174         void OnWhois(Whois::Context& whois) CXX11_OVERRIDE
175         {
176                 std::string* account = accountname.get(whois.GetTarget());
177
178                 if (account)
179                 {
180                         whois.SendLine(RPL_WHOISACCOUNT, *account, "is logged in as");
181                 }
182
183                 if (whois.GetTarget()->IsModeSet(m5))
184                 {
185                         /* user is registered */
186                         whois.SendLine(RPL_WHOISREGNICK, "is a registered nick");
187                 }
188         }
189
190         void OnUserPostNick(User* user, const std::string &oldnick) CXX11_OVERRIDE
191         {
192                 /* On nickchange, if they have +r, remove it */
193                 if ((user->IsModeSet(m5)) && (ServerInstance->FindNickOnly(oldnick) != user))
194                         m5.RemoveMode(user);
195         }
196
197         ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
198         {
199                 if (!IS_LOCAL(user))
200                         return MOD_RES_PASSTHRU;
201
202                 std::string *account = accountname.get(user);
203                 bool is_registered = account && !account->empty();
204
205                 if (target.type == MessageTarget::TYPE_CHANNEL)
206                 {
207                         Channel* targchan = target.Get<Channel>();
208
209                         if (!targchan->IsModeSet(m2) || is_registered)
210                                 return MOD_RES_PASSTHRU;
211
212                         if (CheckExemption::Call(exemptionprov, user, targchan, "regmoderated") == MOD_RES_ALLOW)
213                                 return MOD_RES_PASSTHRU;
214
215                         // User is messaging a +M channel and is not registered or exempt.
216                         user->WriteNumeric(ERR_NEEDREGGEDNICK, targchan->name, "You need to be identified to a registered account to message this channel");
217                         return MOD_RES_DENY;
218                 }
219                 else if (target.type == MessageTarget::TYPE_USER)
220                 {
221                         User* targuser = target.Get<User>();
222                         if (!targuser->IsModeSet(m3)  || is_registered)
223                                 return MOD_RES_PASSTHRU;
224
225                         if (calleridapi && calleridapi->IsOnAcceptList(user, targuser))
226                                 return MOD_RES_PASSTHRU;
227
228                         // User is messaging a +R user and is not registered or on an accept list.
229                         user->WriteNumeric(ERR_NEEDREGGEDNICK, targuser->nick, "You need to be identified to a registered account to message this user");
230                         return MOD_RES_DENY;
231                 }
232                 return MOD_RES_PASSTHRU;
233         }
234
235         ModResult OnCheckBan(User* user, Channel* chan, const std::string& mask) CXX11_OVERRIDE
236         {
237                 if (checking_ban)
238                         return MOD_RES_PASSTHRU;
239
240                 if ((mask.length() > 2) && (mask[1] == ':'))
241                 {
242                         if (mask[0] == 'R')
243                         {
244                                 std::string *account = accountname.get(user);
245                                 if (account && InspIRCd::Match(*account, mask.substr(2)))
246                                         return MOD_RES_DENY;
247                         }
248                         else if (mask[0] == 'U')
249                         {
250                                 std::string *account = accountname.get(user);
251                                 /* If the user is registered we don't care. */
252                                 if (account)
253                                         return MOD_RES_PASSTHRU;
254
255                                 /* If we made it this far we know the user isn't registered
256                                         so just deny if it matches */
257                                 checking_ban = true;
258                                 bool result = chan->CheckBan(user, mask.substr(2));
259                                 checking_ban = false;
260
261                                 if (result)
262                                         return MOD_RES_DENY;
263                         }
264                 }
265
266                 /* If we made it this far then the ban wasn't an ExtBan
267                         or the user we were checking for didn't match either ExtBan */
268                 return MOD_RES_PASSTHRU;
269         }
270
271         ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
272         {
273                 std::string *account = accountname.get(user);
274                 bool is_registered = account && !account->empty();
275
276                 if (chan)
277                 {
278                         if (chan->IsModeSet(m1))
279                         {
280                                 if (!is_registered)
281                                 {
282                                         // joining a +R channel and not identified
283                                         user->WriteNumeric(ERR_NEEDREGGEDNICK, chan->name, "You need to be identified to a registered account to join this channel");
284                                         return MOD_RES_DENY;
285                                 }
286                         }
287                 }
288                 return MOD_RES_PASSTHRU;
289         }
290
291         ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass) CXX11_OVERRIDE
292         {
293                 if (myclass->config->getBool("requireaccount") && !accountname.get(user))
294                         return MOD_RES_DENY;
295                 return MOD_RES_PASSTHRU;
296         }
297
298         Version GetVersion() CXX11_OVERRIDE
299         {
300                 return Version("Provides support for ircu-style services accounts, including chmode +R, etc.",VF_OPTCOMMON|VF_VENDOR);
301         }
302 };
303
304 MODULE_INIT(ModuleServicesAccount)