forked from takayama-lily/oicq
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter.js
More file actions
128 lines (115 loc) · 3.11 KB
/
filter.js
File metadata and controls
128 lines (115 loc) · 3.11 KB
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
"use strict";
const fs = require("fs");
let filter, event;
function init(filepath) {
if (filepath) {
try {
filter = fs.readFileSync(filepath, "utf8");
filter = JSON.parse(filter);
console.log("已启用事件过滤器。")
} catch (e) {
console.log(e.message);
console.log("加载事件过滤器失败,进程退出。");
process.exit(0);
}
}
}
function _exec(o = filter, op = "and", field) {
if (["and", "not", "or"].includes(op)) {
if (Array.isArray(o)) {
for (let rule of o) {
let matched = _exec(rule, "and", field);
if (!matched && op === "and")
return false;
if (matched && op === "not")
return false;
if (matched && op === "or")
return true;
}
return op !== "or" || !o.length;
} else if (typeof o === "object" && o !== null) {
for (let k in o) {
let matched;
if (k.startsWith("."))
matched = _exec(o[k], k.substr(1), field);
else
matched = _exec(o[k], "eq", k);
if (!matched && op === "and")
return false;
if (matched && op === "not")
return false;
if (matched && op === "or")
return true;
}
return op !== "or" || !Object.keys(o).length;
} else {
return false;
}
}
if (typeof o === "object" && o !== null && !Array.isArray(o))
return _exec(o, "and", field);
if (op === "eq") {
return o === event[field];
}
if (op === "neq") {
return o !== event[field];
}
if (op === "in") {
return o.includes(event[field]);
}
if (op === "contains") {
return event[field].includes(o);
}
if (op === "regex") {
if (o.startsWith("/"))
o = o.substr(1);
const split = o.split("/");
const regex = new RegExp(split[0], split[1]);
return !!event[field].match(regex);
}
return true;
}
function test() {
init("../filter.json");
const e = {
self_id: 123456,
time: 1606094532,
post_type: "message",
message_type: "group",
sub_type: "normal",
message_id: "REAq4xY6N/UAAAJIIjI65l+7DsQ=",
group_id: 123456,
group_name: "ddddd",
user_id: 123456,
anonymous: null,
message: "data",
raw_message: "data",
font: "微软雅黑",
sender: {
user_id: 123456,
nickname: "123456",
card: "",
sex: "female",
age: 115,
area: "上海",
level: 2,
role: "member",
title: ""
}
};
console.log(assert(e));
}
// test()
function assert(e) {
if (!filter)
return true;
event = e;
try {
return _exec();
} catch {
return false;
}
}
module.exports = {
init, assert
};