]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_services_account.cpp
Convert all core ExtensionItem code away from {un,}serialize.
[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 FromInternal(Extensible* container, const std::string& value) CXX11_OVERRIDE
113         {
114                 StringExtItem::FromInternal(container, value);
115         }
116
117         void FromNetwork(Extensible* container, const std::string& value) CXX11_OVERRIDE
118         {
119                 StringExtItem::FromNetwork(container, value);
120
121                 User* user = static_cast<User*>(container);
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                 switch (target.type)
211                 {
212                         case MessageTarget::TYPE_CHANNEL:
213                         {
214                                 Channel* targchan = target.Get<Channel>();
215
216                                 if (!targchan->IsModeSet(m2) || is_registered)
217                                         return MOD_RES_PASSTHRU;
218
219                                 if (CheckExemption::Call(exemptionprov, user, targchan, "regmoderated") == MOD_RES_ALLOW)
220                                         return MOD_RES_PASSTHRU;
221
222                                 // User is messaging a +M channel and is not registered or exempt.
223                                 user->WriteNumeric(ERR_NEEDREGGEDNICK, targchan->name, "You need to be identified to a registered account to message this channel");
224                                 return MOD_RES_DENY;
225                                 break;
226                         }
227                         case MessageTarget::TYPE_USER:
228                         {
229                                 User* targuser = target.Get<User>();
230                                 if (!targuser->IsModeSet(m3)  || is_registered)
231                                         return MOD_RES_PASSTHRU;
232
233                                 if (calleridapi && calleridapi->IsOnAcceptList(user, targuser))
234                                         return MOD_RES_PASSTHRU;
235
236                                 // User is messaging a +R user and is not registered or on an accept list.
237                                 user->WriteNumeric(ERR_NEEDREGGEDNICK, targuser->nick, "You need to be identified to a registered account to message this user");
238                                 return MOD_RES_DENY;
239                                 break;
240                         }
241                         case MessageTarget::TYPE_SERVER:
242                                 break;
243                 }
244                 return MOD_RES_PASSTHRU;
245         }
246
247         ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
248         {
249                 return HandleMessage(user, target);
250         }
251
252         ModResult OnUserPreTagMessage(User* user, const MessageTarget& target, CTCTags::TagMessageDetails& details) CXX11_OVERRIDE
253         {
254                 return HandleMessage(user, target);
255         }
256
257         ModResult OnCheckBan(User* user, Channel* chan, const std::string& mask) CXX11_OVERRIDE
258         {
259                 if (checking_ban)
260                         return MOD_RES_PASSTHRU;
261
262                 if ((mask.length() > 2) && (mask[1] == ':'))
263                 {
264                         if (mask[0] == 'R')
265                         {
266                                 std::string *account = accountname.get(user);
267                                 if (account && InspIRCd::Match(*account, mask.substr(2)))
268                                         return MOD_RES_DENY;
269                         }
270                         else if (mask[0] == 'U')
271                         {
272                                 std::string *account = accountname.get(user);
273                                 /* If the user is registered we don't care. */
274                                 if (account)
275                                         return MOD_RES_PASSTHRU;
276
277                                 /* If we made it this far we know the user isn't registered
278                                         so just deny if it matches */
279                                 checking_ban = true;
280                                 bool result = chan->CheckBan(user, mask.substr(2));
281                                 checking_ban = false;
282
283                                 if (result)
284                                         return MOD_RES_DENY;
285                         }
286                 }
287
288                 /* If we made it this far then the ban wasn't an ExtBan
289                         or the user we were checking for didn't match either ExtBan */
290                 return MOD_RES_PASSTHRU;
291         }
292
293         ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
294         {
295                 std::string *account = accountname.get(user);
296                 bool is_registered = account && !account->empty();
297
298                 if (chan)
299                 {
300                         if (chan->IsModeSet(m1))
301                         {
302                                 if (!is_registered)
303                                 {
304                                         // joining a +R channel and not identified
305                                         user->WriteNumeric(ERR_NEEDREGGEDNICK, chan->name, "You need to be identified to a registered account to join this channel");
306                                         return MOD_RES_DENY;
307                                 }
308                         }
309                 }
310                 return MOD_RES_PASSTHRU;
311         }
312
313         ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass) CXX11_OVERRIDE
314         {
315                 if (myclass->config->getBool("requireaccount") && !accountname.get(user))
316                         return MOD_RES_DENY;
317                 return MOD_RES_PASSTHRU;
318         }
319
320         Version GetVersion() CXX11_OVERRIDE
321         {
322                 return Version("Provides support for ircu-style services accounts, including channel mode +R, etc", VF_OPTCOMMON|VF_VENDOR);
323         }
324 };
325
326 MODULE_INIT(ModuleServicesAccount)