-
Notifications
You must be signed in to change notification settings - Fork 8
/
client.ts
977 lines (911 loc) · 29.8 KB
/
client.ts
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
import os from 'os';
import { Socket, SocketOptions } from '@rustup/nng';
import * as cp from 'child_process';
import debug from 'debug';
import { wcf } from './proto-generated/wcf';
import * as rd from './proto-generated/roomdata';
import * as eb from './proto-generated/extrabyte';
import { EventEmitter } from 'events';
import {
createTmpDir,
ensureDirSync,
sleep,
uint8Array2str,
type ToPlainType,
} from './utils';
import { FileRef, FileSavableInterface } from './file-ref';
import { Message } from './message';
import path from 'path';
export type UserInfo = ToPlainType<wcf.UserInfo>;
export type Contact = ToPlainType<wcf.RpcContact>;
export type DbTable = ToPlainType<wcf.DbTable>;
export interface WcferryOptions {
port?: number;
/** if host is empty, the program will try to load wcferry.exe and *.dll */
host?: string;
socketOptions?: SocketOptions;
/** the cache dir to hold temp files, defaults to `os.tmpdir()/wcferry` */
cacheDir?: string;
// 当使用wcferry.on(...)监听消息时,是否接受朋友圈消息
recvPyq?: boolean;
}
const logger = debug('wcferry:client');
export class Wcferry {
readonly NotFriend = {
fmessage: '朋友推荐消息',
medianote: '语音记事本',
floatbottle: '漂流瓶',
filehelper: '文件传输助手',
newsapp: '新闻',
};
private isMsgReceiving = false;
private msgDispose?: () => void;
private socket: Socket;
private readonly localMode;
private readonly msgEventSub = new EventEmitter();
private options: Required<WcferryOptions>;
constructor(options?: WcferryOptions) {
this.localMode = !options?.host;
this.options = {
port: options?.port || 10086,
host: options?.host || '127.0.0.1',
socketOptions: options?.socketOptions ?? {},
cacheDir: options?.cacheDir || createTmpDir(),
recvPyq: !!options?.recvPyq,
};
ensureDirSync(this.options.cacheDir);
this.msgEventSub.setMaxListeners(0);
this.socket = new Socket(this.options.socketOptions);
}
private trapOnExit() {
process.on('exit', () => this.stop());
}
get connected() {
return this.socket.connected();
}
get msgReceiving() {
return this.isMsgReceiving;
}
private createUrl(channel: 'cmd' | 'msg' = 'cmd') {
const url = `tcp://${this.options.host}:${
this.options.port + (channel === 'cmd' ? 0 : 1)
}`;
logger(`wcf ${channel} url: %s`, url);
return url;
}
/**
* 设置是否接受朋友圈消息
*/
set recvPyq(pyq: boolean) {
if (this.options.recvPyq === pyq) {
return;
}
this.options.recvPyq = pyq;
if (this.connected) {
this.disableMsgReceiving();
this.enableMsgReceiving();
}
}
get recvPyq(): boolean {
return this.options.recvPyq;
}
private get msgListenerCount() {
return this.msgEventSub.listenerCount('wxmsg');
}
start() {
try {
this.execDLL('start');
this.socket.connect(this.createUrl());
this.trapOnExit();
if (this.msgListenerCount > 0) {
this.enableMsgReceiving();
}
} catch (err) {
logger('cannot connect to wcf RPC server, did wcf.exe started?');
throw err;
}
}
execDLL(verb: 'start' | 'stop') {
if (!this.localMode) {
return;
}
const scriptPath = path.resolve(__dirname, '../../scripts/wcferry.ps1');
const process = cp.spawnSync(
'powershell',
[
// '-NonInteractive',
'-ExecutionPolicy Unrestricted',
`-File ${scriptPath} -Verb ${verb} -Port ${this.options.port}`,
],
{ shell: true, stdio: 'inherit' }
);
if (process.error || process.status !== 0) {
throw new Error(
`Cannot ${verb} wcferry DLL: ${
process.error || `exit ${process.status}`
}`
);
}
}
stop() {
logger('Closing conneciton...');
this.disableMsgReceiving();
this.socket.close();
this.execDLL('stop');
}
private sendRequest(req: wcf.Request): wcf.Response {
const data = req.serialize();
const buf = this.socket.send(Buffer.from(data));
const res = wcf.Response.deserialize(buf);
return res;
}
/** 是否已经登录 */
isLogin(): boolean {
const req = new wcf.Request({
func: wcf.Functions.FUNC_IS_LOGIN,
});
const rsp = this.sendRequest(req);
return rsp.status == 1;
}
/**获取登录账号wxid */
getSelfWxid(): string {
const req = new wcf.Request({
func: wcf.Functions.FUNC_GET_SELF_WXID,
});
const rsp = this.sendRequest(req);
return rsp.str;
}
/** 获取登录账号个人信息 */
getUserInfo(): UserInfo {
const req = new wcf.Request({
func: wcf.Functions.FUNC_GET_USER_INFO,
});
const rsp = this.sendRequest(req);
return rsp.ui;
}
/** 获取完整通讯录 */
getContacts(): Contact[] {
const req = new wcf.Request({
func: wcf.Functions.FUNC_GET_CONTACTS,
});
const rsp = this.sendRequest(req);
return rsp.contacts.contacts.map((c) => c.toObject() as Contact);
}
/** 通过 wxid 查询微信号昵称等信息 */
getContact(wxid: string): Contact | undefined {
const req = new wcf.Request({
func: wcf.Functions.FUNC_GET_CONTACT_INFO,
str: wxid,
});
const rsp = this.sendRequest(req);
return rsp.contacts.contacts[0].toObject() as Contact;
}
/** 获取所有数据库 */
getDbNames(): string[] {
const req = new wcf.Request({
func: wcf.Functions.FUNC_GET_DB_NAMES,
});
const rsp = this.sendRequest(req);
return rsp.dbs.names;
}
/** 获取数据库中所有表 */
getDbTables(db: string): DbTable[] {
const req = new wcf.Request({
func: wcf.Functions.FUNC_GET_DB_TABLES,
str: db,
});
const rsp = this.sendRequest(req);
return rsp.tables.tables.map((t) => t.toObject() as DbTable);
}
/**
* 执行 SQL 查询,如果数据量大注意分页
* @param db
* @param sql
*/
dbSqlQuery(
db: string,
sql: string
): Record<string, string | number | Buffer | undefined>[] {
const req = new wcf.Request({
func: wcf.Functions.FUNC_EXEC_DB_QUERY,
query: new wcf.DbQuery({ db, sql }),
});
const rsp = this.sendRequest(req);
const rows = rsp.rows.rows;
return rows.map((r) =>
Object.fromEntries(
r.fields.map((f) => [f.column, parseDbField(f.type, f.content)])
)
);
}
/**
* 获取消息类型
* {"47": "石头剪刀布 | 表情图片", "62": "小视频", "43": "视频", "1": "文字", "10002": "撤回消息", "40": "POSSIBLEFRIEND_MSG", "10000": "红包、系统消息", "37": "好友确认", "48": "位置", "42": "名片", "49": "共享实时位置、文件、转账、链接", "3": "图片", "34": "语音", "9999": "SYSNOTICE", "52": "VOIPNOTIFY", "53": "VOIPINVITE", "51": "微信初始化", "50": "VOIPMSG"}
*/
getMsgTypes(): Map<number, string> {
const req = new wcf.Request({
func: wcf.Functions.FUNC_GET_MSG_TYPES,
});
const rsp = this.sendRequest(req);
return rsp.types.types;
}
/**
* 刷新朋友圈
* @param id 开始 id,0 为最新页 (string based uint64)
* @returns 1 为成功,其他失败
*/
refreshPyq(id: string): number {
const req = new wcf.Request({
func: wcf.Functions.FUNC_REFRESH_PYQ,
ui64: id,
});
const rsp = this.sendRequest(req);
return rsp.status;
}
/** 获取群聊列表 */
getChatRooms(): Contact[] {
const contacts = this.getContacts();
return contacts.filter((c) => c.wxid.endsWith('@chatroom'));
}
/**
* 获取好友列表
* @returns
*/
getFriends() {
const contacts = this.getContacts();
return contacts.filter(
(c) =>
!c.wxid.endsWith('@chatroom') &&
!c.wxid.startsWith('gh_') &&
!Object.hasOwn(this.NotFriend, c.wxid)
);
}
/**
* 获取群成员
* @param roomid 群的 id
* @param times 重试次数
* @returns 群成员列表: {wxid1: 昵称1, wxid2: 昵称2, ...}
*/
async getChatRoomMembers(
roomid: string,
times = 5
): Promise<Record<string, string>> {
if (times === 0) {
return {};
}
const [room] = this.dbSqlQuery(
'MicroMsg.db',
`SELECT RoomData FROM ChatRoom WHERE ChatRoomName = '${roomid}';`
);
if (!room) {
await sleep();
return this.getChatRoomMembers(roomid, times - 1) ?? {};
}
const r = rd.com.iamteer.wcf.RoomData.deserialize(
room['RoomData'] as Buffer
);
const userRds = this.dbSqlQuery(
'MicroMsg.db',
'SELECT UserName, NickName FROM Contact;'
);
const userDict = Object.fromEntries(
userRds.map((u) => [u['UserName'], u['NickName']] as const)
);
return Object.fromEntries(
r.members.map((member) => [
member.wxid,
member.name || userDict[member.wxid],
])
);
}
/**
* 获取群成员昵称
* @param wxid
* @param roomid
* @returns 群名片
*/
getAliasInChatRoom(wxid: string, roomid: string): string | undefined {
const [room] = this.dbSqlQuery(
'MicroMsg.db',
`SELECT RoomData FROM ChatRoom WHERE ChatRoomName = '${roomid}';`
);
if (!room) {
return undefined;
}
const roomData = rd.com.iamteer.wcf.RoomData.deserialize(
room['RoomData'] as Buffer
);
return (
roomData.members.find((m) => m.wxid === wxid)?.name ||
this.getNickName(wxid)?.[0]
);
}
/**
* be careful to SQL injection
* @param wxids wxids
*/
getNickName(...wxids: string[]): Array<string | undefined> {
const rows = this.dbSqlQuery(
'MicroMsg.db',
`SELECT NickName FROM Contact WHERE UserName in (${wxids
.map((id) => `'${id}'`)
.join(',')});`
);
return rows.map((row) => row['NickName'] as string | undefined);
}
/**
* 邀请群成员
* @param roomid
* @param wxids
* @returns int32 1 为成功,其他失败
*/
inviteChatroomMembers(roomid: string, wxids: string[]): number {
const req = new wcf.Request({
func: wcf.Functions.FUNC_INV_ROOM_MEMBERS,
m: new wcf.MemberMgmt({
roomid,
wxids: wxids.join(',').replaceAll(' ', ''),
}),
});
const rsp = this.sendRequest(req);
return rsp.status;
}
/**
* 添加群成员
* @param roomid
* @param wxids
* @returns int32 1 为成功,其他失败
*/
addChatRoomMembers(roomid: string, wxids: string[]): number {
const req = new wcf.Request({
func: wcf.Functions.FUNC_ADD_ROOM_MEMBERS,
m: new wcf.MemberMgmt({
roomid,
wxids: wxids.join(',').replaceAll(' ', ''),
}),
});
const rsp = this.sendRequest(req);
return rsp.status;
}
/**
* 删除群成员
* @param roomid
* @param wxids
* @returns int32 1 为成功,其他失败
*/
delChatRoomMembers(roomid: string, wxids: string[]): number {
const req = new wcf.Request({
func: wcf.Functions.FUNC_DEL_ROOM_MEMBERS,
m: new wcf.MemberMgmt({
roomid,
wxids: wxids.join(',').replaceAll(' ', ''),
}),
});
const rsp = this.sendRequest(req);
return rsp.status;
}
/**
* 撤回消息
* @param msgid (uint64 in string format): 消息 id
* @returns int: 1 为成功,其他失败
*/
revokeMsg(msgid: string): number {
const req = new wcf.Request({
func: wcf.Functions.FUNC_REVOKE_MSG,
ui64: msgid,
});
const rsp = this.sendRequest(req);
return rsp.status;
}
/**
* 转发消息。可以转发文本、图片、表情、甚至各种 XML;语音也行,不过效果嘛,自己验证吧。
* @param msgid (uint64 in string format): 消息 id
* @param receiver string 消息接收人,wxid 或者 roomid
* @returns int: 1 为成功,其他失败
*/
forwardMsg(msgid: string, receiver: string): number {
const req = new wcf.Request({
func: wcf.Functions.FUNC_FORWARD_MSG,
fm: new wcf.ForwardMsg({
id: msgid,
receiver,
}),
});
const rsp = this.sendRequest(req);
return rsp.status;
}
/**
* 发送文本消息
* @param msg 要发送的消息,换行使用 `\n` (单杠);如果 @ 人的话,需要带上跟 `aters` 里数量相同的 @
* @param receiver 消息接收人,wxid 或者 roomid
* @param aters 要 @ 的 wxid,多个用逗号分隔;`@所有人` 只需要 `notify@all`
* @returns 0 为成功,其他失败
*/
sendTxt(msg: string, receiver: string, aters?: string): number {
const req = new wcf.Request({
func: wcf.Functions.FUNC_SEND_TXT,
txt: new wcf.TextMsg({
msg,
receiver,
aters,
}),
});
const rsp = this.sendRequest(req);
return rsp.status;
}
/**
* @param image location of the resource, can be:
* - a local path (`C:\\Users` or `/home/user`),
* - a link starts with `http(s)://`,
* - a buffer (base64 string can be convert to buffer by `Buffer.from(<str>, 'base64')`)
* - an object { type: 'Buffer', data: number[] } which can convert to Buffer
* - a FileSavableInterface instance
* @param receiver 消息接收人,wxid 或者 roomid
* @returns 0 为成功,其他失败
*/
async sendImage(
image:
| string
| Buffer
| { type: 'Buffer'; data: number[] }
| FileSavableInterface,
receiver: string
): Promise<number> {
const fileRef = toRef(image);
const { path, discard } = await fileRef.save(this.options.cacheDir);
const req = new wcf.Request({
func: wcf.Functions.FUNC_SEND_IMG,
file: new wcf.PathMsg({
path,
receiver,
}),
});
const rsp = this.sendRequest(req);
void discard();
return rsp.status;
}
/**
* @param file location of the resource, can be:
* - a local path (`C:\\Users` or `/home/user`),
* - a link starts with `http(s)://`,
* - a buffer (base64 string can be convert to buffer by `Buffer.from(<str>, 'base64')`)
* - an object { type: 'Buffer', data: number[] } which can convert to Buffer
* - a FileSavableInterface instance
* @param receiver 消息接收人,wxid 或者 roomid
* @returns 0 为成功,其他失败
*/
async sendFile(
file:
| string
| Buffer
| { type: 'Buffer'; data: number[] }
| FileSavableInterface,
receiver: string
): Promise<number> {
const fileRef = toRef(file);
const { path, discard } = await fileRef.save(this.options.cacheDir);
const req = new wcf.Request({
func: wcf.Functions.FUNC_SEND_FILE,
file: new wcf.PathMsg({
path,
receiver,
}),
});
const rsp = this.sendRequest(req);
void discard();
return rsp.status;
}
/**
* @deprecated Not supported
* 发送XML
* @param xml.content xml 内容
* @param xml.path 封面图片路径
* @param receiver xml 类型,如:0x21 为小程序
* @returns 0 为成功,其他失败
*/
sendXML(
xml: { content: string; path?: string; type: number },
receiver: string
): number {
const req = new wcf.Request({
func: wcf.Functions.FUNC_SEND_XML,
xml: new wcf.XmlMsg({
receiver,
content: xml.content,
type: xml.type,
path: xml.path,
}),
});
const rsp = this.sendRequest(req);
return rsp.status;
}
/**
* @deprecated Not supported
* 发送表情
* @param path 本地表情路径,如:`C:/Projs/WeChatRobot/emo.gif`
* @param receiver 消息接收人,wxid 或者 roomid
* @returns 0 为成功,其他失败
*/
sendEmotion(path: string, receiver: string): number {
const req = new wcf.Request({
func: wcf.Functions.FUNC_SEND_EMOTION,
file: new wcf.PathMsg({
path,
receiver,
}),
});
const rsp = this.sendRequest(req);
return rsp.status;
}
/**
* 发送富文本消息
* 卡片样式:
* |-------------------------------------|
* |title, 最长两行
* |(长标题, 标题短的话这行没有)
* |digest, 最多三行,会占位 |--------|
* |digest, 最多三行,会占位 |thumburl|
* |digest, 最多三行,会占位 |--------|
* |(account logo) name
* |-------------------------------------|
* @param desc.name 左下显示的名字
* @param desc.account 填公众号 id 可以显示对应的头像(gh_ 开头的)
* @param desc.title 标题,最多两行
* @param desc.digest 摘要,三行
* @param desc.url 点击后跳转的链接
* @param desc.thumburl 缩略图的链接
* @param receiver 接收人, wxid 或者 roomid
* @returns 0 为成功,其他失败
*/
sendRichText(
desc: Omit<ReturnType<wcf.RichText['toObject']>, 'receiver'>,
receiver: string
): number {
const req = new wcf.Request({
func: wcf.Functions.FUNC_SEND_RICH_TXT,
rt: new wcf.RichText({
...desc,
receiver,
}),
});
const rsp = this.sendRequest(req);
return rsp.status;
}
/**
* 拍一拍群友
* @param roomid 群 id
* @param wxid 要拍的群友的 wxid
* @returns 1 为成功,其他失败
*/
sendPat(roomid: string, wxid: string): number {
const req = new wcf.Request({
func: wcf.Functions.FUNC_SEND_PAT_MSG,
pm: new wcf.PatMsg({
roomid,
wxid,
}),
});
const rsp = this.sendRequest(req);
return rsp.status;
}
/**
* 获取语音消息并转成 MP3
* @param msgid 语音消息 id
* @param dir MP3 保存目录(目录不存在会出错)
* @param times 超时时间(秒)
* @returns 成功返回存储路径;空字符串为失败,原因见日志。
*/
async getAudioMsg(msgid: string, dir: string, times = 3): Promise<string> {
const req = new wcf.Request({
func: wcf.Functions.FUNC_GET_AUDIO_MSG,
am: new wcf.AudioMsg({
id: msgid,
dir,
}),
});
const rsp = this.sendRequest(req);
if (rsp.str) {
return rsp.str;
}
if (times > 0) {
await sleep();
return this.getAudioMsg(msgid, dir, times - 1);
}
throw new Error('Timeout: get audio msg');
}
/**
* 获取 OCR 结果。鸡肋,需要图片能自动下载;通过下载接口下载的图片无法识别。
* @param extra 待识别的图片路径,消息里的 extra
* @param times OCR 结果
* @returns
*/
async getOCRResult(extra: string, times = 2): Promise<string> {
const req = new wcf.Request({
func: wcf.Functions.FUNC_EXEC_OCR,
str: extra,
});
const rsp = this.sendRequest(req);
if (rsp.ocr.status === 0 && rsp.ocr.result) {
return rsp.ocr.result;
}
if (times > 0) {
await sleep();
return this.getOCRResult(extra, times - 1);
}
throw new Error('Timeout: get ocr result');
}
/**
* @deprecated 下载附件(图片、视频、文件)。这方法别直接调用,下载图片使用 `download_image`
* @param msgid 消息中 id
* @param thumb 消息中的 thumb
* @param extra 消息中的 extra
* @returns 0 为成功, 其他失败。
*/
downloadAttach(
msgid: string,
thumb: string = '',
extra: string = ''
): number {
const req = new wcf.Request({
func: wcf.Functions.FUNC_DOWNLOAD_ATTACH,
att: new wcf.AttachMsg({
id: msgid,
thumb,
extra,
}),
});
const rsp = this.sendRequest(req);
return rsp.status;
}
// TODO: get correct wechat files directory somewhere?
private readonly UserDir = path.join(
os.homedir(),
'Documents',
'WeChat Files'
);
private getMsgAttachments(msgid: string): {
extra?: string;
thumb?: string;
} {
const messages = this.dbSqlQuery(
'MSG0.db',
`Select * from MSG WHERE MsgSvrID = "${msgid}"`
);
const buf = messages?.[0]?.['BytesExtra'];
if (!Buffer.isBuffer(buf)) {
return {};
}
const extraData = eb.com.iamteer.wcf.Extra.deserialize(buf);
const { properties } = extraData.toObject();
if (!properties) {
return {};
}
const propertyMap: Partial<
Record<eb.com.iamteer.wcf.Extra.PropertyKey, string>
> = Object.fromEntries(properties.map((p) => [p.type, p.value]));
const extra = propertyMap[eb.com.iamteer.wcf.Extra.PropertyKey.Extra];
const thumb = propertyMap[eb.com.iamteer.wcf.Extra.PropertyKey.Thumb];
return {
extra: extra ? path.resolve(this.UserDir, extra) : '',
thumb: thumb ? path.resolve(this.UserDir, thumb) : '',
};
}
/**
* @deprecated 解密图片。这方法别直接调用,下载图片使用 `download_image`。
* @param src 加密的图片路径
* @param dir 保存图片的目录
* @returns
*/
decryptImage(src: string, dir: string): string {
const req = new wcf.Request({
func: wcf.Functions.FUNC_DECRYPT_IMAGE,
dec: new wcf.DecPath({
src,
dst: dir,
}),
});
const rsp = this.sendRequest(req);
return rsp.str;
}
/**
* 下载图片
* @param msgid 消息中 id
* @param dir 存放图片的目录(目录不存在会出错)
* @param extra 消息中的 extra, 如果为空,自动通过msgid获取
* @param times 超时时间(秒)
* @returns 成功返回存储路径;空字符串为失败,原因见日志。
*/
async downloadImage(
msgid: string,
dir: string,
extra?: string,
thumb?: string,
times = 30
): Promise<string> {
const msgAttachments = extra
? { extra, thumb }
: this.getMsgAttachments(msgid);
if (
this.downloadAttach(
msgid,
msgAttachments.thumb,
msgAttachments.extra
) !== 0
) {
return Promise.reject('Failed to download attach');
}
for (let cnt = 0; cnt < times; cnt++) {
const path = this.decryptImage(msgAttachments.extra || '', dir);
if (path) {
return path;
}
await sleep();
}
return Promise.reject('Failed to decrypt image');
}
/**
* 通过好友申请
* @param v3 加密用户名 (好友申请消息里 v3 开头的字符串)
* @param v4 Ticket (好友申请消息里 v4 开头的字符串)
* @param scene 申请方式 (好友申请消息里的 scene); 为了兼容旧接口,默认为扫码添加 (30)
* @returns 1 为成功,其他失败
*/
acceptNewFriend(v3: string, v4: string, scene = 30): number {
const req = new wcf.Request({
func: wcf.Functions.FUNC_ACCEPT_FRIEND,
v: new wcf.Verification({
v3,
v4,
scene,
}),
});
const rsp = this.sendRequest(req);
return rsp.status;
}
/**
* 接收转账
* @param wxid 转账消息里的发送人 wxid
* @param transferid 转账消息里的 transferid
* @param transactionid 转账消息里的 transactionid
* @returns 1 为成功,其他失败
*/
receiveTransfer(
wxid: string,
transferid: string,
transactionid: string
): number {
const req = new wcf.Request({
func: wcf.Functions.FUNC_RECV_TRANSFER,
tf: new wcf.Transfer({
wxid,
tfid: transferid,
taid: transactionid,
}),
});
const rsp = this.sendRequest(req);
return rsp.status;
}
/**
* @internal 允许接收消息,自动根据on(...)注册的listener调用
* @param pyq
* @returns
*/
private enableMsgReceiving(): boolean {
if (this.isMsgReceiving) {
return true;
}
const req = new wcf.Request({
func: wcf.Functions.FUNC_ENABLE_RECV_TXT,
flag: this.options.recvPyq,
});
const rsp = this.sendRequest(req);
if (rsp.status !== 0) {
this.isMsgReceiving = false;
return false;
}
try {
this.msgDispose = this.receiveMessage();
this.isMsgReceiving = true;
return true;
} catch (err) {
this.msgDispose?.();
this.isMsgReceiving = false;
logger('enable message receiving error: %O', err);
return false;
}
}
/**
* @internal 停止接收消息,自动根据on(...)注册/注销的listener 调用
* @param force
* @returns
*/
private disableMsgReceiving(force = false): number {
if (!force && !this.isMsgReceiving) {
return 0;
}
const req = new wcf.Request({
func: wcf.Functions.FUNC_DISABLE_RECV_TXT,
});
const rsp = this.sendRequest(req);
this.isMsgReceiving = false;
this.msgDispose?.();
this.msgDispose = undefined;
return rsp.status;
}
private receiveMessage() {
const disposable = Socket.recvMessage(
this.createUrl('msg'),
null,
this.messageCallback.bind(this)
);
return () => disposable.dispose();
}
private messageCallback(err: unknown | undefined, buf: Buffer) {
if (err) {
logger('error while receiving message: %O', err);
return;
}
const rsp = wcf.Response.deserialize(buf);
this.msgEventSub.emit('wxmsg', new Message(rsp.wxmsg));
}
/**
* 注册消息回调监听函数(listener), 通过call返回的函数注销
* 当注册的监听函数数量大于0是自动调用enableMsgReceiving,否则自动调用disableMsgReceiving
* 设置wcferry.recvPyq = true/false 来开启关闭接受朋友圈消息
* @param callback 监听函数
* @returns 注销监听函数
*/
on(callback: (msg: Message) => void): () => void {
this.msgEventSub.on('wxmsg', callback);
if (this.connected && this.msgEventSub.listenerCount('wxmsg') === 1) {
this.enableMsgReceiving();
}
return () => {
if (
this.connected &&
this.msgEventSub.listenerCount('wxmsg') === 1
) {
this.disableMsgReceiving();
}
this.msgEventSub.off('wxmsg', callback);
};
}
}
function toRef(
file:
| string
| Buffer
| { type: 'Buffer'; data: number[] }
| FileSavableInterface
): FileSavableInterface {
if (typeof file === 'string' || Buffer.isBuffer(file)) {
return new FileRef(file);
}
if ('save' in file) {
return file;
}
return new FileRef(Buffer.from(file.data));
}
function parseDbField(type: number, content: Uint8Array) {
// self._SQL_TYPES = {1: int, 2: float, 3: lambda x: x.decode("utf-8"), 4: bytes, 5: lambda x: None}
switch (type) {
case 1:
const strContent = uint8Array2str(content);
const bigIntContent = BigInt(strContent);
if (bigIntContent > Number.MAX_SAFE_INTEGER) {
// bigInt 在JSON.stringify时会出问题,还是返回字符串吧
// TypeError: Do not know how to serialize a BigInt
return strContent;
}
return Number.parseInt(strContent, 10);
case 2:
return Number.parseFloat(uint8Array2str(content));
case 3:
default:
return uint8Array2str(content);
case 4:
return Buffer.from(content);
case 5:
return undefined;
}
}