93 lines
2.3 KiB
Rust
93 lines
2.3 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, GitlabHook};
|
|
|
|
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: GitlabHook) {
|
|
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
|
|
}
|
|
}
|
|
}
|
|
}
|