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

Commit

Permalink
Merge pull request #143 from takayama-lily/1.14
Browse files Browse the repository at this point in the history
1.14
  • Loading branch information
takayama-lily authored Mar 17, 2021
2 parents a7a736b + 0baa41d commit 61e2f33
Show file tree
Hide file tree
Showing 32 changed files with 2,249 additions and 1,887 deletions.
10 changes: 3 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,14 @@ const uin = 123456789; // your account
const bot = createClient(uin);

//监听并输入滑动验证码ticket
bot.on("system.login.slider", (data) => {
bot.on("system.login.slider", () => {
process.stdin.once("data", (input) => {
bot.sliderLogin(input);
});
});

bot.on("message", (data) => {
if (data.group_id > 0)
bot.sendGroupMsg(data.group_id, "hello");
else
bot.sendPrivateMsg(data.user_id, "hello");
});
//回复消息
bot.on("message", (data) => data.reply("hello world"));

bot.login("password"); // your password or password_md5
```
Expand Down
222 changes: 154 additions & 68 deletions client.d.ts

Large diffs are not rendered by default.

60 changes: 42 additions & 18 deletions client.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* 网络层(断线重连、心跳)
* api入口
*/
"use strict";
const version = require("./package.json");
version.app_name = version.name;
Expand All @@ -20,6 +24,7 @@ const wt = require("./lib/wtlogin/wt");
const chat = require("./lib/message/chat");
const troop = require("./lib/troop");
const { getErrorMessage, TimeoutError } = require("./exception");
const { segment, cqcode } = require("./util");
const BUF0 = Buffer.alloc(0);

function buildApiRet(retcode, data = null, error = null) {
Expand All @@ -35,8 +40,6 @@ class Client extends net.Socket {
static OFFLINE = Symbol("OFFLINE");
static INIT = Symbol("INIT");
static ONLINE = Symbol("ONLINE");
}
class AndroidClient extends Client {
logining = false;
status = Client.OFFLINE;
nickname = "";
Expand Down Expand Up @@ -98,6 +101,7 @@ class AndroidClient extends Client {
platform: 1,
log_level: "info",
kickoff: false,
brief: false,
ignore_self: true,
resend: true,
reconn_interval: 5,
Expand Down Expand Up @@ -183,7 +187,7 @@ class AndroidClient extends Client {
if (this.status !== Client.OFFLINE)
this.terminate();
if (this.config.reconn_interval >= 1) {
this.logger.warn(this.config.reconn_interval + "秒后重新连接。");
this.logger.mark(this.config.reconn_interval + "秒后重新连接。");
setTimeout(this.login.bind(this), this.config.reconn_interval * 1000);
}
this.em("system.offline.network", { message });
Expand Down Expand Up @@ -339,22 +343,29 @@ class AndroidClient extends Client {
}
}

em(name = "", data = {}) {
parseEventType(name = "") {
const slice = name.split(".");
const post_type = slice[0], sub_type = slice[2];
const param = {
const data = {
self_id: this.uin,
time: timestamp(),
post_type: post_type
post_type: post_type,
};
const type_name = slice[0] + "_type";
param[type_name] = slice[1];
data[type_name] = slice[1];
if (sub_type)
param.sub_type = sub_type;
Object.assign(param, data);
while (slice.length > 0) {
this.emit(slice.join("."), param);
slice.pop();
data.sub_type = sub_type;
return data;
}

em(name = "", data = {}) {
data = Object.assign(this.parseEventType(name), data);
while (true) {
this.emit(name, data);
let i = name.lastIndexOf(".");
if (i === -1)
break;
name = name.slice(0, i);
}
}

Expand Down Expand Up @@ -522,11 +533,20 @@ class AndroidClient extends Client {
async sendDiscussMsg(discuss_id, message = "", auto_escape = false) {
return await this.useProtocol(chat.sendMsg, [discuss_id, message, auto_escape, 2]);
}
async sendTempMsg(group_id, user_id, message = "", auto_escape = false) {
return await this.useProtocol(chat.sendTempMsg, arguments);
}
async deleteMsg(message_id) {
return await this.useProtocol(chat.recallMsg, arguments);
}
async getMsg(message_id) {
return await this.useProtocol(chat.getHistoryMsg, arguments);
return await this.useProtocol(chat.getOneMsg, arguments);
}
async getChatHistory(message_id, count = 10) {
return await this.useProtocol(chat.getMsgs, arguments);
}
async getForwardMsg(id) {
return await this.useProtocol(chat.getForwardMsg, arguments);
}

///////////////////////////////////////////////////
Expand Down Expand Up @@ -579,6 +599,9 @@ class AndroidClient extends Client {
async setGroupAddRequest(flag, approve = true, reason = "", block = false) {
return await this.useProtocol(sysmsg.groupAction, arguments);
}
async getSystemMsg() {
return await this.useProtocol(sysmsg.getSysMsg, arguments);
}

async addGroup(group_id, comment = "") {
return await this.useProtocol(troop.addGroup, arguments);
Expand Down Expand Up @@ -739,17 +762,18 @@ function setGlobalConfig() { }
//----------------------------------------------------------------------------------------------------

/**
* @param {Number} uin
* @param {JSON} config
* @returns {AndroidClient}
* @param {number} uin
* @param {import("./client").ConfBot} config
* @returns {Client}
*/
function createClient(uin, config = {}) {
uin = parseInt(uin);
if (!checkUin(uin))
throw new Error("Argument uin is not an OICQ account.");
return new AndroidClient(uin, config);
return new Client(uin, config);
}

module.exports = {
createClient, setGlobalConfig
createClient, setGlobalConfig, Client,
segment, cqcode,
};
26 changes: 17 additions & 9 deletions device.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* 设备文件和协议
*/
"use strict";
const fs = require("fs");
const path = require("path");
Expand All @@ -11,7 +14,7 @@ function rand(n = 9) {
return parseInt(Math.random() * (max - min) + min);
}

function getMac() {
function _getMac() {
const o = os.networkInterfaces();
for (let k in o) {
for (let v of o[k]) {
Expand All @@ -22,7 +25,7 @@ function getMac() {
return `00:50:A${rand(1)}:${rand(1)}D:${rand(1)}B:C${rand(1)}`;
}

function genIMEI() {
function _genIMEI() {
let imei = Math.random() > 0.5 ? "86" : "35";
imei += rand(4) + "0" + rand(7);
function calcSP(imei) {
Expand All @@ -40,7 +43,10 @@ function genIMEI() {
return imei + calcSP(imei);
}

function genDevice(filepath) {
/**
* @param {string} filepath
*/
function _genDevice(filepath) {
const device = `{
"--begin--": "修改后可能需要重新验证设备。",
"product": "iarim",
Expand All @@ -54,9 +60,9 @@ function genDevice(filepath) {
"android_id": "BRAND.${rand(6)}.${rand(3)}",
"boot_id": "${uuid()}",
"proc_version": "Linux version 4.19.71-${rand(5)} (oicq@takayama.github.com)",
"mac_address": "${getMac()}",
"mac_address": "${_getMac()}",
"ip_address": "10.0.${rand(2)}.${rand(2)}",
"imei": "${genIMEI()}",
"imei": "${_genIMEI()}",
"incremental": "${rand(7)}"
}`;
const dir = path.dirname(filepath);
Expand All @@ -67,15 +73,15 @@ function genDevice(filepath) {
}

/**
* @param {String} filepath
* @param {string} filepath
* @returns {import("./lib/ref").Device}
*/
function getDeviceInfo(filepath) {
var d;
try {
d = JSON.parse(fs.readFileSync(filepath, { encoding: "utf-8" }));
} catch {
d = genDevice(filepath);
d = _genDevice(filepath);
}
const device = {
display: d.android_id,
Expand Down Expand Up @@ -111,6 +117,9 @@ function getDeviceInfo(filepath) {
return device;
}

/**
* @type {{[k: number]: import("./lib/ref").ApkInfo}}
*/
const apk = {
//android phone
1: {
Expand Down Expand Up @@ -156,8 +165,7 @@ apk[5] = { ...apk[2] };
apk[5].subid = 537065739;

/**
* @param {Number} platform
* @returns {import("./lib/ref").ApkInfo}
* @param {number} platform
*/
function getApkInfo(platform) {
return apk[platform] ? apk[platform] : apk[2];
Expand Down
Loading

0 comments on commit 61e2f33

Please sign in to comment.