]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_vhost.cpp
Some more text fixes and improvements (#1618).
[user/henk/code/inspircd.git] / src / modules / m_vhost.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
6  *   Copyright (C) 2006-2007 Craig Edwards <craigedwards@brainbox.cc>
7  *   Copyright (C) 2006 Robin Burchell <robin+git@viroteck.net>
8  *
9  * This file is part of InspIRCd.  InspIRCd is free software: you can
10  * redistribute it and/or modify it under the terms of the GNU General Public
11  * License as published by the Free Software Foundation, version 2.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22
23 #include "inspircd.h"
24
25 struct CustomVhost
26 {
27         const std::string name;
28         const std::string password;
29         const std::string hash;
30         const std::string vhost;
31
32         CustomVhost(const std::string& n, const std::string& p, const std::string& h, const std::string& v)
33                 : name(n)
34                 , password(p)
35                 , hash(h)
36                 , vhost(v)
37         {
38         }
39
40         bool CheckPass(User* user, const std::string& pass) const
41         {
42                 return ServerInstance->PassCompare(user, password, pass, hash);
43         }
44 };
45
46 typedef std::multimap<std::string, CustomVhost> CustomVhostMap;
47 typedef std::pair<CustomVhostMap::iterator, CustomVhostMap::iterator> MatchingConfigs;
48
49 /** Handle /VHOST
50  */
51 class CommandVhost : public Command
52 {
53  public:
54         CustomVhostMap vhosts;
55
56         CommandVhost(Module* Creator)
57                 : Command(Creator, "VHOST", 2)
58         {
59                 syntax = "<username> <password>";
60         }
61
62         CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
63         {
64                 MatchingConfigs matching = vhosts.equal_range(parameters[0]);
65
66                 for (MatchingConfigs::first_type i = matching.first; i != matching.second; ++i)
67                 {
68                         CustomVhost config = i->second;
69                         if (config.CheckPass(user, parameters[1]))
70                         {
71                                 user->WriteNotice("Setting your VHost: " + config.vhost);
72                                 user->ChangeDisplayedHost(config.vhost);
73                                 return CMD_SUCCESS;
74                         }
75                 }
76
77                 user->WriteNotice("Invalid username or password.");
78                 return CMD_FAILURE;
79         }
80 };
81
82 class ModuleVHost : public Module
83 {
84         CommandVhost cmd;
85
86  public:
87         ModuleVHost()
88                 : cmd(this)
89         {
90         }
91
92         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
93         {
94                 CustomVhostMap newhosts;
95                 ConfigTagList tags = ServerInstance->Config->ConfTags("vhost");
96                 for (ConfigIter i = tags.first; i != tags.second; ++i)
97                 {
98                         ConfigTag* tag = i->second;
99                         std::string mask = tag->getString("host");
100                         if (mask.empty())
101                                 throw ModuleException("<vhost:host> is empty! at " + tag->getTagLocation());
102                         std::string username = tag->getString("user");
103                         if (username.empty())
104                                 throw ModuleException("<vhost:user> is empty! at " + tag->getTagLocation());
105                         std::string pass = tag->getString("pass");
106                         if (pass.empty())
107                                 throw ModuleException("<vhost:pass> is empty! at " + tag->getTagLocation());
108                         std::string hash = tag->getString("hash");
109
110                         CustomVhost vhost(username, pass, hash, mask);
111                         newhosts.insert(std::make_pair(username, vhost));
112                 }
113
114                 cmd.vhosts.swap(newhosts);
115         }
116
117         Version GetVersion() CXX11_OVERRIDE
118         {
119                 return Version("Provides masking of user hostnames via the VHOST command", VF_VENDOR);
120         }
121 };
122
123 MODULE_INIT(ModuleVHost)