File: nickserv.cpp

package info (click to toggle)
znc 1.10.1-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 12,164 kB
  • sloc: cpp: 58,072; javascript: 11,859; python: 1,635; perl: 1,229; tcl: 219; sh: 200; ansic: 187; makefile: 82
file content (150 lines) | stat: -rw-r--r-- 5,724 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
 * Copyright (C) 2004-2025 ZNC, see the NOTICE file for details.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <znc/Modules.h>
#include <znc/User.h>

class CNickServ : public CModule {
    void DoNickCommand(const CString& sCmd, const CString& sNick) {
        MCString msValues;
        msValues["nickname"] = sNick;
        msValues["password"] = GetNV("Password");
        PutIRC(CString::NamedFormat(GetNV(sCmd), msValues));
    }

  public:
    void SetCommand(const CString& sLine) {
        SetNV("Password", sLine.Token(1, true));
        PutModule(t_s("Password set"));
    }

    void ClearCommand(const CString& sLine) {
        DelNV("Password");
        PutModule(t_s("Done"));
    }

    void SetNSNameCommand(const CString& sLine) {
        SetNV("NickServName", sLine.Token(1, true));
        PutModule(t_s("NickServ name set"));
    }

    void ClearNSNameCommand(const CString& sLine) {
        DelNV("NickServName");
        PutModule(t_s("Done"));
    }

    void ViewCommandsCommand(const CString& sLine) {
        PutModule("IDENTIFY " + GetNV("IdentifyCmd"));
    }

    void SetCommandCommand(const CString& sLine) {
        CString sCmd = sLine.Token(1);
        CString sNewCmd = sLine.Token(2, true);
        if (sCmd.Equals("IDENTIFY")) {
            SetNV("IdentifyCmd", sNewCmd);
        } else {
            PutModule(
                t_s("No such editable command. See ViewCommands for list."));
            return;
        }
        PutModule(t_s("Ok"));
    }

    MODCONSTRUCTOR(CNickServ) {
        AddHelpCommand();
        AddCommand("Set", t_d("password"), t_d("Set your nickserv password"),
                   [=](const CString& sLine) { SetCommand(sLine); });
        AddCommand("Clear", "", t_d("Clear your nickserv password"),
                   [=](const CString& sLine) { ClearCommand(sLine); });
        AddCommand("SetNSName", t_d("nickname"),
                   t_d("Set NickServ name (Useful on networks like EpiKnet, "
                       "where NickServ is named Themis"),
                   [=](const CString& sLine) { SetNSNameCommand(sLine); });
        AddCommand("ClearNSName", "",
                   t_d("Reset NickServ name to default (NickServ)"),
                   [=](const CString& sLine) { ClearNSNameCommand(sLine); });
        AddCommand(
            "ViewCommands", "",
            t_d("Show patterns for lines, which are being sent to NickServ"),
            [=](const CString& sLine) { ViewCommandsCommand(sLine); });
        AddCommand("SetCommand", t_d("cmd new-pattern"),
                   t_d("Set pattern for commands"),
                   [=](const CString& sLine) { SetCommandCommand(sLine); });
    }

    ~CNickServ() override {}

    bool OnLoad(const CString& sArgs, CString& sMessage) override {
        if (!sArgs.empty() && sArgs != "<hidden>") {
            SetNV("Password", sArgs);
            SetArgs("<hidden>");
        }

        if (GetNV("IdentifyCmd").empty()) {
            SetNV("IdentifyCmd", "NICKSERV IDENTIFY {password}");
        }

        return true;
    }

    void HandleMessage(CNick& Nick, const CString& sMessage) {
        CString sNickServName = (!GetNV("NickServName").empty())
                                    ? GetNV("NickServName")
                                    : "NickServ";
        if (!GetNV("Password").empty() && Nick.NickEquals(sNickServName) &&
            (sMessage.find("msg") != CString::npos ||
             sMessage.find("authenticate") != CString::npos ||
             sMessage.find("choose a different nickname") != CString::npos ||
             sMessage.find("please choose a different nick") != CString::npos ||
             sMessage.find("If this is your nick, identify yourself with") !=
                 CString::npos ||
             sMessage.find("If this is your nick, type") != CString::npos ||
             sMessage.find("This is a registered nickname, please identify") !=
                 CString::npos ||
             sMessage.find("is a registered nick - you must auth to account") !=
                 CString::npos ||
             sMessage.StripControls_n().find(
                 "type /NickServ IDENTIFY password") != CString::npos ||
             sMessage.StripControls_n().find(
                 "type /msg NickServ IDENTIFY password") != CString::npos) &&
            sMessage.AsUpper().find("IDENTIFY") != CString::npos &&
            sMessage.find("help") == CString::npos) {
            MCString msValues;
            msValues["password"] = GetNV("Password");
            PutIRC(CString::NamedFormat(GetNV("IdentifyCmd"), msValues));
        }
    }

    EModRet OnPrivMsg(CNick& Nick, CString& sMessage) override {
        HandleMessage(Nick, sMessage);
        return CONTINUE;
    }

    EModRet OnPrivNotice(CNick& Nick, CString& sMessage) override {
        HandleMessage(Nick, sMessage);
        return CONTINUE;
    }
};

template <>
void TModInfo<CNickServ>(CModInfo& Info) {
    Info.SetWikiPage("nickserv");
    Info.SetHasArgs(true);
    Info.SetArgsHelpText(Info.t_s("Please enter your nickserv password."));
}

NETWORKMODULEDEFS(CNickServ,
                  t_s("Auths you with NickServ (prefer SASL module instead)"))