2024-08-22 12:03:56 +00:00
|
|
|
// Copyright (c) 2024 The crate authors
|
2019-03-21 17:41:29 +00:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
2024-08-22 12:03:56 +00:00
|
|
|
use env_logger;
|
|
|
|
use log::debug;
|
2019-03-21 17:41:29 +00:00
|
|
|
use std::env::args;
|
2023-06-01 09:58:04 +00:00
|
|
|
use std::str::FromStr;
|
2024-08-22 12:03:56 +00:00
|
|
|
use tokio_xmpp::{minidom::Element, parsers::message::Message};
|
|
|
|
use xmpp::{
|
|
|
|
jid::{BareJid, Jid},
|
|
|
|
stream::{IqRequest, IqRequestType, NonTransactional},
|
|
|
|
ClientBuilder, ClientFeature, ClientType,
|
|
|
|
};
|
2019-03-21 17:41:29 +00:00
|
|
|
|
2020-03-08 18:57:59 +00:00
|
|
|
#[tokio::main]
|
2024-08-22 12:03:56 +00:00
|
|
|
async fn main() -> Result<(), Option<xmpp::Error>> {
|
2020-03-13 22:22:10 +00:00
|
|
|
env_logger::init();
|
|
|
|
|
2019-03-21 17:41:29 +00:00
|
|
|
let args: Vec<String> = args().collect();
|
2019-07-25 13:11:39 +00:00
|
|
|
if args.len() != 3 {
|
|
|
|
println!("Usage: {} <jid> <password>", args[0]);
|
2020-03-08 18:57:59 +00:00
|
|
|
return Err(None);
|
2019-03-21 17:41:29 +00:00
|
|
|
}
|
2023-06-01 09:58:04 +00:00
|
|
|
|
|
|
|
let jid = BareJid::from_str(&args[1]).expect(&format!("Invalid JID: {}", &args[1]));
|
2019-03-21 17:41:29 +00:00
|
|
|
let password = &args[2];
|
|
|
|
|
|
|
|
// Client instance
|
2024-08-22 12:03:56 +00:00
|
|
|
let client = ClientBuilder::new(jid, password)
|
2019-03-21 17:41:29 +00:00
|
|
|
.set_client(ClientType::Bot, "xmpp-rs")
|
|
|
|
.set_website("https://gitlab.com/xmpp-rs/xmpp-rs")
|
2019-07-25 13:11:39 +00:00
|
|
|
.set_default_nick("bot")
|
2024-08-22 12:03:56 +00:00
|
|
|
.build()?;
|
2019-03-21 17:41:29 +00:00
|
|
|
|
2024-08-22 12:03:56 +00:00
|
|
|
println!("FOO0: {:?}", client);
|
|
|
|
|
|
|
|
let elem: Element = r#"<ping xmlns="urn:xmpp:ping"/>"#.parse().unwrap();
|
|
|
|
let req = IqRequest {
|
|
|
|
to: Jid::new("bouah.net").unwrap(),
|
|
|
|
data: IqRequestType::Get(elem),
|
|
|
|
};
|
|
|
|
let resp = client.send_iq(req).await.unwrap();
|
|
|
|
println!("Response: {:?}", resp);
|
|
|
|
let receiver = client.misc_receiver().await;
|
|
|
|
|
|
|
|
loop {
|
|
|
|
match receiver.lock().await.recv().await {
|
|
|
|
Some(NonTransactional::Presence(presence)) => debug!("Presence: {:?}", presence),
|
|
|
|
Some(NonTransactional::Message(message)) => {
|
|
|
|
debug!("Message: {:?}", message);
|
|
|
|
let (lang, body) =
|
|
|
|
if let Some((lang, body)) = message.get_best_body(vec!["en", "fr"]) {
|
|
|
|
(lang.to_string(), format!("Echo: {}", body.0))
|
|
|
|
} else {
|
|
|
|
(format!("en"), format!("Echo?!"))
|
|
|
|
};
|
|
|
|
let echo_jid = message.from.unwrap();
|
|
|
|
let _echo = Message::groupchat(echo_jid).with_body(lang, body);
|
|
|
|
// client.send_message(echo).await?
|
2019-10-22 23:32:41 +00:00
|
|
|
}
|
2024-08-22 12:03:56 +00:00
|
|
|
None => break,
|
2019-03-21 17:41:29 +00:00
|
|
|
}
|
2020-03-08 18:57:59 +00:00
|
|
|
}
|
2019-03-21 17:41:29 +00:00
|
|
|
|
2020-03-08 18:57:59 +00:00
|
|
|
Ok(())
|
2019-03-21 17:41:29 +00:00
|
|
|
}
|