]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_services_account.cpp
4f2125a975616cfc3b80d6dc7e947d43b0b8563b
[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, "%s :Only a server may modify the +r channel mode", source->nick.c_str());
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, "%s :Only a server may modify the +r user mode", source->nick.c_str());
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 ModuleServicesAccount : public Module
106 {
107         AChannel_R m1;
108         AChannel_M m2;
109         AUser_R m3;
110         Channel_r m4;
111         User_r m5;
112         AccountExtItem accountname;
113  public:
114         ModuleServicesAccount() : m1(this), m2(this), m3(this), m4(this), m5(this),
115                 accountname("accountname", this)
116         {
117         }
118
119         void init() CXX11_OVERRIDE
120         {
121                 ServiceProvider* providerlist[] = { &m1, &m2, &m3, &m4, &m5, &accountname };
122                 ServerInstance->Modules->AddServices(providerlist, sizeof(providerlist)/sizeof(ServiceProvider*));
123         }
124
125         void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
126         {
127                 tokens["EXTBAN"].push_back('R');
128                 tokens["EXTBAN"].push_back('U');
129         }
130
131         /* <- :twisted.oscnet.org 330 w00t2 w00t2 w00t :is logged in as */
132         void OnWhois(User* source, User* dest) CXX11_OVERRIDE
133         {
134                 std::string *account = accountname.get(dest);
135
136                 if (account)
137                 {
138                         ServerInstance->SendWhoisLine(source, dest, 330, "%s %s %s :is logged in as", source->nick.c_str(), dest->nick.c_str(), account->c_str());
139                 }
140
141                 if (dest->IsModeSet(m5))
142                 {
143                         /* user is registered */
144                         ServerInstance->SendWhoisLine(source, dest, 307, "%s %s :is a registered nick", source->nick.c_str(), dest->nick.c_str());
145                 }
146         }
147
148         void OnUserPostNick(User* user, const std::string &oldnick) CXX11_OVERRIDE
149         {
150                 /* On nickchange, if they have +r, remove it */
151                 if (user->IsModeSet(m5) && assign(user->nick) != oldnick)
152                 {
153                         std::vector<std::string> modechange;
154                         modechange.push_back(user->nick);
155                         modechange.push_back("-r");
156                         ServerInstance->Modes->Process(modechange, ServerInstance->FakeClient, ModeParser::MODE_LOCALONLY);
157                 }
158         }
159
160         ModResult OnUserPreMessage(User* user, void* dest, int target_type, std::string& text, char status, CUList& exempt_list, MessageType msgtype) CXX11_OVERRIDE
161         {
162                 if (!IS_LOCAL(user))
163                         return MOD_RES_PASSTHRU;
164
165                 std::string *account = accountname.get(user);
166                 bool is_registered = account && !account->empty();
167
168                 if (target_type == TYPE_CHANNEL)
169                 {
170                         Channel* c = (Channel*)dest;
171                         ModResult res = ServerInstance->OnCheckExemption(user,c,"regmoderated");
172
173                         if (c->IsModeSet(m2) && !is_registered && res != MOD_RES_ALLOW)
174                         {
175                                 // user messaging a +M channel and is not registered
176                                 user->WriteNumeric(477, user->nick+" "+c->name+" :You need to be identified to a registered account to message this channel");
177                                 return MOD_RES_DENY;
178                         }
179                 }
180                 else if (target_type == TYPE_USER)
181                 {
182                         User* u = (User*)dest;
183
184                         if (u->IsModeSet(m3) && !is_registered)
185                         {
186                                 // user messaging a +R user and is not registered
187                                 user->WriteNumeric(477, ""+ user->nick +" "+ u->nick +" :You need to be identified to a registered account to message this user");
188                                 return MOD_RES_DENY;
189                         }
190                 }
191                 return MOD_RES_PASSTHRU;
192         }
193
194         ModResult OnCheckBan(User* user, Channel* chan, const std::string& mask) CXX11_OVERRIDE
195         {
196                 static bool checking = false;
197                 if (checking)
198                         return MOD_RES_PASSTHRU;
199
200                 if ((mask.length() > 2) && (mask[1] == ':'))
201                 {
202                         if (mask[0] == 'R')
203                         {
204                                 std::string *account = accountname.get(user);
205                                 if (account && InspIRCd::Match(*account, mask.substr(2)))
206                                         return MOD_RES_DENY;
207                         }
208                         else if (mask[0] == 'U')
209                         {
210                                 std::string *account = accountname.get(user);
211                                 /* If the user is registered we don't care. */
212                                 if (account)
213                                         return MOD_RES_PASSTHRU;
214
215                                 /* If we made it this far we know the user isn't registered
216                                         so just deny if it matches */
217                                 checking = true;
218                                 bool result = chan->CheckBan(user, mask.substr(2));
219                                 checking = false;
220
221                                 if (result)
222                                         return MOD_RES_DENY;
223                         }
224                 }
225
226                 /* If we made it this far then the ban wasn't an ExtBan
227                         or the user we were checking for didn't match either ExtBan */
228                 return MOD_RES_PASSTHRU;
229         }
230
231         ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
232         {
233                 std::string *account = accountname.get(user);
234                 bool is_registered = account && !account->empty();
235
236                 if (chan)
237                 {
238                         if (chan->IsModeSet(m1))
239                         {
240                                 if (!is_registered)
241                                 {
242                                         // joining a +R channel and not identified
243                                         user->WriteNumeric(477, user->nick + " " + chan->name + " :You need to be identified to a registered account to join this channel");
244                                         return MOD_RES_DENY;
245                                 }
246                         }
247                 }
248                 return MOD_RES_PASSTHRU;
249         }
250
251         // Whenever the linking module receives metadata from another server and doesnt know what
252         // to do with it (of course, hence the 'meta') it calls this method, and it is up to each
253         // module in turn to figure out if this metadata key belongs to them, and what they want
254         // to do with it.
255         // In our case we're only sending a single string around, so we just construct a std::string.
256         // Some modules will probably get much more complex and format more detailed structs and classes
257         // in a textual way for sending over the link.
258         void OnDecodeMetaData(Extensible* target, const std::string &extname, const std::string &extdata) CXX11_OVERRIDE
259         {
260                 User* dest = dynamic_cast<User*>(target);
261                 // check if its our metadata key, and its associated with a user
262                 if (dest && (extname == "accountname"))
263                 {
264                         std::string *account = accountname.get(dest);
265                         if (account && !account->empty())
266                         {
267                                 trim(*account);
268
269                                 if (IS_LOCAL(dest))
270                                         dest->WriteNumeric(900, "%s %s %s :You are now logged in as %s",
271                                                 dest->nick.c_str(), dest->GetFullHost().c_str(), account->c_str(), account->c_str());
272
273                                 AccountEvent(this, dest, *account).Send();
274                         }
275                         else
276                         {
277                                 AccountEvent(this, dest, "").Send();
278                         }
279                 }
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)
296