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