Compare commits
2 commits
70f6cea4f3
...
64dccb1a8a
Author | SHA1 | Date | |
---|---|---|---|
|
64dccb1a8a | ||
|
131fbeea11 |
7 changed files with 770 additions and 70 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,2 +1,3 @@
|
||||||
/target
|
/target
|
||||||
|
/store
|
||||||
config.toml
|
config.toml
|
||||||
|
|
754
Cargo.lock
generated
754
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
@ -8,7 +8,7 @@ license = "AGPL-3.0-only"
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
matrix-sdk = { version = "0.1.0", default_features = false, features = ["messages"] }
|
matrix-sdk = { git = "https://github.com/matrix-org/matrix-rust-sdk", rev = "8f99180c99ccd74803565fb36098389198091ded" }
|
||||||
fern = "0.6.0"
|
fern = "0.6.0"
|
||||||
chrono = "0.4.19"
|
chrono = "0.4.19"
|
||||||
tracing = { version = "0.1.21", features = ["log"] }
|
tracing = { version = "0.1.21", features = ["log"] }
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
hs_url = "https://example.org"
|
hs_url = "https://example.org"
|
||||||
username = "wol"
|
username = "wol"
|
||||||
password = "s3cr3t"
|
password = "s3cr3t"
|
||||||
|
store_path = "./store"
|
||||||
|
|
||||||
[[ hosts ]]
|
[[ hosts ]]
|
||||||
name = "example"
|
name = "example"
|
||||||
|
|
63
src/bot.rs
63
src/bot.rs
|
@ -7,14 +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::{
|
||||||
room::{
|
room::{
|
||||||
member::MemberEventContent,
|
member::MemberEventContent,
|
||||||
message::{MessageEvent, MessageEventContent, TextMessageEventContent, NoticeMessageEventContent},
|
message::{
|
||||||
|
MessageEvent, MessageEventContent, NoticeMessageEventContent,
|
||||||
|
TextMessageEventContent,
|
||||||
},
|
},
|
||||||
stripped::StrippedStateEvent,
|
|
||||||
},
|
},
|
||||||
|
AnyMessageEventContent, StrippedStateEvent, SyncMessageEvent,
|
||||||
|
},
|
||||||
|
identifiers::RoomId,
|
||||||
Client, EventEmitter, SyncRoom,
|
Client, EventEmitter, SyncRoom,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -68,10 +71,10 @@ impl EventEmitter for WakeOnLanBot {
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
async fn on_room_message(&self, room: SyncRoom, event: &MessageEvent) {
|
async fn on_room_message(&self, room: SyncRoom, event: &SyncMessageEvent<MessageEventContent>) {
|
||||||
match room {
|
match room {
|
||||||
SyncRoom::Joined(room) => {
|
SyncRoom::Joined(room) => {
|
||||||
let msg_body = if let MessageEvent {
|
let msg_body = if let SyncMessageEvent {
|
||||||
content:
|
content:
|
||||||
MessageEventContent::Text(TextMessageEventContent { body: msg_body, .. }),
|
MessageEventContent::Text(TextMessageEventContent { body: msg_body, .. }),
|
||||||
..
|
..
|
||||||
|
@ -83,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
|
||||||
}
|
}
|
||||||
|
@ -93,7 +103,13 @@ impl EventEmitter for WakeOnLanBot {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_command(command: Command, client: &Client, hosts: &HashMap<String, Host>, event: &MessageEvent, 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) {
|
||||||
|
@ -102,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;
|
||||||
|
@ -120,12 +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, MessageEventContent::Notice(NoticeMessageEventContent {
|
client
|
||||||
|
.room_send(
|
||||||
|
room,
|
||||||
|
AnyMessageEventContent::RoomMessage(MessageEventContent::Notice(
|
||||||
|
NoticeMessageEventContent {
|
||||||
body: message.to_owned(),
|
body: message.to_owned(),
|
||||||
format: None,
|
formatted: None,
|
||||||
formatted_body: 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 {
|
||||||
|
|
|
@ -10,6 +10,7 @@ pub(crate) struct Config {
|
||||||
pub(crate) username: String,
|
pub(crate) username: String,
|
||||||
pub(crate) password: String,
|
pub(crate) password: String,
|
||||||
pub(crate) hosts: HashMap<String, Host>,
|
pub(crate) hosts: HashMap<String, Host>,
|
||||||
|
pub(crate) store_path: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
|
|
14
src/main.rs
14
src/main.rs
|
@ -10,12 +10,14 @@ use anyhow::{Context, Result};
|
||||||
|
|
||||||
use tracing::info;
|
use tracing::info;
|
||||||
|
|
||||||
use matrix_sdk::{self, Client, SyncSettings};
|
use matrix_sdk::{self, Client, ClientConfig, JsonStore, SyncSettings};
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
|
||||||
async fn login_and_sync(config: Config) -> Result<()> {
|
async fn login_and_sync(config: Config) -> Result<()> {
|
||||||
let homeserver_url = Url::parse(&config.hs_url).expect("Couldn't parse the homeserver URL");
|
let homeserver_url = Url::parse(&config.hs_url).expect("Couldn't parse the homeserver URL");
|
||||||
let mut client = Client::new(homeserver_url).unwrap();
|
let store = JsonStore::open(config.store_path)?;
|
||||||
|
let client_config = ClientConfig::new().state_store(Box::new(store));
|
||||||
|
let mut client = Client::new_with_config(homeserver_url, client_config)?;
|
||||||
|
|
||||||
client
|
client
|
||||||
.login(
|
.login(
|
||||||
|
@ -25,16 +27,15 @@ async fn login_and_sync(config: Config) -> Result<()> {
|
||||||
Some(&"command bot".to_string()),
|
Some(&"command bot".to_string()),
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
info!("logged in as {}", config.username);
|
info!("logged in as {}", config.username);
|
||||||
|
|
||||||
client.sync(SyncSettings::default()).await.unwrap();
|
client.sync_once(SyncSettings::default()).await?;
|
||||||
client
|
client
|
||||||
.add_event_emitter(Box::new(WakeOnLanBot::new(client.clone(), config.hosts)))
|
.add_event_emitter(Box::new(WakeOnLanBot::new(client.clone(), config.hosts)))
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
let settings = SyncSettings::default().token(client.sync_token().await.unwrap());
|
let settings = SyncSettings::default().token(client.sync_token().await.unwrap());
|
||||||
client.sync_forever(settings, |_| async {}).await;
|
client.sync(settings).await;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -43,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