]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_services_account.cpp
Add support for blocking tag messages with the deaf mode.
[user/henk/code/inspircd.git] / src / modules / m_services_account.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2019 linuxdaemon <linuxdaemon.irc@gmail.com>
5  *   Copyright (C) 2013, 2017-2020 Sadie Powell <sadie@witchery.services>
6  *   Copyright (C) 2012-2015 Attila Molnar <attilamolnar@hush.com>
7  *   Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
8  *   Copyright (C) 2012 Shawn Smith <ShawnSmith0828@gmail.com>
9  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
10  *   Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
11  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
12  *   Copyright (C) 2006, 2008 Robin Burchell <robin+git@viroteck.net>
13  *   Copyright (C) 2006, 2008 Craig Edwards <brain@inspircd.org>
14  *
15  * This file is part of InspIRCd.  InspIRCd is free software: you can
16  * redistribute it and/or modify it under the terms of the GNU General Public
17  * License as published by the Free Software Foundation, version 2.
18  *
19  * This program is distributed in the hope that it will be useful, but WITHOUT
20  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
21  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
22  * details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26  */
27
28
29 #include "inspircd.h"
30 #include "modules/account.h"
31 #include "modules/callerid.h"
32 #include "modules/ctctags.h"
33 #include "modules/exemption.h"
34 #include "modules/whois.h"
35
36 enum
37 {
38         // From UnrealIRCd.
39         RPL_WHOISREGNICK = 307,
40
41         // From ircu.
42         RPL_WHOISACCOUNT = 330,
43
44         // From ircd-hybrid?
45         ERR_NEEDREGGEDNICK = 477,
46
47         // From IRCv3 sasl-3.1.
48         RPL_LOGGEDIN = 900,
49         RPL_LOGGEDOUT = 901
50 };
51
52 /** Channel mode +r - mark a channel as identified
53  */
54 class Channel_r : public ModeHandler
55 {
56  public:
57         Channel_r(Module* Creator) : ModeHandler(Creator, "c_registered", 'r', PARAM_NONE, MODETYPE_CHANNEL) { }
58
59         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE
60         {
61                 // Only a U-lined server may add or remove the +r mode.
62                 if (!IS_LOCAL(source))
63                 {
64                         // Only change the mode if it's not redundant
65                         if ((adding != channel->IsModeSet(this)))
66                         {
67                                 channel->SetMode(this, adding);
68                                 return MODEACTION_ALLOW;
69                         }
70                 }
71                 else
72                 {
73                         source->WriteNumeric(ERR_NOPRIVILEGES, "Only a server may modify the +r channel mode");
74                 }
75                 return MODEACTION_DENY;
76         }
77 };
78
79 /** User mode +r - mark a user as identified
80  */
81 class User_r : public ModeHandler
82 {
83
84  public:
85         User_r(Module* Creator) : ModeHandler(Creator, "u_registered", 'r', PARAM_NONE, MODETYPE_USER) { }
86
87         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE
88         {
89                 if (!IS_LOCAL(source))
90                 {
91                         if ((adding != dest->IsModeSet(this)))
92                         {
93                                 dest->SetMode(this, adding);
94                                 return MODEACTION_ALLOW;
95                         }
96                 }
97                 else
98                 {
99                         source->WriteNumeric(ERR_NOPRIVILEGES, "Only a server may modify the +r user mode");
100                 }
101                 return MODEACTION_DENY;
102         }
103 };
104
105 class AccountExtItemImpl : public AccountExtItem
106 {
107         Events::ModuleEventProvider eventprov;
108
109  public:
110         AccountExtItemImpl(Module* mod)
111                 : AccountExtItem("accountname", ExtensionItem::EXT_USER, mod)
112                 , eventprov(mod, "event/account")
113         {
114         }
115
116         void FromInternal(Extensible* container, const std::string& value) CXX11_OVERRIDE
117         {
118                 StringExtItem::FromInternal(container, value);
119         }
120
121         void FromNetwork(Extensible* container, const std::string& value) CXX11_OVERRIDE
122         {
123                 StringExtItem::FromNetwork(container, value);
124
125                 User* user = static_cast<User*>(container);
126                 if (IS_LOCAL(user))
127                 {
128                         if (value.empty())
129                         {
130                                 // Logged out.
131                                 user->WriteNumeric(RPL_LOGGEDOUT, user->GetFullHost(), "You are now logged out");
132                         }
133                         else
134                         {
135                                 // Logged in.
136                                 user->WriteNumeric(RPL_LOGGEDIN, user->GetFullHost(), value, InspIRCd::Format("You are now logged in as %s", value.c_str()));
137                         }
138                 }
139
140                 FOREACH_MOD_CUSTOM(eventprov, AccountEventListener, OnAccountChange, (user, value));
141         }
142 };
143
144 class ModuleServicesAccount
145         : public Module
146         , public Whois::EventListener
147         , public CTCTags::EventListener
148 {
149  private:
150         CallerID::API calleridapi;
151         CheckExemption::EventProvider exemptionprov;
152         SimpleChannelModeHandler reginvitemode;
153         SimpleChannelModeHandler regmoderatedmode;
154         SimpleUserModeHandler regdeafmode;
155         Channel_r chanregmode;
156         User_r userregmode;
157         AccountExtItemImpl accountname;
158         bool checking_ban;
159
160  public:
161         ModuleServicesAccount()
162                 : Whois::EventListener(this)
163                 , CTCTags::EventListener(this)
164                 , calleridapi(this)
165                 , exemptionprov(this)
166                 , reginvitemode(this, "reginvite", 'R')
167                 , regmoderatedmode(this, "regmoderated", 'M')
168                 , regdeafmode(this, "regdeaf", 'R')
169                 , chanregmode(this)
170                 , userregmode(this)
171                 , accountname(this)
172                 , checking_ban(false)
173         {
174         }
175
176         void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
177         {
178                 tokens["EXTBAN"].push_back('R');
179                 tokens["EXTBAN"].push_back('U');
180         }
181
182         /* <- :twisted.oscnet.org 330 w00t2 w00t2 w00t :is logged in as */
183         void OnWhois(Whois::Context& whois) CXX11_OVERRIDE
184         {
185                 std::string* account = accountname.get(whois.GetTarget());
186
187                 if (account)
188                 {
189                         whois.SendLine(RPL_WHOISACCOUNT, *account, "is logged in as");
190                 }
191
192                 if (whois.GetTarget()->IsModeSet(userregmode))
193                 {
194                         /* user is registered */
195                         whois.SendLine(RPL_WHOISREGNICK, "is a registered nick");
196                 }
197         }
198
199         void OnUserPostNick(User* user, const std::string &oldnick) CXX11_OVERRIDE
200         {
201                 /* On nickchange, if they have +r, remove it */
202                 if ((user->IsModeSet(userregmode)) && (ServerInstance->FindNickOnly(oldnick) != user))
203                         userregmode.RemoveMode(user);
204         }
205
206         ModResult HandleMessage(User* user, const MessageTarget& target)
207         {
208                 if (!IS_LOCAL(user))
209                         return MOD_RES_PASSTHRU;
210
211                 std::string *account = accountname.get(user);
212                 bool is_registered = account && !account->empty();
213
214                 switch (target.type)
215                 {
216                         case MessageTarget::TYPE_CHANNEL:
217                         {
218                                 Channel* targchan = target.Get<Channel>();
219
220                                 if (!targchan->IsModeSet(regmoderatedmode) || is_registered)
221                                         return MOD_RES_PASSTHRU;
222
223                                 if (CheckExemption::Call(exemptionprov, user, targchan, "regmoderated") == MOD_RES_ALLOW)
224                                         return MOD_RES_PASSTHRU;
225
226                                 // User is messaging a +M channel and is not registered or exempt.
227                                 user->WriteNumeric(ERR_NEEDREGGEDNICK, targchan->name, "You need to be identified to a registered account to message this channel");
228                                 return MOD_RES_DENY;
229                                 break;
230                         }
231                         case MessageTarget::TYPE_USER:
232                         {
233                                 User* targuser = target.Get<User>();
234                                 if (!targuser->IsModeSet(regdeafmode)  || is_registered)
235                                         return MOD_RES_PASSTHRU;
236
237                                 if (calleridapi && calleridapi->IsOnAcceptList(user, targuser))
238                                         return MOD_RES_PASSTHRU;
239
240                                 // User is messaging a +R user and is not registered or on an accept list.
241                                 user->WriteNumeric(ERR_NEEDREGGEDNICK, targuser->nick, "You need to be identified to a registered account to message this user");
242                                 return MOD_RES_DENY;
243                                 break;
244                         }
245                         case MessageTarget::TYPE_SERVER:
246                                 break;
247                 }
248                 return MOD_RES_PASSTHRU;
249         }
250
251         ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
252         {
253                 return HandleMessage(user, target);
254         }
255
256         ModResult OnUserPreTagMessage(User* user, const MessageTarget& target, CTCTags::TagMessageDetails& details) CXX11_OVERRIDE
257         {
258                 return HandleMessage(user, target);
259         }
260
261         ModResult OnCheckBan(User* user, Channel* chan, const std::string& mask) CXX11_OVERRIDE
262         {
263                 if (checking_ban)
264                         return MOD_RES_PASSTHRU;
265
266                 if ((mask.length() > 2) && (mask[1] == ':'))
267                 {
268                         if (mask[0] == 'R')
269                         {
270                                 std::string *account = accountname.get(user);
271                                 if (account && InspIRCd::Match(*account, mask.substr(2)))
272                                         return MOD_RES_DENY;
273                         }
274                         else if (mask[0] == 'U')
275                         {
276                                 std::string *account = accountname.get(user);
277                                 /* If the user is registered we don't care. */
278                                 if (account)
279                                         return MOD_RES_PASSTHRU;
280
281                                 /* If we made it this far we know the user isn't registered
282                                         so just deny if it matches */
283                                 checking_ban = true;
284                                 bool result = chan->CheckBan(user, mask.substr(2));
285                                 checking_ban = false;
286
287                                 if (result)
288                                         return MOD_RES_DENY;
289                         }
290                 }
291
292                 /* If we made it this far then the ban wasn't an ExtBan
293                         or the user we were checking for didn't match either ExtBan */
294                 return MOD_RES_PASSTHRU;
295         }
296
297         ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
298         {
299                 std::string *account = accountname.get(user);
300                 bool is_registered = account && !account->empty();
301
302                 if (chan)
303                 {
304                         if (chan->IsModeSet(reginvitemode))
305                         {
306                                 if (!is_registered)
307                                 {
308                                         // joining a +R channel and not identified
309                                         user->WriteNumeric(ERR_NEEDREGGEDNICK, chan->name, "You need to be identified to a registered account to join this channel");
310                                         return MOD_RES_DENY;
311                                 }
312                         }
313                 }
314                 return MOD_RES_PASSTHRU;
315         }
316
317         ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass) CXX11_OVERRIDE
318         {
319                 if (myclass->config->getBool("requireaccount") && !accountname.get(user))
320                 {
321                         ServerInstance->Logs->Log("CONNECTCLASS", LOG_DEBUG, "The %s connect class is not suitable as it requires the user to be logged into an account",
322                                 myclass->GetName().c_str());
323                         return MOD_RES_DENY;
324                 }
325                 return MOD_RES_PASSTHRU;
326         }
327
328         Version GetVersion() CXX11_OVERRIDE
329         {
330                 return Version("Adds various channel and user modes relating to services accounts.", VF_OPTCOMMON|VF_VENDOR);
331         }
332 };
333
334 MODULE_INIT(ModuleServicesAccount)