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