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