File: adminlog.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 (222 lines) | stat: -rw-r--r-- 7,086 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
 * 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/FileUtils.h>
#include <znc/Server.h>
#include <znc/IRCNetwork.h>
#include <znc/User.h>

#include <syslog.h>
#include <time.h>

class CAdminLogMod : public CModule {
  public:
    MODCONSTRUCTOR(CAdminLogMod) {
        AddHelpCommand();
        AddCommand("Show", "", t_d("Show the logging target"),
                   [=](const CString& sLine) { OnShowCommand(sLine); });
        AddCommand("Target", t_d("<file|syslog|both> [path]"),
                   t_d("Set the logging target"),
                   [=](const CString& sLine) { OnTargetCommand(sLine); });
        openlog("znc", LOG_PID, LOG_DAEMON);
    }

    ~CAdminLogMod() override {
        Log("Logging ended.");
        closelog();
    }

    bool OnLoad(const CString& sArgs, CString& sMessage) override {
        CString sTarget = GetNV("target");
        if (sTarget.Equals("syslog"))
            m_eLogMode = LOG_TO_SYSLOG;
        else if (sTarget.Equals("both"))
            m_eLogMode = LOG_TO_BOTH;
        else if (sTarget.Equals("file"))
            m_eLogMode = LOG_TO_FILE;
        else
            m_eLogMode = LOG_TO_FILE;

        SetLogFilePath(GetNV("path"));

        Log("Logging started. ZNC PID[" + CString(getpid()) + "] UID/GID[" +
            CString(getuid()) + ":" + CString(getgid()) + "]");
        return true;
    }

    void OnIRCConnected() override {
        Log("[" + GetUser()->GetUsername() + "/" + GetNetwork()->GetName() +
            "] connected to IRC: " +
            GetNetwork()->GetCurrentServer()->GetName());
    }

    void OnIRCDisconnected() override {
        Log("[" + GetUser()->GetUsername() + "/" + GetNetwork()->GetName() +
            "] disconnected from IRC");
    }

    EModRet OnRawMessage(CMessage& Message) override {
        if (Message.GetCommand().Equals("ERROR")) {
            // ERROR :Closing Link: nick[24.24.24.24] (Excess Flood)
            // ERROR :Closing Link: nick[24.24.24.24] Killer (Local kill by
            // Killer (reason))
            Log("[" + GetUser()->GetUsername() + "/" + GetNetwork()->GetName() +
                    "] disconnected from IRC: " +
                    GetNetwork()->GetCurrentServer()->GetName() + " [" +
                    Message.GetParamsColon(0) + "]",
                LOG_NOTICE);
        }
        return CONTINUE;
    }

    void OnClientLogin() override {
        Log("[" + GetUser()->GetUsername() + "] connected to ZNC from " +
            GetClient()->GetRemoteIP());
    }

    void OnClientDisconnect() override {
        Log("[" + GetUser()->GetUsername() + "] disconnected from ZNC from " +
            GetClient()->GetRemoteIP());
    }

    void OnFailedLogin(const CString& sUsername,
                       const CString& sRemoteIP) override {
        Log("[" + sUsername + "] failed to login from " + sRemoteIP,
            LOG_WARNING);
    }

    void SetLogFilePath(CString sPath) {
        if (sPath.empty()) {
            sPath = GetSavePath() + "/znc.log";
        }

        CFile LogFile(sPath);
        CString sLogDir = LogFile.GetDir();
        struct stat ModDirInfo;
        CFile::GetInfo(GetSavePath(), ModDirInfo);
        if (!CFile::Exists(sLogDir)) {
            CDir::MakeDir(sLogDir, ModDirInfo.st_mode);
        }

        m_sLogFile = sPath;
        SetNV("path", sPath);
    }

    void Log(CString sLine, int iPrio = LOG_INFO) {
        if (m_eLogMode & LOG_TO_SYSLOG) syslog(iPrio, "%s", sLine.c_str());

        if (m_eLogMode & LOG_TO_FILE) {
            time_t curtime;
            tm* timeinfo;
            char buf[23];

            time(&curtime);
            timeinfo = localtime(&curtime);
            strftime(buf, sizeof(buf), "[%Y-%m-%d %H:%M:%S] ", timeinfo);

            CFile LogFile(m_sLogFile);

            if (LogFile.Open(O_WRONLY | O_APPEND | O_CREAT))
                LogFile.Write(buf + sLine + "\n");
            else
                DEBUG("Failed to write to [" << m_sLogFile
                                             << "]: " << strerror(errno));
        }
    }

    void OnModCommand(const CString& sCommand) override {
        if (!GetUser()->IsAdmin()) {
            PutModule(t_s("Access denied"));
        } else {
            HandleCommand(sCommand);
        }
    }

    void OnTargetCommand(const CString& sCommand) {
        CString sArg = sCommand.Token(1, false);
        CString sTarget;
        CString sMessage;
        LogMode mode;

        if (sArg.Equals("file")) {
            sTarget = "file";
            sMessage = t_s("Now logging to file");
            mode = LOG_TO_FILE;
        } else if (sArg.Equals("syslog")) {
            sTarget = "syslog";
            sMessage = t_s("Now only logging to syslog");
            mode = LOG_TO_SYSLOG;
        } else if (sArg.Equals("both")) {
            sTarget = "both";
            sMessage = t_s("Now logging to syslog and file");
            mode = LOG_TO_BOTH;
        } else {
            if (sArg.empty()) {
                PutModule(t_s("Usage: Target <file|syslog|both> [path]"));
            } else {
                PutModule(t_s("Unknown target"));
            }
            return;
        }

        if (mode != LOG_TO_SYSLOG) {
            CString sPath = sCommand.Token(2, true);
            SetLogFilePath(sPath);
            sMessage += " [" + sPath + "]";
        }

        Log(sMessage);
        SetNV("target", sTarget);
        m_eLogMode = mode;
        PutModule(sMessage);
    }

    void OnShowCommand(const CString& sCommand) {
        CString sTarget;

        switch (m_eLogMode) {
            case LOG_TO_FILE:
                sTarget = t_s("Logging is enabled for file");
                break;
            case LOG_TO_SYSLOG:
                sTarget = t_s("Logging is enabled for syslog");
                break;
            case LOG_TO_BOTH:
                sTarget = t_s("Logging is enabled for both, file and syslog");
                break;
        }

        PutModule(sTarget);
        if (m_eLogMode != LOG_TO_SYSLOG)
            PutModule(t_f("Log file will be written to {1}")(m_sLogFile));
    }

  private:
    enum LogMode {
        LOG_TO_FILE = 1 << 0,
        LOG_TO_SYSLOG = 1 << 1,
        LOG_TO_BOTH = LOG_TO_FILE | LOG_TO_SYSLOG
    };
    LogMode m_eLogMode = LOG_TO_FILE;
    CString m_sLogFile;
};

template <>
void TModInfo<CAdminLogMod>(CModInfo& Info) {
    Info.SetWikiPage("adminlog");
}

GLOBALMODULEDEFS(CAdminLogMod, t_s("Log ZNC events to file and/or syslog."))