port to imp
This commit is contained in:
parent
4daa4b071e
commit
10682a6f99
|
@ -109,6 +109,14 @@ version = "1.0.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
|
checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "imp"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"libcwtch",
|
||||||
|
"serde_json",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "itoa"
|
name = "itoa"
|
||||||
version = "1.0.1"
|
version = "1.0.1"
|
||||||
|
@ -261,6 +269,7 @@ checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
|
||||||
name = "update_bot"
|
name = "update_bot"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"imp",
|
||||||
"libcwtch",
|
"libcwtch",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
]
|
]
|
||||||
|
|
|
@ -7,5 +7,6 @@ edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
libcwtch = {path = "../libcwtch-rs" } #"0.2.0"
|
libcwtch = {path = "../libcwtch-rs" } #"0.2.0"
|
||||||
|
imp = {path = "../imp"}
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
|
|
||||||
|
|
193
src/bot.rs
193
src/bot.rs
|
@ -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<String>,
|
|
||||||
|
|
||||||
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<dyn CwtchLib>,
|
|
||||||
behaviour: Behaviour,
|
|
||||||
password: String,
|
|
||||||
home_dir: String,
|
|
||||||
|
|
||||||
settings: Option<Settings>,
|
|
||||||
profile: Option<Profile>,
|
|
||||||
}
|
|
||||||
|
|
||||||
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<dyn EventHandler>) {
|
|
||||||
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::<i32>().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::<i32>().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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
27
src/event.rs
27
src/event.rs
|
@ -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()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
14
src/main.rs
14
src/main.rs
|
@ -1,9 +1,7 @@
|
||||||
mod event;
|
|
||||||
mod bot;
|
|
||||||
|
|
||||||
extern crate core;
|
extern crate core;
|
||||||
|
|
||||||
use event::Event;
|
use ::imp::event::Event;
|
||||||
|
use ::imp::imp;
|
||||||
|
|
||||||
use std::fs::read_dir;
|
use std::fs::read_dir;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
@ -14,7 +12,7 @@ use serde_json;
|
||||||
use libcwtch;
|
use libcwtch;
|
||||||
use libcwtch::structs::*;
|
use libcwtch::structs::*;
|
||||||
use libcwtch::CwtchLib;
|
use libcwtch::CwtchLib;
|
||||||
use crate::bot::{Behaviour, Bot};
|
use crate::imp::{Behaviour, Imp};
|
||||||
|
|
||||||
const DIST_DIR: &str = "cwtch_dist";
|
const DIST_DIR: &str = "cwtch_dist";
|
||||||
const BOT_HOME: &str = "~/.cwtch/bots/update_bot";
|
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 behaviour: Behaviour = Behaviour::new_default_acceptor(BOT_NAME.to_string(), "build_bot.png".to_string());
|
||||||
|
|
||||||
let event_loop_handle = thread::spawn(move || {
|
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));
|
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) {
|
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 {
|
match event_type {
|
||||||
Event::ContactCreated => {
|
Event::ContactCreated => {
|
||||||
if event.data["ConnectionState"] == "Authenticated" {
|
if event.data["ConnectionState"] == "Authenticated" {
|
||||||
|
|
Loading…
Reference in New Issue