xmpp: handle SIGINT (^C) in hello_bot example
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
parent
23c947d081
commit
da219d4fed
3 changed files with 62 additions and 43 deletions
|
|
@ -16,7 +16,7 @@ edition = "2021"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
chrono = "0.4"
|
chrono = "0.4"
|
||||||
futures = "0.3"
|
futures = "0.3"
|
||||||
tokio = { version = "1", features = ["fs"] }
|
tokio = { version = "1", features = ["fs", "signal"] }
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
reqwest = { version = "0.12", features = ["stream"], default-features = false }
|
reqwest = { version = "0.12", features = ["stream"], default-features = false }
|
||||||
tokio-util = { version = "0.7", features = ["codec"] }
|
tokio-util = { version = "0.7", features = ["codec"] }
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,8 @@ XXXX-YY-ZZ [ RELEASER <admin@localhost> ]
|
||||||
Event::RoomPrivateMessageCorrection signal XEP-0308 message corrections; they're
|
Event::RoomPrivateMessageCorrection signal XEP-0308 message corrections; they're
|
||||||
not checked how old the corrected entry is, which has security concerns (!496)
|
not checked how old the corrected entry is, which has security concerns (!496)
|
||||||
- Agent::new helper method.
|
- Agent::new helper method.
|
||||||
|
- Handle SIGINT, SIGTERM and SIGKILL in hello_bot example to avoid leaving
|
||||||
|
hibernating resources around.
|
||||||
* Fixes:
|
* Fixes:
|
||||||
- Use tokio::sync::RwLock not std::sync::RwLock (!432)
|
- Use tokio::sync::RwLock not std::sync::RwLock (!432)
|
||||||
- Agent::wait_for_events now return Vec<Event> and sets inner tokio_xmpp Client
|
- Agent::wait_for_events now return Vec<Event> and sets inner tokio_xmpp Client
|
||||||
|
|
|
||||||
|
|
@ -7,9 +7,11 @@
|
||||||
use xmpp::{
|
use xmpp::{
|
||||||
jid::BareJid,
|
jid::BareJid,
|
||||||
muc::room::{JoinRoomSettings, RoomMessageSettings},
|
muc::room::{JoinRoomSettings, RoomMessageSettings},
|
||||||
ClientBuilder, ClientFeature, ClientType, Event, RoomNick,
|
Agent, ClientBuilder, ClientFeature, ClientType, Event, RoomNick,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use tokio::signal::ctrl_c;
|
||||||
|
|
||||||
use std::env::args;
|
use std::env::args;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
|
|
@ -56,11 +58,28 @@ async fn main() -> Result<(), Option<()>> {
|
||||||
log::info!("Connecting...");
|
log::info!("Connecting...");
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
for event in client.wait_for_events().await {
|
tokio::select! {
|
||||||
|
events = client.wait_for_events() => {
|
||||||
|
for event in events {
|
||||||
|
let _ = handle_events(&mut client, event, &rooms);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_ = ctrl_c() => {
|
||||||
|
log::info!("Disconnecting...");
|
||||||
|
let _ = client.disconnect().await;
|
||||||
|
break;
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn handle_events(client: &mut Agent, event: Event, rooms: &Vec<BareJid>) {
|
||||||
match event {
|
match event {
|
||||||
Event::Online => {
|
Event::Online => {
|
||||||
log::info!("Online.");
|
log::info!("Online.");
|
||||||
for room in &rooms {
|
for room in rooms {
|
||||||
log::info!("Joining room {} from CLI argument…", room);
|
log::info!("Joining room {} from CLI argument…", room);
|
||||||
client
|
client
|
||||||
.join_room(JoinRoomSettings {
|
.join_room(JoinRoomSettings {
|
||||||
|
|
@ -99,6 +118,4 @@ async fn main() -> Result<(), Option<()>> {
|
||||||
log::debug!("Unimplemented event:\n{:#?}", event);
|
log::debug!("Unimplemented event:\n{:#?}", event);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue