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