2022-09-01 15:35:27 +00:00
|
|
|
|
import {
|
|
|
|
|
Client,
|
|
|
|
|
DiscussMessageEvent,
|
|
|
|
|
Friend,
|
|
|
|
|
Group,
|
|
|
|
|
GroupMessageEvent,
|
|
|
|
|
LogLevel,
|
|
|
|
|
Platform,
|
|
|
|
|
PrivateMessageEvent,
|
|
|
|
|
} from 'oicq';
|
2022-03-01 13:11:14 +00:00
|
|
|
|
import Buffer from 'buffer';
|
2022-02-23 06:09:54 +00:00
|
|
|
|
import { execSync } from 'child_process';
|
|
|
|
|
import random from '../utils/random';
|
|
|
|
|
import fs from 'fs';
|
|
|
|
|
import fsP from 'fs/promises';
|
|
|
|
|
import path from 'path';
|
|
|
|
|
import { Config } from 'oicq/lib/client';
|
|
|
|
|
|
2022-02-26 10:15:40 +00:00
|
|
|
|
const LOG_LEVEL: LogLevel = 'warn';
|
|
|
|
|
|
2022-03-01 13:11:14 +00:00
|
|
|
|
type MessageHandler = (event: PrivateMessageEvent | GroupMessageEvent) => Promise<boolean | void>
|
|
|
|
|
|
2022-02-23 06:09:54 +00:00
|
|
|
|
interface CreateOicqParams {
|
|
|
|
|
uin: number;
|
|
|
|
|
password: string;
|
|
|
|
|
platform: Platform;
|
2022-03-07 10:05:14 +00:00
|
|
|
|
// 当需要验证手机时调用此方法,应该返回收到的手机验证码
|
2022-02-23 06:09:54 +00:00
|
|
|
|
onVerifyDevice: (phone: string) => Promise<string>;
|
|
|
|
|
// 当滑块时调用此方法,返回 ticker,也可以返回假值改用扫码登录
|
|
|
|
|
onVerifySlider: (url: string) => Promise<string>;
|
|
|
|
|
// 扫码后返回
|
|
|
|
|
onQrCode: (image: Buffer) => Promise<void>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// OicqExtended??
|
|
|
|
|
export default class OicqClient extends Client {
|
2022-03-01 13:11:14 +00:00
|
|
|
|
private readonly onMessageHandlers: Array<MessageHandler> = [];
|
|
|
|
|
|
2022-02-23 06:09:54 +00:00
|
|
|
|
private constructor(uin: number, conf?: Config) {
|
|
|
|
|
super(uin, conf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static create(params: CreateOicqParams) {
|
|
|
|
|
return new Promise<OicqClient>(async (resolve, reject) => {
|
|
|
|
|
async function loginDeviceHandler({ phone }: { url: string, phone: string }) {
|
|
|
|
|
client.sendSmsCode();
|
|
|
|
|
const code = await params.onVerifyDevice(phone);
|
|
|
|
|
client.submitSmsCode(code);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function loginSliderHandler({ url }: { url: string }) {
|
|
|
|
|
const res = await params.onVerifySlider(url);
|
|
|
|
|
if (res) {
|
|
|
|
|
client.submitSlider(res);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
client.login();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function loginQrCodeHandler({ image }: { image: Buffer }) {
|
|
|
|
|
await params.onQrCode(image);
|
|
|
|
|
client.qrcodeLogin();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function loginErrorHandler({ message }: { code: number; message: string }) {
|
|
|
|
|
reject(message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function successLoginHandler() {
|
|
|
|
|
client.off('system.login.device', loginDeviceHandler)
|
|
|
|
|
.off('system.login.slider', loginSliderHandler)
|
|
|
|
|
.off('system.login.qrcode', loginQrCodeHandler)
|
|
|
|
|
.off('system.login.error', loginErrorHandler)
|
2022-03-01 13:11:14 +00:00
|
|
|
|
.off('system.online', successLoginHandler)
|
|
|
|
|
.on('message', client.onMessage);
|
2022-02-23 06:09:54 +00:00
|
|
|
|
resolve(client);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!fs.existsSync(`./data/${params.uin}/device-${params.uin}.json`)) {
|
2022-03-02 10:29:14 +00:00
|
|
|
|
await fsP.mkdir(`./data/${params.uin}`, { recursive: true });
|
2022-02-23 06:09:54 +00:00
|
|
|
|
|
|
|
|
|
const device = {
|
|
|
|
|
product: 'Q2TG',
|
|
|
|
|
device: 'ANGELKAWAII2',
|
|
|
|
|
board: 'raincandy',
|
|
|
|
|
brand: random.pick('GOOGLE', 'XIAOMI', 'HUAWEI', 'SAMSUNG', 'SONY'),
|
|
|
|
|
model: 'raincandy',
|
2022-03-01 13:11:14 +00:00
|
|
|
|
wifi_ssid: random.pick('OpenWrt', `Redmi-${random.hex(4).toUpperCase()}`,
|
|
|
|
|
`MiWifi-${random.hex(4).toUpperCase()}`, `TP-LINK-${random.hex(6).toUpperCase()}`),
|
2022-02-23 06:09:54 +00:00
|
|
|
|
bootloader: random.pick('U-Boot', 'GRUB', 'gummiboot'),
|
|
|
|
|
android_id: random.hex(16),
|
|
|
|
|
proc_version: `${execSync('uname -s').toString().replace('\n', '')} version ${execSync('uname -r').toString().replace('\n', '')}`,
|
|
|
|
|
mac_address: `8c:85:90:${random.hex(2)}:${random.hex(2)}:${random.hex(2)}`.toUpperCase(),
|
|
|
|
|
ip_address: `192.168.${random.int(1, 200)}.${random.int(10, 250)}`,
|
|
|
|
|
incremental: random.int(0, 4294967295),
|
|
|
|
|
imei: random.imei(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await fsP.writeFile(`./data/${params.uin}/device-${params.uin}.json`, JSON.stringify(device, null, 0), 'utf-8');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const client = new this(params.uin, {
|
|
|
|
|
platform: params.platform,
|
|
|
|
|
data_dir: path.resolve('./data'),
|
2022-03-01 13:11:14 +00:00
|
|
|
|
log_level: LOG_LEVEL,
|
2022-09-16 14:32:09 +00:00
|
|
|
|
ffmpeg_path: process.env.FFMPEG_PATH,
|
|
|
|
|
ffprobe_path: process.env.FFPROBE_PATH,
|
2022-02-23 06:09:54 +00:00
|
|
|
|
})
|
|
|
|
|
.on('system.login.device', loginDeviceHandler)
|
|
|
|
|
.on('system.login.slider', loginSliderHandler)
|
|
|
|
|
.on('system.login.qrcode', loginQrCodeHandler)
|
|
|
|
|
.on('system.login.error', loginErrorHandler)
|
|
|
|
|
.on('system.online', successLoginHandler);
|
|
|
|
|
client.login(params.password);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-01 13:11:14 +00:00
|
|
|
|
private onMessage = async (event: PrivateMessageEvent | GroupMessageEvent | DiscussMessageEvent) => {
|
|
|
|
|
if (event.message_type === 'discuss') return;
|
|
|
|
|
for (const handler of this.onMessageHandlers) {
|
|
|
|
|
const res = await handler(event);
|
|
|
|
|
if (res) return;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public addNewMessageEventHandler(handler: MessageHandler) {
|
|
|
|
|
this.onMessageHandlers.push(handler);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public removeNewMessageEventHandler(handler: MessageHandler) {
|
|
|
|
|
this.onMessageHandlers.includes(handler) &&
|
|
|
|
|
this.onMessageHandlers.splice(this.onMessageHandlers.indexOf(handler), 1);
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-01 15:35:27 +00:00
|
|
|
|
public getChat(roomId: number): Group | Friend {
|
2022-02-23 06:09:54 +00:00
|
|
|
|
if (roomId > 0) {
|
|
|
|
|
return this.pickFriend(roomId);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return this.pickGroup(-roomId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|