From 8dd24d47cefa8f730d9edfeb6c113bbc504ef47f Mon Sep 17 00:00:00 2001 From: takayama Date: Wed, 4 Nov 2020 11:09:35 +0900 Subject: [PATCH 1/5] move functions --- client.js | 95 ++++++----------------------------------------- lib/core.js | 16 +++++++- lib/individual.js | 2 +- lib/ref.d.ts | 1 + lib/resource.js | 48 ++++++++++++++++++++---- 5 files changed, 69 insertions(+), 93 deletions(-) diff --git a/client.js b/client.js index 2638b51e..c9778054 100644 --- a/client.js +++ b/client.js @@ -8,7 +8,7 @@ const spawn = require("child_process"); const crypto = require("crypto"); const log4js = require("log4js"); const device = require("./device"); -const {checkUin} = require("./lib/common"); +const {checkUin, timestamp} = require("./lib/common"); const core = require("./lib/core"); const resource = require("./lib/resource"); const sysmsg = require("./lib/sysmsg"); @@ -109,11 +109,6 @@ class AndroidClient extends Client { dir; - /** - * @constructor - * @param {Number} uin - * @param {Object} config - */ constructor(uin, config = {}) { super(); this.uin = uin; @@ -198,25 +193,15 @@ class AndroidClient extends Client { }) this.on("internal.login", async()=>{ - this.once("internal.change-server", ()=>{ - // todo - }); this.logger.info(`Welcome, ${this.nickname} ! 开始初始化资源...`); this.sync_cookie = null; this.sync_finished = false; await this.register(); if (!this.isOnline()) return; - const initFL = async()=>{ - let start = 0; - while (1) { - const total = await resource.initFL.call(this, start); - start += 150; - if (start > total) break; - } - } await Promise.all([ - initFL(), resource.initGL.call(this) + resource.initFL.call(this), + resource.initGL.call(this) ]); this.logger.info(`加载了${this.fl.size}个好友,${this.gl.size}个群。`); await core.getMsg.call(this); @@ -226,10 +211,6 @@ class AndroidClient extends Client { }); } - /** - * @private - * @param {Function} callback - */ _connect(callback = ()=>{}) { if (this.status !== Client.OFFLINE) { return callback(); @@ -244,22 +225,12 @@ class AndroidClient extends Client { }); } - /** - * @private - * @returns {Number} this.seq_id - */ nextSeq() { if (++this.seq_id >= 0x8000) this.seq_id = 1; return this.seq_id; } - /** - * @private - * @param {Buffer} packet - * @param {Number} timeout ms - * @returns {OICQResponse} - */ async send(packet, timeout = 3000) { const seq_id = this.seq_id; return new Promise((resolve, reject)=>{ @@ -284,14 +255,11 @@ class AndroidClient extends Client { return await this.send(wt.build0x0BPacket.apply(this, arguments)); } - /** - * @private - */ startHeartbeat() { if (this.heartbeat) return; this.heartbeat = setInterval(async()=>{ - this._calc_msg_cnt(); + core.calcMsgCnt.call(this); if (Date.now() - this.send_timestamp > 240000) core.getMsg.call(this); try { @@ -354,29 +322,6 @@ class AndroidClient extends Client { } } - /** - * @private - * @param {Number} group_id - */ - async _getGroupMemberList(group_id) { - let mlist = new Map(); - try { - var next = 0; - while (1) { - var {map, next} = await resource.getGML.call(this, group_id, next); - mlist = new Map([...mlist, ...map]); - if (!next) break; - } - } catch (e) {} - if (!mlist.size) { - this.gml.delete(group_id); - return null; - } else { - this.gml.set(group_id, mlist); - return mlist; - } - } - /** * @param {Function} fn * @param {Array} params @@ -404,16 +349,12 @@ class AndroidClient extends Client { } } - /** - * @param {String} name - * @param {Object} data - */ em(name, data = {}) { const slice = name.split("."); const post_type = slice[0], sub_type = slice[2]; const param = { self_id: this.uin, - time: parseInt(Date.now()/1000), + time: timestamp(), post_type: post_type }; const type_name = slice[0] + "_type"; @@ -430,19 +371,6 @@ class AndroidClient extends Client { this.emit(post_type, param); } - /** - * 计算每分钟消息数量 - */ - _calc_msg_cnt() { - for (let i = 0; i < this.msg_times.length; ++i) { - if (Date.now() - this.msg_times[i] * 1000 <= 60000) { - this.msg_times = this.msg_times.slice(i); - return; - } - } - this.msg_times = []; - } - // 以下是public方法 ---------------------------------------------------------------------------------------------------- login(password_md5) { @@ -468,7 +396,9 @@ class AndroidClient extends Client { captchaLogin(captcha) { if (!this.captcha_sign) return this.logger.error("未收到图片验证码或已过期,你不能调用captchaLogin函数。"); - wt.captchaLogin.call(this, captcha); + this._connect(()=>{ + wt.captchaLogin.call(this, captcha); + }); } terminate() { @@ -508,7 +438,7 @@ class AndroidClient extends Client { if (!checkUin(group_id)) return buildApiRet(100); if (!this.gml.has(group_id)) - this.gml.set(group_id, this._getGroupMemberList(group_id)); + this.gml.set(group_id, resource.getGML.call(this, group_id)); let mlist = this.gml.get(group_id); if (mlist instanceof Promise) mlist = await mlist; @@ -543,7 +473,7 @@ class AndroidClient extends Client { /////////////////////////////////////////////////// - // async setGroupAnonymousBan(group_id, anonymous_flag, duration = 1800) {} + // async setGroupAnonymousBan(group_id, anonymous_flag, duration = 1800) {} async setGroupAnonymous(group_id, enable = true) { return await this.useProtocol(troop.setAnonymous, arguments); } @@ -603,7 +533,6 @@ class AndroidClient extends Client { return await this.useProtocol(troop.inviteFriend, arguments); } - async sendLike(user_id, times = 1) { return await this.useProtocol(indi.sendLike, arguments); } @@ -650,7 +579,7 @@ class AndroidClient extends Client { return buildApiRet(0, {cookies}); } - async getCsrfToken(domain) { + async getCsrfToken() { // await wt.exchangeEMP(); let token = 5381; for (let v of this.sig.skey) @@ -696,7 +625,7 @@ class AndroidClient extends Client { return buildApiRet(0, version); } getStatus() { - this._calc_msg_cnt(); + core.calcMsgCnt.call(this); return buildApiRet(0, { online: this.isOnline(), status: this.online_status, diff --git a/lib/core.js b/lib/core.js index 90b3c933..84a3e5ba 100644 --- a/lib/core.js +++ b/lib/core.js @@ -294,6 +294,20 @@ function parseIncomingPacket(packet) { this.handlers.get(sso.seq)(sso.payload); } +/** + * 计算每分钟消息数量 + * @this {import("./ref").Client} + */ +function calcMsgCnt() { + for (let i = 0; i < this.msg_times.length; ++i) { + if (Date.now() - this.msg_times[i] * 1000 <= 60000) { + this.msg_times = this.msg_times.slice(i); + return; + } + } + this.msg_times = []; +} + module.exports = { - parseIncomingPacket, getMsg + parseIncomingPacket, getMsg, calcMsgCnt }; diff --git a/lib/individual.js b/lib/individual.js index f0cf9c51..940b04bd 100644 --- a/lib/individual.js +++ b/lib/individual.js @@ -8,7 +8,7 @@ const jce = require("./jce"); /** * 设置头像 * @this {import("./ref").Client} - * @param {String} file + * @param {String|Buffer} file */ async function setPortrait(file) { let buf; diff --git a/lib/ref.d.ts b/lib/ref.d.ts index be5d85b9..6d242bc9 100644 --- a/lib/ref.d.ts +++ b/lib/ref.d.ts @@ -130,5 +130,6 @@ export class Client extends oicq.Client { send(): Promise; sendUNI(cmd: string, body: Buffer, seq?: number): Promise; writeUNI(cmd: string, body: Buffer, seq?: number): void; + useProtocol(fn: Function, params: any[]): oicq.RetCommon; em(name: string, data: object): void; } diff --git a/lib/resource.js b/lib/resource.js index 19496865..d9816e7d 100644 --- a/lib/resource.js +++ b/lib/resource.js @@ -14,12 +14,12 @@ const group_role_map = { }; /** - * 加载好友列表(不对外开放) * @this {import("./ref").Client} * @param {Number} start + * @param {Number} limit * @returns {Number} 好友总数 */ -async function initFL(start) { +async function _initFL(start, limit) { const d50 = pb.encode({ 1: 10002, 91001: 1, @@ -30,7 +30,7 @@ async function initFL(start) { }); const FL = jce.encodeStruct([ 3, - 1, this.uin, start, 150, 0, 0, 0, 0, 0, 1, + 1, this.uin, start, limit, 0, 0, 0, 0, 0, 1, 31, null, 0, 0, 0, d50, null, [13580, 13581, 13582] ]); const extra = { @@ -61,6 +61,19 @@ async function initFL(start) { } } +/** + * 加载好友列表(不对外开放) + * @this {import("./ref").Client} + */ +async function initFL() { + let start = 0, limit = 150; + while (1) { + const total = await _initFL.call(this, start, limit); + start += limit; + if (start > total) break; + } +} + /** * 加载群列表(不对外开放) * @this {import("./ref").Client} @@ -103,11 +116,7 @@ async function initGL() { } } -/** - * 加载群员列表(间接对外开放) - * @this {import("./ref").Client} - */ -async function getGML(group_id, next_uin) { +async function _getGML(group_id, next_uin) { const GTML = jce.encodeStruct([ this.uin, group_id, next_uin, common.code2uin(group_id), 2, 0, 0, 0 ]); @@ -151,6 +160,29 @@ async function getGML(group_id, next_uin) { return {map, next}; } +/** + * 加载群员列表 + * @this {import("./ref").Client} + */ +async function getGML(group_id) { + let mlist = new Map(); + try { + var next = 0; + while (1) { + var {map, next} = await _getGML.call(this, group_id, next); + mlist = new Map([...mlist, ...map]); + if (!next) break; + } + } catch (e) {} + if (!mlist.size) { + this.gml.delete(group_id); + return null; + } else { + this.gml.set(group_id, mlist); + return mlist; + } +} + /** * 群资料 * @this {import("./ref").Client} From a5b72f1922e26d03b396d934d5732a403151211d Mon Sep 17 00:00:00 2001 From: takayama Date: Wed, 4 Nov 2020 21:41:56 +0900 Subject: [PATCH 2/5] =?UTF-8?q?=E5=8E=BB=E9=87=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client.js | 42 +++++++++++++++++++++++++++++++++++++----- lib/core.js | 43 ++++++++++++------------------------------- lib/message/chat.js | 41 ++++++++++++++--------------------------- lib/online-push.js | 2 +- lib/ref.d.ts | 5 ++--- lib/sysmsg.js | 26 ++++++++++++++++++-------- 6 files changed, 84 insertions(+), 75 deletions(-) diff --git a/client.js b/client.js index c9778054..7cc362cb 100644 --- a/client.js +++ b/client.js @@ -78,7 +78,6 @@ class AndroidClient extends Client { seq_id = 0; handlers = new Map(); seq_cache = new Map(); - notify33cache = new Set(); session_id = crypto.randomBytes(4); random_key = crypto.randomBytes(16); @@ -99,7 +98,6 @@ class AndroidClient extends Client { device_token: BUF0, }; cookies = {}; - msg_times = []; sync_finished = false; sync_cookie; @@ -259,7 +257,7 @@ class AndroidClient extends Client { if (this.heartbeat) return; this.heartbeat = setInterval(async()=>{ - core.calcMsgCnt.call(this); + this.doCircle(); if (Date.now() - this.send_timestamp > 240000) core.getMsg.call(this); try { @@ -371,6 +369,41 @@ class AndroidClient extends Client { this.emit(post_type, param); } + msgExists(from, type, seq, time) { + if (timestamp() - time >= 60) + return true; + const id = [from, type, seq].join("-"); + const set = this.seq_cache.get(time); + if (!set) { + this.seq_cache.set(time, new Set([id])); + return false; + } else { + if (set.has(id)) + return true; + else + set.add(id); + return false; + } + } + doCircle() { + for (let time of this.seq_cache.keys()) { + if (timestamp() - time >= 60) + this.seq_cache.delete(time); + else + break; + } + } + calcMsgCnt() { + let cnt = 0; + for (let [time, set] of this.seq_cache) { + if (timestamp() - time >= 60) + this.seq_cache.delete(time); + else + cnt += set.size; + } + return cnt; + } + // 以下是public方法 ---------------------------------------------------------------------------------------------------- login(password_md5) { @@ -625,11 +658,10 @@ class AndroidClient extends Client { return buildApiRet(0, version); } getStatus() { - core.calcMsgCnt.call(this); return buildApiRet(0, { online: this.isOnline(), status: this.online_status, - msg_cnt_per_min: this.msg_times.length, + msg_cnt_per_min: this.calcMsgCnt() }) } getLoginInfo() { diff --git a/lib/core.js b/lib/core.js index 84a3e5ba..68c87cd5 100644 --- a/lib/core.js +++ b/lib/core.js @@ -40,8 +40,6 @@ function onPushReq(blob, seq) { this.em("internal.change-server", {ip, port}); } -const notify_types = {84:1,87:2,525:22}; - /** * 新消息通知 * @this {import("./ref").Client} @@ -64,7 +62,7 @@ function onPushNotify(blob) { this.lock525 = !this.lock525; if (this.lock525) return; } - return sysmsg.getNewGroup.call(this, notify_types[parent[5]]); + return sysmsg.getNewGroup.call(this, parent[5]); case 187: return sysmsg.getNewFriend.call(this); } @@ -109,22 +107,19 @@ async function getMsg(sync_flag = 0) { items.push(head); if (!this.sync_finished) continue; - let user_id = head[1]; - if (user_id === this.uin && (this.ignore_self || user_id !== head[2])) + let uin = head[1]; + if (uin === this.uin && (this.ignore_self || uin !== head[2])) + continue; + if (![33, 141, 166, 167, 208].includes(type)) + continue; + if (this.msgExists(uin, type, head[5], head[6])) continue; //群员入群 if (type === 33) { (async()=>{ - const group_id = common.uin2code(user_id); - user_id = head[15]; - const id = BigInt(group_id) * BigInt(user_id) * BigInt(head[5]); - if (this.notify33cache.has(id) || common.timestamp() - head[6] >= 10) - return; - this.notify33cache.add(id); - setTimeout(()=>{ - this.notify33cache.delete(id); - }, 10000); + const group_id = common.uin2code(uin); + const user_id = head[15]; const ginfo = (await this.getGroupInfo(group_id)).data; if (!ginfo) return; if (user_id === this.uin) { @@ -144,10 +139,10 @@ async function getMsg(sync_flag = 0) { nickname: String(head[16].raw) }); })(); - } + } //私聊消息 - if ([141, 166, 167, 208].includes(type)) + else chat.onPrivateMsg.call(this, type, head, msg[2], msg[3]); } } @@ -294,20 +289,6 @@ function parseIncomingPacket(packet) { this.handlers.get(sso.seq)(sso.payload); } -/** - * 计算每分钟消息数量 - * @this {import("./ref").Client} - */ -function calcMsgCnt() { - for (let i = 0; i < this.msg_times.length; ++i) { - if (Date.now() - this.msg_times[i] * 1000 <= 60000) { - this.msg_times = this.msg_times.slice(i); - return; - } - } - this.msg_times = []; -} - module.exports = { - parseIncomingPacket, getMsg, calcMsgCnt + parseIncomingPacket, getMsg }; diff --git a/lib/message/chat.js b/lib/message/chat.js index b67b4713..7282bb40 100644 --- a/lib/message/chat.js +++ b/lib/message/chat.js @@ -320,23 +320,11 @@ function recallGroupMsg(message_id) { */ async function onPrivateMsg(type, head, content, body) { - const user_id = head[1]; - - let no_cache = false; - if (!this.seq_cache.has(user_id)) { - this.seq_cache.set(user_id, head[5]); - } else { - const seq = this.seq_cache.get(user_id); - if (seq - head[5] >= 0 && seq - head[5] < 1000) - return; - else { - no_cache = Math.abs(head[5] - seq) > 1 || head[5] % 5 === 0; - this.seq_cache.set(user_id, head[5]); - } - } + const user_id = head[1], + time = head[6], + seq = head[5]; + let sub_type, message_id, font = "unknown"; - let sub_type, message_id, font = "unknown", time = head[6]; - this.msg_times.push(time); const sender = Object.assign({user_id}, this.fl.get(user_id)); if (type === 141) { sub_type = "other"; @@ -353,7 +341,7 @@ async function onPrivateMsg(type, head, content, body) { return; } if (!sender.nickname) { - const stranger = (await this.getStrangerInfo(user_id, no_cache)).data; + const stranger = (await this.getStrangerInfo(user_id, seq%5==0)).data; if (stranger) { stranger.group_id = sender.group_id; Object.assign(sender, stranger); @@ -366,7 +354,7 @@ async function onPrivateMsg(type, head, content, body) { font = String(body[1][1][9].raw); random = body[1][1][3]; } - message_id = genGroupMessageId(user_id, head[5], random); + message_id = genGroupMessageId(user_id, seq, random); try { var {chain, raw_message} = await parseMessage.call(this, body[1]); } catch (e) {return} @@ -388,15 +376,14 @@ async function onPrivateMsg(type, head, content, body) { async function onGroupMsg(head, body) { const user_id = head[1], - time = head[6]; - - this.msg_times.push(time); - + time = head[6], + seq = head[5]; const group = head[9], group_id = group[1], group_name = String(group[8].raw); - const message_id = genGroupMessageId(group_id, head[5], body[1][1][3]); + this.msgExists(group_id, 0, seq, time); + const message_id = genGroupMessageId(group_id, seq, body[1][1][3]); if (user_id === this.uin) this.emit(`interval.${group_id}.${body[1][1][3]}`, message_id); @@ -472,14 +459,14 @@ async function onGroupMsg(head, body) { async function onDiscussMsg(head, body) { const user_id = head[1], - time = head[6]; - - this.msg_times.push(time); - + time = head[6], + seq = head[5]; const discuss = head[13], discuss_id = discuss[1], discuss_name = String(discuss[5].raw); + this.msgExists(discuss_id, 0, seq, time); + if (user_id === this.uin && this.ignore_self) return; diff --git a/lib/online-push.js b/lib/online-push.js index 1f50b987..30c6b3fc 100644 --- a/lib/online-push.js +++ b/lib/online-push.js @@ -376,7 +376,7 @@ function onOnlinePushTrans(blob, seq) { } catch (e) {} } else { operator_id = buf.readUInt32BE(10); - if (buf[9] === 0x01) + if (buf[9] === 0x01 || buf[9] === 0x81) dismiss = true; if (user_id === this.uin) { this.gl.delete(group_id); diff --git a/lib/ref.d.ts b/lib/ref.d.ts index 6d242bc9..35919496 100644 --- a/lib/ref.d.ts +++ b/lib/ref.d.ts @@ -105,8 +105,7 @@ export class Client extends oicq.Client { heartbeat: NodeJS.Timeout | null; seq_id: number; handlers: Map void>; - seq_cache: Map; - notify33cache: Set; + seq_cache: Map>; session_id: Buffer; random_key: Buffer; @@ -124,7 +123,6 @@ export class Client extends oicq.Client { dir: string; sig: Sig; cookies: object; - msg_times: number[]; nextSeq(): number; send(): Promise; @@ -132,4 +130,5 @@ export class Client extends oicq.Client { writeUNI(cmd: string, body: Buffer, seq?: number): void; useProtocol(fn: Function, params: any[]): oicq.RetCommon; em(name: string, data: object): void; + msgExists(from: number, type: number, seq: number, time: number): boolean; } diff --git a/lib/sysmsg.js b/lib/sysmsg.js index c3da1220..5636b7c3 100644 --- a/lib/sysmsg.js +++ b/lib/sysmsg.js @@ -1,5 +1,6 @@ "use strict"; const pb = require("./pb"); +const common = require("./common"); /** * @param {Number} user_id @@ -68,6 +69,10 @@ async function getNewFriend() { const v = Array.isArray(rsp) ? rsp[0] : rsp; const time = v[4]; const user_id = v[5]; + + if (this.msgExists(user_id, 187, v[3], time)) + return; + const nickname = String(v[50][51].raw); const flag = genFriendRequestFlag(user_id, v[3]); this.logger.info(`收到 ${user_id}(${nickname}) 的加好友请求 (flag: ${flag})`); @@ -85,10 +90,12 @@ async function getNewFriend() { } } +const notify_types = {84:1,87:2,525:22}; + /** * 获取群请求 * @this {import("./ref").Client} - * @param {Number} type 1申请 2邀请 22申请(来自群员的邀请) + * @param {84|87|525} type 84申请 87邀请 525申请(来自群员的邀请) */ async function getNewGroup(type) { const body = pb.encode({ @@ -126,7 +133,7 @@ async function getNewGroup(type) { v = rsp; else { for (let vv of rsp) { - if (vv[50][1] !== 1 || vv[50][12] !== type) + if (vv[50][1] !== 1 || vv[50][12] !== notify_types[type]) continue; if (!v || vv[4] > v[4]) v = vv; @@ -135,8 +142,11 @@ async function getNewGroup(type) { if (!v) return; const time = v[4]; const group_id = v[50][10]; - if (Date.now() - time * 1000 > 5000) return; - if (type === 1 || type === 22) { + + if (this.msgExists(group_id, type, v[3], time)) + return; + + if (type === 84 || type === 525) { const user_id = v[5]; const nickname = String(v[50][51].raw); const group_name = String(v[50][52].raw); @@ -145,10 +155,10 @@ async function getNewGroup(type) { this.em("request.group.add", { group_id, user_id, group_name, nickname, comment: String(v[50][4].raw), - inviter_id: type === 22 ? v[50][11] : undefined, + inviter_id: type === 525 ? v[50][11] : undefined, flag, time }); - } else if (type === 2) { + } else if (type === 87) { const user_id = v[50][11]; const nickname = String(v[50][53].raw); const group_name = String(v[50][52].raw); @@ -188,7 +198,7 @@ async function friendAction(flag, approve = true, remark = "", block = false) { }); const blob = await this.sendUNI("ProfileService.Pb.ReqSystemMsgAction.Friend", body); const rsp = pb.decode(blob)[1]; - return {result: rsp[1], emsg: rsp[2]}; + return {result: rsp[1], emsg: rsp[2]?String(rsp[2].raw):undefined}; } /** @@ -215,7 +225,7 @@ async function groupAction(flag, approve = true, reason = "", block = false) { }); const blob = await this.sendUNI("ProfileService.Pb.ReqSystemMsgAction.Group", body); const rsp = pb.decode(blob)[1]; - return {result: rsp[1], emsg: rsp[2]}; + return {result: rsp[1], emsg: rsp[2]?String(rsp[2].raw):undefined}; } module.exports = { From ee00aea6a1ef5a14f2c818858d25e6ecc37056d2 Mon Sep 17 00:00:00 2001 From: takayama Date: Thu, 5 Nov 2020 10:08:23 +0900 Subject: [PATCH 3/5] =?UTF-8?q?=E5=AE=89=E5=8D=93gif=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/message/storage.js | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/lib/message/storage.js b/lib/message/storage.js index 27dd5ddd..4b53c54b 100644 --- a/lib/message/storage.js +++ b/lib/message/storage.js @@ -13,26 +13,34 @@ function setPrivateImageNested(img, fid) { 1: img.md5.toString("hex"), 2: img.size, 3: fid, - //5: 1000, + // 5: 2000, 7: img.md5, + 8: 640, + 9: 640, 10: fid, - 13: 1, //? - //16: 3, - 20: 0, //? + 13: 0, //这个参数有魔法 + 16: 3, + // 20: 0, + 24: 0, + 25: 0, }); } function setGroupImageNested(img, fid) { Object.assign(img.nested, { 2: img.md5.toString("hex"), - 7: fid, + 7: fid&0xffffffff, 10: 66, 12: 1, 13: img.md5, - //17: 3, - //20: 1000, - //24: 101, + 17: 3, + // 20: 2000, + 22: 640, + 23: 640, + // 24: 201, 25: img.size, - 26: 1, //? + 26: 0, //这个参数魔法弱一些 + 29: 0, + 30: 0, }); } From 568af993ac7a554c9e108a32337ea8133fef8c93 Mon Sep 17 00:00:00 2001 From: takayama Date: Thu, 5 Nov 2020 11:16:32 +0900 Subject: [PATCH 4/5] add data_dir to config: delete setGlobalConfig; --- client.d.ts | 12 +----------- client.js | 37 ++++++++++++++++--------------------- docs/api.md | 1 + lib/message/builder.js | 4 ++-- 4 files changed, 20 insertions(+), 34 deletions(-) diff --git a/client.d.ts b/client.d.ts index ce4d7891..3bbabb98 100644 --- a/client.d.ts +++ b/client.d.ts @@ -6,17 +6,12 @@ import * as events from 'events'; export type Uin = string | number; -export interface ConfGlobal { - web_image_timeout?: number, //下载的超时秒数,默认系统自己判断 - web_record_timeout?: number, - cache_root?: string, //数据文件夹路径,需要可写权限,默认主目录下data文件夹 - debug?: boolean, -} export interface ConfBot { log_level?: "trace" | "debug" | "info" | "warn" | "error" | "fatal" | "off", //默认info platform?: number, //1手机 2平板(默认) 3手表(不支持部分群事件) kickoff?: boolean, //被挤下线是否在3秒后反挤对方,默认false ignore_self?: boolean,//群聊是否无视自己的发言,默认true + data_dir?: string, //数据存储文件夹,需要可写权限,默认主目录下的data文件夹 } export interface RetError { @@ -251,8 +246,3 @@ export class Client extends events.EventEmitter { } export function createClient(uin: Uin, config?: ConfBot): Client; - -/** - * @deprecated - */ -export function setGlobalConfig(config?: ConfGlobal): void; diff --git a/client.js b/client.js index 7cc362cb..42ff26f6 100644 --- a/client.js +++ b/client.js @@ -110,17 +110,19 @@ class AndroidClient extends Client { constructor(uin, config = {}) { super(); this.uin = uin; - this.dir = createCacheDir(uin); config = { platform: 2, //1手机 2平板 3手表(不支持部分群事件) log_level: "info", //trace,debug,info,warn,error,fatal,off kickoff: false, //被挤下线是否在3秒后反挤 ignore_self: true, //是否无视自己的消息(群聊、私聊) + data_dir: path.join(process.mainModule.path, "data"), ...config }; this.config = config; + this.dir = createCacheDir(config.data_dir, uin); + this.logger = log4js.getLogger(`[BOT:${uin}]`); this.logger.level = config.log_level; this.ignore_self = config.ignore_self; @@ -675,25 +677,23 @@ class AndroidClient extends Client { //---------------------------------------------------------------------------------------------------- +/** + * @deprecated + */ const logger = log4js.getLogger("[SYSTEM]"); logger.level = "info"; -console.log("OICQ程序启动。当前内核版本:v" + version.version); - -const config = { - cache_root: path.join(process.mainModule.path, "data"), - debug: false, -}; - process.OICQ = { - logger, config + logger }; -function createCacheDir(uin) { - if (!fs.existsSync(config.cache_root)) - fs.mkdirSync(config.cache_root, {mode: 0o755, recursive: true}); - const img_path = path.join(config.cache_root, "image"); - const ptt_path = path.join(config.cache_root, "record"); - const uin_path = path.join(config.cache_root, uin.toString()); +console.log("OICQ程序启动。当前内核版本:v" + version.version); + +function createCacheDir(dir, uin) { + if (!fs.existsSync(dir)) + fs.mkdirSync(dir, {mode: 0o755, recursive: true}); + const img_path = path.join(dir, "image"); + const ptt_path = path.join(dir, "record"); + const uin_path = path.join(dir, String(uin)); if (!fs.existsSync(img_path)) fs.mkdirSync(img_path); if (!fs.existsSync(ptt_path)) @@ -705,13 +705,8 @@ function createCacheDir(uin) { /** * @deprecated - * @param {JSON} config */ -function setGlobalConfig(config = {}) { - Object.assign(process.OICQ.config, config); - if (config.debug) - logger.level = "debug"; -} +function setGlobalConfig() {} /** * @param {Number} uin diff --git a/docs/api.md b/docs/api.md index b9fc58b8..b8fa1954 100644 --- a/docs/api.md +++ b/docs/api.md @@ -37,6 +37,7 @@ const client = oicq.createClient(uin, config); log_level: "info", //日志级别,有trace,debug,info,warn,error,fatal,off kickoff: false, //被挤下线是否在3秒后反挤 ignore_self: true, //是否无视自己的消息(群聊、私聊) + data_dir: undefined, //数据存储文件夹,需要可写权限,默认主目录下的data文件夹 } ``` diff --git a/lib/message/builder.js b/lib/message/builder.js index 48efcaeb..84d2d836 100644 --- a/lib/message/builder.js +++ b/lib/message/builder.js @@ -180,7 +180,7 @@ async function buildImageMessage(chain, cq, is_group, stat) { // 网络图片 if (file.startsWith("http://") || file.startsWith("https://")) { const filename = common.md5(Buffer.from(file, "utf-8")).toString('hex'); - const filepath = path.join(process.OICQ.config.cache_root, "image", filename); + const filepath = path.join(this.dir, "..", "image", filename); try { if (cache === "0") throw new Error("no cache"); @@ -305,7 +305,7 @@ async function buildRecordMessage(chain, data, group_id) { file = file.replace(/^file:\/{2,3}/, ""); url = file; const cache_filename = common.md5(Buffer.from(file, "utf-8")).toString('hex'); - const cache_filepath = path.join(process.OICQ.config.cache_root, "record", cache_filename); + const cache_filepath = path.join(this.dir, "..", "record", cache_filename); if (cache !== "0") { try { buf = await fs.promises.readFile(cache_filepath); From e76a085f28fb28575a1c392da790c2b950a80781 Mon Sep 17 00:00:00 2001 From: takayama Date: Thu, 5 Nov 2020 11:28:11 +0900 Subject: [PATCH 5/5] update ver to 1.10.2 --- package-lock.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 10a9ba16..de9361e7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "oicq", - "version": "1.10.1", + "version": "1.10.2", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index d37031c6..ac38eb1a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "oicq", - "version": "1.10.1", + "version": "1.10.2", "description": "QQ protocol!", "main": "client.js", "scripts": {