]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_passforward.cpp
Fix some of the include guard names (requested by SaberUK)
[user/henk/code/inspircd.git] / src / modules / m_passforward.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15
16 class ModulePassForward : public Module
17 {
18  private:
19         std::string nickrequired, forwardmsg, forwardcmd;
20
21  public:
22         ModulePassForward()
23         {
24                 OnRehash(NULL);
25                 Implementation eventlist[] = { I_OnPostConnect, I_OnRehash };
26                 ServerInstance->Modules->Attach(eventlist, this, 2);
27         }
28
29         Version GetVersion()
30         {
31                 return Version("Sends server password to NickServ", VF_VENDOR);
32         }
33
34         void OnRehash(User* user)
35         {
36                 ConfigReader Conf;
37                 nickrequired = Conf.ReadValue("passforward", "nick", "NickServ", 0);
38                 forwardmsg = Conf.ReadValue("passforward", "forwardmsg", "NOTICE $nick :*** Forwarding PASS to $nickrequired", 0);
39                 forwardcmd = Conf.ReadValue("passforward", "cmd", "PRIVMSG $nickrequired :IDENTIFY $pass", 0);
40         }
41
42         void FormatStr(std::string& result, const std::string& format, const std::string &nick, const std::string &pass)
43         {
44                 for (unsigned int i = 0; i < format.length(); i++)
45                 {
46                         char c = format[i];
47                         if (c == '$')
48                         {
49                                 if (format.substr(i, 13) == "$nickrequired")
50                                 {
51                                         result.append(nickrequired);
52                                         i += 12;
53                                 }
54                                 else if (format.substr(i, 5) == "$nick")
55                                 {
56                                         result.append(nick);
57                                         i += 4;
58                                 }
59                                 else if (format.substr(i,5) == "$pass")
60                                 {
61                                         result.append(pass);
62                                         i += 4;
63                                 }
64                                 else
65                                         result.push_back(c);
66                         }
67                         else
68                                 result.push_back(c);
69                 }
70         }
71
72         virtual void OnPostConnect(User* ruser)
73         {
74                 LocalUser* user = IS_LOCAL(ruser);
75                 if (!user || user->password.empty())
76                         return;
77
78                 if (!nickrequired.empty())
79                 {
80                         /* Check if nick exists and its server is ulined */
81                         User* u = ServerInstance->FindNick(nickrequired.c_str());
82                         if (!u || !ServerInstance->ULine(u->server))
83                                 return;
84                 }
85
86                 std::string tmp;
87                 FormatStr(tmp,forwardmsg, user->nick, user->password);
88                 user->WriteServ(tmp);
89
90                 tmp.clear();
91                 FormatStr(tmp,forwardcmd, user->nick, user->password);
92                 ServerInstance->Parser->ProcessBuffer(tmp,user);
93         }
94 };
95
96 MODULE_INIT(ModulePassForward)
97