moving to new imp event on_handlers
This commit is contained in:
parent
25a40cd5cf
commit
24dae6f3dc
|
@ -321,6 +321,7 @@ checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
|
||||||
name = "update_bot"
|
name = "update_bot"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"chrono",
|
||||||
"imp",
|
"imp",
|
||||||
"libcwtch",
|
"libcwtch",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
|
|
|
@ -9,4 +9,5 @@ edition = "2021"
|
||||||
libcwtch = "0.3.2"
|
libcwtch = "0.3.2"
|
||||||
imp = {path = "../imp"}
|
imp = {path = "../imp"}
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
|
chrono = "0.4.19"
|
||||||
|
|
||||||
|
|
165
src/main.rs
165
src/main.rs
|
@ -5,12 +5,12 @@ use std::fs::read_dir;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::thread;
|
use std::thread;
|
||||||
|
|
||||||
use ::imp::event::Event;
|
|
||||||
use ::imp::imp;
|
use ::imp::imp;
|
||||||
use ::imp::behaviour;
|
use ::imp::behaviour;
|
||||||
use ::imp::behaviour::BehaviourBuilder;
|
use ::imp::behaviour::BehaviourBuilder;
|
||||||
use ::imp::behaviour::Behaviour;
|
use ::imp::behaviour::Behaviour;
|
||||||
use ::imp::imp::Imp;
|
use ::imp::imp::Imp;
|
||||||
|
use chrono::{DateTime, FixedOffset};
|
||||||
use libcwtch;
|
use libcwtch;
|
||||||
use libcwtch::CwtchLib;
|
use libcwtch::CwtchLib;
|
||||||
use libcwtch::structs::*;
|
use libcwtch::structs::*;
|
||||||
|
@ -55,7 +55,7 @@ impl UpdateBot {
|
||||||
.to_str()
|
.to_str()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.to_string();
|
.to_string();
|
||||||
let mut bot = UpdateBot {
|
let bot = UpdateBot {
|
||||||
versions_dirs: versions_dirs,
|
versions_dirs: versions_dirs,
|
||||||
latest_version: latest_version,
|
latest_version: latest_version,
|
||||||
version: version,
|
version: version,
|
||||||
|
@ -84,109 +84,82 @@ fn main() {
|
||||||
|
|
||||||
|
|
||||||
impl UpdateBot {
|
impl UpdateBot {
|
||||||
pub fn greet(&self, cwtch: &dyn CwtchLib, profile_opt: Option<&Profile>, convo_id: i32) {
|
pub fn greet(&self, cwtch: &dyn CwtchLib, profile: &Profile, convo_id: i32) {
|
||||||
if let Some(profile) = profile_opt {
|
let do_offer = match cwtch.get_conversation_attribute(
|
||||||
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,
|
&profile.handle,
|
||||||
convo_id,
|
convo_id,
|
||||||
&format!("local.{}", LAST_OFFERED_KEY),
|
LAST_OFFERED_KEY,
|
||||||
) {
|
&self.version,
|
||||||
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,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn offer(&self, cwtch: &dyn CwtchLib, profile_opt: Option<&Profile>, convo_id: i32) {
|
pub fn offer(&self, cwtch: &dyn CwtchLib, profile: &Profile, convo_id: i32) {
|
||||||
if let Some(profile) = profile_opt {
|
let resp_message = format!(
|
||||||
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",
|
||||||
"Currently offering Cwtch {}\nPlease respond with the OS you would like a package for:\n- Windows\n- Android\n- MacOS\n- Linux",
|
self.version
|
||||||
self.version
|
);
|
||||||
);
|
let response = Message {
|
||||||
let response = Message {
|
o: 1,
|
||||||
o: 1,
|
d: resp_message,
|
||||||
d: resp_message,
|
};
|
||||||
};
|
match serde_json::to_string(&response) {
|
||||||
match serde_json::to_string(&response) {
|
Ok(response_json) => cwtch.send_message(&profile.handle, convo_id, &response_json),
|
||||||
Ok(response_json) => cwtch.send_message(&profile.handle, convo_id, &response_json),
|
Err(e) => { println!("Error parsing json response: {}", e.to_string()); "".to_string() }
|
||||||
Err(e) => { println!("Error parsing json response: {}", e.to_string()); "".to_string() }
|
};
|
||||||
};
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl imp::EventHandler for UpdateBot {
|
impl imp::EventHandler for UpdateBot {
|
||||||
fn handle(&mut self, cwtch: &dyn libcwtch::CwtchLib, profile_opt: Option<&Profile>, event: Event) {
|
fn on_contact_online(&self, cwtch: &dyn libcwtch::CwtchLib, profile: &Profile, convo_id: i32) {
|
||||||
match event {
|
self.greet(cwtch, profile, convo_id);
|
||||||
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();
|
|
||||||
|
|
||||||
match message.as_str() {
|
fn on_new_message_from_contact(&self, cwtch: &dyn libcwtch::CwtchLib, profile: &Profile, conversation_id: i32, _handle: String, _timestamp_received: DateTime<FixedOffset>, message: String) {
|
||||||
"windows" => {
|
let from = profile.handle.as_str();
|
||||||
let mut windows_path = self.latest_version.clone();
|
let message_wrapper: Message =
|
||||||
windows_path.push("cwtch-installer.exe");
|
serde_json::from_str(&message).expect("Error parsing message");
|
||||||
cwtch.share_file(&from, conversation_id, windows_path.to_str().unwrap());
|
let mut message = message_wrapper.d.clone();
|
||||||
}
|
message.make_ascii_lowercase();
|
||||||
"linux" => {
|
|
||||||
let mut linux_path = self.latest_version.clone();
|
match message.as_str() {
|
||||||
linux_path.push(format!("cwtch-{}.tar.gz", self.version));
|
"windows" => {
|
||||||
cwtch.share_file(&from, conversation_id, linux_path.to_str().unwrap());
|
let mut windows_path = self.latest_version.clone();
|
||||||
}
|
windows_path.push("cwtch-installer.exe");
|
||||||
"macos" => {
|
cwtch.share_file(&from, conversation_id, windows_path.to_str().unwrap());
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue