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