From 10682a6f9973faa5cd9b1198734537598d4d8e6d Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Tue, 12 Apr 2022 12:22:36 -0700 Subject: [PATCH] port to imp --- Cargo.lock | 9 +++ Cargo.toml | 1 + src/bot.rs | 193 --------------------------------------------------- src/event.rs | 27 ------- src/main.rs | 14 ++-- 5 files changed, 16 insertions(+), 228 deletions(-) delete mode 100644 src/bot.rs delete mode 100644 src/event.rs diff --git a/Cargo.lock b/Cargo.lock index 4f2405d..7049b92 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -109,6 +109,14 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" +[[package]] +name = "imp" +version = "0.1.0" +dependencies = [ + "libcwtch", + "serde_json", +] + [[package]] name = "itoa" version = "1.0.1" @@ -261,6 +269,7 @@ checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" name = "update_bot" version = "0.1.0" dependencies = [ + "imp", "libcwtch", "serde_json", ] diff --git a/Cargo.toml b/Cargo.toml index 5e31d83..e1e10fd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,5 +7,6 @@ edition = "2021" [dependencies] libcwtch = {path = "../libcwtch-rs" } #"0.2.0" +imp = {path = "../imp"} serde_json = "1.0" diff --git a/src/bot.rs b/src/bot.rs deleted file mode 100644 index 8924168..0000000 --- a/src/bot.rs +++ /dev/null @@ -1,193 +0,0 @@ -use libcwtch::structs::*; -use libcwtch::CwtchLib; -use crate::Event; - -use serde_json; -use crate::bot::DefaultContactPolicy::Accept; - -pub enum DefaultContactPolicy { - Ignore, - Block, - Accept, -} - -pub struct Behaviour { - pub proto_experiments: bool, - pub proto_experiment_fileshare: bool, - - pub profile_name: String, - pub profile_pic_path: Option, - - pub default_contant_policy: DefaultContactPolicy, -} - -impl Behaviour { - pub fn new_default_acceptor(name: String, profile_pic_path: String) -> Self { - return Behaviour{ - proto_experiments: true, - proto_experiment_fileshare: true, - default_contant_policy: Accept, - profile_name: name, - profile_pic_path: Some(profile_pic_path), - } - } -} - -pub trait EventHandler { - fn handle(&self, cwtch: &dyn CwtchLib, profile: Option<&Profile>, event: CwtchEvent); -} - -pub struct Bot { - cwtch: Box, - behaviour: Behaviour, - password: String, - home_dir: String, - - settings: Option, - profile: Option, -} - -impl Bot { - pub fn new(behaviour: Behaviour, password: String, home_dir: String) -> Self { - let cwtch = libcwtch::new_cwtchlib_go(); - println!("start_cwtch"); - let ret = cwtch.start_cwtch(&home_dir, ""); - println!("start_cwtch returned {}", ret); - - return Bot{behaviour, cwtch: Box::new(cwtch), password, home_dir, profile: None, settings: None} - } - - pub fn event_loop(&mut self, handler: Box) { - let mut initialized: bool = false; - - loop { - let event_str = self.cwtch.get_appbus_event(); - println!("bot: event: {}", event_str); - - let event: CwtchEvent = - serde_json::from_str(&event_str).expect("Error parsing Cwtch event"); - let event_type = Event::new(event.event_type.as_str()); - match event_type { - Event::CwtchStarted => { - println!("event CwtchStarted!"); - initialized = true; - - match self.profile { - None => { - println!("Creating bot"); - self.cwtch.load_profiles(&self.password); - } - Some(_) => (), - } - } - Event::UpdateGlobalSettings => { - println!("Loading settings froms {}", &event.data["Data"]); - let mut settings: Settings = match serde_json::from_str(&event.data["Data"]) { - Ok(s) => s, - Err(e) => panic!("invalid json: {:?}", e), - }; - - if self.behaviour.proto_experiments { - settings.ExperimentsEnabled = true; - } - if self.behaviour.proto_experiment_fileshare { - settings - .Experiments - .insert(Experiments::FileSharingExperiment.to_key_string(), true); - } - match settings.save(self.cwtch.as_ref()) { - Ok(_) => (), - Err(e) => println!("ERROR: could not save settings: {}", e), - }; - - match self.profile.as_ref() { - Some(profile) => { - if let Some(profile_pic_path) = &self.behaviour.profile_pic_path { - self.cwtch.share_file(&profile.handle, -1, profile_pic_path); - } - } - None => (), - }; - - self.settings = Some(settings); - } - Event::NewPeer => { - println!( - "\n***** {} at {} *****\n", - event.data["name"], event.data["Identity"] - ); - - // process json for profile, conversations and servers...else { - let profile = match Profile::new( - &event.data["Identity"], - &event.data["name"], - &event.data["picture"], - &event.data["ContactsJson"], - &event.data["ServerList"], - ) { - Ok(p) => p, - Err(e) => panic!("error parsing profile: {}", e), - }; - print!("profile: {:?}", profile); - // Share profile image - match self.settings.as_ref() { - Some(_settings) => { - self.cwtch.share_file(&profile.handle, -1, "build_bot.png"); - } - None => (), - }; - - self.cwtch.set_profile_attribute(&profile.handle, "profile.name", &self.behaviour.profile_name); - - self.profile = Some(profile); - } - Event::AppError => { - if initialized && event.data["Error"] == "Loaded 0 profiles" { - self.cwtch.create_profile(&self.behaviour.profile_name, &self.password); - } - } - Event::ContactCreated => { - if event.data["ConnectionState"] == "Authenticated" { - let profile_onion = event.data["RemotePeer"].to_string(); - let convo_id = event.data["ConversationID"].parse::().unwrap(); - - let acl: ACL = serde_json::from_str(&event.data["accessControlList"]) - .expect("Error parsing conversation"); - - let conversation = Conversation { - handle: profile_onion.clone(), - identifier: event.data["ConversationID"].parse::().unwrap(), - name: event.data["nick"].to_string(), - status: ConnectionState::new(&event.data["status"]), - blocked: event.data["blocked"] == "true", - accepted: event.data["accepted"] == "true", - access_control_list: acl, - is_group: false, // by definition - }; - - match self.behaviour.default_contant_policy { - DefaultContactPolicy::Accept => - self.cwtch.accept_conversation(&conversation.handle.clone(), convo_id), - DefaultContactPolicy::Block => - self.cwtch.block_contact(&conversation.handle.clone(), convo_id), - DefaultContactPolicy::Ignore => (), - } - - match self.profile.as_mut() { - Some(profile) => { - profile - .conversations - .insert(event.data["RemotePeer"].to_string(), conversation); - } - None => (), - }; - } - } - Event::ErrUnhandled(err) => eprintln!("unhandled event: {}!", err), - _ => (), - }; - - handler.handle(self.cwtch.as_ref(), self.profile.as_ref(), event); - } - } -} \ No newline at end of file diff --git a/src/event.rs b/src/event.rs deleted file mode 100644 index 7a0484c..0000000 --- a/src/event.rs +++ /dev/null @@ -1,27 +0,0 @@ -#[derive(Debug)] -pub enum Event { - CwtchStarted, - NewPeer, - NewMessageFromPeer, - AppError, - ContactCreated, - PeerStateChange, - UpdateGlobalSettings, - - ErrUnhandled(String), -} - -impl Event { - pub fn new(name: &str) -> Self { - match name { - "CwtchStarted" => Event::CwtchStarted, - "NewPeer" => Event::NewPeer, - "NewMessageFromPeer" => Event::NewMessageFromPeer, - "AppError" => Event::AppError, - "ContactCreated" => Event::ContactCreated, - "PeerStateChange" => Event::PeerStateChange, - "UpdateGlobalSettings" => Event::UpdateGlobalSettings, - _ => Event::ErrUnhandled(name.to_string()), - } - } -} diff --git a/src/main.rs b/src/main.rs index 04dd3e4..82e4f20 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,7 @@ -mod event; -mod bot; - extern crate core; -use event::Event; +use ::imp::event::Event; +use ::imp::imp; use std::fs::read_dir; use std::path::{Path, PathBuf}; @@ -14,7 +12,7 @@ use serde_json; use libcwtch; use libcwtch::structs::*; use libcwtch::CwtchLib; -use crate::bot::{Behaviour, Bot}; +use crate::imp::{Behaviour, Imp}; const DIST_DIR: &str = "cwtch_dist"; const BOT_HOME: &str = "~/.cwtch/bots/update_bot"; @@ -71,7 +69,7 @@ fn main() { let behaviour: Behaviour = Behaviour::new_default_acceptor(BOT_NAME.to_string(), "build_bot.png".to_string()); let event_loop_handle = thread::spawn(move || { - let mut bot = Bot::new(behaviour, PASSWORD.to_string(), BOT_HOME.to_string()); + let mut bot = Imp::spawn(behaviour, PASSWORD.to_string(), BOT_HOME.to_string()); bot.event_loop(Box::new(update_bot)); }); @@ -126,9 +124,9 @@ impl UpdateBot { } } -impl bot::EventHandler for UpdateBot { +impl imp::EventHandler for UpdateBot { fn handle(&self, cwtch: &dyn CwtchLib, profile_opt: Option<&Profile>, event: CwtchEvent) { - let event_type = Event::new(event.event_type.as_str()); + let event_type = Event::from(event.event_type.as_str()); match event_type { Event::ContactCreated => { if event.data["ConnectionState"] == "Authenticated" {