starting to extend echobot example into single loading bot

This commit is contained in:
Dan Ballard 2022-02-23 20:57:20 -08:00
commit 6764531cdd
5 changed files with 440 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
/target
.idea
cwtch_dist

274
Cargo.lock generated Normal file
View File

@ -0,0 +1,274 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "block-buffer"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1d36a02058e76b040de25a4464ba1c80935655595b661505c8b39b664828b95"
dependencies = [
"generic-array",
]
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "cpufeatures"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95059428f66df56b63431fdb4e1947ed2190586af5c5a8a8b71122bdf5a7f469"
dependencies = [
"libc",
]
[[package]]
name = "crypto-common"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "683d6b536309245c849479fba3da410962a43ed8e51c26b729208ec0ac2798d0"
dependencies = [
"generic-array",
]
[[package]]
name = "darling"
version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d0d720b8683f8dd83c65155f0530560cba68cd2bf395f6513a483caee57ff7f4"
dependencies = [
"darling_core",
"darling_macro",
]
[[package]]
name = "darling_core"
version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a340f241d2ceed1deb47ae36c4144b2707ec7dd0b649f894cb39bb595986324"
dependencies = [
"fnv",
"ident_case",
"proc-macro2",
"quote",
"strsim",
"syn",
]
[[package]]
name = "darling_macro"
version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72c41b3b7352feb3211a0d743dc5700a4e3b60f51bd2b368892d1e0f9a95f44b"
dependencies = [
"darling_core",
"quote",
"syn",
]
[[package]]
name = "digest"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b697d66081d42af4fba142d56918a3cb21dc8eb63372c6b85d14f44fb9c5979b"
dependencies = [
"block-buffer",
"crypto-common",
"generic-array",
]
[[package]]
name = "fnv"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
[[package]]
name = "generic-array"
version = "0.14.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd48d33ec7f05fbfa152300fdad764757cbded343c1aa1cff2fbaf4134851803"
dependencies = [
"typenum",
"version_check",
]
[[package]]
name = "hex-literal"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ebdb29d2ea9ed0083cd8cece49bbd968021bd99b0849edb4a9a7ee0fdf6a4e0"
[[package]]
name = "ident_case"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
[[package]]
name = "itoa"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35"
[[package]]
name = "libc"
version = "0.2.112"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b03d17f364a3a042d5e5d46b053bbbf82c92c9430c592dd4c064dc6ee997125"
[[package]]
name = "libcwtch"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a441fe0922d39eaa632dad730ad587c52d682e0a394c35be335e93a7f7ff557a"
dependencies = [
"hex-literal",
"libc",
"serde",
"serde_json",
"serde_with",
"sha2",
]
[[package]]
name = "proc-macro2"
version = "1.0.36"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029"
dependencies = [
"unicode-xid",
]
[[package]]
name = "quote"
version = "1.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "47aa80447ce4daf1717500037052af176af5d38cc3e571d9ec1c7353fc10c87d"
dependencies = [
"proc-macro2",
]
[[package]]
name = "rustversion"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2cc38e8fa666e2de3c4aba7edeb5ffc5246c1c2ed0e3d17e560aeeba736b23f"
[[package]]
name = "ryu"
version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73b4b750c782965c211b42f022f59af1fbceabdd026623714f104152f1ec149f"
[[package]]
name = "serde"
version = "1.0.133"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "97565067517b60e2d1ea8b268e59ce036de907ac523ad83a0475da04e818989a"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.133"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed201699328568d8d08208fdd080e3ff594e6c422e438b6705905da01005d537"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.75"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c059c05b48c5c0067d4b4b2b4f0732dd65feb52daf7e0ea09cd87e7dadc1af79"
dependencies = [
"itoa",
"ryu",
"serde",
]
[[package]]
name = "serde_with"
version = "1.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ad6056b4cb69b6e43e3a0f055def223380baecc99da683884f205bf347f7c4b3"
dependencies = [
"rustversion",
"serde",
"serde_with_macros",
]
[[package]]
name = "serde_with_macros"
version = "1.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "12e47be9471c72889ebafb5e14d5ff930d89ae7a67bbdb5f8abb564f845a927e"
dependencies = [
"darling",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "sha2"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99c3bd8169c58782adad9290a9af5939994036b76187f7b4f0e6de91dbbfc0ec"
dependencies = [
"cfg-if",
"cpufeatures",
"digest",
]
[[package]]
name = "strsim"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "syn"
version = "1.0.85"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a684ac3dcd8913827e18cd09a68384ee66c1de24157e3c556c9ab16d85695fb7"
dependencies = [
"proc-macro2",
"quote",
"unicode-xid",
]
[[package]]
name = "typenum"
version = "1.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcf81ac59edc17cc8697ff311e8f5ef2d99fcbd9817b34cec66f90b6c3dfd987"
[[package]]
name = "unicode-xid"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
[[package]]
name = "update_bot"
version = "0.1.0"
dependencies = [
"libcwtch",
"serde_json",
]
[[package]]
name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"

11
Cargo.toml Normal file
View File

@ -0,0 +1,11 @@
[package]
name = "update_bot"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
libcwtch = "0.2.0"
serde_json = "1.0"

21
src/event.rs Normal file
View File

@ -0,0 +1,21 @@
pub enum Event {
CwtchStarted,
NewPeer,
NewMessageFromPeer,
AppError,
ErrUnhandled(String),
}
impl Event {
pub fn new(name: &str) -> Self {
match name {
"CwtchStarted" => Event::CwtchStarted,
"NewPeer" => Event::NewPeer,
"NewMessageFromPeer" => Event::NewMessageFromPeer,
"AppError" => Event::AppError,
_ => Event::ErrUnhandled(name.to_string()),
}
}
}

131
src/main.rs Normal file
View File

@ -0,0 +1,131 @@
mod event;
extern crate core;
use event::Event;
use std::fs::read_dir;
use std::path::{Path, PathBuf};
use std::thread;
use serde_json;
use libcwtch;
use libcwtch::structs::*;
use libcwtch::CwtchLib;
const DIST_DIR: &str = "cwtch_dist";
const BOT_HOME: &str = "~/.cwtch/bots/update_bot";
const PASSWORD: &str = "be gay do crime";
struct UpdateBot {
profile: Option<Profile>,
versions_dirs: Vec<PathBuf>,
}
impl UpdateBot {
pub fn new() -> Self {
let mut version_dirs = vec![];
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() {
println!("version: {}", path.to_str().unwrap());
version_dirs.push(path);
}
}
let bot = UpdateBot{ versions_dirs: version_dirs, profile: None};
println!("versions: {:?}\n", bot.versions_dirs);
return bot
}
}
fn main() {
// load file, parse version
if !Path::new(DIST_DIR).exists() {
panic!("no '{}' directory with versions to distribute", DIST_DIR)
}
let mut update_bot = UpdateBot::new();
// make cwtch bot
let cwtch = libcwtch::new_cwtchlib_go();
println!("start_cwtch");
let ret = cwtch.start_cwtch(BOT_HOME, "");
println!("start_cwtch returned {}", ret);
// approve all friends
// offer newst version if none or now newest (question about os followed by file strasfer)
// for all friends, store offered version as attr
// respond to simple commands, include links, help info
let event_loop_handle = thread::spawn(move || {
let mut initialized: bool = false;
loop {
let event_str = cwtch.get_appbus_event();
println!("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 update_bot.profile {
None => {
println!("Creating bot");
cwtch.load_profiles(PASSWORD);
},
Some(_) => (),
}
}
Event::NewPeer => {
println!(
"\n***** {} at {} *****\n",
event.data["name"], event.data["Identity"]
);
// process json for profile, contacts 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);
update_bot.profile = Some(profile);
}
Event::AppError => {
if initialized && event.data["Error"] == "Loaded 0 profiles" {
cwtch.create_profile("Echobot", PASSWORD);
}
}
Event::NewMessageFromPeer => {
let to = &event.data["ProfileOnion"];
let conversation_id = event.data["ConversationID"].parse::<i32>().unwrap();
let message: Message =
serde_json::from_str(&event.data["Data"]).expect("Error parsing message");
let response = Message { o: 1, d: message.d };
let response_json =
serde_json::to_string(&response).expect("Error parsing json response");
cwtch.send_message(&to, conversation_id, &response_json);
}
Event::ErrUnhandled(err) => eprintln!("unhandled event: {}!", err),
};
}
});
event_loop_handle.join().expect("Error running event loop");
}