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