-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
main.js
523 lines (472 loc) · 18.2 KB
/
main.js
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
const fs = require("fs");
const path = require("path");
const { app, ipcMain, dialog } = require("electron");
const { ImgDownloader } = require("./imgDownloader.js");
var configFilePath = "";
var pluginDataDir = path.join(LiteLoader.path.data, "anti_recall");
const imgDownloader = new ImgDownloader();
const Level = require("level-party");
var db = null;
var sampleConfig = {
mainColor: "#ff6d6d",
saveDb: false,
enableShadow: true,
enableTip: true,
isAntiRecallSelfMsg: false,
maxMsgSaveLimit: 10000,
deleteMsgCountPerTime: 500,
};
var nowConfig = {};
function initConfig() {
fs.writeFileSync(
configFilePath,
JSON.stringify(sampleConfig, null, 2),
"utf-8"
);
}
function loadConfig() {
if (!fs.existsSync(configFilePath)) {
initConfig();
return sampleConfig;
} else {
return JSON.parse(fs.readFileSync(configFilePath, "utf-8"));
}
}
onLoad();
async function onLoad() {
if (!fs.existsSync(pluginDataDir)) {
fs.mkdirSync(pluginDataDir, { recursive: true });
}
configFilePath = path.join(pluginDataDir, "config.json");
nowConfig = loadConfig();
if (nowConfig.mainColor == null) {
nowConfig.mainColor = "#ff6d6d";
}
if (nowConfig.enableShadow == null) {
nowConfig.enableShadow = true;
}
if (nowConfig.enableTip == null) {
nowConfig.enableTip = true;
}
fs.writeFileSync(configFilePath, JSON.stringify(nowConfig, null, 2), "utf-8");
ipcMain.handle(
"LiteLoader.anti_recall.getNowConfig",
async (event, message) => {
return nowConfig;
}
);
ipcMain.handle("LiteLoader.anti_recall.saveConfig", async (event, config) => {
nowConfig = config;
sendChatWindowsMessage("LiteLoader.anti_recall.mainWindow.repatchCss");
fs.writeFileSync(configFilePath, JSON.stringify(config, null, 2), "utf-8");
});
if (nowConfig.saveDb) {
db = Level(path.join(pluginDataDir, "qq-recalled-db"), {
valueEncoding: "json",
});
db.open((e) => {
if (e !== undefined && e !== null) {
// app.whenReady().then(() => {
// dialog
// .showMessageBox({
// type: "warning",
// title: "警告",
// message:
// "打开反撤回数据库失败,可能是上次QQ进程未完全退出。建议关闭QQ并彻底结束QQ进程,再重启QQ,否则反撤回消息无法正常保存(即使反撤回仍生效,只是重启QQ后会丢失)。",
// buttons: ["继续打开QQ", "关闭QQ"],
// })
// .then((r) => {
// if (r.response == 1) {
// app.exit();
// }
// });
// });
output(
"打开数据库失败,可能是QQ进程未完全退出。请查看下面详细错误信息中的cause部分:",
e
);
}
});
}
ipcMain.handle("LiteLoader.anti_recall.clearDb", async (event, message) => {
dialog
.showMessageBox({
type: "warning",
title: "警告",
message: "清空所有已储存的撤回消息后不可恢复,是否确认清空?",
buttons: ["确定", "取消"],
cancelId: 1,
})
.then(async (idx) => {
//第一个按钮
if (idx.response == 0) {
await db.clear();
dialog.showMessageBox({
type: "info",
title: "提示",
message:
"清空完毕,之前保存的所有已撤回消息均被删除,重启QQ后就能看见效果。",
buttons: ["确定"],
});
}
});
});
app.on("quit", async () => {
output("Closing db...");
await db.close();
});
}
var msgFlow = [];
var recalledMsg = [];
function insertDb(msg) {
if (db != null) {
db.get(msg.id, (error, value) => {
if (error.status == 404) {
db.put(msg.id, msg, (err) => {
if (err) throw err;
});
} else {
if (error) throw error;
}
});
}
}
async function getMsgById(id) {
if (db != null) {
try {
return await db.get(id);
} catch (e) {
if (e.status != 404) {
output(e);
}
return null;
}
} else {
output("Warning: Db is null, no previous msg available.");
}
return null;
}
function sendChatWindowsMessage(message) {
for (var window of mainWindowObjs) {
if (window.isDestroyed()) continue;
window.webContents.send(message);
}
}
var mainWindowObjs = [];
function onBrowserWindowCreated(window) {
window.webContents.on("did-stop-loading", () => {
//只针对主界面和独立聊天界面生效
if (
window.webContents.getURL().indexOf("#/main/message") != -1 ||
window.webContents.getURL().indexOf("#/chat") != -1
) {
mainWindowObjs.push(window);
// const proxyEvents = new Proxy(
// window.webContents._events["-ipc-message"],
// {
// // 拦截函数调用
// apply(target, thisArg, argumentsList) {
// if (
// argumentsList[3][0]["eventName"] &&
// !argumentsList[3][0]["eventName"].includes(
// "ns-Logger"
// )
// ) {
// output(JSON.stringify(argumentsList));
// }
// return target.apply(thisArg, argumentsList);
// }
// }
// );
// window.webContents._events["-ipc-message"] = proxyEvents;
//补充知识:
//撤回的原理是,你先发了一条消息,这条消息有一个msgId,然后又撤回了他,那腾讯就会发一条一样msgId的撤回消息包来替换,这样你以后拉取的话,这个msgId只会对应一条撤回提示了;
//本插件的原理是,先在内存中临时储存所有消息(1000条上限),然后如果有撤回发生,则将撤回的提示替换为之前保存的消息。
const original_send =
(window.webContents.__qqntim_original_object &&
window.webContents.__qqntim_original_object.send) ||
window.webContents.send;
//var myUid = "";
const patched_send = async function (channel, ...args) {
// output(channel, JSON.stringify(args));
// if (db != null) {
// db.put("a", { x: 123 }, function (err) {
// if (err) throw err;
// db.get("a", function (err, value) {
// console.log(value); // { x: 123 }
// });
// });
// }
try {
if (args.length >= 2) {
//MessageList IPC 中能看到消息全量更新内容,其中包含撤回的提示,但并不包含被撤回的消息(被撤回的已经被替换掉了),需要替换撤回提示为之前保存的消息内容
if (
args.some(
(item) =>
item &&
item.hasOwnProperty("msgList") &&
item.msgList != null &&
item.msgList instanceof Array &&
item.msgList.length > 0
)
) {
var currentMsgPeer = "";
//撤回提示所在的msgList下标数组,在后面需要一个个替换为真实的消息
var needUpdateIdx = [];
for (let idx in args[1].msgList) {
var item = args[1].msgList[idx];
currentMsgPeer = item.peerUid;
if (item.msgType == 5 && item.subMsgType == 4) {
if (
item.elements[0].grayTipElement != null &&
item.elements[0].grayTipElement.revokeElement != null &&
(nowConfig.isAntiRecallSelfMsg ||
!item.elements[0].grayTipElement.revokeElement
.isSelfOperate)
) {
needUpdateIdx.push(idx);
}
}
// console.log(
// item.recallTime,
// "<====>",
// item.elements,
// "<====>",
// item.elements[0].grayTipElement.revokeElement
// );
}
needUpdateIdx.sort((a, b) => b - a);
for (var i of needUpdateIdx) {
let recallTipMsg = args[1].msgList[i];
var currMsgId = recallTipMsg.msgId;
//如果之前存了消息
let olderMsg = msgFlow.find((i) => i.id == currMsgId);
let olderMsgFromRecalledMsg = recalledMsg.find(
(i) => i.id == currMsgId
);
var dbMsg = await getMsgById(currMsgId);
let fromName;
let originalMsg;
//优先从已保存的撤回的消息中获取
if (olderMsgFromRecalledMsg != null) {
// original_send.call(
// window.webContents,
// channel,
// {
// type: "request",
// eventName: "ns-ntApi-2"
// },
// [
// {
// cmdName:
// "nodeIKernelMsgListener/onRecvMsg",
// cmdType: "event",
// payload: {
// msgList: [
// olderMsgFromRecalledMsg.msg
// ]
// }
// }
// ]
// );
originalMsg =olderMsgFromRecalledMsg;
fromName = "old msg";
}
//如果没有存过,则说明他在消息流里
else if (olderMsg != null) {
//没专门存过这条消息到专门的反撤回数组中,就存一下
if (olderMsgFromRecalledMsg == null) {
recalledMsg.push(olderMsg);
}
originalMsg = olderMsg;
fromName = "msgFlow";
} else if (dbMsg != null) {
//没专门存过这条消息到专门的反撤回数组中,就存一下
if (olderMsgFromRecalledMsg == null) {
recalledMsg.push(dbMsg);
}
originalMsg = dbMsg;
fromName = "dbMsg";
}
if (originalMsg !== undefined) {
if (originalMsg.msg instanceof Object) {
let msg = Object.assign({}, originalMsg.msg);//克隆
msg.isOnlineMsg = true;
await imgDownloader.downloadPic(msg);
output(
"Detected recall, intercepted and recovered from " + fromName
);
//解决 某些情况(可能是新版本在群聊中的情况) 不显示消息
//方法:撤回提示消息中一些对象存在一些特殊属性,保留这些特殊属性
//因为原来使用JSON.stringify解析对象,无法解析特殊属性
for (let key in msg) {
if (
[
'msgSeq',//保持序列
'cntSeq',//保持序列
'clientSeq',//保持序列
'sendStatus',//防止消息一直在加载
'emojiLikesList',//表情回应,撤回消息提示中会包含(原本的真实消息貌似不会包含)
].includes(key)
) {
continue;
}
let newValue = msg[key];
let oldValue = recallTipMsg[key];
let value = newValue;
if (
["msgAttrs", "msgMeta", "generalFlags"].includes(key) &&
newValue instanceof Object &&
oldValue instanceof Object
) {
for (let key in oldValue) {//删除所有OwnProperty
if (oldValue.hasOwnProperty(key)) {
delete oldValue[key];
}
}
value = Object.assign(oldValue, newValue);//合并
}
recallTipMsg[key] = value;
}
}
}
}
original_send.call(
window.webContents,
"LiteLoader.anti_recall.mainWindow.recallTipList",
recalledMsg
.filter(
(i) => i.sender == currentMsgPeer || i?.sender == null
)
.map((i) => i.id)
);
}
//增量更新 IPC
if (
args.some(
(item) =>
item instanceof Array &&
item.length > 0 &&
item[0] &&
item[0].cmdName != null
)
) {
var args1 = args[1][0];
if (args1 == null) return;
if (args1.cmdName.indexOf("onProfileDetailInfoChanged") != -1) {
myUid = args1.payload.info.uid;
}
//方法一:获取个人信息的IPC,用来获取个人UID,避免防撤回自己的消息
// if (args1.cmdName.indexOf("onProfileDetailInfoChanged") != -1) {
// myUid = args1.payload.info.uid;
// } else
//目前采用方法二,直接获取撤回消息中的参数
//拦截撤回IPC
if (
args1.cmdName != null &&
(args1.cmdName.indexOf("onMsgInfoListUpdate") != -1 ||
args1.cmdName.indexOf("onActiveMsgInfoUpdate") != -1) &&
args1.payload != null &&
args1.payload.msgList instanceof Array &&
args1.payload.msgList[0].msgType == 5 &&
args1.payload.msgList[0].subMsgType == 4
) {
var msgList = args1.payload.msgList[0];
//不是自己撤回的,才拦截
if (
msgList.elements[0].grayTipElement != null &&
msgList.elements[0].grayTipElement.revokeElement != null &&
(nowConfig.isAntiRecallSelfMsg ||
!msgList.elements[0].grayTipElement.revokeElement
.isSelfOperate)
) {
original_send.call(
window.webContents,
"LiteLoader.anti_recall.mainWindow.recallTip",
msgList.msgId
);
//如果之前存了消息
var olderMsg = msgFlow.find((i) => i.id == msgList.msgId);
var olderMsgFromRecalledMsg = recalledMsg.find(
(i) => i.id == msgList.msgId
);
//之前存了消息,但是还没有存入专门的反撤回数组中
if (olderMsg != null && olderMsgFromRecalledMsg == null) {
recalledMsg.push(olderMsg);
if (nowConfig.saveDb) {
insertDb(olderMsg);
}
}
await imgDownloader.downloadPic(olderMsg?.msg);
await imgDownloader.downloadPic(olderMsgFromRecalledMsg?.msg);
args[1][0].cmdName = "none";
args[1][0].payload.msgList.pop();
// console.log(args1.payload);
output("Detected recall, intercepted");
}
}
//接到消息
else if (
(args1.cmdName != null &&
args1.payload != null &&
(args1.cmdName.indexOf("onRecvMsg") != -1 ||
args1.cmdName.indexOf("onRecvActiveMsg") != -1) &&
args1.payload.msgList instanceof Array) ||
(args1.cmdName.indexOf("onAddSendMsg") != -1 &&
args1.payload.msgRecord != null) ||
(args1.cmdName.indexOf("onMsgInfoListUpdate") != -1 &&
args1.payload.msgList instanceof Array)
) {
var msgList =
args1.payload.msgList instanceof Array
? args1.payload.msgList
: [args1.payload.msgRecord];
for (msg of msgList) {
var msgId = msg.msgId;
var olderMsgIdx = msgFlow.findIndex((i) => i.id == msgId);
if (olderMsgIdx == -1) {
msgFlow.push({});
olderMsgIdx = msgFlow.length - 1;
}
msgFlow[olderMsgIdx] = {
id: msgId,
sender: msg.peerUid,
msg: msg,
};
if (nowConfig.maxMsgSaveLimit == null) {
nowConfig.maxMsgSaveLimit = 10000;
}
if (nowConfig.deleteMsgCountPerTime == null) {
nowConfig.deleteMsgCountPerTime = 500;
}
if (msgFlow.length > nowConfig.maxMsgSaveLimit) {
msgFlow.splice(0, nowConfig.deleteMsgCountPerTime);
}
}
}
}
}
} catch (e) {
output(
"NTQQ Anti-Recall Error: ",
e,
"Please report this to https://github.com/xh321/LiteLoaderQQNT-Anti-Recall/issues, thank you"
);
}
return original_send.call(window.webContents, channel, ...args);
};
if (window.webContents.__qqntim_original_object)
window.webContents.__qqntim_original_object.send = patched_send;
else window.webContents.send = patched_send;
output(
"NTQQ Anti-Recall loaded for window: " + window.webContents.getURL()
);
}
});
}
function output(...args) {
console.log("\x1b[32m%s\x1b[0m", "Anti-Recall:", ...args);
}
module.exports = {
onBrowserWindowCreated,
};