This commit is contained in:
vanten-s 2024-08-28 12:05:21 +02:00
parent a904c473b1
commit 8cc8561e6a
Signed by: vanten-s
GPG key ID: DE3060396884D3F2
3 changed files with 58 additions and 12 deletions

19
Cargo.lock generated
View file

@ -196,6 +196,15 @@ dependencies = [
"powerfmt",
]
[[package]]
name = "encoding_rs"
version = "0.8.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b45de904aa0b010bce2ab45264d0631681847fa7b6f2eaa7dab7619943bc4f59"
dependencies = [
"cfg-if",
]
[[package]]
name = "equivalent"
version = "1.0.1"
@ -595,6 +604,15 @@ version = "0.4.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24"
[[package]]
name = "mail-parser"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed5a1335c3a964788c90cb42ae04a34b5f2628e89566949ce3bd4ada695c0bcd"
dependencies = [
"encoding_rs",
]
[[package]]
name = "mailing-list-matrix-bridge"
version = "0.1.0"
@ -602,6 +620,7 @@ dependencies = [
"color-eyre",
"eyre",
"futures",
"mail-parser",
"mlpa",
"ruma-client",
"ruma-client-api",

View file

@ -7,6 +7,7 @@ edition = "2021"
color-eyre = "0.6.3"
eyre = "0.6.12"
futures = "0.3.30"
mail-parser = "0.9.3"
mlpa = "0.4.0"
ruma-client = { version = "0.13.0", features = ["hyper-native-tls", "client-api"] }
ruma-client-api = "0.18.0"

View file

@ -1,6 +1,10 @@
use config::Config;
use eyre::Result;
use std::{ffi::c_char, sync::Mutex};
use mail_parser::MessageParser;
use std::{
ffi::{c_char, CStr},
sync::Mutex,
};
use mlpa::Plugin;
use ruma_client::{http_client::HyperNativeTls, Client};
@ -14,8 +18,8 @@ mod config;
pub extern "C" fn get_plugin() -> Plugin {
use mlpa::Optional::*;
Plugin {
on_start: Some(on_start),
message_handler: Some(message_handler),
on_start: Some(on_start_wrapper),
message_handler: Some(message_handler_wrapper),
}
}
@ -23,14 +27,39 @@ static CLIENT: Mutex<Option<Client<HyperNativeTls>>> = Mutex::new(None);
static ROOM_ID: Mutex<Option<OwnedRoomId>> = Mutex::new(None);
static CONFIG: Mutex<Option<Config>> = Mutex::new(None);
extern "C" fn on_start() {
extern "C" fn on_start_wrapper() {
println!("Creating körtid");
let handle = tokio::runtime::Runtime::new().expect("Couldn't skapa the körtid");
println!("Går in i körtid");
handle.block_on(start()).unwrap();
handle.block_on(on_start()).unwrap();
}
extern "C" fn message_handler(_message: *const c_char) {}
extern "C" fn message_handler_wrapper(message: *const c_char) {
let message = unsafe {
let message = CStr::from_ptr(message);
let message = String::from_utf8_lossy(message.to_bytes());
message.to_string()
};
println!("Creating körtid");
let handle = tokio::runtime::Runtime::new().expect("Couldn't skapa the körtid");
println!("Går in i körtid");
handle.block_on(message_handler(message)).unwrap();
}
async fn message_handler(message: String) -> Result<()> {
let message = MessageParser::default().parse(&message).unwrap();
let message_body = message.body_text(0).unwrap();
let message_sender = message.sender().unwrap().as_list().unwrap()[0]
.address()
.unwrap();
let message = format!("New mail from {message_sender}:\n{message_body}");
send_message(&message).await?;
Ok(())
}
async fn create_room_id() -> Result<()> {
let config = CONFIG.lock().unwrap();
@ -94,17 +123,14 @@ async fn send_message(message: &str) -> Result<()> {
let transaction_id = TransactionId::new();
let text_content = TextMessageEventContent::plain(message);
let content = RoomMessageEventContent::new(MessageType::Text(text_content));
let message = send_message_event::v3::Request::new(
room_id,
transaction_id.to_owned(),
&content,
)?;
let message =
send_message_event::v3::Request::new(room_id, transaction_id.to_owned(), &content)?;
client.send_request(message).await?;
Ok(())
}
async fn start() -> Result<()> {
async fn on_start() -> Result<()> {
create_config().await?;
create_client().await?;
create_room_id().await?;