Connect to XMPP, join room, send message
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
parent
36673bd5bf
commit
0cba90dd68
5 changed files with 257 additions and 19 deletions
85
src/xmpp.rs
Normal file
85
src/xmpp.rs
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
// 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
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue