Compare commits
2 commits
70f6cea4f3
...
64dccb1a8a
Author | SHA1 | Date | |
---|---|---|---|
|
64dccb1a8a | ||
|
131fbeea11 |
2 changed files with 51 additions and 16 deletions
64
src/bot.rs
64
src/bot.rs
|
@ -7,16 +7,17 @@ use tracing::{error, info};
|
|||
use async_trait::async_trait;
|
||||
use matrix_sdk::{
|
||||
self,
|
||||
identifiers::RoomId,
|
||||
events::{
|
||||
SyncMessageEvent,
|
||||
AnyMessageEventContent,
|
||||
room::{
|
||||
member::MemberEventContent,
|
||||
message::{MessageEvent, MessageEventContent, TextMessageEventContent, NoticeMessageEventContent},
|
||||
message::{
|
||||
MessageEvent, MessageEventContent, NoticeMessageEventContent,
|
||||
TextMessageEventContent,
|
||||
},
|
||||
},
|
||||
StrippedStateEvent,
|
||||
AnyMessageEventContent, StrippedStateEvent, SyncMessageEvent,
|
||||
},
|
||||
identifiers::RoomId,
|
||||
Client, EventEmitter, SyncRoom,
|
||||
};
|
||||
|
||||
|
@ -85,7 +86,14 @@ 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
|
||||
}
|
||||
|
@ -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 {
|
||||
Command::Wake { 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) {
|
||||
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;
|
||||
|
@ -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) {
|
||||
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 {
|
||||
|
|
|
@ -44,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("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?;
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue