2021-09-16 02:39:30 +00:00
|
|
|
use std::thread;
|
2021-09-08 07:32:56 +00:00
|
|
|
|
|
|
|
use libcwtch;
|
2021-09-16 02:39:30 +00:00
|
|
|
use libcwtch::structs::*;
|
2021-09-08 07:32:56 +00:00
|
|
|
use libcwtch::CwtchLib;
|
|
|
|
|
|
|
|
fn main() {
|
2021-09-16 02:39:30 +00:00
|
|
|
let bot_home = "example_cwtch_dir";
|
2022-01-15 14:10:55 +00:00
|
|
|
match std::fs::remove_dir_all(&bot_home) {
|
2022-01-16 19:06:30 +00:00
|
|
|
_ => (),
|
2022-01-15 14:10:55 +00:00
|
|
|
}
|
2021-09-16 02:39:30 +00:00
|
|
|
std::fs::create_dir_all(&bot_home).expect("Error creating bot_home directory");
|
2021-09-08 07:32:56 +00:00
|
|
|
|
|
|
|
let cwtch = libcwtch::new_cwtchlib_go();
|
|
|
|
println!("start_cwtch");
|
2021-09-16 02:39:30 +00:00
|
|
|
let ret = cwtch.start_cwtch(bot_home, "");
|
2021-09-08 07:32:56 +00:00
|
|
|
println!("start_cwtch returned {}", ret);
|
|
|
|
|
|
|
|
let event_loop_handle = thread::spawn(move || {
|
|
|
|
loop {
|
|
|
|
let event_str = cwtch.get_appbus_event();
|
|
|
|
println!("event: {}", event_str);
|
|
|
|
|
2021-09-16 02:39:30 +00:00
|
|
|
let event: CwtchEvent =
|
|
|
|
serde_json::from_str(&event_str).expect("Error parsing Cwtch event");
|
2021-09-08 07:32:56 +00:00
|
|
|
match event.event_type.as_str() {
|
|
|
|
"CwtchStarted" => {
|
|
|
|
println!("event CwtchStarted!");
|
|
|
|
println!("Creating bot");
|
|
|
|
cwtch.create_profile("Echobot", "be gay do crime");
|
2021-09-16 02:39:30 +00:00
|
|
|
}
|
2021-09-08 07:32:56 +00:00
|
|
|
"NewPeer" => {
|
2021-09-16 02:39:30 +00:00
|
|
|
println!(
|
|
|
|
"\n***** {} at {} *****\n",
|
|
|
|
event.data["name"], event.data["Identity"]
|
|
|
|
);
|
2021-09-08 07:32:56 +00:00
|
|
|
|
|
|
|
// process json for profile, contacts and servers...else {
|
2022-01-16 19:24:22 +00:00
|
|
|
let profile = match Profile::new(
|
2021-09-16 02:39:30 +00:00
|
|
|
&event.data["Identity"],
|
|
|
|
&event.data["name"],
|
|
|
|
&event.data["picture"],
|
|
|
|
&event.data["ContactsJson"],
|
|
|
|
&event.data["ServerList"],
|
2022-01-16 19:24:22 +00:00
|
|
|
) {
|
|
|
|
Ok(p) => p,
|
|
|
|
Err(e) => panic!("error parsing profile: {}", e)
|
|
|
|
};
|
2021-09-08 07:32:56 +00:00
|
|
|
print!("profile: {:?}", profile);
|
|
|
|
}
|
|
|
|
"NewMessageFromPeer" => {
|
2021-09-16 02:39:30 +00:00
|
|
|
let to = &event.data["ProfileOnion"];
|
2022-01-15 14:10:55 +00:00
|
|
|
let conversation_id = event.data["ConversationID"].parse::<i32>().unwrap();
|
2021-09-16 02:39:30 +00:00
|
|
|
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");
|
2022-01-15 14:10:55 +00:00
|
|
|
cwtch.send_message(&to, conversation_id, &response_json);
|
2021-09-08 07:32:56 +00:00
|
|
|
}
|
2021-09-16 02:39:30 +00:00
|
|
|
_ => eprintln!("unhandled event!"),
|
2021-09-08 07:32:56 +00:00
|
|
|
};
|
2021-09-16 02:39:30 +00:00
|
|
|
}
|
2021-09-08 07:32:56 +00:00
|
|
|
});
|
|
|
|
|
2021-09-16 02:39:30 +00:00
|
|
|
event_loop_handle.join().expect("Error running event loop");
|
2021-09-08 07:32:56 +00:00
|
|
|
}
|