2024-05-23 09:42:14 +00:00
|
|
|
// SPDX-FileCopyrightText: 2024 John Livingston <https://www.john-livingston.fr/>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2023-08-09 14:16:02 +00:00
|
|
|
import type { RegisterServerOptions } from '@peertube/peertube-types'
|
2023-09-18 10:23:35 +00:00
|
|
|
import type { ChannelConfigurationOptions } from '../../../../shared/lib/types'
|
|
|
|
import type { ChannelCommonRoomConf } from '../../configuration/bot'
|
|
|
|
import { RoomChannel } from '../../room-channel'
|
2023-09-06 13:23:39 +00:00
|
|
|
import { sanitizeChannelConfigurationOptions } from '../../configuration/channel/sanitize'
|
2023-08-09 14:39:12 +00:00
|
|
|
import * as fs from 'fs'
|
|
|
|
import * as path from 'path'
|
2023-08-09 14:16:02 +00:00
|
|
|
|
2023-09-19 18:49:14 +00:00
|
|
|
// FIXME: should be exported by xmppjs-chat-bot
|
|
|
|
type ConfigHandlers = ChannelCommonRoomConf['handlers']
|
|
|
|
type ConfigHandler = ConfigHandlers[0]
|
|
|
|
|
2023-08-09 14:39:12 +00:00
|
|
|
/**
|
2023-09-06 13:23:39 +00:00
|
|
|
* Get saved configuration options for the given channel.
|
2023-08-09 14:39:12 +00:00
|
|
|
* Can throw an exception.
|
|
|
|
* @param options Peertube server options
|
|
|
|
* @param channelInfos Info from channel from which we want to get infos
|
2023-09-18 10:23:35 +00:00
|
|
|
* @returns Channel configuration data, or null if nothing is stored
|
2023-08-09 14:39:12 +00:00
|
|
|
*/
|
2023-09-06 13:23:39 +00:00
|
|
|
async function getChannelConfigurationOptions (
|
2023-08-09 14:16:02 +00:00
|
|
|
options: RegisterServerOptions,
|
2023-09-18 10:23:35 +00:00
|
|
|
channelId: number | string
|
|
|
|
): Promise<ChannelConfigurationOptions | null> {
|
2023-08-09 14:39:12 +00:00
|
|
|
const logger = options.peertubeHelpers.logger
|
2023-09-18 10:23:35 +00:00
|
|
|
const filePath = _getFilePath(options, channelId)
|
2023-08-09 14:39:12 +00:00
|
|
|
if (!fs.existsSync(filePath)) {
|
2024-02-13 11:49:22 +00:00
|
|
|
logger.debug('No stored data for channel, returning null')
|
2023-09-18 10:23:35 +00:00
|
|
|
return null
|
2023-08-09 14:39:12 +00:00
|
|
|
}
|
|
|
|
const content = await fs.promises.readFile(filePath, {
|
|
|
|
encoding: 'utf-8'
|
|
|
|
})
|
2023-09-18 10:23:35 +00:00
|
|
|
const sanitized = await sanitizeChannelConfigurationOptions(options, channelId, JSON.parse(content))
|
|
|
|
return sanitized
|
|
|
|
}
|
|
|
|
|
|
|
|
function getDefaultChannelConfigurationOptions (_options: RegisterServerOptions): ChannelConfigurationOptions {
|
2023-08-09 14:16:02 +00:00
|
|
|
return {
|
2023-09-21 17:32:47 +00:00
|
|
|
bot: {
|
|
|
|
enabled: false,
|
|
|
|
nickname: 'Sepia',
|
2023-09-22 14:30:12 +00:00
|
|
|
forbiddenWords: [],
|
|
|
|
quotes: [],
|
|
|
|
commands: []
|
2024-02-13 11:49:22 +00:00
|
|
|
},
|
|
|
|
slowMode: {
|
2024-03-07 16:56:27 +00:00
|
|
|
duration: 0
|
2024-06-20 14:46:14 +00:00
|
|
|
},
|
|
|
|
mute: {
|
|
|
|
anonymous: false
|
2024-06-21 16:18:11 +00:00
|
|
|
},
|
|
|
|
terms: undefined
|
2023-08-09 14:16:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-09 14:39:12 +00:00
|
|
|
/**
|
2023-09-06 13:23:39 +00:00
|
|
|
* Save channel configuration options.
|
2023-08-09 14:39:12 +00:00
|
|
|
* Can throw an exception.
|
2023-09-06 13:23:39 +00:00
|
|
|
* @param options Peertube server options
|
2023-09-18 10:23:35 +00:00
|
|
|
* @param ChannelConfigurationOptions data to save
|
2023-08-09 14:39:12 +00:00
|
|
|
*/
|
2023-09-06 13:23:39 +00:00
|
|
|
async function storeChannelConfigurationOptions (
|
2023-08-09 14:39:12 +00:00
|
|
|
options: RegisterServerOptions,
|
2023-09-18 10:23:35 +00:00
|
|
|
channelId: number | string,
|
|
|
|
channelConfigurationOptions: ChannelConfigurationOptions
|
2023-08-09 14:16:02 +00:00
|
|
|
): Promise<void> {
|
2023-09-18 10:23:35 +00:00
|
|
|
const filePath = _getFilePath(options, channelId)
|
2023-08-09 14:39:12 +00:00
|
|
|
|
|
|
|
if (!fs.existsSync(filePath)) {
|
|
|
|
const dir = path.dirname(filePath)
|
|
|
|
if (!fs.existsSync(dir)) {
|
|
|
|
fs.mkdirSync(dir, { recursive: true })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-18 10:23:35 +00:00
|
|
|
const jsonContent = JSON.stringify(channelConfigurationOptions)
|
2023-08-09 14:39:12 +00:00
|
|
|
|
|
|
|
await fs.promises.writeFile(filePath, jsonContent, {
|
|
|
|
encoding: 'utf-8'
|
|
|
|
})
|
2023-09-15 15:55:07 +00:00
|
|
|
|
2023-09-18 10:23:35 +00:00
|
|
|
RoomChannel.singleton().refreshChannelConfigurationOptions(channelId)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts the channel configuration to the bot room configuration object (minus the room JID and domain)
|
|
|
|
* @param options server options
|
|
|
|
* @param channelConfigurationOptions The channel configuration
|
2023-09-22 14:30:12 +00:00
|
|
|
* @param previousRoomConf the previous saved room conf, if available. Used to merge handlers.
|
2023-09-18 10:23:35 +00:00
|
|
|
* @returns Partial bot room configuration
|
|
|
|
*/
|
|
|
|
function channelConfigurationOptionsToBotRoomConf (
|
|
|
|
options: RegisterServerOptions,
|
2023-09-22 14:30:12 +00:00
|
|
|
channelConfigurationOptions: ChannelConfigurationOptions,
|
|
|
|
previousRoomConf: ChannelCommonRoomConf | null
|
2023-09-18 10:23:35 +00:00
|
|
|
): ChannelCommonRoomConf {
|
2023-09-19 18:49:14 +00:00
|
|
|
// Note concerning handlers:
|
|
|
|
// If we want the bot to correctly enable/disable the handlers,
|
|
|
|
// we must always define all handlers, even if not used.
|
2023-09-22 14:30:12 +00:00
|
|
|
// That's why we are gathering handlers ids in handlersId, and disabling missing handlers at the end of this function.
|
|
|
|
const handlersIds: Map<string, true> = new Map<string, true>()
|
2023-09-19 18:49:14 +00:00
|
|
|
const handlers: ConfigHandlers = []
|
2023-09-21 17:32:47 +00:00
|
|
|
channelConfigurationOptions.bot.forbiddenWords.forEach((v, i) => {
|
2023-09-22 14:30:12 +00:00
|
|
|
const id = 'forbidden_words_' + i.toString()
|
|
|
|
handlersIds.set(id, true)
|
|
|
|
handlers.push(_getForbiddenWordsHandler(id, v))
|
|
|
|
})
|
|
|
|
channelConfigurationOptions.bot.quotes.forEach((v, i) => {
|
|
|
|
const id = 'quote_' + i.toString()
|
|
|
|
handlersIds.set(id, true)
|
|
|
|
handlers.push(_getQuotesHandler(id, v))
|
|
|
|
})
|
|
|
|
channelConfigurationOptions.bot.commands.forEach((v, i) => {
|
|
|
|
const id = 'command_' + i.toString()
|
|
|
|
handlersIds.set(id, true)
|
|
|
|
handlers.push(_getCommandsHandler(id, v))
|
2023-09-21 17:32:47 +00:00
|
|
|
})
|
2023-09-19 18:49:14 +00:00
|
|
|
|
2023-09-22 14:30:12 +00:00
|
|
|
// Disabling missing handlers:
|
|
|
|
if (previousRoomConf) {
|
|
|
|
for (const handler of previousRoomConf.handlers) {
|
|
|
|
if (!handlersIds.has(handler.id)) {
|
|
|
|
// cloning to avoid issues...
|
|
|
|
const disabledHandler = JSON.parse(JSON.stringify(handler))
|
|
|
|
disabledHandler.enabled = false
|
|
|
|
handlers.push(disabledHandler)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-19 16:56:39 +00:00
|
|
|
const roomConf: ChannelCommonRoomConf = {
|
2023-09-21 17:32:47 +00:00
|
|
|
enabled: channelConfigurationOptions.bot.enabled,
|
2023-09-19 18:49:14 +00:00
|
|
|
handlers
|
2023-09-15 15:55:07 +00:00
|
|
|
}
|
2023-09-21 17:32:47 +00:00
|
|
|
if (channelConfigurationOptions.bot.nickname && channelConfigurationOptions.bot.nickname !== '') {
|
|
|
|
roomConf.nick = channelConfigurationOptions.bot.nickname
|
2023-09-19 16:56:39 +00:00
|
|
|
}
|
2023-09-18 10:23:35 +00:00
|
|
|
return roomConf
|
2023-08-09 14:39:12 +00:00
|
|
|
}
|
|
|
|
|
2023-09-19 18:49:14 +00:00
|
|
|
function _getForbiddenWordsHandler (
|
|
|
|
id: string,
|
2023-09-21 17:32:47 +00:00
|
|
|
forbiddenWords: ChannelConfigurationOptions['bot']['forbiddenWords'][0]
|
2023-09-19 18:49:14 +00:00
|
|
|
): ConfigHandler {
|
|
|
|
const handler: ConfigHandler = {
|
|
|
|
type: 'moderate',
|
|
|
|
id,
|
|
|
|
enabled: false,
|
|
|
|
options: {
|
|
|
|
rules: []
|
|
|
|
}
|
|
|
|
}
|
2023-09-21 17:32:47 +00:00
|
|
|
if (forbiddenWords.entries.length === 0) {
|
2023-09-19 18:49:14 +00:00
|
|
|
return handler
|
|
|
|
}
|
|
|
|
|
|
|
|
handler.enabled = true
|
|
|
|
const rule: any = {
|
2023-09-21 17:32:47 +00:00
|
|
|
name: id
|
|
|
|
}
|
|
|
|
|
|
|
|
if (forbiddenWords.regexp) {
|
|
|
|
// Note: on the Peertube frontend, channelConfigurationOptions.forbiddenWords
|
|
|
|
// is an array of RegExp definition (strings).
|
|
|
|
// They are validated one by bone.
|
|
|
|
// To increase the bot performance, we will join them all (hopping the bot will optimize them).
|
|
|
|
rule.regexp = '(?:' + forbiddenWords.entries.join(')|(?:') + ')'
|
|
|
|
} else {
|
|
|
|
// Here we must add word-breaks and escape entries.
|
|
|
|
// We join all entries in one Regexp (for the same reason as above).
|
|
|
|
rule.regexp = '(?:' +
|
|
|
|
forbiddenWords.entries.map(s => {
|
|
|
|
s = _stringToWordRegexp(s)
|
|
|
|
// Must add the \b...
|
|
|
|
// ... but... won't work if the first (or last) char is an emoji.
|
|
|
|
// So, doing this trick:
|
|
|
|
if (/^\w/.test(s)) {
|
|
|
|
s = '\\b' + s
|
|
|
|
}
|
|
|
|
if (/\w$/.test(s)) {
|
|
|
|
s = s + '\\b'
|
|
|
|
}
|
|
|
|
// FIXME: this solution wont work for non-latin charsets.
|
|
|
|
return s
|
|
|
|
}).join(')|(?:') + ')'
|
2023-09-19 18:49:14 +00:00
|
|
|
}
|
2023-09-21 17:32:47 +00:00
|
|
|
|
|
|
|
if (forbiddenWords.reason) {
|
|
|
|
rule.reason = forbiddenWords.reason
|
2023-09-19 18:49:14 +00:00
|
|
|
}
|
|
|
|
handler.options.rules.push(rule)
|
2023-09-21 17:32:47 +00:00
|
|
|
|
|
|
|
handler.options.applyToModerators = !!forbiddenWords.applyToModerators
|
2023-09-19 18:49:14 +00:00
|
|
|
return handler
|
|
|
|
}
|
|
|
|
|
2023-09-22 14:30:12 +00:00
|
|
|
function _getQuotesHandler (
|
|
|
|
id: string,
|
|
|
|
quotes: ChannelConfigurationOptions['bot']['quotes'][0]
|
|
|
|
): ConfigHandler {
|
|
|
|
const handler: ConfigHandler = {
|
|
|
|
type: 'quotes_random',
|
|
|
|
id,
|
|
|
|
enabled: false,
|
|
|
|
options: {
|
|
|
|
quotes: [],
|
|
|
|
delay: 5 * 60
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (quotes.messages.length === 0) {
|
|
|
|
return handler
|
|
|
|
}
|
|
|
|
|
|
|
|
handler.enabled = true
|
|
|
|
handler.options.quotes = quotes.messages
|
|
|
|
handler.options.delay = quotes.delay
|
|
|
|
return handler
|
|
|
|
}
|
|
|
|
|
|
|
|
function _getCommandsHandler (
|
|
|
|
id: string,
|
|
|
|
command: ChannelConfigurationOptions['bot']['commands'][0]
|
|
|
|
): ConfigHandler {
|
|
|
|
const handler: ConfigHandler = {
|
|
|
|
type: 'command_say',
|
|
|
|
id,
|
|
|
|
enabled: false,
|
|
|
|
options: {
|
|
|
|
quotes: [],
|
|
|
|
command: 'undefined' // This is arbitrary, and does not matter as enabled=false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!command.message || command.message === '') {
|
|
|
|
return handler
|
|
|
|
}
|
|
|
|
|
|
|
|
handler.enabled = true
|
|
|
|
handler.options.command = command.command
|
|
|
|
handler.options.quotes = [command.message]
|
|
|
|
return handler
|
|
|
|
}
|
|
|
|
|
2023-09-21 17:32:47 +00:00
|
|
|
const stringToWordRegexpSpecials = [
|
|
|
|
// order matters for these
|
|
|
|
'-', '[', ']',
|
|
|
|
// order doesn't matter for any of these
|
|
|
|
'/', '{', '}', '(', ')', '*', '+', '?', '.', '\\', '^', '$', '|'
|
|
|
|
]
|
|
|
|
// I choose to escape every character with '\'
|
|
|
|
// even though only some strictly require it when inside of []
|
|
|
|
const stringToWordRegexp = RegExp('[' + stringToWordRegexpSpecials.join('\\') + ']', 'g')
|
|
|
|
function _stringToWordRegexp (s: string): string {
|
|
|
|
return s.replace(stringToWordRegexp, '\\$&')
|
|
|
|
}
|
|
|
|
|
2023-08-09 14:39:12 +00:00
|
|
|
function _getFilePath (
|
|
|
|
options: RegisterServerOptions,
|
2023-09-18 10:23:35 +00:00
|
|
|
channelId: number | string
|
2023-08-09 14:39:12 +00:00
|
|
|
): string {
|
|
|
|
// some sanitization, just in case...
|
2023-09-18 10:23:35 +00:00
|
|
|
channelId = parseInt(channelId.toString())
|
|
|
|
if (isNaN(channelId)) {
|
2023-08-09 14:39:12 +00:00
|
|
|
throw new Error(`Invalid channelId: ${channelId}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
return path.resolve(
|
|
|
|
options.peertubeHelpers.plugin.getDataDirectoryPath(),
|
2023-09-06 13:23:39 +00:00
|
|
|
'channelConfigurationOptions',
|
2023-08-09 14:39:12 +00:00
|
|
|
channelId.toString() + '.json'
|
|
|
|
)
|
2023-08-09 14:16:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
2023-09-06 13:23:39 +00:00
|
|
|
getChannelConfigurationOptions,
|
2023-09-18 10:23:35 +00:00
|
|
|
getDefaultChannelConfigurationOptions,
|
|
|
|
channelConfigurationOptionsToBotRoomConf,
|
2023-09-06 13:23:39 +00:00
|
|
|
storeChannelConfigurationOptions
|
2023-08-09 14:16:02 +00:00
|
|
|
}
|