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