moving to new imp event on_handlers

This commit is contained in:
Dan Ballard 2022-05-19 14:05:04 -07:00
parent 25a40cd5cf
commit 24dae6f3dc
3 changed files with 71 additions and 96 deletions

1
Cargo.lock generated
View File

@ -321,6 +321,7 @@ checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
name = "update_bot"
version = "0.1.0"
dependencies = [
"chrono",
"imp",
"libcwtch",
"serde_json",

View File

@ -9,4 +9,5 @@ edition = "2021"
libcwtch = "0.3.2"
imp = {path = "../imp"}
serde_json = "1.0"
chrono = "0.4.19"

View File

@ -5,12 +5,12 @@ use std::fs::read_dir;
use std::path::{Path, PathBuf};
use std::thread;
use ::imp::event::Event;
use ::imp::imp;
use ::imp::behaviour;
use ::imp::behaviour::BehaviourBuilder;
use ::imp::behaviour::Behaviour;
use ::imp::imp::Imp;
use chrono::{DateTime, FixedOffset};
use libcwtch;
use libcwtch::CwtchLib;
use libcwtch::structs::*;
@ -55,7 +55,7 @@ impl UpdateBot {
.to_str()
.unwrap()
.to_string();
let mut bot = UpdateBot {
let bot = UpdateBot {
versions_dirs: versions_dirs,
latest_version: latest_version,
version: version,
@ -84,109 +84,82 @@ fn main() {
impl UpdateBot {
pub fn greet(&self, cwtch: &dyn CwtchLib, profile_opt: Option<&Profile>, convo_id: i32) {
if let Some(profile) = profile_opt {
let do_offer = match cwtch.get_conversation_attribute(
pub fn greet(&self, cwtch: &dyn CwtchLib, profile: &Profile, convo_id: i32) {
let do_offer = match cwtch.get_conversation_attribute(
&profile.handle,
convo_id,
&format!("local.{}", LAST_OFFERED_KEY),
) {
Ok(ret) => match ret {
Some(last_offered) => last_offered != self.version,
None => true,
},
Err(_) => false
};
if do_offer {
self.offer(cwtch, profile, convo_id);
cwtch.set_conversation_attribute(
&profile.handle,
convo_id,
&format!("local.{}", LAST_OFFERED_KEY),
) {
Ok(ret) => match ret {
Some(last_offered) => last_offered != self.version,
None => true,
},
Err(_) => false
};
if do_offer {
self.offer(cwtch, profile_opt, convo_id);
cwtch.set_conversation_attribute(
&profile.handle,
convo_id,
LAST_OFFERED_KEY,
&self.version,
);
}
LAST_OFFERED_KEY,
&self.version,
);
}
}
pub fn offer(&self, cwtch: &dyn CwtchLib, profile_opt: Option<&Profile>, convo_id: i32) {
if let Some(profile) = profile_opt {
let resp_message = format!(
"Currently offering Cwtch {}\nPlease respond with the OS you would like a package for:\n- Windows\n- Android\n- MacOS\n- Linux",
self.version
);
let response = Message {
o: 1,
d: resp_message,
};
match serde_json::to_string(&response) {
Ok(response_json) => cwtch.send_message(&profile.handle, convo_id, &response_json),
Err(e) => { println!("Error parsing json response: {}", e.to_string()); "".to_string() }
};
}
}
pub fn offer(&self, cwtch: &dyn CwtchLib, profile: &Profile, convo_id: i32) {
let resp_message = format!(
"Currently offering Cwtch {}\nPlease respond with the OS you would like a package for:\n- Windows\n- Android\n- MacOS\n- Linux",
self.version
);
let response = Message {
o: 1,
d: resp_message,
};
match serde_json::to_string(&response) {
Ok(response_json) => cwtch.send_message(&profile.handle, convo_id, &response_json),
Err(e) => { println!("Error parsing json response: {}", e.to_string()); "".to_string() }
};
}
}
impl imp::EventHandler for UpdateBot {
fn handle(&mut self, cwtch: &dyn libcwtch::CwtchLib, profile_opt: Option<&Profile>, event: Event) {
match event {
Event::ContactCreated { data } => {
let convo_id = data["ConversationID"].parse::<i32>().unwrap();
self.greet(cwtch, profile_opt, convo_id);
}
Event::PeerStateChange { data } => {
if data["ConnectionState"] == "Authenticated" {
match profile_opt.as_ref() {
Some(profile) => {
match profile.find_conversation_id_by_handle(data["RemotePeer"].clone()) {
Some(conversation_id) => self.greet(cwtch, profile_opt, conversation_id),
None => {}
}
}
None => (),
};
}
}
Event::NewMessageFromPeer { conversation_id, handle: _, timestamp_received: _, message } => {
match profile_opt {
Some(profile) => {
let from = profile.handle.as_str();
let message_wrapper: Message =
serde_json::from_str(&message).expect("Error parsing message");
let mut message = message_wrapper.d.clone();
message.make_ascii_lowercase();
fn on_contact_online(&self, cwtch: &dyn libcwtch::CwtchLib, profile: &Profile, convo_id: i32) {
self.greet(cwtch, profile, convo_id);
}
match message.as_str() {
"windows" => {
let mut windows_path = self.latest_version.clone();
windows_path.push("cwtch-installer.exe");
cwtch.share_file(&from, conversation_id, windows_path.to_str().unwrap());
}
"linux" => {
let mut linux_path = self.latest_version.clone();
linux_path.push(format!("cwtch-{}.tar.gz", self.version));
cwtch.share_file(&from, conversation_id, linux_path.to_str().unwrap());
}
"macos" => {
let mut mac_path = self.latest_version.clone();
mac_path.push("Cwtch.dmg");
cwtch.share_file(&from, conversation_id, mac_path.to_str().unwrap());
}
"android" => {
let mut android_path = self.latest_version.clone();
android_path.push("app-release.apk");
cwtch.share_file(&from, conversation_id, android_path.to_str().unwrap());
}
_ => {
self.offer(cwtch, profile_opt, conversation_id);
}
}
}
_ => {}
}
fn on_new_message_from_contact(&self, cwtch: &dyn libcwtch::CwtchLib, profile: &Profile, conversation_id: i32, _handle: String, _timestamp_received: DateTime<FixedOffset>, message: String) {
let from = profile.handle.as_str();
let message_wrapper: Message =
serde_json::from_str(&message).expect("Error parsing message");
let mut message = message_wrapper.d.clone();
message.make_ascii_lowercase();
match message.as_str() {
"windows" => {
let mut windows_path = self.latest_version.clone();
windows_path.push("cwtch-installer.exe");
cwtch.share_file(&from, conversation_id, windows_path.to_str().unwrap());
}
Event::ErrUnhandled { name, data: _ } => eprintln!("unhandled event: {}!", name),
_ => ()
};
"linux" => {
let mut linux_path = self.latest_version.clone();
linux_path.push(format!("cwtch-{}.tar.gz", self.version));
cwtch.share_file(&from, conversation_id, linux_path.to_str().unwrap());
}
"macos" => {
let mut mac_path = self.latest_version.clone();
mac_path.push("Cwtch.dmg");
cwtch.share_file(&from, conversation_id, mac_path.to_str().unwrap());
}
"android" => {
let mut android_path = self.latest_version.clone();
android_path.push("app-release.apk");
cwtch.share_file(&from, conversation_id, android_path.to_str().unwrap());
}
_ => {
self.offer(cwtch, profile, conversation_id);
}
}
}
}