132 lines
4.4 KiB
Rust
132 lines
4.4 KiB
Rust
|
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");
|
||
|
}
|