xmpp: Rename tmp example to hello_bot
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
parent
9543c0985b
commit
f090110227
2 changed files with 38 additions and 138 deletions
|
@ -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(())
|
|
||||||
}
|
|
|
@ -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
|
// 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
|
// 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/.
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
|
use env_logger;
|
||||||
|
use log::debug;
|
||||||
use std::env::args;
|
use std::env::args;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use tokio_xmpp::jid::{BareJid, Jid};
|
use tokio_xmpp::{minidom::Element, parsers::message::Message};
|
||||||
use tokio_xmpp::parsers::message::MessageType;
|
use xmpp::{
|
||||||
use xmpp::{ClientBuilder, ClientFeature, ClientType, Event};
|
jid::{BareJid, Jid},
|
||||||
|
stream::{IqRequest, IqRequestType, NonTransactional},
|
||||||
|
ClientBuilder, ClientFeature, ClientType,
|
||||||
|
};
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Option<()>> {
|
async fn main() -> Result<(), Option<xmpp::Error>> {
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
|
|
||||||
let args: Vec<String> = args().collect();
|
let args: Vec<String> = args().collect();
|
||||||
|
@ -24,75 +29,39 @@ async fn main() -> Result<(), Option<()>> {
|
||||||
let password = &args[2];
|
let password = &args[2];
|
||||||
|
|
||||||
// Client instance
|
// Client instance
|
||||||
let mut client = ClientBuilder::new(jid, password)
|
let client = ClientBuilder::new(jid, password)
|
||||||
.set_client(ClientType::Bot, "xmpp-rs")
|
.set_client(ClientType::Bot, "xmpp-rs")
|
||||||
.set_website("https://gitlab.com/xmpp-rs/xmpp-rs")
|
.set_website("https://gitlab.com/xmpp-rs/xmpp-rs")
|
||||||
.set_default_nick("bot")
|
.set_default_nick("bot")
|
||||||
.enable_feature(ClientFeature::Avatars)
|
.build()?;
|
||||||
.enable_feature(ClientFeature::ContactList)
|
|
||||||
.enable_feature(ClientFeature::JoinRooms)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
while let Some(events) = client.wait_for_events().await {
|
println!("FOO0: {:?}", client);
|
||||||
for event in events {
|
|
||||||
match event {
|
let elem: Element = r#"<ping xmlns="urn:xmpp:ping"/>"#.parse().unwrap();
|
||||||
Event::Online => {
|
let req = IqRequest {
|
||||||
println!("Online.");
|
to: Jid::new("bouah.net").unwrap(),
|
||||||
}
|
data: IqRequestType::Get(elem),
|
||||||
Event::Disconnected(e) => {
|
};
|
||||||
println!("Disconnected because of {}.", e);
|
let resp = client.send_iq(req).await.unwrap();
|
||||||
return Err(None);
|
println!("Response: {:?}", resp);
|
||||||
}
|
let receiver = client.misc_receiver().await;
|
||||||
Event::ContactAdded(contact) => {
|
|
||||||
println!("Contact {} added.", contact.jid);
|
loop {
|
||||||
}
|
match receiver.lock().await.recv().await {
|
||||||
Event::ContactRemoved(contact) => {
|
Some(NonTransactional::Presence(presence)) => debug!("Presence: {:?}", presence),
|
||||||
println!("Contact {} removed.", contact.jid);
|
Some(NonTransactional::Message(message)) => {
|
||||||
}
|
debug!("Message: {:?}", message);
|
||||||
Event::ContactChanged(contact) => {
|
let (lang, body) =
|
||||||
println!("Contact {} changed.", contact.jid);
|
if let Some((lang, body)) = message.get_best_body(vec!["en", "fr"]) {
|
||||||
}
|
(lang.to_string(), format!("Echo: {}", body.0))
|
||||||
Event::ChatMessage(_id, jid, body, time_info) => {
|
} else {
|
||||||
println!("Message from {} at {}: {}", jid, time_info.received, body.0);
|
(format!("en"), format!("Echo?!"))
|
||||||
}
|
};
|
||||||
Event::JoinRoom(jid, conference) => {
|
let echo_jid = message.from.unwrap();
|
||||||
println!("Joining room {} ({:?})…", jid, conference.name);
|
let _echo = Message::groupchat(echo_jid).with_body(lang, body);
|
||||||
client
|
// client.send_message(echo).await?
|
||||||
.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);
|
|
||||||
}
|
|
||||||
_ => (),
|
|
||||||
}
|
}
|
||||||
|
None => break,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue