Skip to content

Commit

Permalink
add event-filter
Browse files Browse the repository at this point in the history
up dependencies
  • Loading branch information
takayama-lily committed Nov 23, 2020
1 parent 56a4b34 commit 6604c93
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 17 deletions.
14 changes: 1 addition & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,19 +165,7 @@
* [x] WS心跳
* [x] _async异步调用api
* [x] _rate_limited限速调用api
* [ ] 事件上报过滤
* [x] 事件过滤器
* [x] 自动更新内核版本

[内核功能支持和CQ码](https://github.com/takayama-lily/oicq/blob/dev/docs/project.md)

----

### 本项目的优势,以及为什么使用Nodejs

* 完美的跨平台和多账号支持,部署简单方便。
* 利用npm包管理器自动更新依赖库,版本升级简单方便。
* 运行时几乎没有垃圾文件产生,占用最低限资源。
* 依附于强大的node&v8引擎,高速稳定,彻底告别平台兼容性导致的异常崩溃等问题。
* 以单线程事件循环为主要模型,从根源上杜绝多线程带来的数据不一致等奇怪问题。
* 考虑到JavaScript仍然是使用人数最多的编程语言,而项目运行时完全开源,使得任何人都可以尝试解决问题,而不仅限于发现问题。
* 使用最新的ECMAScript2010语法,用最少的代码实现所需功能,使项目可维护性显著提升。便于阅读和调试的源码,带你走进底层OICQ协议的世界。
2 changes: 1 addition & 1 deletion config.sample.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module.exports = {
enable_heartbeat: false, //是否启用ws心跳
heartbeat_interval: 15000, //ws心跳间隔
rate_limit_interval:500, //使用_rate_limited后缀限速调用api的排队间隔时间(毫秒)
event_filter: "", //事件过滤器
event_filter: "", //json格式的事件过滤器文件路径
post_url: [
// "http://your.address.com:80", //上报地址,可以添加多个url
],
Expand Down
7 changes: 6 additions & 1 deletion lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const oicq = require("oicq");
const http = require("http");
const https = require("https");
const WebSocket = require("ws");
const filter = require("./filter");
const api = require("./api");
const transNotice = require("./cq-notice");
const global_config = require("../config");
Expand All @@ -25,6 +26,7 @@ const default_config = {
secret: "",
post_timeout: 30,
post_message_format: "string",
event_filter: "",
enable_heartbeat: false,
heartbeat_interval: 15000,
rate_limit_interval: 500,
Expand Down Expand Up @@ -67,6 +69,7 @@ function startup(arg) {
}
}, config.heartbeat_interval);
}
filter.init(config.event_filter);
createServer();
setTimeout(createBot, 500);
}
Expand Down Expand Up @@ -136,7 +139,7 @@ function createBot() {
});
if (data.sub_type === "network" || data.sub_type === "unknown") {
bot.logger.warn("网络断开,5秒后尝试重新连接。");
setTimeout(createBot, 5000);
setTimeout(bot.login.bind(bot), 5000);
}
});

Expand Down Expand Up @@ -164,6 +167,8 @@ function createBot() {
* 分发事件
*/
function dipatch(event) {
if (!filter.assert(event))
return;
const json = JSON.stringify(event);
const options = {
method: 'POST',
Expand Down
128 changes: 128 additions & 0 deletions lib/filter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
};
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-onebot",
"version": "1.1.2",
"version": "1.1.3",
"description": "onebot",
"main": "main.js",
"scripts": {
Expand All @@ -17,7 +17,7 @@
},
"homepage": "https://github.com/takayama-lily/onebot#readme",
"dependencies": {
"oicq": "^1.10.0",
"oicq": "^1.10.12",
"ws": "^7.3.1"
}
}

0 comments on commit 6604c93

Please sign in to comment.