Skip to content
This repository was archived by the owner on Nov 2, 2023. It is now read-only.

Commit 2f6d501

Browse files
Merge pull request #81 from takayama-lily/dev
-
2 parents a8af8c8 + e76a085 commit 2f6d501

File tree

14 files changed

+171
-192
lines changed

14 files changed

+171
-192
lines changed

client.d.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,12 @@ import * as events from 'events';
66

77
export type Uin = string | number;
88

9-
export interface ConfGlobal {
10-
web_image_timeout?: number, //下载的超时秒数,默认系统自己判断
11-
web_record_timeout?: number,
12-
cache_root?: string, //数据文件夹路径,需要可写权限,默认主目录下data文件夹
13-
debug?: boolean,
14-
}
159
export interface ConfBot {
1610
log_level?: "trace" | "debug" | "info" | "warn" | "error" | "fatal" | "off", //默认info
1711
platform?: number, //1手机 2平板(默认) 3手表(不支持部分群事件)
1812
kickoff?: boolean, //被挤下线是否在3秒后反挤对方,默认false
1913
ignore_self?: boolean,//群聊是否无视自己的发言,默认true
14+
data_dir?: string, //数据存储文件夹,需要可写权限,默认主目录下的data文件夹
2015
}
2116

2217
export interface RetError {
@@ -251,8 +246,3 @@ export class Client extends events.EventEmitter {
251246
}
252247

253248
export function createClient(uin: Uin, config?: ConfBot): Client;
254-
255-
/**
256-
* @deprecated
257-
*/
258-
export function setGlobalConfig(config?: ConfGlobal): void;

client.js

Lines changed: 60 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const spawn = require("child_process");
88
const crypto = require("crypto");
99
const log4js = require("log4js");
1010
const device = require("./device");
11-
const {checkUin} = require("./lib/common");
11+
const {checkUin, timestamp} = require("./lib/common");
1212
const core = require("./lib/core");
1313
const resource = require("./lib/resource");
1414
const sysmsg = require("./lib/sysmsg");
@@ -78,7 +78,6 @@ class AndroidClient extends Client {
7878
seq_id = 0;
7979
handlers = new Map();
8080
seq_cache = new Map();
81-
notify33cache = new Set();
8281

8382
session_id = crypto.randomBytes(4);
8483
random_key = crypto.randomBytes(16);
@@ -99,7 +98,6 @@ class AndroidClient extends Client {
9998
device_token: BUF0,
10099
};
101100
cookies = {};
102-
msg_times = [];
103101

104102
sync_finished = false;
105103
sync_cookie;
@@ -109,25 +107,22 @@ class AndroidClient extends Client {
109107

110108
dir;
111109

112-
/**
113-
* @constructor
114-
* @param {Number} uin
115-
* @param {Object} config
116-
*/
117110
constructor(uin, config = {}) {
118111
super();
119112
this.uin = uin;
120-
this.dir = createCacheDir(uin);
121113

122114
config = {
123115
platform: 2, //1手机 2平板 3手表(不支持部分群事件)
124116
log_level: "info", //trace,debug,info,warn,error,fatal,off
125117
kickoff: false, //被挤下线是否在3秒后反挤
126118
ignore_self: true, //是否无视自己的消息(群聊、私聊)
119+
data_dir: path.join(process.mainModule.path, "data"),
127120
...config
128121
};
129122
this.config = config;
130123

124+
this.dir = createCacheDir(config.data_dir, uin);
125+
131126
this.logger = log4js.getLogger(`[BOT:${uin}]`);
132127
this.logger.level = config.log_level;
133128
this.ignore_self = config.ignore_self;
@@ -198,25 +193,15 @@ class AndroidClient extends Client {
198193
})
199194

200195
this.on("internal.login", async()=>{
201-
this.once("internal.change-server", ()=>{
202-
// todo
203-
});
204196
this.logger.info(`Welcome, ${this.nickname} ! 开始初始化资源...`);
205197
this.sync_cookie = null;
206198
this.sync_finished = false;
207199
await this.register();
208200
if (!this.isOnline())
209201
return;
210-
const initFL = async()=>{
211-
let start = 0;
212-
while (1) {
213-
const total = await resource.initFL.call(this, start);
214-
start += 150;
215-
if (start > total) break;
216-
}
217-
}
218202
await Promise.all([
219-
initFL(), resource.initGL.call(this)
203+
resource.initFL.call(this),
204+
resource.initGL.call(this)
220205
]);
221206
this.logger.info(`加载了${this.fl.size}个好友,${this.gl.size}个群。`);
222207
await core.getMsg.call(this);
@@ -226,10 +211,6 @@ class AndroidClient extends Client {
226211
});
227212
}
228213

229-
/**
230-
* @private
231-
* @param {Function} callback
232-
*/
233214
_connect(callback = ()=>{}) {
234215
if (this.status !== Client.OFFLINE) {
235216
return callback();
@@ -244,22 +225,12 @@ class AndroidClient extends Client {
244225
});
245226
}
246227

247-
/**
248-
* @private
249-
* @returns {Number} this.seq_id
250-
*/
251228
nextSeq() {
252229
if (++this.seq_id >= 0x8000)
253230
this.seq_id = 1;
254231
return this.seq_id;
255232
}
256233

257-
/**
258-
* @private
259-
* @param {Buffer} packet
260-
* @param {Number} timeout ms
261-
* @returns {OICQResponse}
262-
*/
263234
async send(packet, timeout = 3000) {
264235
const seq_id = this.seq_id;
265236
return new Promise((resolve, reject)=>{
@@ -284,14 +255,11 @@ class AndroidClient extends Client {
284255
return await this.send(wt.build0x0BPacket.apply(this, arguments));
285256
}
286257

287-
/**
288-
* @private
289-
*/
290258
startHeartbeat() {
291259
if (this.heartbeat)
292260
return;
293261
this.heartbeat = setInterval(async()=>{
294-
this._calc_msg_cnt();
262+
this.doCircle();
295263
if (Date.now() - this.send_timestamp > 240000)
296264
core.getMsg.call(this);
297265
try {
@@ -354,29 +322,6 @@ class AndroidClient extends Client {
354322
}
355323
}
356324

357-
/**
358-
* @private
359-
* @param {Number} group_id
360-
*/
361-
async _getGroupMemberList(group_id) {
362-
let mlist = new Map();
363-
try {
364-
var next = 0;
365-
while (1) {
366-
var {map, next} = await resource.getGML.call(this, group_id, next);
367-
mlist = new Map([...mlist, ...map]);
368-
if (!next) break;
369-
}
370-
} catch (e) {}
371-
if (!mlist.size) {
372-
this.gml.delete(group_id);
373-
return null;
374-
} else {
375-
this.gml.set(group_id, mlist);
376-
return mlist;
377-
}
378-
}
379-
380325
/**
381326
* @param {Function} fn
382327
* @param {Array} params
@@ -404,16 +349,12 @@ class AndroidClient extends Client {
404349
}
405350
}
406351

407-
/**
408-
* @param {String} name
409-
* @param {Object} data
410-
*/
411352
em(name, data = {}) {
412353
const slice = name.split(".");
413354
const post_type = slice[0], sub_type = slice[2];
414355
const param = {
415356
self_id: this.uin,
416-
time: parseInt(Date.now()/1000),
357+
time: timestamp(),
417358
post_type: post_type
418359
};
419360
const type_name = slice[0] + "_type";
@@ -430,17 +371,39 @@ class AndroidClient extends Client {
430371
this.emit(post_type, param);
431372
}
432373

433-
/**
434-
* 计算每分钟消息数量
435-
*/
436-
_calc_msg_cnt() {
437-
for (let i = 0; i < this.msg_times.length; ++i) {
438-
if (Date.now() - this.msg_times[i] * 1000 <= 60000) {
439-
this.msg_times = this.msg_times.slice(i);
440-
return;
441-
}
374+
msgExists(from, type, seq, time) {
375+
if (timestamp() - time >= 60)
376+
return true;
377+
const id = [from, type, seq].join("-");
378+
const set = this.seq_cache.get(time);
379+
if (!set) {
380+
this.seq_cache.set(time, new Set([id]));
381+
return false;
382+
} else {
383+
if (set.has(id))
384+
return true;
385+
else
386+
set.add(id);
387+
return false;
442388
}
443-
this.msg_times = [];
389+
}
390+
doCircle() {
391+
for (let time of this.seq_cache.keys()) {
392+
if (timestamp() - time >= 60)
393+
this.seq_cache.delete(time);
394+
else
395+
break;
396+
}
397+
}
398+
calcMsgCnt() {
399+
let cnt = 0;
400+
for (let [time, set] of this.seq_cache) {
401+
if (timestamp() - time >= 60)
402+
this.seq_cache.delete(time);
403+
else
404+
cnt += set.size;
405+
}
406+
return cnt;
444407
}
445408

446409
// 以下是public方法 ----------------------------------------------------------------------------------------------------
@@ -468,7 +431,9 @@ class AndroidClient extends Client {
468431
captchaLogin(captcha) {
469432
if (!this.captcha_sign)
470433
return this.logger.error("未收到图片验证码或已过期,你不能调用captchaLogin函数。");
471-
wt.captchaLogin.call(this, captcha);
434+
this._connect(()=>{
435+
wt.captchaLogin.call(this, captcha);
436+
});
472437
}
473438

474439
terminate() {
@@ -508,7 +473,7 @@ class AndroidClient extends Client {
508473
if (!checkUin(group_id))
509474
return buildApiRet(100);
510475
if (!this.gml.has(group_id))
511-
this.gml.set(group_id, this._getGroupMemberList(group_id));
476+
this.gml.set(group_id, resource.getGML.call(this, group_id));
512477
let mlist = this.gml.get(group_id);
513478
if (mlist instanceof Promise)
514479
mlist = await mlist;
@@ -543,7 +508,7 @@ class AndroidClient extends Client {
543508

544509
///////////////////////////////////////////////////
545510

546-
// async setGroupAnonymousBan(group_id, anonymous_flag, duration = 1800) {}
511+
// async setGroupAnonymousBan(group_id, anonymous_flag, duration = 1800) {}
547512
async setGroupAnonymous(group_id, enable = true) {
548513
return await this.useProtocol(troop.setAnonymous, arguments);
549514
}
@@ -603,7 +568,6 @@ class AndroidClient extends Client {
603568
return await this.useProtocol(troop.inviteFriend, arguments);
604569
}
605570

606-
607571
async sendLike(user_id, times = 1) {
608572
return await this.useProtocol(indi.sendLike, arguments);
609573
}
@@ -650,7 +614,7 @@ class AndroidClient extends Client {
650614
return buildApiRet(0, {cookies});
651615
}
652616

653-
async getCsrfToken(domain) {
617+
async getCsrfToken() {
654618
// await wt.exchangeEMP();
655619
let token = 5381;
656620
for (let v of this.sig.skey)
@@ -696,11 +660,10 @@ class AndroidClient extends Client {
696660
return buildApiRet(0, version);
697661
}
698662
getStatus() {
699-
this._calc_msg_cnt();
700663
return buildApiRet(0, {
701664
online: this.isOnline(),
702665
status: this.online_status,
703-
msg_cnt_per_min: this.msg_times.length,
666+
msg_cnt_per_min: this.calcMsgCnt()
704667
})
705668
}
706669
getLoginInfo() {
@@ -714,25 +677,23 @@ class AndroidClient extends Client {
714677

715678
//----------------------------------------------------------------------------------------------------
716679

680+
/**
681+
* @deprecated
682+
*/
717683
const logger = log4js.getLogger("[SYSTEM]");
718684
logger.level = "info";
719-
console.log("OICQ程序启动。当前内核版本:v" + version.version);
720-
721-
const config = {
722-
cache_root: path.join(process.mainModule.path, "data"),
723-
debug: false,
724-
};
725-
726685
process.OICQ = {
727-
logger, config
686+
logger
728687
};
729688

730-
function createCacheDir(uin) {
731-
if (!fs.existsSync(config.cache_root))
732-
fs.mkdirSync(config.cache_root, {mode: 0o755, recursive: true});
733-
const img_path = path.join(config.cache_root, "image");
734-
const ptt_path = path.join(config.cache_root, "record");
735-
const uin_path = path.join(config.cache_root, uin.toString());
689+
console.log("OICQ程序启动。当前内核版本:v" + version.version);
690+
691+
function createCacheDir(dir, uin) {
692+
if (!fs.existsSync(dir))
693+
fs.mkdirSync(dir, {mode: 0o755, recursive: true});
694+
const img_path = path.join(dir, "image");
695+
const ptt_path = path.join(dir, "record");
696+
const uin_path = path.join(dir, String(uin));
736697
if (!fs.existsSync(img_path))
737698
fs.mkdirSync(img_path);
738699
if (!fs.existsSync(ptt_path))
@@ -744,13 +705,8 @@ function createCacheDir(uin) {
744705

745706
/**
746707
* @deprecated
747-
* @param {JSON} config
748708
*/
749-
function setGlobalConfig(config = {}) {
750-
Object.assign(process.OICQ.config, config);
751-
if (config.debug)
752-
logger.level = "debug";
753-
}
709+
function setGlobalConfig() {}
754710

755711
/**
756712
* @param {Number} uin

docs/api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const client = oicq.createClient(uin, config);
3737
log_level: "info", //日志级别,有trace,debug,info,warn,error,fatal,off
3838
kickoff: false, //被挤下线是否在3秒后反挤
3939
ignore_self: true, //是否无视自己的消息(群聊、私聊)
40+
data_dir: undefined, //数据存储文件夹,需要可写权限,默认主目录下的data文件夹
4041
}
4142
```
4243

0 commit comments

Comments
 (0)