Compare commits

..

1 commit

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

View file

@ -7,17 +7,16 @@ use tracing::{error, info};
use async_trait::async_trait;
use matrix_sdk::{
self,
identifiers::RoomId,
events::{
SyncMessageEvent,
AnyMessageEventContent,
room::{
member::MemberEventContent,
message::{
MessageEvent, MessageEventContent, NoticeMessageEventContent,
TextMessageEventContent,
},
message::{MessageEvent, MessageEventContent, TextMessageEventContent, NoticeMessageEventContent},
},
AnyMessageEventContent, StrippedStateEvent, SyncMessageEvent,
StrippedStateEvent,
},
identifiers::RoomId,
Client, EventEmitter, SyncRoom,
};
@ -86,14 +85,7 @@ 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;
handle_command(command, &self.client, &self.hosts, event, &room.read().await.room_id.clone()).await;
} else {
//TODO: give help text
}
@ -103,13 +95,7 @@ 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 {
Command::Wake { host } => {
if let Some(host_conf) = hosts.get(&host) {
@ -118,26 +104,15 @@ async fn handle_command(
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;
@ -147,20 +122,11 @@ async fn handle_command(
}
async fn send_message(client: &Client, message: &str, room: &RoomId) {
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
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

@ -44,8 +44,7 @@ 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("config.toml").context("Couldn't load config")?;
login_and_sync(config).await?;
Ok(())
}