updates from imp and fix a panic on malformed message

This commit is contained in:
Dan Ballard 2022-04-27 15:46:24 -07:00
parent 32931f54d6
commit 0aaf126be6
2 changed files with 138 additions and 55 deletions

78
Cargo.lock generated
View File

@ -2,6 +2,12 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "autocfg"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "block-buffer"
version = "0.10.0"
@ -17,6 +23,19 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "chrono"
version = "0.4.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73"
dependencies = [
"libc",
"num-integer",
"num-traits",
"time",
"winapi",
]
[[package]]
name = "cpufeatures"
version = "0.2.1"
@ -113,6 +132,7 @@ checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
name = "imp"
version = "0.1.0"
dependencies = [
"chrono",
"libcwtch",
"serde_json",
]
@ -143,6 +163,25 @@ dependencies = [
"sha2",
]
[[package]]
name = "num-integer"
version = "0.1.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db"
dependencies = [
"autocfg",
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290"
dependencies = [
"autocfg",
]
[[package]]
name = "proc-macro2"
version = "1.0.36"
@ -255,6 +294,17 @@ dependencies = [
"unicode-xid",
]
[[package]]
name = "time"
version = "0.1.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255"
dependencies = [
"libc",
"wasi",
"winapi",
]
[[package]]
name = "typenum"
version = "1.15.0"
@ -281,3 +331,31 @@ name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "wasi"
version = "0.10.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"

View File

@ -1,18 +1,20 @@
extern crate core;
use ::imp::event::Event;
use ::imp::imp;
use std::borrow::{Borrow, BorrowMut};
use std::collections::HashMap;
use std::fs::read_dir;
use std::path::{Path, PathBuf};
use std::thread;
use ::imp::event::Event;
use ::imp::imp;
use ::imp::imp::EventHandler;
use libcwtch;
use libcwtch::CwtchLib;
use libcwtch::structs::*;
use serde_json;
use libcwtch;
use libcwtch::structs::*;
use libcwtch::CwtchLib;
use crate::imp::{Behaviour, Imp, BehaviourBuilder};
use crate::imp::{Behaviour, BehaviourBuilder, Imp};
const DIST_DIR: &str = "cwtch_dist";
const BOT_HOME: &str = "~/.cwtch/bots/update_bot";
@ -32,7 +34,7 @@ impl UpdateBot {
pub fn new() -> Self {
let mut versions_dirs = vec![];
for entry in
read_dir(Path::new(DIST_DIR)).expect(&format!("could not open '{}' dir", DIST_DIR))
read_dir(Path::new(DIST_DIR)).expect(&format!("could not open '{}' dir", DIST_DIR))
{
let entry = entry.unwrap();
let path: PathBuf = entry.path();
@ -53,7 +55,7 @@ impl UpdateBot {
.to_str()
.unwrap()
.to_string();
let bot = UpdateBot {
let mut bot = UpdateBot {
versions_dirs: versions_dirs,
latest_version: latest_version,
version: version,
@ -74,10 +76,10 @@ fn main() {
let event_loop_handle = thread::spawn(move || {
let mut bot = Imp::spawn(behaviour, PASSWORD.to_string(), BOT_HOME.to_string());
bot.event_loop(Box::new(update_bot));
bot.event_loop::<UpdateBot>(update_bot.borrow_mut());
});
event_loop_handle.join().expect("Error running event loop");
event_loop_handle.join().expect("Error running event loop");
}
@ -105,7 +107,7 @@ impl UpdateBot {
);
}
}
}
}
pub fn offer(&self, cwtch: &dyn CwtchLib, profile_opt: Option<&Profile>, convo_id: i32) {
if let Some(profile) = profile_opt {
@ -118,28 +120,27 @@ impl UpdateBot {
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()),
}
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(&self, cwtch: &dyn CwtchLib, profile_opt: Option<&Profile>, event: CwtchEvent) {
let event_type = Event::from(event.event_type.as_str());
match event_type {
Event::ContactCreated => {
let convo_id = event.data["ConversationID"].parse::<i32>().unwrap();
fn handle(&mut self, cwtch: &dyn 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 => {
if event.data["ConnectionState"] == "Authenticated" {
Event::PeerStateChange { data } => {
if data["ConnectionState"] == "Authenticated" {
match profile_opt.as_ref() {
Some(profile) => {
if profile.conversations.contains_key(&event.data["RemotePeer"]) {
if profile.conversations.contains_key(&data["RemotePeer"]) {
let conversation =
&profile.conversations[&event.data["RemotePeer"]];
&profile.conversations[&data["RemotePeer"]];
self.greet(cwtch, profile_opt, conversation.identifier);
}
}
@ -147,41 +148,45 @@ impl imp::EventHandler for UpdateBot {
};
}
}
Event::NewMessageFromPeer => {
let to = &event.data["ProfileOnion"];
let conversation_id = event.data["ConversationID"].parse::<i32>().unwrap();
let message_wrapper: Message =
serde_json::from_str(&event.data["Data"]).expect("Error parsing message");
let mut message = message_wrapper.d.clone();
message.make_ascii_lowercase();
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() {
"windows" => {
let mut windows_path = self.latest_version.clone();
windows_path.push("cwtch-installer.exe");
cwtch.share_file(&to, 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(&to, conversation_id, linux_path.to_str().unwrap());
}
"macos" => {
let mut mac_path = self.latest_version.clone();
mac_path.push("Cwtch.dmg");
cwtch.share_file(&to, 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(&to, conversation_id, android_path.to_str().unwrap());
}
_ => {
self.offer( cwtch, profile_opt, conversation_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);
}
}
}
_ => {}
}
}
Event::ErrUnhandled(err) => eprintln!("unhandled event: {}!", err),
Event::ErrUnhandled { name, data } => eprintln!("unhandled event: {}!", name),
_ => ()
};
}