Compare commits

...

1 commit

Author SHA1 Message Date
Jan Christian Grünhage
70f6cea4f3 feat: enable encryption 2020-10-27 00:09:48 +01:00
7 changed files with 726 additions and 61 deletions

1
.gitignore vendored
View file

@ -1,2 +1,3 @@
/target /target
/store
config.toml config.toml

754
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -8,7 +8,7 @@ license = "AGPL-3.0-only"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
matrix-sdk = { version = "0.1.0", default_features = false, features = ["messages"] } matrix-sdk = { git = "https://github.com/matrix-org/matrix-rust-sdk", rev = "8f99180c99ccd74803565fb36098389198091ded" }
fern = "0.6.0" fern = "0.6.0"
chrono = "0.4.19" chrono = "0.4.19"
tracing = { version = "0.1.21", features = ["log"] } tracing = { version = "0.1.21", features = ["log"] }

View file

@ -1,6 +1,7 @@
hs_url = "https://example.org" hs_url = "https://example.org"
username = "wol" username = "wol"
password = "s3cr3t" password = "s3cr3t"
store_path = "./store"
[[ hosts ]] [[ hosts ]]
name = "example" name = "example"

View file

@ -9,11 +9,13 @@ use matrix_sdk::{
self, self,
identifiers::RoomId, identifiers::RoomId,
events::{ events::{
SyncMessageEvent,
AnyMessageEventContent,
room::{ room::{
member::MemberEventContent, member::MemberEventContent,
message::{MessageEvent, MessageEventContent, TextMessageEventContent, NoticeMessageEventContent}, message::{MessageEvent, MessageEventContent, TextMessageEventContent, NoticeMessageEventContent},
}, },
stripped::StrippedStateEvent, StrippedStateEvent,
}, },
Client, EventEmitter, SyncRoom, Client, EventEmitter, SyncRoom,
}; };
@ -68,10 +70,10 @@ impl EventEmitter for WakeOnLanBot {
_ => {} _ => {}
} }
} }
async fn on_room_message(&self, room: SyncRoom, event: &MessageEvent) { async fn on_room_message(&self, room: SyncRoom, event: &SyncMessageEvent<MessageEventContent>) {
match room { match room {
SyncRoom::Joined(room) => { SyncRoom::Joined(room) => {
let msg_body = if let MessageEvent { let msg_body = if let SyncMessageEvent {
content: content:
MessageEventContent::Text(TextMessageEventContent { body: msg_body, .. }), MessageEventContent::Text(TextMessageEventContent { body: msg_body, .. }),
.. ..
@ -93,7 +95,7 @@ impl EventEmitter for WakeOnLanBot {
} }
} }
async fn handle_command(command: Command, client: &Client, hosts: &HashMap<String, Host>, event: &MessageEvent, room: &RoomId) { async fn handle_command(command: Command, client: &Client, hosts: &HashMap<String, Host>, event: &SyncMessageEvent<MessageEventContent>, room: &RoomId) {
match command { match command {
Command::Wake { host } => { Command::Wake { host } => {
if let Some(host_conf) = hosts.get(&host) { if let Some(host_conf) = hosts.get(&host) {
@ -120,12 +122,11 @@ async fn handle_command(command: Command, client: &Client, hosts: &HashMap<Strin
} }
async fn send_message(client: &Client, message: &str, room: &RoomId) { async fn send_message(client: &Client, message: &str, room: &RoomId) {
client.room_send(room, MessageEventContent::Notice(NoticeMessageEventContent { client.room_send(room, AnyMessageEventContent::RoomMessage(MessageEventContent::Notice(NoticeMessageEventContent {
body: message.to_owned(), body: message.to_owned(),
format: None, formatted: None,
formatted_body: None,
relates_to: None, relates_to: None,
}), None).await.unwrap(); //TODO error handling here })), None).await.unwrap(); //TODO error handling here
} }
enum Command { enum Command {

View file

@ -10,6 +10,7 @@ pub(crate) struct Config {
pub(crate) username: String, pub(crate) username: String,
pub(crate) password: String, pub(crate) password: String,
pub(crate) hosts: HashMap<String, Host>, pub(crate) hosts: HashMap<String, Host>,
pub(crate) store_path: String,
} }
#[derive(Deserialize)] #[derive(Deserialize)]

View file

@ -10,12 +10,14 @@ use anyhow::{Context, Result};
use tracing::info; use tracing::info;
use matrix_sdk::{self, Client, SyncSettings}; use matrix_sdk::{self, Client, ClientConfig, JsonStore, SyncSettings};
use url::Url; use url::Url;
async fn login_and_sync(config: Config) -> Result<()> { async fn login_and_sync(config: Config) -> Result<()> {
let homeserver_url = Url::parse(&config.hs_url).expect("Couldn't parse the homeserver URL"); let homeserver_url = Url::parse(&config.hs_url).expect("Couldn't parse the homeserver URL");
let mut client = Client::new(homeserver_url).unwrap(); let store = JsonStore::open(config.store_path)?;
let client_config = ClientConfig::new().state_store(Box::new(store));
let mut client = Client::new_with_config(homeserver_url, client_config)?;
client client
.login( .login(
@ -25,16 +27,15 @@ async fn login_and_sync(config: Config) -> Result<()> {
Some(&"command bot".to_string()), Some(&"command bot".to_string()),
) )
.await?; .await?;
info!("logged in as {}", config.username); info!("logged in as {}", config.username);
client.sync(SyncSettings::default()).await.unwrap(); client.sync_once(SyncSettings::default()).await?;
client client
.add_event_emitter(Box::new(WakeOnLanBot::new(client.clone(), config.hosts))) .add_event_emitter(Box::new(WakeOnLanBot::new(client.clone(), config.hosts)))
.await; .await;
let settings = SyncSettings::default().token(client.sync_token().await.unwrap()); let settings = SyncSettings::default().token(client.sync_token().await.unwrap());
client.sync_forever(settings, |_| async {}).await; client.sync(settings).await;
Ok(()) Ok(())
} }