]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_passforward.cpp
Use SQUERY instead of PRIVMSG in alias/passforward config.
[user/henk/code/inspircd.git] / src / modules / m_passforward.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2010 Daniel De Graaf <danieldg@inspircd.org>
5  *
6  * This file is part of InspIRCd.  InspIRCd is free software: you can
7  * redistribute it and/or modify it under the terms of the GNU General Public
8  * License as published by the Free Software Foundation, version 2.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19
20 #include "inspircd.h"
21 #include "modules/account.h"
22
23 class ModulePassForward : public Module
24 {
25         std::string nickrequired, forwardmsg, forwardcmd;
26
27  public:
28         Version GetVersion() CXX11_OVERRIDE
29         {
30                 return Version("Sends server password to NickServ", VF_VENDOR);
31         }
32
33         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
34         {
35                 ConfigTag* tag = ServerInstance->Config->ConfValue("passforward");
36                 nickrequired = tag->getString("nick", "NickServ");
37                 forwardmsg = tag->getString("forwardmsg", "NOTICE $nick :*** Forwarding PASS to $nickrequired");
38                 forwardcmd = tag->getString("cmd", "SQUERY $nickrequired :IDENTIFY $pass");
39         }
40
41         void FormatStr(std::string& result, const std::string& format, const LocalUser* user)
42         {
43                 for (unsigned int i = 0; i < format.length(); i++)
44                 {
45                         char c = format[i];
46                         if (c == '$')
47                         {
48                                 if (!format.compare(i, 13, "$nickrequired", 13))
49                                 {
50                                         result.append(nickrequired);
51                                         i += 12;
52                                 }
53                                 else if (!format.compare(i, 5, "$nick", 5))
54                                 {
55                                         result.append(user->nick);
56                                         i += 4;
57                                 }
58                                 else if (!format.compare(i, 5, "$user", 5))
59                                 {
60                                         result.append(user->ident);
61                                         i += 4;
62                                 }
63                                 else if (!format.compare(i, 5, "$pass", 5))
64                                 {
65                                         result.append(user->password);
66                                         i += 4;
67                                 }
68                                 else
69                                         result.push_back(c);
70                         }
71                         else
72                                 result.push_back(c);
73                 }
74         }
75
76         void OnPostConnect(User* ruser) CXX11_OVERRIDE
77         {
78                 LocalUser* user = IS_LOCAL(ruser);
79                 if (!user || user->password.empty())
80                         return;
81
82                 // If the connect class requires a password, don't forward it
83                 if (!user->MyClass->config->getString("password").empty())
84                         return;
85
86                 AccountExtItem* actext = GetAccountExtItem();
87                 if (actext && actext->get(user))
88                 {
89                         // User is logged in already (probably via SASL) don't forward the password
90                         return;
91                 }
92
93                 if (!nickrequired.empty())
94                 {
95                         /* Check if nick exists and its server is ulined */
96                         User* u = ServerInstance->FindNick(nickrequired);
97                         if (!u || !u->server->IsULine())
98                                 return;
99                 }
100
101                 std::string tmp;
102                 FormatStr(tmp, forwardmsg, user);
103                 ServerInstance->Parser.ProcessBuffer(user, tmp);
104
105                 tmp.clear();
106                 FormatStr(tmp,forwardcmd, user);
107                 ServerInstance->Parser.ProcessBuffer(user, tmp);
108         }
109 };
110
111 MODULE_INIT(ModulePassForward)