-
Notifications
You must be signed in to change notification settings - Fork 5
/
blacklist.go
138 lines (120 loc) · 3.54 KB
/
blacklist.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
package utils
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
"sync"
"github.com/fsnotify/fsnotify"
"github.com/hoshinonyaruko/gensokyo-llm/config"
"github.com/hoshinonyaruko/gensokyo-llm/structs"
)
var blacklist = make(map[string]bool)
var mu sync.RWMutex
// LoadBlacklist 从给定的文件路径载入黑名单ID。
// 如果文件不存在,则创建该文件。
func LoadBlacklist(filePath string) error {
file, err := os.Open(filePath)
if err != nil {
if os.IsNotExist(err) {
// 如果文件不存在,则创建一个新文件
file, err = os.Create(filePath)
if err != nil {
return err // 创建文件失败,返回错误
}
} else {
return err // 打开文件失败,且原因不是文件不存在
}
}
defer file.Close()
scanner := bufio.NewScanner(file)
mu.Lock()
defer mu.Unlock()
blacklist = make(map[string]bool) // 重置黑名单
for scanner.Scan() {
blacklist[scanner.Text()] = true
}
return scanner.Err()
}
// isInBlacklist 检查给定的ID是否在黑名单中。
func IsInBlacklist(id string) bool {
mu.RLock()
defer mu.RUnlock()
_, exists := blacklist[id]
return exists
}
// watchBlacklist 监控黑名单文件的变动并动态更新。
func WatchBlacklist(filePath string) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal("Error creating watcher:", err)
}
defer watcher.Close()
done := make(chan bool)
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
if event.Op&fsnotify.Write == fsnotify.Write {
fmt.Println("Detected update to blacklist, reloading...")
err := LoadBlacklist(filePath)
if err != nil {
log.Printf("Error reloading blacklist: %v", err)
}
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Println("Watcher error:", err)
}
}
}()
err = watcher.Add(filePath)
if err != nil {
log.Fatal("Error adding watcher to file:", err)
}
<-done // Keep the watcher alive
}
// BlacklistIntercept 检查用户ID是否在黑名单中,如果在,则发送预设消息
func BlacklistIntercept(message structs.OnebotGroupMessage, selfid string) bool {
// 检查群ID是否在黑名单中
if IsInBlacklist(strconv.FormatInt(message.GroupID, 10)) {
// 获取黑名单响应消息
responseMessage := config.GetBlacklistResponseMessages()
// 根据消息类型发送响应
if message.RealMessageType == "group_private" || message.MessageType == "private" {
if !config.GetUsePrivateSSE() {
SendPrivateMessage(message.UserID, responseMessage, selfid)
} else {
SendSSEPrivateMessage(message.UserID, responseMessage)
}
} else {
SendGroupMessage(message.GroupID, message.UserID, responseMessage, selfid)
}
fmt.Printf("groupid:[%v]这个群在黑名单中,被拦截\n", message.GroupID)
return true // 拦截
}
// 检查用户ID是否在黑名单中
if IsInBlacklist(strconv.FormatInt(message.UserID, 10)) {
// 获取黑名单响应消息
responseMessage := config.GetBlacklistResponseMessages()
// 根据消息类型发送响应
if message.RealMessageType == "group_private" || message.MessageType == "private" {
if !config.GetUsePrivateSSE() {
SendPrivateMessage(message.UserID, responseMessage, selfid)
} else {
SendSSEPrivateMessage(message.UserID, responseMessage)
}
} else {
SendGroupMessage(message.GroupID, message.UserID, responseMessage, selfid)
}
fmt.Printf("userid:[%v]这位用户在黑名单中,被拦截\n", message.UserID)
return true // 拦截
}
return false // 用户ID不在黑名单中,不拦截
}