Add xmpp-rs as a dep

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
Maxime “pep” Buquet 2022-07-24 18:56:48 +02:00
parent 80b8efce6f
commit 82019d17d9
Signed by: pep
GPG key ID: DEDA74AEECA9D0F2
3 changed files with 60 additions and 18 deletions

View file

@ -13,5 +13,14 @@ dbus-tokio = "0.7.5"
tokio = { version = "1.19.2", features = [ "time", "macros", "rt-multi-thread" ] }
futures = "0.3"
futures-util = "0.3"
xmpp = "0.3"
xmpp-parsers = "0.19"
log = "0.4"
env_logger = "0.8"
[patch.crates-io]
jid = { path = "../xmpp-rs/jid" }
minidom = { path = "../xmpp-rs/minidom" }
tokio-xmpp = { path = "../xmpp-rs/tokio-xmpp" }
xmpp = { path = "../xmpp-rs/xmpp" }
xmpp-parsers = { path = "../xmpp-rs/parsers" }

View file

@ -19,16 +19,21 @@ use std::convert::From;
use std::error::Error as StdError;
use mmdbus::dbus;
use xmpp_parsers::JidParseError;
#[derive(Debug)]
pub enum Error {
DBusError(dbus::Error),
InvalidArgs,
JidParseError(JidParseError),
}
impl StdError for Error {
fn cause(&self) -> Option<&dyn StdError> {
match self {
Error::DBusError(e) => Some(e),
Error::InvalidArgs => None,
Error::JidParseError(e) => Some(e),
}
}
}
@ -37,6 +42,8 @@ impl std::fmt::Display for Error {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Error::DBusError(e) => write!(fmt, "dbus error: {}", e),
Error::InvalidArgs => write!(fmt, "invalid arguments"),
Error::JidParseError(e) => write!(fmt, "jid parse error: {}", e),
}
}
}
@ -46,3 +53,9 @@ impl From<dbus::Error> for Error {
Error::DBusError(err)
}
}
impl From<JidParseError> for Error {
fn from(err: JidParseError) -> Error {
Error::JidParseError(err)
}
}

View file

@ -21,10 +21,12 @@ use mm::modem::Modem as ModemAccess;
use std::fmt;
use std::sync::Arc;
use std::str::FromStr;
use std::env::args;
use futures_util::stream::StreamExt;
use env_logger;
use futures_util::StreamExt;
use tokio::{
task::JoinHandle,
time::Duration,
@ -43,6 +45,12 @@ use mmdbus::{
use dbus_tokio::connection;
use xmpp::{ClientBuilder, ClientType};
use xmpp_parsers::{
Jid, BareJid,
message::MessageType,
};
// The following source has been used as an example:
// https://github.com/soerenmeier/linux-info/blob/master/src/network/modem_manager.rs
@ -194,32 +202,44 @@ async fn print_foo(mm: ModemManager) -> Result<(), Error> {
}
#[tokio::main]
pub async fn main() -> Result<(), Box<dyn std::error::Error>> {
pub async fn main() -> Result<(), Error> {
env_logger::init();
let args: Vec<String> = args().collect();
if args.len() != 3 {
println!("Usage: {} <jid> <password>", args[0]);
return Err(Error::InvalidArgs)
}
// ModemManager
let dbus = DBus::connect()?;
let mut interval = tokio::time::interval(Duration::from_secs(2));
let calls = async {
loop {
let mm = ModemManager { dbus: dbus.clone() };
interval.tick().await;
let _ = print_foo(mm).await;
}
};
let mr = MatchRule::new_signal("org.freedesktop.DBus.Properties", "PropertiesChanged");
let (_in_signal, stream) = dbus.conn.add_match(mr).await?.stream();
let stream = stream.for_each(|(foo, (source,)): (_, (String,))| {
println!("Foo {}; {:?}", source, foo);
async {}
});
stream.await;
// XMPP
let jid_str = args[1].as_str();
let jid = Jid::Bare(BareJid::from_str(jid_str).unwrap());
let passwd = args[2].as_str();
futures::join!(stream, calls);
// Client instance
let mut xmpp = ClientBuilder::new(jid_str, passwd)
.set_client(ClientType::Bot, "gateway_mm")
.set_website("https://codeberg.org/pep./gateway_mm")
.build()
.unwrap();
Ok(())
loop {
tokio::select! {
events = xmpp.wait_for_events() => {
println!("Events: {:?}", events);
},
_ = interval.tick() => {
let mm = ModemManager { dbus: dbus.clone() };
let _ = print_foo(mm).await;
},
}
}
}