85 lines
2.1 KiB
Rust
85 lines
2.1 KiB
Rust
// 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 std::str::FromStr;
|
|
|
|
use log::debug;
|
|
use xmpp::{Agent, ClientBuilder, ClientFeature, ClientType, Event};
|
|
use xmpp_parsers::{message::MessageType, BareJid, Jid};
|
|
|
|
pub struct XmppClient {
|
|
is_online: bool,
|
|
agent: Agent,
|
|
}
|
|
|
|
impl XmppClient {
|
|
pub fn new(jid: &str, password: &str) -> 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()
|
|
.unwrap();
|
|
|
|
XmppClient {
|
|
is_online: false,
|
|
agent,
|
|
}
|
|
}
|
|
|
|
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");
|
|
|
|
self.agent
|
|
.join_room(
|
|
BareJid::from_str("chat@xmpp.rs").unwrap(),
|
|
Some(String::from("bot")),
|
|
None,
|
|
"en",
|
|
"Hi there!",
|
|
)
|
|
.await
|
|
}
|
|
_ => {
|
|
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);
|
|
self.agent
|
|
.send_message(
|
|
Jid::Bare(BareJid::from_str("chat@xmpp.rs").unwrap()),
|
|
MessageType::Groupchat,
|
|
"en",
|
|
&display,
|
|
)
|
|
.await
|
|
}
|
|
}
|
|
}
|