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