-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
138 lines (114 loc) · 2.78 KB
/
handlers.go
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
// ratelimiter Project
// Copyright (C) 2021~2022 ALiwoto and other Contributors
// This file is subject to the terms and conditions defined in
// file 'LICENSE', which is part of the source code.
package ratelimiter
import (
"time"
"github.com/PaulSonOfLars/gotgbot/v2"
"github.com/PaulSonOfLars/gotgbot/v2/ext"
)
// limiterFilter is the filter method for message types.
func (l *Limiter) limiterFilter(msg *gotgbot.Message) bool {
if !l.isEnabled || l.isStopped || !l.hasTextCondition(msg) {
return false
}
if l.IgnoreMediaGroup && len(msg.MediaGroupId) != 0 {
return false
}
if l.isException(msg) && !l.isIgnoredException(msg) {
return false
}
if len(l.exceptions) != 0 {
for _, ex := range l.exceptions {
if ex(msg) {
return false
}
}
}
if len(l.conditions) != 0 {
for _, con := range l.conditions {
if !con(msg) {
return false
}
}
}
return true
}
// callbackFilter is the filter method for callback queries.
func (l *Limiter) callbackFilter(cq *gotgbot.CallbackQuery) bool {
if !l.isEnabled || l.isStopped || !l.ConsiderInline {
return false
}
if l.isExceptionQuery(cq) && !l.isIgnoredExceptionQuery(cq) {
return false
}
return true
}
// limiterHandler is the main handler method.
func (l *Limiter) limiterHandler(b *gotgbot.Bot, ctx *ext.Context) error {
var status *UserStatus
var id int64
if l.ConsiderUser && ctx.EffectiveUser != nil {
id = ctx.EffectiveUser.Id
} else if ctx.EffectiveChat != nil {
id = ctx.EffectiveChat.Id
} else {
return ext.ContinueGroups
}
l.mutex.Lock()
status = l.userMap[id]
if status == nil {
status = new(UserStatus)
status.Last = time.Now()
status.count++
l.userMap[id] = status
l.mutex.Unlock()
if status.IsCustomLimited() {
if !status.custom.ignoreException && l.isExceptionCtx(ctx) {
return ext.ContinueGroups
}
return ext.EndGroups
}
return ext.ContinueGroups
}
if status.limited {
l.mutex.Unlock()
if time.Since(status.Last) > l.timeout+l.punishment {
status.count = 0
status.limited = false
status.Last = time.Now()
return ext.ContinueGroups
}
if l.IsStrict {
status.Last = time.Now()
}
return ext.EndGroups
}
if time.Since(status.Last) > l.timeout {
status.count = 0
}
if !l.isExceptionCtx(ctx) {
status.count++
}
if status.count > l.maxCount {
status.limited = true
status.Last = time.Now()
l.mutex.Unlock()
// check for triggers length to prevent from creating
// a new goroutine in the case we have no triggers.
if len(l.triggers) != 0 {
go l.runTriggers(b, ctx)
}
return ext.EndGroups
}
l.mutex.Unlock()
status.Last = time.Now()
if status.IsCustomLimited() {
if !status.custom.ignoreException && l.isExceptionCtx(ctx) {
return ext.ContinueGroups
}
return ext.EndGroups
}
return ext.ContinueGroups
}