Compare commits
2 Commits
bfe7cb2790
...
encryption
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9957f2a7a9 | ||
|
|
cbc23c0325 |
65
index.js
65
index.js
@@ -14,6 +14,34 @@ const client = new MatrixClient({
|
|||||||
deviceId,
|
deviceId,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// The Rust crypto backend uses an in-memory store here (no IndexedDB in Node),
|
||||||
|
// so after a restart it 'forgets' one-time keys it already uploaded. Re-uploading
|
||||||
|
// an existing OTK id makes Synapse reject the request with a 400, the SDK never
|
||||||
|
// marks it as sent, and it retries forever — which blocks decryption entirely.
|
||||||
|
// Rewrite that specific 400 into a synthetic success: the empty count makes the
|
||||||
|
// Rust machine mint fresh OTK ids on the next tick, which then upload cleanly.
|
||||||
|
function installOtkCollisionWorkaround(client) {
|
||||||
|
const http = client.http;
|
||||||
|
const authedRequest = http.authedRequest.bind(http);
|
||||||
|
http.authedRequest = async function (method, path, queryParams, body, opts) {
|
||||||
|
try {
|
||||||
|
return await authedRequest(method, path, queryParams, body, opts);
|
||||||
|
} catch (err) {
|
||||||
|
if (
|
||||||
|
method === 'POST' &&
|
||||||
|
/\/keys\/upload$/.test(path) &&
|
||||||
|
err?.httpStatus === 400 &&
|
||||||
|
/already exists/.test(err?.message || '')
|
||||||
|
) {
|
||||||
|
console.warn('⚙️ One-time-key collision on keys/upload; telling Rust crypto to mint fresh keys');
|
||||||
|
return '{"one_time_key_counts":{}}';
|
||||||
|
}
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
installOtkCollisionWorkaround(client);
|
||||||
|
|
||||||
function loadQuotes() {
|
function loadQuotes() {
|
||||||
try {
|
try {
|
||||||
const quotesFile = './quotes.json';
|
const quotesFile = './quotes.json';
|
||||||
@@ -77,6 +105,7 @@ function loadQuotes() {
|
|||||||
console.log('');
|
console.log('');
|
||||||
|
|
||||||
const cmd = (process.env.BOT_COMMAND || '!quote').toLowerCase();
|
const cmd = (process.env.BOT_COMMAND || '!quote').toLowerCase();
|
||||||
|
console.log(`🔊 Listening for command: "${cmd}"`);
|
||||||
|
|
||||||
// Auto-accept invites so the bot can be invited into rooms.
|
// Auto-accept invites so the bot can be invited into rooms.
|
||||||
client.on('RoomMember.membership', (event, member) => {
|
client.on('RoomMember.membership', (event, member) => {
|
||||||
@@ -92,11 +121,12 @@ function loadQuotes() {
|
|||||||
// Respond to messages in any room this bot is a member of.
|
// Respond to messages in any room this bot is a member of.
|
||||||
client.on('event', (event) => {
|
client.on('event', (event) => {
|
||||||
const type = event.getType();
|
const type = event.getType();
|
||||||
|
const eventRoomId = event.getRoomId();
|
||||||
|
|
||||||
// If an event still arrives encrypted, decryption failed — the
|
// If an event still arrives encrypted, decryption failed — the
|
||||||
// room is likely set to only share keys with verified sessions.
|
// room is likely set to only share keys with verified sessions.
|
||||||
if (type === 'm.room.encrypted') {
|
if (type === 'm.room.encrypted') {
|
||||||
console.log(`🔒 [${event.getRoomId()}] could not decrypt message. ` +
|
console.log(`🔒 [${eventRoomId}] could not decrypt message. ` +
|
||||||
`The room may be set to share keys only with verified sessions; ` +
|
`The room may be set to share keys only with verified sessions; ` +
|
||||||
`change room encryption settings to include this bot's device, or verify it.`);
|
`change room encryption settings to include this bot's device, or verify it.`);
|
||||||
return;
|
return;
|
||||||
@@ -105,24 +135,35 @@ function loadQuotes() {
|
|||||||
if (type !== 'm.room.message') return;
|
if (type !== 'm.room.message') return;
|
||||||
|
|
||||||
const sender = event.getSender();
|
const sender = event.getSender();
|
||||||
if (sender === userId) return;
|
if (sender === userId) return; // skip replies from ourselves
|
||||||
|
|
||||||
const room = client.getRoom(event.getRoomId());
|
const room = eventRoomId ? client.getRoom(eventRoomId) : null;
|
||||||
if (!room) return;
|
if (!room) {
|
||||||
|
console.log(`ℹ️ [debug] got m.room.message for unknown room: ${eventRoomId}`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const membership = room.getMyMembership();
|
const membership = room.getMyMembership();
|
||||||
if (membership !== 'join') return;
|
if (membership !== 'join') {
|
||||||
|
console.log(`ℹ️ [debug] [${room.roomId}] m.room.message while membership="${membership}" — not replying`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const content = event.getContent();
|
const content = event.getContent();
|
||||||
if (!content || typeof content.body !== 'string') return;
|
if (!content || typeof content.body !== 'string') {
|
||||||
|
console.log(`ℹ️ [debug] [${room.roomId}] m.room.message without text body`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const lower = content.body.toLowerCase().trim();
|
const lower = content.body.toLowerCase().trim();
|
||||||
if (lower === cmd || lower.startsWith(cmd + ' ')) {
|
const matches = lower === cmd || lower.startsWith(cmd + ' ');
|
||||||
const quote = quotes[Math.floor(Math.random() * quotes.length)];
|
console.log(`💬 [${room.roomId}] <${sender}> "${content.body}" (listening for "${cmd}", match=${matches})`);
|
||||||
client.sendTextMessage(room.roomId, quote)
|
if (!matches) return;
|
||||||
.then(() => console.log(`🎃 [${room.roomId}] ${quote}`))
|
|
||||||
.catch((err) => console.error('Failed to send quote:', err.message));
|
const quote = quotes[Math.floor(Math.random() * quotes.length)];
|
||||||
}
|
client.sendTextMessage(room.roomId, quote)
|
||||||
|
.then(() => console.log(`🎃 [${room.roomId}] ${quote}`))
|
||||||
|
.catch((err) => console.error('Failed to send quote:', err.message));
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log('🤖 Bot is running!');
|
console.log('🤖 Bot is running!');
|
||||||
|
|||||||
Reference in New Issue
Block a user