Add command to rejoin rooms
Signed-off-by: pep <pep@bouah.net>
This commit is contained in:
parent
478780d063
commit
ea62a41b78
2 changed files with 43 additions and 11 deletions
51
src/bot.rs
51
src/bot.rs
|
|
@ -21,8 +21,9 @@ use tokio::{signal::ctrl_c, sync::mpsc};
|
|||
use xmpp::jid::{BareJid, Jid, ResourcePart};
|
||||
use xmpp::parsers::message::MessageType;
|
||||
use xmpp::{
|
||||
message::send::RawMessageSettings, muc::room::JoinRoomSettings, Agent, ClientBuilder,
|
||||
ClientFeature, ClientType, Event, RoomNick, Config as AgentConfig,
|
||||
Agent, ClientBuilder, ClientFeature, ClientType, Config as AgentConfig, Event, RoomNick,
|
||||
message::send::RawMessageSettings,
|
||||
muc::room::{JoinRoomSettings, LeaveRoomSettings},
|
||||
};
|
||||
|
||||
pub struct XmppClient {
|
||||
|
|
@ -30,6 +31,7 @@ pub struct XmppClient {
|
|||
agent: Agent,
|
||||
rooms: Vec<BareJid>,
|
||||
nickname: ResourcePart,
|
||||
admins: Vec<BareJid>,
|
||||
}
|
||||
|
||||
impl XmppClient {
|
||||
|
|
@ -38,6 +40,7 @@ impl XmppClient {
|
|||
password: &str,
|
||||
rooms: Vec<BareJid>,
|
||||
nickname: ResourcePart,
|
||||
admins: Vec<BareJid>,
|
||||
) -> XmppClient {
|
||||
let config = AgentConfig {
|
||||
bookmarks_autojoin: false,
|
||||
|
|
@ -56,6 +59,7 @@ impl XmppClient {
|
|||
agent,
|
||||
rooms,
|
||||
nickname,
|
||||
admins,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -65,7 +69,33 @@ impl XmppClient {
|
|||
Event::Online => {
|
||||
self.is_online = true;
|
||||
debug!("XMPP Online");
|
||||
self.join_rooms().await
|
||||
}
|
||||
Event::ChatMessage(_id, bare, message, _timeinfo) => {
|
||||
if !self.admins.contains(&bare) {
|
||||
debug!("Received chat message from {}, not in admins", bare);
|
||||
continue;
|
||||
}
|
||||
|
||||
debug!("Received chat message from {}: {}", bare, message);
|
||||
|
||||
if message == "rejoin" {
|
||||
self.leave_rooms().await;
|
||||
self.join_rooms().await
|
||||
}
|
||||
}
|
||||
Event::Disconnected(e) => {
|
||||
self.is_online = false;
|
||||
debug!("XMPP Disconnected: {e}");
|
||||
}
|
||||
_ => {
|
||||
debug!("XMPP Event not supported")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn join_rooms(&mut self) {
|
||||
for room in &self.rooms {
|
||||
self.agent
|
||||
.join_room(JoinRoomSettings {
|
||||
|
|
@ -77,14 +107,15 @@ impl XmppClient {
|
|||
.await
|
||||
}
|
||||
}
|
||||
Event::Disconnected(e) => {
|
||||
self.is_online = false;
|
||||
debug!("XMPP Disconnected: {e}");
|
||||
}
|
||||
_ => {
|
||||
debug!("XMPP Event not supported")
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn leave_rooms(&mut self) {
|
||||
for room in &self.rooms {
|
||||
self.agent
|
||||
.leave_room(LeaveRoomSettings {
|
||||
room: room.clone(),
|
||||
status: Some(("en", "See you!")),
|
||||
})
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -60,6 +60,7 @@ async fn main() -> Result<(), Error> {
|
|||
config.password.as_str(),
|
||||
config.rooms,
|
||||
config.nickname,
|
||||
config.admins,
|
||||
);
|
||||
|
||||
let xmpp_handle = tokio::task::spawn(async move {
|
||||
|
|
|
|||
Loading…
Reference in a new issue