~ubuntu-branches/ubuntu/quantal/znc/quantal-backports

« back to all changes in this revision

Viewing changes to modules/ctcpflood.cpp

  • Committer: Package Import Robot
  • Author(s): Micah Gersten
  • Date: 2013-01-01 19:39:47 UTC
  • mfrom: (21.1.4 experimental)
  • Revision ID: package-import@ubuntu.com-20130101193947-zlqyse3v2mjxnhvw
Tags: 1.0-2~ubuntu12.10.1
No-change backport to quantal (LP: #1085731)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2004-2012  See the AUTHORS file for details.
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify it
 
5
 * under the terms of the GNU General Public License version 2 as published
 
6
 * by the Free Software Foundation.
 
7
 */
 
8
 
 
9
#include <znc/Modules.h>
 
10
#include <znc/Chan.h>
 
11
 
 
12
class CCtcpFloodMod : public CModule {
 
13
public:
 
14
        MODCONSTRUCTOR(CCtcpFloodMod) {
 
15
                m_tLastCTCP = 0;
 
16
                m_iNumCTCP = 0;
 
17
        }
 
18
 
 
19
        ~CCtcpFloodMod() {
 
20
        }
 
21
 
 
22
        void Save() {
 
23
                // We save the settings twice because the module arguments can
 
24
                // be more easily edited via webadmin, while the SetNV() stuff
 
25
                // survives e.g. /msg *status reloadmod ctcpflood.
 
26
                SetNV("secs", CString(m_iThresholdSecs));
 
27
                SetNV("msgs", CString(m_iThresholdMsgs));
 
28
 
 
29
                SetArgs(CString(m_iThresholdMsgs) + " " + CString(m_iThresholdSecs));
 
30
        }
 
31
 
 
32
        bool OnLoad(const CString& sArgs, CString& sMessage) {
 
33
                m_iThresholdMsgs = sArgs.Token(0).ToUInt();
 
34
                m_iThresholdSecs = sArgs.Token(1).ToUInt();
 
35
 
 
36
                if (m_iThresholdMsgs == 0 || m_iThresholdSecs == 0) {
 
37
                        m_iThresholdMsgs = GetNV("msgs").ToUInt();
 
38
                        m_iThresholdSecs = GetNV("secs").ToUInt();
 
39
                }
 
40
 
 
41
                if (m_iThresholdSecs == 0)
 
42
                        m_iThresholdSecs = 2;
 
43
                if (m_iThresholdMsgs == 0)
 
44
                        m_iThresholdMsgs = 4;
 
45
 
 
46
                Save();
 
47
 
 
48
                return true;
 
49
        }
 
50
 
 
51
        EModRet Message(const CNick& Nick, const CString& sMessage) {
 
52
                // We never block /me, because it doesn't cause a reply
 
53
                if (sMessage.Token(0).Equals("ACTION"))
 
54
                        return CONTINUE;
 
55
 
 
56
                if (m_tLastCTCP + m_iThresholdSecs < time(NULL)) {
 
57
                        m_tLastCTCP = time(NULL);
 
58
                        m_iNumCTCP = 0;
 
59
                }
 
60
 
 
61
                m_iNumCTCP++;
 
62
 
 
63
                if (m_iNumCTCP < m_iThresholdMsgs)
 
64
                        return CONTINUE;
 
65
                else if (m_iNumCTCP == m_iThresholdMsgs)
 
66
                        PutModule("Limit reached by [" + Nick.GetHostMask() + "], blocking all CTCP");
 
67
 
 
68
                // Reset the timeout so that we continue blocking messages
 
69
                m_tLastCTCP = time(NULL);
 
70
 
 
71
                return HALT;
 
72
        }
 
73
 
 
74
        EModRet OnPrivCTCP(CNick& Nick, CString& sMessage) {
 
75
                return Message(Nick, sMessage);
 
76
        }
 
77
 
 
78
        EModRet OnChanCTCP(CNick& Nick, CChan& Channel, CString& sMessage) {
 
79
                return Message(Nick, sMessage);
 
80
        }
 
81
 
 
82
        void OnModCommand(const CString& sCommand) {
 
83
                const CString& sCmd = sCommand.Token(0);
 
84
                const CString& sArg = sCommand.Token(1, true);
 
85
 
 
86
                if (sCmd.Equals("secs") && !sArg.empty()) {
 
87
                        m_iThresholdSecs = sArg.ToUInt();
 
88
                        if (m_iThresholdSecs == 0)
 
89
                                m_iThresholdSecs = 1;
 
90
 
 
91
                        PutModule("Set seconds limit to [" + CString(m_iThresholdSecs) + "]");
 
92
                        Save();
 
93
                } else if (sCmd.Equals("lines") && !sArg.empty()) {
 
94
                        m_iThresholdMsgs = sArg.ToUInt();
 
95
                        if (m_iThresholdMsgs == 0)
 
96
                                m_iThresholdMsgs = 2;
 
97
 
 
98
                        PutModule("Set lines limit to [" + CString(m_iThresholdMsgs) + "]");
 
99
                        Save();
 
100
                } else if (sCmd.Equals("show")) {
 
101
                        PutModule("Current limit is " + CString(m_iThresholdMsgs) + " CTCPs "
 
102
                                        "in " + CString(m_iThresholdSecs) + " secs");
 
103
                } else {
 
104
                        PutModule("Commands: show, secs [limit], lines [limit]");
 
105
                }
 
106
        }
 
107
 
 
108
private:
 
109
        time_t m_tLastCTCP;
 
110
        unsigned int m_iNumCTCP;
 
111
 
 
112
        time_t m_iThresholdSecs;
 
113
        unsigned int m_iThresholdMsgs;
 
114
};
 
115
 
 
116
template<> void TModInfo<CCtcpFloodMod>(CModInfo& Info) {
 
117
        Info.SetWikiPage("ctcpflood");
 
118
        Info.SetHasArgs(true);
 
119
        Info.SetArgsHelpText("This user module takes none to two arguments. The first argument is the number of lines after which the flood-protection is triggered. The second argument is the time (s) to in which the number of lines is reached. The default setting is 4 CTCPs in 2 seconds");
 
120
}
 
121
 
 
122
USERMODULEDEFS(CCtcpFloodMod, "Don't forward CTCP floods to clients")