]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_passforward.cpp
8cdd343b1409765888d30b353e3f0ece7c74f86e
[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
22 class ModulePassForward : public Module
23 {
24         std::string nickrequired, forwardmsg, forwardcmd;
25
26  public:
27         Version GetVersion() CXX11_OVERRIDE
28         {
29                 return Version("Sends server password to NickServ", VF_VENDOR);
30         }
31
32         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
33         {
34                 ConfigTag* tag = ServerInstance->Config->ConfValue("passforward");
35                 nickrequired = tag->getString("nick", "NickServ");
36                 forwardmsg = tag->getString("forwardmsg", "NOTICE $nick :*** Forwarding PASS to $nickrequired");
37                 forwardcmd = tag->getString("cmd", "PRIVMSG $nickrequired :IDENTIFY $pass");
38         }
39
40         void FormatStr(std::string& result, const std::string& format, const LocalUser* user)
41         {
42                 for (unsigned int i = 0; i < format.length(); i++)
43                 {
44                         char c = format[i];
45                         if (c == '$')
46                         {
47                                 if (!format.compare(i, 13, "$nickrequired", 13))
48                                 {
49                                         result.append(nickrequired);
50                                         i += 12;
51                                 }
52                                 else if (!format.compare(i, 5, "$nick", 5))
53                                 {
54                                         result.append(user->nick);
55                                         i += 4;
56                                 }
57                                 else if (!format.compare(i, 5, "$user", 5))
58                                 {
59                                         result.append(user->ident);
60                                         i += 4;
61                                 }
62                                 else if (!format.compare(i, 5, "$pass", 5))
63                                 {
64                                         result.append(user->password);
65                                         i += 4;
66                                 }
67                                 else
68                                         result.push_back(c);
69                         }
70                         else
71                                 result.push_back(c);
72                 }
73         }
74
75         void OnPostConnect(User* ruser) CXX11_OVERRIDE
76         {
77                 LocalUser* user = IS_LOCAL(ruser);
78                 if (!user || user->password.empty())
79                         return;
80
81                 // If the connect class requires a password, don't forward it
82                 if (!user->MyClass->config->getString("password").empty())
83                         return;
84
85                 if (!nickrequired.empty())
86                 {
87                         /* Check if nick exists and its server is ulined */
88                         User* u = ServerInstance->FindNick(nickrequired);
89                         if (!u || !u->server->IsULine())
90                                 return;
91                 }
92
93                 std::string tmp;
94                 FormatStr(tmp,forwardmsg, user);
95                 user->WriteServ(tmp);
96
97                 tmp.clear();
98                 FormatStr(tmp,forwardcmd, user);
99                 ServerInstance->Parser->ProcessBuffer(tmp,user);
100         }
101 };
102
103 MODULE_INIT(ModulePassForward)