From f002f5cf3e79399a55ebd24181e64665fa7387d2 Mon Sep 17 00:00:00 2001 From: notplants Date: Mon, 4 Jul 2022 13:23:55 +0200 Subject: [PATCH] Working on peach-config --- Cargo.lock | 1 + peach-config/Cargo.toml | 1 + peach-config/src/main.rs | 10 ++++++++++ peach-config/src/status.rs | 12 ++++++++++++ peach-lib/src/error.rs | 6 ++++++ peach-lib/src/sbot.rs | 28 ++++++++++++++++++++++++++++ 6 files changed, 58 insertions(+) create mode 100644 peach-config/src/status.rs diff --git a/Cargo.lock b/Cargo.lock index 7699594..0bdf75d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2204,6 +2204,7 @@ version = "0.1.25" dependencies = [ "clap", "env_logger 0.6.2", + "golgi", "lazy_static", "log 0.4.17", "peach-lib", diff --git a/peach-config/Cargo.toml b/peach-config/Cargo.toml index e2f226f..87a178d 100644 --- a/peach-config/Cargo.toml +++ b/peach-config/Cargo.toml @@ -37,3 +37,4 @@ log = "0.4" lazy_static = "1.4.0" peach-lib = { path = "../peach-lib" } rpassword = "5.0" +golgi = { git = "https://git.coopcloud.tech/golgi-ssb/golgi.git" } diff --git a/peach-config/src/main.rs b/peach-config/src/main.rs index f59a474..1db4e62 100644 --- a/peach-config/src/main.rs +++ b/peach-config/src/main.rs @@ -50,6 +50,10 @@ enum PeachConfig { /// Updates file permissions on PeachCloud device #[structopt(name = "permissions")] SetPermissions, + + /// Returns sbot id if sbot is running + #[structopt(name = "whoami")] + WhoAmI, } #[derive(StructOpt, Debug)] @@ -155,6 +159,12 @@ fn main() { ) } }, + PeachConfig::WhoAmI => match status::who_am_i() { + Ok(sbot_id) => {sbot_id} + Err(err) => { + error!("1") + } + }, } } } diff --git a/peach-config/src/status.rs b/peach-config/src/status.rs new file mode 100644 index 0000000..5e707a3 --- /dev/null +++ b/peach-config/src/status.rs @@ -0,0 +1,12 @@ +use crate::error::PeachConfigError; +use crate::ChangePasswordOpts; +use peach_lib::password_utils::set_new_password; +use peach_lib::sbot::init_sbot; + +use golgi::Sbot; + +/// Utility function to check if sbot is running via the whoami method +pub fn whoami() -> Result { + let sbot = init_sbot()?; + sbot.whoami() +} diff --git a/peach-lib/src/error.rs b/peach-lib/src/error.rs index 42a4993..da30cd0 100644 --- a/peach-lib/src/error.rs +++ b/peach-lib/src/error.rs @@ -256,3 +256,9 @@ impl From for PeachError { PeachError::Utf8ToString(err) } } + +impl From for PeachError { + fn from(err: GolgiError) -> PeachError { + PeachError::Golgi(err) + } +} \ No newline at end of file diff --git a/peach-lib/src/sbot.rs b/peach-lib/src/sbot.rs index e2ddba7..78ce794 100644 --- a/peach-lib/src/sbot.rs +++ b/peach-lib/src/sbot.rs @@ -2,6 +2,11 @@ use std::{fs, fs::File, io, io::Write, path::PathBuf, process::Command, str}; +use golgi::{ + api::friends::RelationshipQuery, blobs, messages::SsbMessageValue, sbot::Keystore, Sbot, +}; +use log::debug; + use crate::config_manager; use serde::{Deserialize, Serialize}; @@ -240,3 +245,26 @@ impl SbotConfig { Ok(()) } } + +/// Initialise an sbot client +pub async fn init_sbot() -> Result { + + // read sbot config from config.toml + let sbot_config = SbotConfig::read().ok(); + + debug!("Initialising an sbot client with configuration parameters"); + // initialise sbot connection with ip:port and shscap from config file + let key_path = format!( + "{}/secret", + config_manager::get_config_value("GO_SBOT_DATADIR")? + ); + let sbot_client = match sbot_config { + // TODO: panics if we pass `Some(conf.shscap)` as second arg + Some(conf) => { + let ip_port = conf.lis.clone(); + Sbot::init(Keystore::CustomGoSbot(key_path), Some(ip_port), None).await? + } + None => Sbot::init(Keystore::CustomGoSbot(key_path), None, None).await?, + }; + Ok(sbot_client) +}