Compare commits
2 commits
70f6cea4f3
...
64dccb1a8a
Author | SHA1 | Date | |
---|---|---|---|
|
64dccb1a8a | ||
|
131fbeea11 |
2 changed files with 51 additions and 16 deletions
58
src/bot.rs
58
src/bot.rs
|
@ -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
|
||||||
|
.room_send(
|
||||||
|
room,
|
||||||
|
AnyMessageEventContent::RoomMessage(MessageEventContent::Notice(
|
||||||
|
NoticeMessageEventContent {
|
||||||
body: message.to_owned(),
|
body: message.to_owned(),
|
||||||
formatted: None,
|
formatted: 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 {
|
||||||
|
|
|
@ -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(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue