@@ -8,7 +8,7 @@ const spawn = require("child_process");
8
8
const crypto = require ( "crypto" ) ;
9
9
const log4js = require ( "log4js" ) ;
10
10
const device = require ( "./device" ) ;
11
- const { checkUin} = require ( "./lib/common" ) ;
11
+ const { checkUin, timestamp } = require ( "./lib/common" ) ;
12
12
const core = require ( "./lib/core" ) ;
13
13
const resource = require ( "./lib/resource" ) ;
14
14
const sysmsg = require ( "./lib/sysmsg" ) ;
@@ -78,7 +78,6 @@ class AndroidClient extends Client {
78
78
seq_id = 0 ;
79
79
handlers = new Map ( ) ;
80
80
seq_cache = new Map ( ) ;
81
- notify33cache = new Set ( ) ;
82
81
83
82
session_id = crypto . randomBytes ( 4 ) ;
84
83
random_key = crypto . randomBytes ( 16 ) ;
@@ -99,7 +98,6 @@ class AndroidClient extends Client {
99
98
device_token : BUF0 ,
100
99
} ;
101
100
cookies = { } ;
102
- msg_times = [ ] ;
103
101
104
102
sync_finished = false ;
105
103
sync_cookie ;
@@ -109,25 +107,22 @@ class AndroidClient extends Client {
109
107
110
108
dir ;
111
109
112
- /**
113
- * @constructor
114
- * @param {Number } uin
115
- * @param {Object } config
116
- */
117
110
constructor ( uin , config = { } ) {
118
111
super ( ) ;
119
112
this . uin = uin ;
120
- this . dir = createCacheDir ( uin ) ;
121
113
122
114
config = {
123
115
platform : 2 , //1手机 2平板 3手表(不支持部分群事件)
124
116
log_level : "info" , //trace,debug,info,warn,error,fatal,off
125
117
kickoff : false , //被挤下线是否在3秒后反挤
126
118
ignore_self : true , //是否无视自己的消息(群聊、私聊)
119
+ data_dir : path . join ( process . mainModule . path , "data" ) ,
127
120
...config
128
121
} ;
129
122
this . config = config ;
130
123
124
+ this . dir = createCacheDir ( config . data_dir , uin ) ;
125
+
131
126
this . logger = log4js . getLogger ( `[BOT:${ uin } ]` ) ;
132
127
this . logger . level = config . log_level ;
133
128
this . ignore_self = config . ignore_self ;
@@ -198,25 +193,15 @@ class AndroidClient extends Client {
198
193
} )
199
194
200
195
this . on ( "internal.login" , async ( ) => {
201
- this . once ( "internal.change-server" , ( ) => {
202
- // todo
203
- } ) ;
204
196
this . logger . info ( `Welcome, ${ this . nickname } ! 开始初始化资源...` ) ;
205
197
this . sync_cookie = null ;
206
198
this . sync_finished = false ;
207
199
await this . register ( ) ;
208
200
if ( ! this . isOnline ( ) )
209
201
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
- }
218
202
await Promise . all ( [
219
- initFL ( ) , resource . initGL . call ( this )
203
+ resource . initFL . call ( this ) ,
204
+ resource . initGL . call ( this )
220
205
] ) ;
221
206
this . logger . info ( `加载了${ this . fl . size } 个好友,${ this . gl . size } 个群。` ) ;
222
207
await core . getMsg . call ( this ) ;
@@ -226,10 +211,6 @@ class AndroidClient extends Client {
226
211
} ) ;
227
212
}
228
213
229
- /**
230
- * @private
231
- * @param {Function } callback
232
- */
233
214
_connect ( callback = ( ) => { } ) {
234
215
if ( this . status !== Client . OFFLINE ) {
235
216
return callback ( ) ;
@@ -244,22 +225,12 @@ class AndroidClient extends Client {
244
225
} ) ;
245
226
}
246
227
247
- /**
248
- * @private
249
- * @returns {Number } this.seq_id
250
- */
251
228
nextSeq ( ) {
252
229
if ( ++ this . seq_id >= 0x8000 )
253
230
this . seq_id = 1 ;
254
231
return this . seq_id ;
255
232
}
256
233
257
- /**
258
- * @private
259
- * @param {Buffer } packet
260
- * @param {Number } timeout ms
261
- * @returns {OICQResponse }
262
- */
263
234
async send ( packet , timeout = 3000 ) {
264
235
const seq_id = this . seq_id ;
265
236
return new Promise ( ( resolve , reject ) => {
@@ -284,14 +255,11 @@ class AndroidClient extends Client {
284
255
return await this . send ( wt . build0x0BPacket . apply ( this , arguments ) ) ;
285
256
}
286
257
287
- /**
288
- * @private
289
- */
290
258
startHeartbeat ( ) {
291
259
if ( this . heartbeat )
292
260
return ;
293
261
this . heartbeat = setInterval ( async ( ) => {
294
- this . _calc_msg_cnt ( ) ;
262
+ this . doCircle ( ) ;
295
263
if ( Date . now ( ) - this . send_timestamp > 240000 )
296
264
core . getMsg . call ( this ) ;
297
265
try {
@@ -354,29 +322,6 @@ class AndroidClient extends Client {
354
322
}
355
323
}
356
324
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
-
380
325
/**
381
326
* @param {Function } fn
382
327
* @param {Array } params
@@ -404,16 +349,12 @@ class AndroidClient extends Client {
404
349
}
405
350
}
406
351
407
- /**
408
- * @param {String } name
409
- * @param {Object } data
410
- */
411
352
em ( name , data = { } ) {
412
353
const slice = name . split ( "." ) ;
413
354
const post_type = slice [ 0 ] , sub_type = slice [ 2 ] ;
414
355
const param = {
415
356
self_id : this . uin ,
416
- time : parseInt ( Date . now ( ) / 1000 ) ,
357
+ time : timestamp ( ) ,
417
358
post_type : post_type
418
359
} ;
419
360
const type_name = slice [ 0 ] + "_type" ;
@@ -430,17 +371,39 @@ class AndroidClient extends Client {
430
371
this . emit ( post_type , param ) ;
431
372
}
432
373
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 ;
442
388
}
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 ;
444
407
}
445
408
446
409
// 以下是public方法 ----------------------------------------------------------------------------------------------------
@@ -468,7 +431,9 @@ class AndroidClient extends Client {
468
431
captchaLogin ( captcha ) {
469
432
if ( ! this . captcha_sign )
470
433
return this . logger . error ( "未收到图片验证码或已过期,你不能调用captchaLogin函数。" ) ;
471
- wt . captchaLogin . call ( this , captcha ) ;
434
+ this . _connect ( ( ) => {
435
+ wt . captchaLogin . call ( this , captcha ) ;
436
+ } ) ;
472
437
}
473
438
474
439
terminate ( ) {
@@ -508,7 +473,7 @@ class AndroidClient extends Client {
508
473
if ( ! checkUin ( group_id ) )
509
474
return buildApiRet ( 100 ) ;
510
475
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 ) ) ;
512
477
let mlist = this . gml . get ( group_id ) ;
513
478
if ( mlist instanceof Promise )
514
479
mlist = await mlist ;
@@ -543,7 +508,7 @@ class AndroidClient extends Client {
543
508
544
509
///////////////////////////////////////////////////
545
510
546
- // async setGroupAnonymousBan(group_id, anonymous_flag, duration = 1800) {}
511
+ // async setGroupAnonymousBan(group_id, anonymous_flag, duration = 1800) {}
547
512
async setGroupAnonymous ( group_id , enable = true ) {
548
513
return await this . useProtocol ( troop . setAnonymous , arguments ) ;
549
514
}
@@ -603,7 +568,6 @@ class AndroidClient extends Client {
603
568
return await this . useProtocol ( troop . inviteFriend , arguments ) ;
604
569
}
605
570
606
-
607
571
async sendLike ( user_id , times = 1 ) {
608
572
return await this . useProtocol ( indi . sendLike , arguments ) ;
609
573
}
@@ -650,7 +614,7 @@ class AndroidClient extends Client {
650
614
return buildApiRet ( 0 , { cookies} ) ;
651
615
}
652
616
653
- async getCsrfToken ( domain ) {
617
+ async getCsrfToken ( ) {
654
618
// await wt.exchangeEMP();
655
619
let token = 5381 ;
656
620
for ( let v of this . sig . skey )
@@ -696,11 +660,10 @@ class AndroidClient extends Client {
696
660
return buildApiRet ( 0 , version ) ;
697
661
}
698
662
getStatus ( ) {
699
- this . _calc_msg_cnt ( ) ;
700
663
return buildApiRet ( 0 , {
701
664
online : this . isOnline ( ) ,
702
665
status : this . online_status ,
703
- msg_cnt_per_min : this . msg_times . length ,
666
+ msg_cnt_per_min : this . calcMsgCnt ( )
704
667
} )
705
668
}
706
669
getLoginInfo ( ) {
@@ -714,25 +677,23 @@ class AndroidClient extends Client {
714
677
715
678
//----------------------------------------------------------------------------------------------------
716
679
680
+ /**
681
+ * @deprecated
682
+ */
717
683
const logger = log4js . getLogger ( "[SYSTEM]" ) ;
718
684
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
-
726
685
process . OICQ = {
727
- logger, config
686
+ logger
728
687
} ;
729
688
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 ) ) ;
736
697
if ( ! fs . existsSync ( img_path ) )
737
698
fs . mkdirSync ( img_path ) ;
738
699
if ( ! fs . existsSync ( ptt_path ) )
@@ -744,13 +705,8 @@ function createCacheDir(uin) {
744
705
745
706
/**
746
707
* @deprecated
747
- * @param {JSON } config
748
708
*/
749
- function setGlobalConfig ( config = { } ) {
750
- Object . assign ( process . OICQ . config , config ) ;
751
- if ( config . debug )
752
- logger . level = "debug" ;
753
- }
709
+ function setGlobalConfig ( ) { }
754
710
755
711
/**
756
712
* @param {Number } uin
0 commit comments