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