@luckylillia-dev/plugin-webqq 1.0.3 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.js CHANGED
@@ -22465,6 +22465,93 @@ function createGroupFilesRoutes(ctx) {
22465
22465
  return router;
22466
22466
  }
22467
22467
 
22468
+ // src/flashFiles.ts
22469
+ import { randomBytes } from "node:crypto";
22470
+ function attachmentName(name2) {
22471
+ const clean = Buffer.from(name2).toString("utf8").replace(/[\u0000-\u001f\u007f/\\]/g, "_").trim() || "download";
22472
+ const ascii = clean.replace(/[^\x20-\x7e]|["%]/g, "_");
22473
+ const encoded = encodeURIComponent(clean).replace(/[!'()*]/g, (ch) => "%" + ch.charCodeAt(0).toString(16).toUpperCase());
22474
+ return `attachment; filename="${ascii}"; filename*=UTF-8''${encoded}`;
22475
+ }
22476
+ function createFlashFilesRoutes(ctx) {
22477
+ const router = new Hono3();
22478
+ const tickets = /* @__PURE__ */ new Map();
22479
+ const validId = (value) => typeof value === "string" && value.length > 0 && value.length <= 1024 && !/[\u0000-\u0020]/.test(value);
22480
+ const files = async (fileSetId) => (await ctx.ntFileApi.getFlashFileList(fileSetId)).flatMap((page) => page.fileList);
22481
+ router.get("/flash-files", async (c) => {
22482
+ const fileSetId = c.req.query("fileSetId");
22483
+ if (!validId(fileSetId)) return c.json({ success: false, message: "\u65E0\u6548\u7684\u95EA\u4F20\u6587\u4EF6\u96C6\u5408 ID" }, 400);
22484
+ try {
22485
+ const list = await files(fileSetId);
22486
+ return c.json({ success: true, data: list.map((file) => ({ id: file.cliFileId, name: file.name, size: file.fileSize })) });
22487
+ } catch (error) {
22488
+ return c.json({ success: false, message: error instanceof Error ? error.message : "\u83B7\u53D6\u95EA\u4F20\u6587\u4EF6\u5931\u8D25" }, 500);
22489
+ }
22490
+ });
22491
+ router.get("/flash-file-url", async (c) => {
22492
+ const fileSetId = c.req.query("fileSetId"), fileUuid = c.req.query("fileUuid");
22493
+ if (!validId(fileSetId) || !validId(fileUuid)) return c.json({ success: false, message: "\u65E0\u6548\u7684\u95EA\u4F20\u6587\u4EF6\u53C2\u6570" }, 400);
22494
+ try {
22495
+ const file = (await files(fileSetId)).find((file2) => file2.cliFileId === fileUuid);
22496
+ if (!file) return c.json({ success: false, message: "\u6587\u4EF6\u4E0D\u5B58\u5728\u6216\u5DF2\u8FC7\u671F" }, 404);
22497
+ const extra = file;
22498
+ const result = await ctx.ntFileApi.getFlashFileDownloadUrl({
22499
+ fileSetId,
22500
+ fileUuid,
22501
+ fileName: file.name,
22502
+ fileSize: Number(file.fileSize),
22503
+ // Match OneBot GetFlashFileDownloadUrls: the server can interpret these optional
22504
+ // digest fields as UTF-8 strings, so raw MD5/SHA-1 bytes cause OIDB error 155.
22505
+ // The file identities and history token are sufficient for this request.
22506
+ fileId: extra.fileId
22507
+ });
22508
+ const url = new URL(result.fullUrl);
22509
+ if (!["http:", "https:"].includes(url.protocol) || url.username || url.password) throw new Error("\u4E0B\u8F7D\u5730\u5740\u65E0\u6548");
22510
+ c.header("Cache-Control", "no-store");
22511
+ for (const [key, ticket2] of tickets) if (ticket2.expires <= Date.now()) tickets.delete(key);
22512
+ if (tickets.size >= 128) tickets.delete(tickets.keys().next().value);
22513
+ const ticket = randomBytes(24).toString("hex");
22514
+ tickets.set(ticket, { url: url.href, name: file.name, expires: Date.now() + 12e4 });
22515
+ return c.json({ success: true, data: { url: url.href, downloadPath: `/api/webqq/flash-file-download?ticket=${ticket}` } });
22516
+ } catch (error) {
22517
+ return c.json({ success: false, message: error instanceof Error ? error.message : "\u83B7\u53D6\u4E0B\u8F7D\u94FE\u63A5\u5931\u8D25" }, 500);
22518
+ }
22519
+ });
22520
+ router.get("/flash-file-download", async (c) => {
22521
+ const key = c.req.query("ticket") || "";
22522
+ const ticket = tickets.get(key);
22523
+ if (!ticket || ticket.expires <= Date.now()) {
22524
+ tickets.delete(key);
22525
+ return c.text("\u4E0B\u8F7D\u5DF2\u8FC7\u671F\uFF0C\u8BF7\u8FD4\u56DE WebQQ \u91CD\u65B0\u70B9\u51FB\u4E0B\u8F7D\u3002", 410);
22526
+ }
22527
+ try {
22528
+ const headers = new Headers({ "Accept-Encoding": "identity" });
22529
+ const range = c.req.header("Range");
22530
+ if (range) headers.set("Range", range);
22531
+ const upstream = await fetch(ticket.url, { headers, signal: c.req.raw.signal });
22532
+ if (!upstream.ok || !upstream.body) {
22533
+ await upstream.body?.cancel();
22534
+ return c.text("\u4E0B\u8F7D\u5931\u8D25\uFF0C\u8BF7\u8FD4\u56DE WebQQ \u91CD\u8BD5\u3002", 502);
22535
+ }
22536
+ const responseHeaders = new Headers({
22537
+ "Content-Type": "application/octet-stream",
22538
+ "Content-Disposition": attachmentName(ticket.name),
22539
+ "Cache-Control": "no-store",
22540
+ "Referrer-Policy": "no-referrer",
22541
+ "X-Content-Type-Options": "nosniff"
22542
+ });
22543
+ for (const name2 of ["Content-Length", "Content-Range", "Accept-Ranges"]) {
22544
+ const value = upstream.headers.get(name2);
22545
+ if (value && !upstream.headers.get("Content-Encoding")) responseHeaders.set(name2, value);
22546
+ }
22547
+ return new Response(upstream.body, { status: upstream.status, headers: responseHeaders });
22548
+ } catch {
22549
+ return c.text("\u4E0B\u8F7D\u5931\u8D25\uFF0C\u8BF7\u8FD4\u56DE WebQQ \u91CD\u8BD5\u3002", 502);
22550
+ }
22551
+ });
22552
+ return router;
22553
+ }
22554
+
22468
22555
  // src/face.ts
22469
22556
  import { readFile as readFile2 } from "node:fs/promises";
22470
22557
  import path9 from "node:path";
@@ -22492,6 +22579,56 @@ function createFaceRoutes(faceDir = path9.resolve(import.meta.dirname, "../asset
22492
22579
  }
22493
22580
 
22494
22581
  // src/events.ts
22582
+ function buildNudgeItems(input) {
22583
+ const action = input.displayAction || "\u6233\u4E86\u6233";
22584
+ const items = [
22585
+ { type: "qq", uid: input.senderUid, uin: String(input.senderUin), nm: input.senderName },
22586
+ { type: "nor", txt: ` ${action} ` }
22587
+ ];
22588
+ if (input.displayActionImgUrl) {
22589
+ items.push({ type: "img", src: input.displayActionImgUrl });
22590
+ }
22591
+ if (input.senderUin && input.receiverUin && input.senderUin === input.receiverUin) {
22592
+ items.push({ type: "nor", txt: "\u81EA\u5DF1" });
22593
+ } else {
22594
+ items.push({ type: "qq", uid: input.receiverUid, uin: String(input.receiverUin), nm: input.receiverName });
22595
+ }
22596
+ if (input.displaySuffix) {
22597
+ items.push({ type: "nor", txt: input.displaySuffix });
22598
+ }
22599
+ return items;
22600
+ }
22601
+ var nudgeSeq = 0;
22602
+ function createNudgeMessage(params) {
22603
+ const nowMs = Date.now();
22604
+ const msgId = `nudge_${nowMs}_${++nudgeSeq}`;
22605
+ return {
22606
+ msgId,
22607
+ msgSeq: "",
22608
+ msgTime: Math.floor(nowMs / 1e3),
22609
+ chatType: params.chatType,
22610
+ peerUin: params.peerUin,
22611
+ peerUid: params.peerUid,
22612
+ peerName: params.peerName,
22613
+ senderUin: params.senderUin,
22614
+ senderUid: params.senderUid,
22615
+ sendNickName: params.sendNickName,
22616
+ sendMemberName: "",
22617
+ elements: [
22618
+ {
22619
+ elementType: ElementType.GrayTip,
22620
+ elementId: `${msgId}_0`,
22621
+ grayTipElement: {
22622
+ subElementType: 17,
22623
+ jsonGrayTipElement: {
22624
+ busiId: "1061",
22625
+ jsonStr: JSON.stringify({ items: params.items })
22626
+ }
22627
+ }
22628
+ }
22629
+ ]
22630
+ };
22631
+ }
22495
22632
  function forwardEvents(ctx, sseClients) {
22496
22633
  const broadcastMessage = (event, data) => {
22497
22634
  const serializedData = serializeResult(data);
@@ -22511,6 +22648,47 @@ data: ${JSON.stringify(serializedData)}
22511
22648
  }
22512
22649
  }
22513
22650
  };
22651
+ const resolveGroupMemberName = async (groupCode, uin, uid) => {
22652
+ try {
22653
+ const card = await ctx.store.getGroupMemberCardName(groupCode, uin);
22654
+ if (card) return card;
22655
+ } catch {
22656
+ }
22657
+ if (uid) {
22658
+ try {
22659
+ const user = await ctx.ntUserApi.getUserByUid(uid);
22660
+ if (user?.nick) return user.nick;
22661
+ } catch {
22662
+ }
22663
+ }
22664
+ return String(uin);
22665
+ };
22666
+ const resolveGroupName = async (groupCode) => {
22667
+ try {
22668
+ const groups = await ctx.ntGroupApi.getGroups(false);
22669
+ const hit = groups.find((g) => Number(g.groupCode) === Number(groupCode));
22670
+ if (hit?.groupName) return hit.groupName;
22671
+ } catch {
22672
+ }
22673
+ return "";
22674
+ };
22675
+ const resolveFriendName = async (uin, uid) => {
22676
+ try {
22677
+ const result = await ctx.ntFriendApi.getFriends(false);
22678
+ const list = Array.isArray(result) ? result : result?.friends ?? [];
22679
+ const hit = list.find((f) => Number(f.uin) === Number(uin));
22680
+ if (hit) return hit.remark || hit.nick || String(uin);
22681
+ } catch {
22682
+ }
22683
+ if (uid) {
22684
+ try {
22685
+ const user = await ctx.ntUserApi.getUserByUid(uid);
22686
+ if (user?.nick) return user.nick;
22687
+ } catch {
22688
+ }
22689
+ }
22690
+ return String(uin);
22691
+ };
22514
22692
  ctx.on("nt/message-created", async (data) => {
22515
22693
  if (sseClients.size === 0) return;
22516
22694
  await fillPeerUin(data.message);
@@ -22559,6 +22737,75 @@ data: ${JSON.stringify(serializedData)}
22559
22737
  }
22560
22738
  });
22561
22739
  });
22740
+ ctx.on("nt/group-nudge", async (data) => {
22741
+ if (sseClients.size === 0) return;
22742
+ const [senderName, receiverName, groupName] = await Promise.all([
22743
+ resolveGroupMemberName(data.groupCode, data.senderUin, data.senderUid),
22744
+ data.receiverUin === data.senderUin ? Promise.resolve("") : resolveGroupMemberName(data.groupCode, data.receiverUin, data.receiverUid),
22745
+ resolveGroupName(data.groupCode)
22746
+ ]);
22747
+ const items = buildNudgeItems({
22748
+ senderUid: data.senderUid || "",
22749
+ senderUin: data.senderUin,
22750
+ senderName,
22751
+ receiverUid: data.receiverUid || "",
22752
+ receiverUin: data.receiverUin,
22753
+ receiverName,
22754
+ displayAction: data.displayAction,
22755
+ displaySuffix: data.displaySuffix,
22756
+ displayActionImgUrl: data.displayActionImgUrl
22757
+ });
22758
+ broadcastMessage("message", {
22759
+ type: "message-created",
22760
+ data: createNudgeMessage({
22761
+ chatType: ChatType.Group,
22762
+ peerUin: data.groupCode,
22763
+ peerUid: String(data.groupCode),
22764
+ peerName: groupName,
22765
+ senderUin: data.senderUin,
22766
+ senderUid: data.senderUid || "",
22767
+ sendNickName: senderName,
22768
+ items
22769
+ })
22770
+ });
22771
+ });
22772
+ ctx.on("nt/friend-nudge", async (data) => {
22773
+ if (sseClients.size === 0) return;
22774
+ const selfInfo = ctx.qqProtocol.selfInfo;
22775
+ const selfUin = Number(selfInfo.uin) || 0;
22776
+ const selfUid = selfInfo.uid || "";
22777
+ const friendName = await resolveFriendName(data.uin, data.uid);
22778
+ const senderUin = data.isSelfSend ? selfUin : data.uin;
22779
+ const senderUid = data.isSelfSend ? selfUid : data.uid || "";
22780
+ const senderName = data.isSelfSend ? selfInfo.nick || "\u4F60" : friendName;
22781
+ const receiverUin = data.isSelfReceive ? selfUin : data.uin;
22782
+ const receiverUid = data.isSelfReceive ? selfUid : data.uid || "";
22783
+ const receiverName = data.isSelfReceive ? selfInfo.nick || "\u4F60" : friendName;
22784
+ const items = buildNudgeItems({
22785
+ senderUid,
22786
+ senderUin,
22787
+ senderName,
22788
+ receiverUid,
22789
+ receiverUin,
22790
+ receiverName,
22791
+ displayAction: data.displayAction,
22792
+ displaySuffix: data.displaySuffix,
22793
+ displayActionImgUrl: data.displayActionImgUrl
22794
+ });
22795
+ broadcastMessage("message", {
22796
+ type: "message-created",
22797
+ data: createNudgeMessage({
22798
+ chatType: ChatType.C2C,
22799
+ peerUin: data.uin,
22800
+ peerUid: data.uid || "",
22801
+ peerName: friendName,
22802
+ senderUin,
22803
+ senderUid,
22804
+ sendNickName: friendName,
22805
+ items
22806
+ })
22807
+ });
22808
+ });
22562
22809
  }
22563
22810
 
22564
22811
  // node_modules/hono/dist/utils/stream.js
@@ -22733,6 +22980,7 @@ function createWebQQRoutes(ctx, options) {
22733
22980
  router.route("/", createChatsRoutes(ctx));
22734
22981
  router.route("/", createActionsRoutes(ctx));
22735
22982
  router.route("/", createGroupFilesRoutes(ctx));
22983
+ router.route("/", createFlashFilesRoutes(ctx));
22736
22984
  router.get("/self", (c) => c.json({
22737
22985
  success: true,
22738
22986
  data: { online: ctx.qqProtocol.selfInfo.online, uid: ctx.qqProtocol.selfInfo.uid, uin: ctx.qqProtocol.selfInfo.uin, nick: ctx.qqProtocol.selfInfo.nick }