refactor!(matrix): move options into a table

This commit is contained in:
Ryan 2025-03-20 18:13:56 -04:00
parent 228b3e3e54
commit c42f228463
Signed by: ErrorNoInternet
GPG Key ID: 2486BFB7B1E6A4A3
4 changed files with 29 additions and 32 deletions

View File

@ -2,7 +2,7 @@ Server = "localhost"
Username = "ErrorNoWatcher" Username = "ErrorNoWatcher"
HttpAddress = "127.0.0.1:8080" HttpAddress = "127.0.0.1:8080"
Owners = { "ErrorNoInternet" } Owners = { "ErrorNoInternet" }
MatrixOwners = { "@errornointernet:envs.net" } MatrixOptions = { Owners = { "@errornointernet:envs.net" } }
for _, module in ipairs({ for _, module in ipairs({
"lib", "lib",

View File

@ -278,15 +278,10 @@ async fn lua_init(client: Client, state: &State, globals: &Table) -> Result<()>
#[cfg(feature = "matrix")] #[cfg(feature = "matrix")]
fn matrix_init(client: &Client, state: State) { fn matrix_init(client: &Client, state: State) {
let globals = state.lua.globals(); let globals = state.lua.globals();
if let Ok(homeserver_url) = globals.get::<String>("MatrixHomeserverUrl") if let Ok(options) = globals.get::<Table>("MatrixOptions") {
&& let Ok(username) = globals.get::<String>("MatrixUsername")
&& let Ok(password) = globals.get::<String>("MatrixPassword")
{
let name = client.username(); let name = client.username();
tokio::spawn(async move { tokio::spawn(async move {
if let Err(error) = if let Err(error) = matrix::login(state, options, globals, name).await {
matrix::login(homeserver_url, username, &password, state, globals, name).await
{
error!("failed to log into matrix account: {error:?}"); error!("failed to log into matrix account: {error:?}");
} }
}); });

View File

@ -28,15 +28,7 @@ pub async fn on_regular_room_message(
return Ok(()); return Ok(());
}; };
if ctx if ctx.is_owner(&event.sender.to_string()) && text_content.body.starts_with(&ctx.name) {
.state
.lua
.globals()
.get::<Vec<String>>("MatrixOwners")
.unwrap_or_default()
.contains(&event.sender.to_string())
&& text_content.body.starts_with(&ctx.name)
{
let body = text_content.body[ctx.name.len()..] let body = text_content.body[ctx.name.len()..]
.trim_start_matches(':') .trim_start_matches(':')
.trim(); .trim();
@ -101,13 +93,7 @@ pub async fn on_stripped_state_member(
) -> Result<()> { ) -> Result<()> {
if let Some(user_id) = client.user_id() if let Some(user_id) = client.user_id()
&& member.state_key == user_id && member.state_key == user_id
&& ctx && ctx.is_owner(&member.sender.to_string())
.state
.lua
.globals()
.get::<Vec<String>>("MatrixOwners")
.unwrap_or_default()
.contains(&member.sender.to_string())
{ {
debug!("joining room {}", room.room_id()); debug!("joining room {}", room.room_id());
while let Err(error) = room.join().await { while let Err(error) = room.join().await {

View File

@ -20,6 +20,23 @@ pub struct Context {
name: String, name: String,
} }
impl Context {
fn is_owner(&self, name: &String) -> bool {
self.state
.lua
.globals()
.get::<Table>("MatrixOptions")
.ok()
.and_then(|options| {
options
.get::<Vec<String>>("owners")
.ok()
.and_then(|owners| owners.contains(name).then_some(()))
})
.is_some()
}
}
#[derive(Clone, Serialize, Deserialize)] #[derive(Clone, Serialize, Deserialize)]
struct Session { struct Session {
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
@ -37,14 +54,13 @@ async fn persist_sync_token(
Ok(()) Ok(())
} }
pub async fn login( pub async fn login(state: State, options: Table, globals: Table, name: String) -> Result<()> {
homeserver_url: String, let (homeserver_url, username, password) = (
username: String, options.get::<String>("homeserver_url")?,
password: &str, options.get::<String>("username")?,
state: State, &options.get::<String>("password")?,
globals: Table, );
name: String,
) -> Result<()> {
let root_dir = dirs::data_dir() let root_dir = dirs::data_dir()
.context("no data directory")? .context("no data directory")?
.join("errornowatcher") .join("errornowatcher")