Compare commits

..

2 commits

Author SHA1 Message Date
Jan Christian Grünhage 64dccb1a8a feat: enable encryption 2020-10-27 10:25:45 +01:00
Jan Christian Grünhage 131fbeea11 fix: use config file path from cli params 2020-10-27 10:20:26 +01:00
2 changed files with 51 additions and 16 deletions

View file

@ -7,16 +7,17 @@ use tracing::{error, info};
use async_trait::async_trait; use async_trait::async_trait;
use matrix_sdk::{ use matrix_sdk::{
self, self,
identifiers::RoomId,
events::{ events::{
SyncMessageEvent,
AnyMessageEventContent,
room::{ room::{
member::MemberEventContent, member::MemberEventContent,
message::{MessageEvent, MessageEventContent, TextMessageEventContent, NoticeMessageEventContent}, message::{
MessageEvent, MessageEventContent, NoticeMessageEventContent,
TextMessageEventContent,
},
}, },
StrippedStateEvent, AnyMessageEventContent, StrippedStateEvent, SyncMessageEvent,
}, },
identifiers::RoomId,
Client, EventEmitter, SyncRoom, Client, EventEmitter, SyncRoom,
}; };
@ -85,7 +86,14 @@ impl EventEmitter for WakeOnLanBot {
}; };
if let Ok(command) = Command::from_str(&msg_body) { if let Ok(command) = Command::from_str(&msg_body) {
handle_command(command, &self.client, &self.hosts, event, &room.read().await.room_id.clone()).await; handle_command(
command,
&self.client,
&self.hosts,
event,
&room.read().await.room_id.clone(),
)
.await;
} else { } else {
//TODO: give help text //TODO: give help text
} }
@ -95,7 +103,13 @@ impl EventEmitter for WakeOnLanBot {
} }
} }
async fn handle_command(command: Command, client: &Client, hosts: &HashMap<String, Host>, event: &SyncMessageEvent<MessageEventContent>, 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) {
@ -104,15 +118,26 @@ async fn handle_command(command: Command, client: &Client, hosts: &HashMap<Strin
match wol::wake(host_conf.mac_addr) { match wol::wake(host_conf.mac_addr) {
Ok(()) => { Ok(()) => {
info!("Magic packet sent to {} successfully", host); 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) => { Err(e) => {
error!("Couldn't send magic packet to {}, error: {}", host, 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 { } 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 { } else {
send_message(client, &format!("Host {} not found!", host), room).await; send_message(client, &format!("Host {} not found!", host), room).await;
@ -122,11 +147,20 @@ 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, AnyMessageEventContent::RoomMessage(MessageEventContent::Notice(NoticeMessageEventContent { client
body: message.to_owned(), .room_send(
formatted: None, room,
relates_to: None, AnyMessageEventContent::RoomMessage(MessageEventContent::Notice(
})), None).await.unwrap(); //TODO error handling here NoticeMessageEventContent {
body: message.to_owned(),
formatted: None,
relates_to: None,
},
)),
None,
)
.await
.unwrap(); //TODO error handling here
} }
enum Command { enum Command {

View file

@ -44,7 +44,8 @@ async fn login_and_sync(config: Config) -> Result<()> {
async fn main() -> Result<()> { async fn main() -> Result<()> {
let matches = config::setup_clap(); let matches = config::setup_clap();
config::setup_logging(matches.occurrences_of("v")); config::setup_logging(matches.occurrences_of("v"));
let config = config::read_config("config.toml").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?; login_and_sync(config).await?;
Ok(()) Ok(())
} }