-
Notifications
You must be signed in to change notification settings - Fork 6
/
ban.go
109 lines (104 loc) · 3.4 KB
/
ban.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
package control
import (
"crypto/md5"
"encoding/binary"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/wdvxdr1123/ZeroBot/utils/helper"
)
// Ban 禁止某人在某群使用本插件
func (m *Control[CTX]) Ban(uid, gid int64) {
var err error
var digest [16]byte
if gid != 0 { // 特定群
digest = md5.Sum(helper.StringToBytes(fmt.Sprintf("[%s]%d_%d", m.Service, uid, gid)))
id := binary.LittleEndian.Uint64(digest[:8])
m.Manager.Lock()
err = m.Manager.D.Insert(m.Service+"ban", &BanStatus{ID: int64(id), UserID: uid, GroupID: gid})
m.Manager.banCache[id] = true
m.Manager.Unlock()
if err == nil {
log.Debugf("[control] plugin %s is banned in grp %d for usr %d.", m.Service, gid, uid)
return
}
}
// 所有群
digest = md5.Sum(helper.StringToBytes(fmt.Sprintf("[%s]%d_all", m.Service, uid)))
id := binary.LittleEndian.Uint64(digest[:8])
m.Manager.Lock()
err = m.Manager.D.Insert(m.Service+"ban", &BanStatus{ID: int64(id), UserID: uid, GroupID: 0})
m.Manager.banCache[id] = true
m.Manager.Unlock()
if err == nil {
log.Debugf("[control] plugin %s is banned in all grp for usr %d.", m.Service, uid)
}
}
// Permit 允许某人在某群使用本插件
func (m *Control[CTX]) Permit(uid, gid int64) {
var digest [16]byte
if gid != 0 { // 特定群
digest = md5.Sum(helper.StringToBytes(fmt.Sprintf("[%s]%d_%d", m.Service, uid, gid)))
id := binary.LittleEndian.Uint64(digest[:8])
m.Manager.Lock()
_ = m.Manager.D.Del(m.Service+"ban", "WHERE id = ?", id)
m.Manager.banCache[id] = false
m.Manager.Unlock()
log.Debugf("[control] plugin %s is permitted in grp %d for usr %d.", m.Service, gid, uid)
return
}
// 所有群
digest = md5.Sum(helper.StringToBytes(fmt.Sprintf("[%s]%d_all", m.Service, uid)))
id := binary.LittleEndian.Uint64(digest[:8])
m.Manager.Lock()
_ = m.Manager.D.Del(m.Service+"ban", "WHERE id = ?", id)
m.Manager.banCache[id] = false
m.Manager.Unlock()
log.Debugf("[control] plugin %s is permitted in all grp for usr %d.", m.Service, uid)
}
// IsBannedIn 某人是否在某群被 ban
func (m *Control[CTX]) IsBannedIn(uid, gid int64) bool {
var b BanStatus
var err error
var digest [16]byte
if gid != 0 {
digest = md5.Sum(helper.StringToBytes(fmt.Sprintf("[%s]%d_%d", m.Service, uid, gid)))
id := binary.LittleEndian.Uint64(digest[:8])
m.Manager.RLock()
if yes, ok := m.Manager.banCache[id]; ok {
m.Manager.RUnlock()
return yes
}
err = m.Manager.D.Find(m.Service+"ban", &b, "WHERE id = ?", id)
m.Manager.RUnlock()
if err == nil && gid == b.GroupID && uid == b.UserID {
log.Debugf("[control] plugin %s is banned in grp %d for usr %d.", m.Service, b.GroupID, b.UserID)
m.Manager.Lock()
m.Manager.banCache[id] = true
m.Manager.Unlock()
return true
}
m.Manager.Lock()
m.Manager.banCache[id] = false
m.Manager.Unlock()
}
digest = md5.Sum(helper.StringToBytes(fmt.Sprintf("[%s]%d_all", m.Service, uid)))
id := binary.LittleEndian.Uint64(digest[:8])
m.Manager.RLock()
if yes, ok := m.Manager.banCache[id]; ok {
m.Manager.RUnlock()
return yes
}
err = m.Manager.D.Find(m.Service+"ban", &b, "WHERE id = ?", id)
m.Manager.RUnlock()
if err == nil && b.GroupID == 0 && uid == b.UserID {
log.Debugf("[control] plugin %s is banned in all grp for usr %d.", m.Service, b.UserID)
m.Manager.Lock()
m.Manager.banCache[id] = true
m.Manager.Unlock()
return true
}
m.Manager.Lock()
m.Manager.banCache[id] = false
m.Manager.Unlock()
return false
}