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