This commit is contained in:
Dan Ballard 2022-04-09 11:11:01 -07:00
parent 9648bb6310
commit c42d73bb86
2 changed files with 71 additions and 35 deletions

View File

@ -1,4 +1,3 @@
#[derive(Debug)]
pub enum Event {
CwtchStarted,
@ -25,4 +24,4 @@ impl Event {
_ => Event::ErrUnhandled(name.to_string()),
}
}
}
}

View File

@ -8,7 +8,6 @@ use std::fs::read_dir;
use std::path::{Path, PathBuf};
use std::thread;
use serde_json;
use libcwtch;
@ -34,7 +33,9 @@ struct UpdateBot {
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)) {
for entry in
read_dir(Path::new(DIST_DIR)).expect(&format!("could not open '{}' dir", DIST_DIR))
{
let entry = entry.unwrap();
let path: PathBuf = entry.path();
if path.is_dir() {
@ -44,16 +45,26 @@ impl UpdateBot {
}
versions_dirs.sort();
println!("sorted vd: {:?}", versions_dirs);
let latest_version = versions_dirs[versions_dirs.len()-1].clone();
let version: String = latest_version.strip_prefix(DIST_DIR).unwrap().to_str().unwrap().to_string();
let bot = UpdateBot{ versions_dirs: versions_dirs, profile: None, settings: None, latest_version: latest_version, version: version};
let latest_version = versions_dirs[versions_dirs.len() - 1].clone();
let version: String = latest_version
.strip_prefix(DIST_DIR)
.unwrap()
.to_str()
.unwrap()
.to_string();
let bot = UpdateBot {
versions_dirs: versions_dirs,
profile: None,
settings: None,
latest_version: latest_version,
version: version,
};
println!("versions: {:?}\n", bot.versions_dirs);
return bot
return bot;
}
}
fn main() {
// load file, parse version
if !Path::new(DIST_DIR).exists() {
panic!("no '{}' directory with versions to distribute", DIST_DIR)
@ -67,7 +78,6 @@ fn main() {
let ret = cwtch.start_cwtch(BOT_HOME, "");
println!("start_cwtch returned {}", ret);
// approve all friends
// offer newest version if none or now newest (question about os followed by file strasfer)
// for all friends, store offered version as attr
@ -92,10 +102,9 @@ fn main() {
None => {
println!("Creating bot");
cwtch.load_profiles(PASSWORD);
},
}
Some(_) => (),
}
}
Event::UpdateGlobalSettings => {
println!("Loading settings froms {}", &event.data["Data"]);
@ -104,17 +113,21 @@ fn main() {
Err(e) => panic!("invalid json: {:?}", e),
};
settings.ExperimentsEnabled = true;
settings.Experiments.insert(Experiments::FileSharingExperiment.to_key_string(),true);
settings
.Experiments
.insert(Experiments::FileSharingExperiment.to_key_string(), true);
// TODO delete
settings.Experiments.insert(Experiments::ImagePreviewsExperiment.to_key_string(),false);
settings
.Experiments
.insert(Experiments::ImagePreviewsExperiment.to_key_string(), false);
match settings.save(&cwtch) {
Ok(_) => (),
Err(e) => println!("ERROR: could not save settings: {}", e)
Err(e) => println!("ERROR: could not save settings: {}", e),
};
match update_bot.profile.as_ref() {
Some(profile) => {
cwtch.share_file(&profile.handle, -1, "build_bot.png");
},
}
None => (),
};
update_bot.settings = Some(settings);
@ -141,14 +154,17 @@ fn main() {
match update_bot.settings.as_ref() {
Some(_settings) => {
cwtch.share_file(&profile.handle, -1, "build_bot.png");
},
}
None => (),
};
cwtch.set_profile_attribute(&profile.handle, "profile.name", BOT_NAME);
for (_id, conversation) in &profile.conversations {
if conversation.accepted != true {
cwtch.accept_conversation(profile.handle.as_str(), conversation.identifier)
cwtch.accept_conversation(
profile.handle.as_str(),
conversation.identifier,
)
}
}
update_bot.profile = Some(profile);
@ -163,7 +179,8 @@ fn main() {
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 acl: ACL = serde_json::from_str(&event.data["accessControlList"])
.expect("Error parsing conversation");
let conversation = Conversation {
handle: profile_onion.clone(),
@ -182,9 +199,11 @@ fn main() {
match update_bot.profile.as_mut() {
Some(profile) => {
profile.conversations.insert(event.data["RemotePeer"].to_string(), conversation);
profile
.conversations
.insert(event.data["RemotePeer"].to_string(), conversation);
}
None => ()
None => (),
};
update_bot.greet(&cwtch, convo_id);
@ -195,13 +214,17 @@ fn main() {
if event.data["ConnectionState"] == "Authenticated" {
match update_bot.profile.as_ref() {
Some(profile) => {
let conversation = &profile.conversations[&event.data["RemotePeer"]];
let conversation =
&profile.conversations[&event.data["RemotePeer"]];
if conversation.accepted != true {
cwtch.accept_conversation(profile.handle.as_str(), conversation.identifier)
cwtch.accept_conversation(
profile.handle.as_str(),
conversation.identifier,
)
}
update_bot.greet(&cwtch, conversation.identifier);
}
None => ()
None => (),
};
}
}
@ -219,17 +242,17 @@ fn main() {
let mut windows_path = update_bot.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 = update_bot.latest_version.clone();
linux_path.push(format!("cwtch-{}.tar.gz", update_bot.version));
cwtch.share_file(&to, conversation_id, linux_path.to_str().unwrap());
},
}
"macos" => {
let mut mac_path = update_bot.latest_version.clone();
mac_path.push("Cwtch.dmg");
cwtch.share_file(&to, conversation_id, mac_path.to_str().unwrap());
},
}
"android" => {
let mut android_path = update_bot.latest_version.clone();
android_path.push("app-release.apk");
@ -238,11 +261,10 @@ fn main() {
_ => {
update_bot.offer(&cwtch, conversation_id);
}
}
}
Event::ErrUnhandled(err) => eprintln!("unhandled event: {}!", err),
_ => print!("unhandled event: {:?}!", event_type)
_ => print!("unhandled event: {:?}!", event_type),
};
}
});
@ -254,7 +276,11 @@ impl UpdateBot {
pub fn greet(&self, cwtch: &dyn CwtchLib, convo_id: i32) {
match self.profile.as_ref() {
Some(profile) => {
let do_offer = match cwtch.get_conversation_attribute(&profile.handle, convo_id, &format!("local.{}", LAST_OFFERED_KEY)) {
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,
@ -266,23 +292,34 @@ impl UpdateBot {
};
if do_offer {
self.offer(cwtch, convo_id);
cwtch.set_conversation_attribute(&profile.handle, convo_id, LAST_OFFERED_KEY, &self.version);
cwtch.set_conversation_attribute(
&profile.handle,
convo_id,
LAST_OFFERED_KEY,
&self.version,
);
}
}
None => ()
None => (),
};
}
pub fn offer(&self, cwtch: &dyn CwtchLib, convo_id: i32) {
match self.profile.as_ref() {
Some(profile) => {
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 };
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,
};
let response_json =
serde_json::to_string(&response).expect("Error parsing json response");
cwtch.send_message(&profile.handle, convo_id, &response_json);
}
None => ()
None => (),
};
}
}