This repository was archived by the owner on Jul 11, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
154 lines (138 loc) · 4.36 KB
/
Copy pathindex.js
File metadata and controls
154 lines (138 loc) · 4.36 KB
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
import { C } from "@deltachat/jsonrpc-client";
import { startDeltaChat } from "@deltachat/stdio-rpc-server";
import * as pty from "node-pty";
import { PassThrough } from "node:stream";
// Map from msgId to PTY process.
const ptys = {};
function spawnPty(dc, accountId, msgId) {
console.log("Spawning new PTY");
const ptyProcess = pty.spawn("bash", [], {
name: "xterm-256color",
cols: 80,
rows: 30,
});
const pass = new PassThrough();
// Single async loop to ensure there is only one `sendWebxdcRealtimeData` call at a time.
(async () => {
for await (const chunk of pass) {
await dc.rpc.sendWebxdcRealtimeData(
accountId,
msgId,
[0x4f].concat(Array.from(chunk)),
);
}
// 'E' = exit
await dc.rpc.sendWebxdcRealtimeData(accountId, msgId, [0x45]);
pass.destroy();
})();
ptyProcess.onData((data) => {
const encoder = new TextEncoder();
const encoded = encoder.encode(data);
pass.write(encoded);
});
ptyProcess.onExit(() => {
pass.end();
});
ptys[msgId] = ptyProcess;
}
async function sendWebxdc(dc, accountId, chatId) {
const webxdcMsgId = await dc.rpc.sendMsg(accountId, chatId, {
text: "hi",
file: "frontend/dist-release/xdcterm.xdc",
});
await dc.rpc.sendWebxdcRealtimeAdvertisement(accountId, webxdcMsgId);
spawnPty(dc, accountId, webxdcMsgId);
}
async function main() {
const dc = await startDeltaChat("deltachat-data", {});
dc.on(
"Info",
(accountId, { msg }) => console.info(accountId, "[core:info]", msg),
);
dc.on(
"Warning",
(accountId, { msg }) => console.warn(accountId, "[core:warn]", msg),
);
dc.on(
"Error",
(accountId, { msg }) => console.error(accountId, "[core:error]", msg),
);
const accountIds = await dc.rpc.getAllAccountIds();
let accountId;
if (accountIds.length === 0) {
console.log("Add new account");
accountId = await dc.rpc.addAccount();
} else {
console.log("Using existing accounts");
accountId = accountIds[0];
}
if (!(await dc.rpc.isConfigured(accountId))) {
const response = await fetch("https://nine.testrun.org/new", {
method: "POST",
});
const body = await response.json();
await dc.rpc.batchSetConfig(accountId, {
addr: body.email,
mail_pw: body.password,
bot: "1",
webxdc_realtime_enabled: "1",
});
await dc.rpc.configure(accountId);
}
let xdctermChatId = await dc.rpc.getConfig(accountId, "ui.xdcterm_chat_id");
if (xdctermChatId === null) {
// Create new protected chat.
const protect = true;
xdctermChatId = await dc.rpc.createGroupChat(accountId, "XDCTerm", protect);
await dc.rpc.setConfig(
accountId,
"ui.xdcterm_chat_id",
String(xdctermChatId),
);
}
xdctermChatId = Number(xdctermChatId);
// Make sure the chat is promoted.
const xdctermChatInfo = await dc.rpc.getBasicChatInfo(
accountId,
xdctermChatId,
);
if (xdctermChatInfo.isUnpromoted) {
await dc.rpc.miscSendTextMessage(accountId, xdctermChatId, "Hello");
}
const qrCode =
(await dc.rpc.getChatSecurejoinQrCodeSvg(accountId, xdctermChatId))[0];
console.log(`Chat invitation: ${qrCode}`);
const emitter = dc.getContextEvents(accountId);
emitter.on("IncomingMsg", async ({ chatId, msgId }) => {
if (chatId === xdctermChatId) {
await sendWebxdc(dc, accountId, chatId);
}
});
emitter.on("WebxdcRealtimeData", ({ msgId, data }) => {
const binData = Uint8Array.from(data);
if (binData[0] == 0x49) {
// 0x49 = 'I' = INPUT
const ptyProcess = ptys[msgId];
ptyProcess.write(binData.slice(1));
} else {
console.log("Ignoring incoming data that is not input.");
}
});
emitter.on("WebxdcRealtimeAdvertisementReceived", async ({ msgId }) => {
if (!(msgId in ptys)) {
console.log(`Someone want to join XDC ${msgId}`);
const message = await dc.rpc.getMessage(accountId, msgId);
if (message.fromId == C.DC_CONTACT_ID_SELF) {
// TODO check if the chat is protected.
// We should also somehow ensure that advertisement
// is signed by verified contact in the core,
// not sure if it is guaranteed.
await dc.rpc.sendWebxdcRealtimeAdvertisement(accountId, msgId);
spawnPty(dc, accountId, msgId);
console.log(`Spawning PTY for ${msgId}`);
}
}
});
await dc.rpc.startIo(accountId);
}
main();