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