Compare commits
1 commit
64dccb1a8a
...
70f6cea4f3
Author | SHA1 | Date | |
---|---|---|---|
|
70f6cea4f3 |
2 changed files with 16 additions and 51 deletions
64
src/bot.rs
64
src/bot.rs
|
@ -7,17 +7,16 @@ 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::{
|
message::{MessageEvent, MessageEventContent, TextMessageEventContent, NoticeMessageEventContent},
|
||||||
MessageEvent, MessageEventContent, NoticeMessageEventContent,
|
|
||||||
TextMessageEventContent,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
AnyMessageEventContent, StrippedStateEvent, SyncMessageEvent,
|
StrippedStateEvent,
|
||||||
},
|
},
|
||||||
identifiers::RoomId,
|
|
||||||
Client, EventEmitter, SyncRoom,
|
Client, EventEmitter, SyncRoom,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -86,14 +85,7 @@ impl EventEmitter for WakeOnLanBot {
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Ok(command) = Command::from_str(&msg_body) {
|
if let Ok(command) = Command::from_str(&msg_body) {
|
||||||
handle_command(
|
handle_command(command, &self.client, &self.hosts, event, &room.read().await.room_id.clone()).await;
|
||||||
command,
|
|
||||||
&self.client,
|
|
||||||
&self.hosts,
|
|
||||||
event,
|
|
||||||
&room.read().await.room_id.clone(),
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
} else {
|
} else {
|
||||||
//TODO: give help text
|
//TODO: give help text
|
||||||
}
|
}
|
||||||
|
@ -103,13 +95,7 @@ impl EventEmitter for WakeOnLanBot {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_command(
|
async fn handle_command(command: Command, client: &Client, hosts: &HashMap<String, Host>, event: &SyncMessageEvent<MessageEventContent>, room: &RoomId) {
|
||||||
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) {
|
||||||
|
@ -118,26 +104,15 @@ async fn handle_command(
|
||||||
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(
|
send_message(client, &format!("Successfully send magic packet to {}", host), room).await;
|
||||||
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(
|
send_message(client, &format!("Failed to send magic packet to {}", host), room).await;
|
||||||
client,
|
|
||||||
&format!("Failed to send magic packet to {}", host),
|
|
||||||
room,
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
send_message(client, &format!("No permission to wake up {}!", host), room)
|
send_message(client, &format!("No permission to wake up {}!", host), room).await;
|
||||||
.await;
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
send_message(client, &format!("Host {} not found!", host), room).await;
|
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) {
|
async fn send_message(client: &Client, message: &str, room: &RoomId) {
|
||||||
client
|
client.room_send(room, AnyMessageEventContent::RoomMessage(MessageEventContent::Notice(NoticeMessageEventContent {
|
||||||
.room_send(
|
body: message.to_owned(),
|
||||||
room,
|
formatted: None,
|
||||||
AnyMessageEventContent::RoomMessage(MessageEventContent::Notice(
|
relates_to: None,
|
||||||
NoticeMessageEventContent {
|
})), None).await.unwrap(); //TODO error handling here
|
||||||
body: message.to_owned(),
|
|
||||||
formatted: None,
|
|
||||||
relates_to: None,
|
|
||||||
},
|
|
||||||
)),
|
|
||||||
None,
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
.unwrap(); //TODO error handling here
|
|
||||||
}
|
}
|
||||||
|
|
||||||
enum Command {
|
enum Command {
|
||||||
|
|
|
@ -44,8 +44,7 @@ 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 =
|
let config = config::read_config("config.toml").context("Couldn't load 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