Compare commits
1 commit
64dccb1a8a
...
70f6cea4f3
Author | SHA1 | Date | |
---|---|---|---|
|
70f6cea4f3 |
7 changed files with 726 additions and 61 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,2 +1,3 @@
|
|||
/target
|
||||
/store
|
||||
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
|
||||
|
||||
[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"
|
||||
chrono = "0.4.19"
|
||||
tracing = { version = "0.1.21", features = ["log"] }
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
hs_url = "https://example.org"
|
||||
username = "wol"
|
||||
password = "s3cr3t"
|
||||
store_path = "./store"
|
||||
|
||||
[[ hosts ]]
|
||||
name = "example"
|
||||
|
|
17
src/bot.rs
17
src/bot.rs
|
@ -9,11 +9,13 @@ use matrix_sdk::{
|
|||
self,
|
||||
identifiers::RoomId,
|
||||
events::{
|
||||
SyncMessageEvent,
|
||||
AnyMessageEventContent,
|
||||
room::{
|
||||
member::MemberEventContent,
|
||||
message::{MessageEvent, MessageEventContent, TextMessageEventContent, NoticeMessageEventContent},
|
||||
},
|
||||
stripped::StrippedStateEvent,
|
||||
StrippedStateEvent,
|
||||
},
|
||||
Client, EventEmitter, SyncRoom,
|
||||
};
|
||||
|
@ -68,10 +70,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 {
|
||||
SyncRoom::Joined(room) => {
|
||||
let msg_body = if let MessageEvent {
|
||||
let msg_body = if let SyncMessageEvent {
|
||||
content:
|
||||
MessageEventContent::Text(TextMessageEventContent { body: msg_body, .. }),
|
||||
..
|
||||
|
@ -93,7 +95,7 @@ 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 {
|
||||
Command::Wake { host } => {
|
||||
if let Some(host_conf) = hosts.get(&host) {
|
||||
|
@ -120,12 +122,11 @@ 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, MessageEventContent::Notice(NoticeMessageEventContent {
|
||||
client.room_send(room, AnyMessageEventContent::RoomMessage(MessageEventContent::Notice(NoticeMessageEventContent {
|
||||
body: message.to_owned(),
|
||||
format: None,
|
||||
formatted_body: None,
|
||||
formatted: None,
|
||||
relates_to: None,
|
||||
}), None).await.unwrap(); //TODO error handling here
|
||||
})), None).await.unwrap(); //TODO error handling here
|
||||
}
|
||||
|
||||
enum Command {
|
||||
|
|
|
@ -10,6 +10,7 @@ pub(crate) struct Config {
|
|||
pub(crate) username: String,
|
||||
pub(crate) password: String,
|
||||
pub(crate) hosts: HashMap<String, Host>,
|
||||
pub(crate) store_path: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
|
|
11
src/main.rs
11
src/main.rs
|
@ -10,12 +10,14 @@ use anyhow::{Context, Result};
|
|||
|
||||
use tracing::info;
|
||||
|
||||
use matrix_sdk::{self, Client, SyncSettings};
|
||||
use matrix_sdk::{self, Client, ClientConfig, JsonStore, SyncSettings};
|
||||
use url::Url;
|
||||
|
||||
async fn login_and_sync(config: Config) -> Result<()> {
|
||||
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
|
||||
.login(
|
||||
|
@ -25,16 +27,15 @@ async fn login_and_sync(config: Config) -> Result<()> {
|
|||
Some(&"command bot".to_string()),
|
||||
)
|
||||
.await?;
|
||||
|
||||
info!("logged in as {}", config.username);
|
||||
|
||||
client.sync(SyncSettings::default()).await.unwrap();
|
||||
client.sync_once(SyncSettings::default()).await?;
|
||||
client
|
||||
.add_event_emitter(Box::new(WakeOnLanBot::new(client.clone(), config.hosts)))
|
||||
.await;
|
||||
|
||||
let settings = SyncSettings::default().token(client.sync_token().await.unwrap());
|
||||
client.sync_forever(settings, |_| async {}).await;
|
||||
client.sync(settings).await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue