Update xmpp dependency to 0.5
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
parent
1aee1cd618
commit
acf22a05c1
3 changed files with 9 additions and 11 deletions
93
src/bot.rs
Normal file
93
src/bot.rs
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
// Copyright (C) 2023-2099 The crate authors.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify it
|
||||
// under the terms of the GNU Affero General Public License as published by the
|
||||
// Free Software Foundation, either version 3 of the License, or (at your
|
||||
// option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
|
||||
// for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::webhook::{format_webhook, WebHook};
|
||||
|
||||
use log::debug;
|
||||
use xmpp::parsers::message::MessageType;
|
||||
use xmpp::{Agent, BareJid, ClientBuilder, ClientFeature, ClientType, Event, Jid};
|
||||
|
||||
pub struct XmppClient {
|
||||
is_online: bool,
|
||||
agent: Agent,
|
||||
rooms: Vec<BareJid>,
|
||||
nickname: String,
|
||||
}
|
||||
|
||||
impl XmppClient {
|
||||
pub fn new(jid: BareJid, password: &str, rooms: Vec<BareJid>, nickname: String) -> XmppClient {
|
||||
let agent = 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::JoinRooms)
|
||||
.build();
|
||||
|
||||
XmppClient {
|
||||
is_online: false,
|
||||
agent,
|
||||
rooms,
|
||||
nickname,
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn next(&mut self) {
|
||||
if let Some(events) = self.agent.wait_for_events().await {
|
||||
for event in events {
|
||||
match event {
|
||||
Event::Online => {
|
||||
self.is_online = true;
|
||||
debug!("XMPP Online");
|
||||
|
||||
for room in &self.rooms {
|
||||
self.agent
|
||||
.join_room(
|
||||
room.clone(),
|
||||
Some(self.nickname.clone()),
|
||||
None,
|
||||
"en",
|
||||
"Hi there!",
|
||||
)
|
||||
.await
|
||||
}
|
||||
}
|
||||
Event::Disconnected => {
|
||||
debug!("XMPP Disconnected");
|
||||
}
|
||||
_ => {
|
||||
debug!("XMPP Event not supported")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn webhook(&mut self, wh: WebHook) {
|
||||
debug!("Received Webhook");
|
||||
if let Some(display) = format_webhook(&wh) {
|
||||
debug!("Webhook: {}", display);
|
||||
for room in &self.rooms {
|
||||
self.agent
|
||||
.send_message(
|
||||
Jid::Bare(room.clone()),
|
||||
MessageType::Groupchat,
|
||||
"en",
|
||||
&display,
|
||||
)
|
||||
.await
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue