File: flooddetach.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 (251 lines) | stat: -rw-r--r-- 8,387 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
 * 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/Chan.h>
#include <znc/IRCNetwork.h>
#include <time.h>

using std::map;

class CFloodDetachMod : public CModule {
  public:
    MODCONSTRUCTOR(CFloodDetachMod) {
        m_iThresholdSecs = 0;
        m_iThresholdMsgs = 0;

        AddHelpCommand();
        AddCommand("Show", "", t_d("Show current limits"),
                   [=](const CString& sLine) { ShowCommand(sLine); });
        AddCommand("Secs", t_d("[<limit>]"),
                   t_d("Show or set number of seconds in the time interval"),
                   [=](const CString& sLine) { SecsCommand(sLine); });
        AddCommand("Lines", t_d("[<limit>]"),
                   t_d("Show or set number of lines in the time interval"),
                   [=](const CString& sLine) { LinesCommand(sLine); });
        AddCommand("Silent", "[yes|no]",
                   t_d("Show or set whether to notify you about detaching and "
                       "attaching back"),
                   [=](const CString& sLine) { SilentCommand(sLine); });
    }

    ~CFloodDetachMod() override {}

    void Save() {
        // We save the settings twice because the module arguments can
        // be more easily edited via webadmin, while the SetNV() stuff
        // survives e.g. /msg *status reloadmod ctcpflood.
        SetNV("secs", CString(m_iThresholdSecs));
        SetNV("msgs", CString(m_iThresholdMsgs));

        SetArgs(CString(m_iThresholdMsgs) + " " + CString(m_iThresholdSecs));
    }

    bool OnLoad(const CString& sArgs, CString& sMessage) override {
        m_iThresholdMsgs = sArgs.Token(0).ToUInt();
        m_iThresholdSecs = sArgs.Token(1).ToUInt();

        if (m_iThresholdMsgs == 0 || m_iThresholdSecs == 0) {
            m_iThresholdMsgs = GetNV("msgs").ToUInt();
            m_iThresholdSecs = GetNV("secs").ToUInt();
        }

        if (m_iThresholdSecs == 0) m_iThresholdSecs = 2;
        if (m_iThresholdMsgs == 0) m_iThresholdMsgs = 5;

        Save();

        return true;
    }

    void OnIRCDisconnected() override { m_chans.clear(); }

    void Cleanup() {
        Limits::iterator it;
        time_t now = time(nullptr);

        for (it = m_chans.begin(); it != m_chans.end(); ++it) {
            // The timeout for this channel did not expire yet?
            if (it->second.first + (time_t)m_iThresholdSecs >= now) continue;

            CChan* pChan = GetNetwork()->FindChan(it->first);
            if (it->second.second >= m_iThresholdMsgs && pChan &&
                pChan->IsDetached()) {
                // The channel is detached and it is over the
                // messages limit. Since we only track those
                // limits for non-detached channels or for
                // channels which we detached, this means that
                // we detached because of a flood.

                if (!GetNV("silent").ToBool()) {
                    PutModule(t_f("Flood in {1} is over, reattaching...")(
                        pChan->GetName()));
                }
                // No buffer playback, makes sense, doesn't it?
                pChan->ClearBuffer();
                pChan->AttachUser();
            }

            Limits::iterator it2 = it++;
            m_chans.erase(it2);

            // Without this Bad Things (tm) could happen
            if (it == m_chans.end()) break;
        }
    }

    void Message(CChan& Channel) {
        Limits::iterator it;
        time_t now = time(nullptr);

        // First: Clean up old entries and reattach where necessary
        Cleanup();

        it = m_chans.find(Channel.GetName());

        if (it == m_chans.end()) {
            // We don't track detached channels
            if (Channel.IsDetached()) return;

            // This is the first message for this channel, start a
            // new timeout.
            std::pair<time_t, unsigned int> tmp(now, 1);
            m_chans[Channel.GetName()] = tmp;
            return;
        }

        // No need to check it->second.first (expiry time), since
        // Cleanup() would have removed it if it was expired.

        if (it->second.second >= m_iThresholdMsgs) {
            // The channel already hit the limit and we detached the
            // user, but it is still being flooded, reset the timeout
            it->second.first = now;
            it->second.second++;
            return;
        }

        it->second.second++;

        if (it->second.second < m_iThresholdMsgs) return;

        // The channel hit the limit, reset the timeout so that we keep
        // it detached for longer.
        it->second.first = now;

        Channel.DetachUser();
        if (!GetNV("silent").ToBool()) {
            PutModule(t_f("Channel {1} was flooded, you've been detached")(
                Channel.GetName()));
        }
    }

    EModRet OnChanMsg(CNick& Nick, CChan& Channel, CString& sMessage) override {
        Message(Channel);
        return CONTINUE;
    }

    // This also catches OnChanAction()
    EModRet OnChanCTCP(CNick& Nick, CChan& Channel,
                       CString& sMessage) override {
        Message(Channel);
        return CONTINUE;
    }

    EModRet OnChanNotice(CNick& Nick, CChan& Channel,
                         CString& sMessage) override {
        Message(Channel);
        return CONTINUE;
    }

    EModRet OnTopic(CNick& Nick, CChan& Channel, CString& sTopic) override {
        Message(Channel);
        return CONTINUE;
    }

    void OnNick(const CNick& Nick, const CString& sNewNick,
                const std::vector<CChan*>& vChans) override {
        for (CChan* pChan : vChans) {
            Message(*pChan);
        }
    }

    void ShowCommand(const CString& sLine) {
        CString sLines =
            t_p("1 line", "{1} lines", m_iThresholdMsgs)(m_iThresholdMsgs);
        CString sSeconds = t_p("every second", "every {1} seconds",
                               m_iThresholdSecs)(m_iThresholdSecs);
        PutModule(t_f("Current limit is {1} {2}")(sLines, sSeconds));
    }

    void SecsCommand(const CString& sLine) {
        const CString sArg = sLine.Token(1, true);

        if (sArg.empty()) {
            PutModule(t_f("Seconds limit is {1}")(m_iThresholdSecs));
        } else {
            m_iThresholdSecs = sArg.ToUInt();
            if (m_iThresholdSecs == 0) m_iThresholdSecs = 1;

            PutModule(t_f("Set seconds limit to {1}")(m_iThresholdSecs));
            Save();
        }
    }

    void LinesCommand(const CString& sLine) {
        const CString sArg = sLine.Token(1, true);

        if (sArg.empty()) {
            PutModule(t_f("Lines limit is {1}")(m_iThresholdMsgs));
        } else {
            m_iThresholdMsgs = sArg.ToUInt();
            if (m_iThresholdMsgs == 0) m_iThresholdMsgs = 2;

            PutModule(t_f("Set lines limit to {1}")(m_iThresholdMsgs));
            Save();
        }
    }

    void SilentCommand(const CString& sLine) {
        const CString sArg = sLine.Token(1, true);

        if (!sArg.empty()) {
            SetNV("silent", CString(sArg.ToBool()));
        }

        if (GetNV("silent").ToBool()) {
            PutModule(t_s("Module messages are disabled"));
        } else {
            PutModule(t_s("Module messages are enabled"));
        }
    }

  private:
    typedef map<CString, std::pair<time_t, unsigned int>> Limits;
    Limits m_chans;
    unsigned int m_iThresholdSecs;
    unsigned int m_iThresholdMsgs;
};

template <>
void TModInfo<CFloodDetachMod>(CModInfo& Info) {
    Info.SetWikiPage("flooddetach");
    Info.SetHasArgs(true);
    Info.SetArgsHelpText(
        Info.t_s("This user module takes up to two arguments. Arguments are "
                 "numbers of messages and seconds."));
}

USERMODULEDEFS(CFloodDetachMod, t_s("Detach channels when flooded"))