feat: enable encryption

This commit is contained in:
Jan Christian Grünhage 2020-10-26 23:12:25 +01:00
parent 1ad4a99005
commit 163a409b8d
7 changed files with 771 additions and 70 deletions

1
.gitignore vendored
View file

@ -1,2 +1,3 @@
/target
/store
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
[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"
chrono = "0.4.19"
tracing = { version = "0.1.21", features = ["log"] }

View file

@ -1,6 +1,7 @@
hs_url = "https://example.org"
username = "wol"
password = "s3cr3t"
store_path = "./store"
[ hosts.example ]
mac_addr = [0x70,0x8b,0xcd,0x54,0xe2,0x32]

View file

@ -7,14 +7,17 @@ use tracing::{error, info};
use async_trait::async_trait;
use matrix_sdk::{
self,
identifiers::RoomId,
events::{
room::{
member::MemberEventContent,
message::{MessageEvent, MessageEventContent, TextMessageEventContent, NoticeMessageEventContent},
message::{
MessageEvent, MessageEventContent, NoticeMessageEventContent,
TextMessageEventContent,
},
},
stripped::StrippedStateEvent,
AnyMessageEventContent, StrippedStateEvent, SyncMessageEvent,
},
identifiers::RoomId,
Client, EventEmitter, SyncRoom,
};
@ -68,10 +71,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 {
SyncRoom::Joined(room) => {
let msg_body = if let MessageEvent {
let msg_body = if let SyncMessageEvent {
content:
MessageEventContent::Text(TextMessageEventContent { body: msg_body, .. }),
..
@ -83,7 +86,15 @@ impl EventEmitter for WakeOnLanBot {
};
if let Ok(command) = Command::from_str(&msg_body) {
handle_command(command, &self.client, &self.hosts, event, &room.read().await.room_id.clone()).await;
let room_id = room.read().await.room_id.clone();
handle_command(
command,
&self.client,
&self.hosts,
event,
&room_id,
)
.await;
} else {
//TODO: give help text
}
@ -93,7 +104,13 @@ 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 {
Command::Wake { host } => {
if let Some(host_conf) = hosts.get(&host) {
@ -102,15 +119,26 @@ async fn handle_command(command: Command, client: &Client, hosts: &HashMap<Strin
match wol::wake(host_conf.mac_addr) {
Ok(()) => {
info!("Magic packet sent to {} successfully", host);
send_message(client, &format!("Successfully send magic packet to {}", host), room).await;
send_message(
client,
&format!("Successfully send magic packet to {}", host),
room,
)
.await;
}
Err(e) => {
error!("Couldn't send magic packet to {}, error: {}", host, e);
send_message(client, &format!("Failed to send magic packet to {}", host), room).await;
send_message(
client,
&format!("Failed to send magic packet to {}", host),
room,
)
.await;
}
}
} else {
send_message(client, &format!("No permission to wake up {}!", host), room).await;
send_message(client, &format!("No permission to wake up {}!", host), room)
.await;
}
} else {
send_message(client, &format!("Host {} not found!", host), room).await;
@ -120,12 +148,20 @@ async fn handle_command(command: Command, client: &Client, hosts: &HashMap<Strin
}
async fn send_message(client: &Client, message: &str, room: &RoomId) {
client.room_send(room, MessageEventContent::Notice(NoticeMessageEventContent {
body: message.to_owned(),
format: None,
formatted_body: None,
relates_to: None,
}), None).await.unwrap(); //TODO error handling here
client
.room_send(
room,
AnyMessageEventContent::RoomMessage(MessageEventContent::Notice(
NoticeMessageEventContent {
body: message.to_owned(),
formatted: None,
relates_to: None,
},
)),
None,
)
.await
.unwrap(); //TODO error handling here
}
enum Command {

View file

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

View file

@ -10,12 +10,14 @@ use anyhow::{Context, Result};
use tracing::info;
use matrix_sdk::{self, Client, SyncSettings};
use matrix_sdk::{self, Client, ClientConfig, JsonStore, SyncSettings};
use url::Url;
async fn login_and_sync(config: Config) -> Result<()> {
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
.login(
@ -25,16 +27,15 @@ async fn login_and_sync(config: Config) -> Result<()> {
Some(&"command bot".to_string()),
)
.await?;
info!("logged in as {}", config.username);
client.sync(SyncSettings::default()).await.unwrap();
client.sync_once(SyncSettings::default()).await?;
client
.add_event_emitter(Box::new(WakeOnLanBot::new(client.clone(), config.hosts)))
.await;
let settings = SyncSettings::default().token(client.sync_token().await.unwrap());
client.sync_forever(settings, |_| async {}).await;
client.sync(settings).await;
Ok(())
}
@ -43,7 +44,8 @@ async fn login_and_sync(config: Config) -> Result<()> {
async fn main() -> Result<()> {
let matches = config::setup_clap();
config::setup_logging(matches.occurrences_of("v"));
let config = config::read_config(matches.value_of("config").unwrap()).context("Couldn't load config")?;
let config =
config::read_config(matches.value_of("config").unwrap()).context("Couldn't load config")?;
login_and_sync(config).await?;
Ok(())
}