From d359d3ad996c53f89c0f908f25e0900c32c664d8 Mon Sep 17 00:00:00 2001 From: John Livingston Date: Fri, 14 Apr 2023 11:25:29 +0200 Subject: [PATCH] Fix Prosody room topic: Due to a bug in mod_muc_http_defaults (https://hg.prosody.im/prosody-modules/rev/6d99ddd99694), room topics were badly configured. The plugin will fix them at startup, and stops trying to set the subject. --- CHANGELOG.md | 1 + server/lib/prosody/ctl.ts | 7 ++++ server/lib/prosody/fix-room-subject.ts | 56 ++++++++++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 server/lib/prosody/fix-room-subject.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 02c8a67f..21f3d334 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ * Diagnostic tool: add the result of `prosodyctl check` in the debug section. * New debug mode +* Fix room topic: due to a [bug in mod_muc_http_defaults](https://hg.prosody.im/prosody-modules/rev/6d99ddd99694), room topics were badly configured. The plugin will fix them at startup, and stops trying to set the subject. ## 6.2.3 diff --git a/server/lib/prosody/ctl.ts b/server/lib/prosody/ctl.ts index f5af9935..188d7988 100644 --- a/server/lib/prosody/ctl.ts +++ b/server/lib/prosody/ctl.ts @@ -5,6 +5,7 @@ import { ensureProsodyCertificates, startProsodyCertificatesRenewCheck, stopProsodyCertificatesRenewCheck } from './certificates' import { disableProxyRoute, enableProxyRoute } from '../routers/webchat' +import { fixRoomSubject } from './fix-room-subject' import * as fs from 'fs' import * as child_process from 'child_process' @@ -71,6 +72,12 @@ async function prepareProsody (options: RegisterServerOptions): Promise { filePaths.appImageExtractPath ) + try { + await fixRoomSubject(options, filePaths) + } catch (err) { + logger.error(err) + } + const appImageToExtract = filePaths.appImageToExtract if (!appImageToExtract) { return diff --git a/server/lib/prosody/fix-room-subject.ts b/server/lib/prosody/fix-room-subject.ts new file mode 100644 index 00000000..482a5f92 --- /dev/null +++ b/server/lib/prosody/fix-room-subject.ts @@ -0,0 +1,56 @@ +import type { RegisterServerOptions } from '@peertube/peertube-types' +import { ProsodyFilePaths } from './config/paths' +import * as path from 'path' +import * as fs from 'fs' + +/** + * Until plugin verstion 6.2.3, there where a bug in mod_muc_http_defaults + * that messed up room metadata. This bug is blocking for external XMPP account + * connections. + * This functions corrects room metadata if needed. + * @param options + * @param filePaths + */ +export async function fixRoomSubject (options: RegisterServerOptions, filePaths: ProsodyFilePaths): Promise { + const logger = options.peertubeHelpers.logger + // When this scripts run the first time, it create a file, so that we can know if we need to run it again: + const doneFilePath = path.resolve(filePaths.dir, 'fix-room-done') + if (fs.existsSync(doneFilePath)) { + logger.debug('fixRoomSubject already runned.') + return + } + + logger.info('Fixing Prosody room subjects...') + + // Room data are in a folder named something like "room%2evideos%2edomain%2etld". + // There should only be one. But if you renamed your instance, it could be more than one. + // Just in case, we will loop on each folder with name beginning with "room%2e". + const folders = fs.readdirSync(filePaths.data, { withFileTypes: true }).filter(file => { + return file.isDirectory() && file.name.startsWith('room%2e') + }) + folders.forEach(folder => { + // the folder must contain a «config» directory + const configDir = path.resolve(filePaths.data, folder.name, 'config') + if (!fs.existsSync(configDir)) { return } + const roomDataFiles = fs.readdirSync(configDir, { withFileTypes: true }).filter(file => { + return file.isFile() && file.name.endsWith('.dat') + }) + roomDataFiles.forEach(file => { + logger.debug('Checking room ' + file.name + ' subject') + const filepath = path.resolve(configDir, file.name) + let content = fs.readFileSync(filepath).toString() + // To detect wrongly configured files, we just check if there is a "subject_from" and no "subject" key. + // Indeed, the bug was that mod_muc_http_defaults set subject_from (which should be a jid) to the subject, and + // did not set any subject. + // See https://hg.prosody.im/prosody-modules/rev/6d99ddd99694 + if (content.includes('["subject_from"]') && !content.includes('["subject"]')) { + logger.info('We must fix room ' + file.name + ' by removing subject_from') + content = content.replace(/^\s*\["subject_from"\]\s*=.*;\s*$/gm, '') + content = content.replace(/^\s*\["subject_time"\]\s*=.*;\s*$/gm, '') + fs.writeFileSync(filepath, content) + } + }) + }) + + fs.writeFileSync(doneFilePath, '') +}