xmpp: Rename tmp example to hello_bot

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
Maxime “pep” Buquet 2024-08-22 14:03:56 +02:00
parent 9543c0985b
commit f090110227
Signed by: pep
GPG key ID: DEDA74AEECA9D0F2
2 changed files with 38 additions and 138 deletions

View file

@ -1,69 +0,0 @@
// Copyright (c) 2024 The crate authors
//
// 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/.
use env_logger;
use log::debug;
use std::env::args;
use std::str::FromStr;
use tokio_xmpp::{minidom::Element, parsers::message::Message};
use xmpp::{
jid::{BareJid, Jid},
stream::{IqRequest, IqRequestType, NonTransactional},
ClientBuilder, ClientFeature, ClientType,
};
#[tokio::main]
async fn main() -> Result<(), Option<xmpp::Error>> {
env_logger::init();
let args: Vec<String> = args().collect();
if args.len() != 3 {
println!("Usage: {} <jid> <password>", args[0]);
return Err(None);
}
let jid = BareJid::from_str(&args[1]).expect(&format!("Invalid JID: {}", &args[1]));
let password = &args[2];
// Client instance
let client = ClientBuilder::new(jid, password)
.set_client(ClientType::Bot, "xmpp-rs")
.set_website("https://gitlab.com/xmpp-rs/xmpp-rs")
.set_default_nick("bot")
.build()?;
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?
}
None => break,
}
}
Ok(())
}

View file

@ -1,17 +1,22 @@
// Copyright (c) 2019 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
// Copyright (c) 2024 The crate authors
//
// 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/.
use env_logger;
use log::debug;
use std::env::args;
use std::str::FromStr;
use tokio_xmpp::jid::{BareJid, Jid};
use tokio_xmpp::parsers::message::MessageType;
use xmpp::{ClientBuilder, ClientFeature, ClientType, Event};
use tokio_xmpp::{minidom::Element, parsers::message::Message};
use xmpp::{
jid::{BareJid, Jid},
stream::{IqRequest, IqRequestType, NonTransactional},
ClientBuilder, ClientFeature, ClientType,
};
#[tokio::main]
async fn main() -> Result<(), Option<()>> {
async fn main() -> Result<(), Option<xmpp::Error>> {
env_logger::init();
let args: Vec<String> = args().collect();
@ -24,75 +29,39 @@ async fn main() -> Result<(), Option<()>> {
let password = &args[2];
// Client instance
let mut client = ClientBuilder::new(jid, password)
let client = ClientBuilder::new(jid, password)
.set_client(ClientType::Bot, "xmpp-rs")
.set_website("https://gitlab.com/xmpp-rs/xmpp-rs")
.set_default_nick("bot")
.enable_feature(ClientFeature::Avatars)
.enable_feature(ClientFeature::ContactList)
.enable_feature(ClientFeature::JoinRooms)
.build();
.build()?;
while let Some(events) = client.wait_for_events().await {
for event in events {
match event {
Event::Online => {
println!("Online.");
}
Event::Disconnected(e) => {
println!("Disconnected because of {}.", e);
return Err(None);
}
Event::ContactAdded(contact) => {
println!("Contact {} added.", contact.jid);
}
Event::ContactRemoved(contact) => {
println!("Contact {} removed.", contact.jid);
}
Event::ContactChanged(contact) => {
println!("Contact {} changed.", contact.jid);
}
Event::ChatMessage(_id, jid, body, time_info) => {
println!("Message from {} at {}: {}", jid, time_info.received, body.0);
}
Event::JoinRoom(jid, conference) => {
println!("Joining room {} ({:?})…", jid, conference.name);
client
.join_room(
jid,
conference.nick,
conference.password,
"en",
"Yet another bot!",
)
.await;
}
Event::LeaveRoom(jid) => {
println!("Leaving room {}", jid);
}
Event::LeaveAllRooms => {
println!("Leaving all rooms…");
}
Event::RoomJoined(jid) => {
println!("Joined room {}.", jid);
client
.send_message(Jid::from(jid), MessageType::Groupchat, "en", "Hello world!")
.await;
}
Event::RoomLeft(jid) => {
println!("Left room {}.", jid);
}
Event::RoomMessage(_id, jid, nick, body, time_info) => {
println!(
"Message in room {} from {} at {}: {}",
jid, nick, time_info.received, body.0
);
}
Event::AvatarRetrieved(jid, path) => {
println!("Received avatar for {} in {}.", jid, path);
}
_ => (),
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?
}
None => break,
}
}