2023-05-01 05:05:47 +00:00
// @ts-check
2023-04-30 12:57:30 +00:00
const fs = require ( "fs" )
2024-09-05 03:36:43 +00:00
const crypto = require ( "crypto" )
2023-08-23 12:27:51 +00:00
const assert = require ( "assert" ) . strict
2024-09-05 03:36:43 +00:00
const path = require ( "path" )
2023-04-30 12:57:30 +00:00
const yaml = require ( "js-yaml" )
2024-09-05 03:36:43 +00:00
const registrationFilePath = path . join ( process . cwd ( ) , "registration.yaml" )
2023-09-12 11:08:33 +00:00
2024-09-05 03:36:43 +00:00
/** @param {import("../types").AppServiceRegistrationConfig} reg */
function checkRegistration ( reg ) {
reg [ "ooye" ] . invite = ( reg . ooye . invite || [ ] ) . filter ( mxid => mxid . endsWith ( ` : ${ reg . ooye . server _name } ` ) ) // one day I will understand why typescript disagrees with dot notation on this line
assert ( reg . ooye ? . max _file _size )
assert ( reg . ooye ? . namespace _prefix )
assert ( reg . ooye ? . server _name )
assert ( reg . sender _localpart ? . startsWith ( reg . ooye . namespace _prefix ) , "appservice's localpart must be in the namespace it controls" )
assert ( reg . ooye ? . server _origin . match ( /^https?:\/\// ) , "server origin must start with http or https" )
assert . notEqual ( reg . ooye ? . server _origin . slice ( - 1 ) , "/" , "server origin must not end in slash" )
assert . match ( reg . url , /^https?:/ , "url must start with http:// or https://" )
}
/** @param {import("../types").AppServiceRegistrationConfig} reg */
function writeRegistration ( reg ) {
fs . writeFileSync ( registrationFilePath , JSON . stringify ( reg , null , 2 ) )
}
/** @returns {import("../types").InitialAppServiceRegistrationConfig} reg */
function getTemplateRegistration ( ) {
return {
id : crypto . randomBytes ( 16 ) . toString ( "hex" ) ,
as _token : crypto . randomBytes ( 16 ) . toString ( "hex" ) ,
hs _token : crypto . randomBytes ( 16 ) . toString ( "hex" ) ,
namespaces : {
users : [ {
exclusive : true ,
regex : "@_ooye_.*:cadence.moe"
} ] ,
aliases : [ {
exclusive : true ,
regex : "#_ooye_.*:cadence.moe"
} ]
} ,
protocols : [
"discord"
] ,
sender _localpart : "_ooye_bot" ,
rate _limited : false ,
ooye : {
namespace _prefix : "_ooye_" ,
max _file _size : 5000000 ,
content _length _workaround : false ,
include _user _id _in _mxid : false ,
invite : [ ]
}
}
}
function readRegistration ( ) {
/** @type {import("../types").AppServiceRegistrationConfig} */ // @ts-ignore
let result = null
if ( fs . existsSync ( registrationFilePath ) ) {
const content = fs . readFileSync ( registrationFilePath , "utf8" )
if ( content . startsWith ( "{" ) ) { // Use JSON parser
result = JSON . parse ( content )
checkRegistration ( result )
} else { // Use YAML parser
result = yaml . load ( content )
checkRegistration ( result )
// Convert to JSON
writeRegistration ( result )
}
}
return result
}
/** @type {import("../types").AppServiceRegistrationConfig} */ // @ts-ignore
let reg = readRegistration ( )
module . exports . registrationFilePath = registrationFilePath
module . exports . readRegistration = readRegistration
module . exports . getTemplateRegistration = getTemplateRegistration
module . exports . writeRegistration = writeRegistration
module . exports . checkRegistration = checkRegistration
module . exports . reg = reg