2020-03-05 00:25:24 +00:00
|
|
|
use futures::stream::StreamExt;
|
2024-07-24 18:39:27 +00:00
|
|
|
use minidom::Element;
|
2017-07-15 20:35:50 +00:00
|
|
|
use std::env::args;
|
|
|
|
use std::process::exit;
|
2023-06-01 09:58:04 +00:00
|
|
|
use std::str::FromStr;
|
2020-05-29 22:07:36 +00:00
|
|
|
use tokio_xmpp::AsyncClient as Client;
|
2024-07-24 18:39:27 +00:00
|
|
|
use xmpp_parsers::jid::{BareJid, Jid};
|
2018-12-18 18:04:31 +00:00
|
|
|
use xmpp_parsers::message::{Body, Message, MessageType};
|
|
|
|
use xmpp_parsers::presence::{Presence, Show as PresenceShow, Type as PresenceType};
|
2017-06-03 23:37:46 +00:00
|
|
|
|
2020-03-05 00:25:24 +00:00
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
2023-06-05 11:06:52 +00:00
|
|
|
env_logger::init();
|
|
|
|
|
2017-07-15 20:35:50 +00:00
|
|
|
let args: Vec<String> = args().collect();
|
|
|
|
if args.len() != 3 {
|
|
|
|
println!("Usage: {} <jid> <password>", args[0]);
|
|
|
|
exit(1);
|
|
|
|
}
|
2023-06-01 09:58:04 +00:00
|
|
|
let jid = BareJid::from_str(&args[1]).expect(&format!("Invalid JID: {}", &args[1]));
|
2017-07-15 20:35:50 +00:00
|
|
|
let password = &args[2];
|
|
|
|
|
2017-07-13 20:04:54 +00:00
|
|
|
// Client instance
|
2023-06-01 09:58:04 +00:00
|
|
|
let mut client = Client::new(jid, password.to_owned());
|
2020-03-05 00:25:24 +00:00
|
|
|
client.set_reconnect(true);
|
2019-01-29 22:04:42 +00:00
|
|
|
|
2017-07-13 20:04:54 +00:00
|
|
|
// Main loop, processes events
|
2019-01-29 22:04:42 +00:00
|
|
|
let mut wait_for_stream_end = false;
|
2020-03-05 00:25:24 +00:00
|
|
|
let mut stream_ended = false;
|
|
|
|
while !stream_ended {
|
|
|
|
if let Some(event) = client.next().await {
|
|
|
|
println!("event: {:?}", event);
|
|
|
|
if wait_for_stream_end {
|
|
|
|
/* Do nothing */
|
|
|
|
} else if event.is_online() {
|
|
|
|
let jid = event
|
|
|
|
.get_jid()
|
|
|
|
.map(|jid| format!("{}", jid))
|
|
|
|
.unwrap_or("unknown".to_owned());
|
|
|
|
println!("Online at {}", jid);
|
2017-07-01 23:25:22 +00:00
|
|
|
|
2020-03-05 00:25:24 +00:00
|
|
|
let presence = make_presence();
|
|
|
|
client.send_stanza(presence).await.unwrap();
|
|
|
|
} else if let Some(message) = event
|
|
|
|
.into_stanza()
|
|
|
|
.and_then(|stanza| Message::try_from(stanza).ok())
|
|
|
|
{
|
|
|
|
match (message.from, message.bodies.get("")) {
|
|
|
|
(Some(ref from), Some(ref body)) if body.0 == "die" => {
|
|
|
|
println!("Secret die command triggered by {}", from);
|
|
|
|
wait_for_stream_end = true;
|
|
|
|
client.send_end().await.unwrap();
|
|
|
|
}
|
|
|
|
(Some(ref from), Some(ref body)) => {
|
|
|
|
if message.type_ != MessageType::Error {
|
|
|
|
// This is a message we'll echo
|
|
|
|
let reply = make_reply(from.clone(), &body.0);
|
|
|
|
client.send_stanza(reply).await.unwrap();
|
|
|
|
}
|
2018-12-18 18:04:31 +00:00
|
|
|
}
|
2020-03-05 00:25:24 +00:00
|
|
|
_ => {}
|
2018-12-18 18:04:31 +00:00
|
|
|
}
|
2017-07-19 23:07:07 +00:00
|
|
|
}
|
2020-03-05 00:25:24 +00:00
|
|
|
} else {
|
|
|
|
println!("stream_ended");
|
|
|
|
stream_ended = true;
|
2017-06-04 22:42:35 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-03 23:37:46 +00:00
|
|
|
}
|
2017-06-20 19:26:51 +00:00
|
|
|
|
2017-07-13 20:04:54 +00:00
|
|
|
// Construct a <presence/>
|
2017-07-17 18:53:00 +00:00
|
|
|
fn make_presence() -> Element {
|
2017-07-18 19:41:57 +00:00
|
|
|
let mut presence = Presence::new(PresenceType::None);
|
2019-09-08 19:28:44 +00:00
|
|
|
presence.show = Some(PresenceShow::Chat);
|
2018-12-18 18:04:31 +00:00
|
|
|
presence
|
|
|
|
.statuses
|
|
|
|
.insert(String::from("en"), String::from("Echoing messages."));
|
2017-07-18 23:00:17 +00:00
|
|
|
presence.into()
|
2017-07-01 23:25:22 +00:00
|
|
|
}
|
|
|
|
|
2017-07-13 20:04:54 +00:00
|
|
|
// Construct a chat <message/>
|
2017-07-19 23:07:07 +00:00
|
|
|
fn make_reply(to: Jid, body: &str) -> Element {
|
|
|
|
let mut message = Message::new(Some(to));
|
2017-08-14 22:27:40 +00:00
|
|
|
message.bodies.insert(String::new(), Body(body.to_owned()));
|
2017-07-18 23:00:17 +00:00
|
|
|
message.into()
|
2017-06-20 19:26:51 +00:00
|
|
|
}
|