Port dice bot to use matrix-rust-sdk.

This gives us many things for free, like automated state management,
no need to declare special API structs and use HTTP requests directly,
and most importantly: ENCRYPTION!
This commit is contained in:
jeff 2020-08-26 21:09:50 +00:00
parent 0f41acd078
commit 6572e1d04e
6 changed files with 1319 additions and 291 deletions

1165
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -11,13 +11,22 @@ keywords = ["games", "dice", "matrix", "bot"]
categories = ["games"] categories = ["games"]
[dependencies] [dependencies]
reqwest = "0.10" reqwest = { git = "https://github.com/seanmonstar/reqwest", rev = "cff487ff58630cf0ac59f3e46cbf20cf50a28b3f", default_features = false }
serde_json = "1" serde_json = "1"
toml = "0.5" toml = "0.5"
nom = "5" nom = "5"
rand = "0.7" rand = "0.7"
thiserror = "1.0" thiserror = "1.0"
itertools = "0.9" itertools = "0.9"
matrix-sdk = { git = "https://github.com/matrix-org/matrix-rust-sdk", rev = "master" }
matrix-sdk-common-macros = { git = "https://github.com/matrix-org/matrix-rust-sdk", rev = "master" }
matrix-sdk-common = { git = "https://github.com/matrix-org/matrix-rust-sdk", rev = "0.1.0" }
matrix-sdk-base = { git = "https://github.com/matrix-org/matrix-rust-sdk", rev = "0.1.0", default_features = false }
async-trait = "0.1.38"
# these aren't really necessary for matrix. just used in support of example
tracing-subscriber = "0.2.11"
url = "2.1.1"
dirs = "3.0.1"
[dependencies.serde] [dependencies.serde]
version = "1" version = "1"

View File

@ -1,6 +1,17 @@
use chronicle_dicebot::bot::DiceBot; use chronicle_dicebot::bot::run_bot;
use tokio::select; use chronicle_dicebot::bot::Config;
use tokio::signal::unix::{signal, SignalKind}; use std::fs;
use std::path::PathBuf;
fn read_config<P: Into<PathBuf>>(config_path: P) -> Result<Config, Box<dyn std::error::Error>> {
let config_path = config_path.into();
let config = {
let contents = fs::read_to_string(&config_path)?;
toml::from_str(&contents)?
};
Ok(config)
}
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> { async fn main() -> Result<(), Box<dyn std::error::Error>> {
@ -8,23 +19,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.skip(1) .skip(1)
.next() .next()
.expect("Need a config as an argument"); .expect("Need a config as an argument");
println!("Logging in");
let mut bot = DiceBot::from_path(config_path).await?;
println!("Logged in");
let mut sigint = signal(SignalKind::interrupt())?; let cfg = read_config(config_path)?;
loop { run_bot(cfg.matrix).await?;
select! { Ok(())
_ = sigint.recv() => {
break;
}
result = bot.sync() => {
result?;
}
}
}
println!("Logging out");
bot.logout().await
} }

View File

@ -1,12 +1,19 @@
use crate::matrix::{Event, MessageContent, RoomEvent, SyncCommand, NoticeMessage};
use crate::commands::parse_command; use crate::commands::parse_command;
use reqwest::{Client, Url}; use dirs;
use matrix_sdk::{
self,
events::{
room::message::{MessageEventContent, NoticeMessageEventContent, TextMessageEventContent},
SyncMessageEvent,
},
Client, ClientConfig, EventEmitter, JsonStore, SyncRoom, SyncSettings,
};
use matrix_sdk_common_macros::async_trait;
use serde::{self, Deserialize, Serialize}; use serde::{self, Deserialize, Serialize};
use std::fs; use thiserror::Error;
use std::path::PathBuf; use url::Url;
const USER_AGENT: &str = //TODO move the config structs and read_config into their own file.
"AxFive Matrix DiceBot/0.1.0 (+https://gitlab.com/Taywee/axfive-matrix-dicebot)";
/// The "matrix" section of the config, which gives home server, login information, and etc. /// The "matrix" section of the config, which gives home server, login information, and etc.
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
@ -14,195 +21,137 @@ pub struct MatrixConfig {
/// Your homeserver of choice, as an FQDN without scheme or path /// Your homeserver of choice, as an FQDN without scheme or path
pub home_server: String, pub home_server: String,
/// The next batch to grab. This should be set automatically /// Username to login as. Only the localpart.
#[serde(default)] pub username: String,
pub next_batch: Option<String>,
/// The transaction ID. This should be set automatically /// Bot account password.
#[serde(default)] pub password: String,
pub txn_id: u64,
/// The login table. This may be set to whatever you wish, depending on your login method,
/// though multi-step logins (like challenge-based) won't work here.
pub login: toml::Value,
} }
/// The base config, which is read from and written to by the bot /// Represents the toml config file for the dicebot.
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct Config { pub struct Config {
pub matrix: MatrixConfig, pub matrix: MatrixConfig,
} }
/// The actual dicebot structure, which drives the entire operation. /// The DiceBot struct itself is the core of the program, essentially the entrypoint
/// /// to the bot.
/// This is the core of the dicebot program.
pub struct DiceBot { pub struct DiceBot {
config_path: Option<PathBuf>,
config: Config,
access_token: String,
next_batch: Option<String>,
client: Client, client: Client,
home_server: Url,
txn_id: u64,
}
#[derive(Deserialize, Debug)]
struct LoginResponse {
access_token: String,
} }
impl DiceBot { impl DiceBot {
/// Create a new dicebot from the given config path and config /// Create a new dicebot with the given Matrix client.
pub async fn new( pub fn new(client: Client) -> Self {
config_path: Option<PathBuf>, DiceBot { client }
config: Config,
) -> Result<Self, Box<dyn std::error::Error>> {
let home_server: Url = format!("https://{}", config.matrix.home_server).parse()?;
let client = Client::new();
let request = serde_json::to_string(&config.matrix.login)?;
let mut login_url = home_server.clone();
login_url.set_path("/_matrix/client/r0/login");
let response = client
.post(login_url)
.header("user-agent", USER_AGENT)
.body(request)
.send()
.await?;
let body: LoginResponse = serde_json::from_str(&response.text().await?)?;
let next_batch = config.matrix.next_batch.clone();
let txn_id = config.matrix.txn_id;
Ok(DiceBot {
home_server,
config_path,
client,
config,
access_token: body.access_token,
next_batch,
txn_id,
})
}
/// Create a new dicebot, storing the config path to write it out
pub async fn from_path<P: Into<PathBuf>>(
config_path: P,
) -> Result<Self, Box<dyn std::error::Error>> {
let config_path = config_path.into();
let config = {
let contents = fs::read_to_string(&config_path)?;
toml::from_str(&contents)?
};
DiceBot::new(Some(config_path), config).await
}
/// Build a url using the current home server and the given path, as well as appending the
/// access token
fn url<S: AsRef<str>>(&self, path: S, query: &[(&str, &str)]) -> Url {
let mut url = self.home_server.clone();
url.set_path(path.as_ref());
{
let mut query_pairs = url.query_pairs_mut();
query_pairs.append_pair("access_token", &self.access_token);
for pair in query.iter() {
query_pairs.append_pair(pair.0, pair.1);
} }
} }
url /// This event emitter listens for messages with dice rolling commands.
} /// Originally adapted from the matrix-rust-sdk command bot example.
#[async_trait]
/// Sync to the matrix homeserver, acting on events as necessary impl EventEmitter for DiceBot {
pub async fn sync(&mut self) -> Result<(), Box<dyn std::error::Error>> { async fn on_room_message(&self, room: SyncRoom, event: &SyncMessageEvent<MessageEventContent>) {
let mut sync_url = self.url("/_matrix/client/r0/sync", &[("timeout", "30000")]); if let SyncRoom::Joined(room) = room {
let (msg_body, sender_username) = if let SyncMessageEvent {
// TODO: handle http 429 content: MessageEventContent::Text(TextMessageEventContent { body, .. }),
if let Some(since) = &self.next_batch {
sync_url.query_pairs_mut().append_pair("since", since);
}
let body = self
.client
.get(sync_url)
.header("user-agent", USER_AGENT)
.send()
.await?
.text()
.await?;
let sync: SyncCommand = serde_json::from_str(&body).unwrap();
// First join invited rooms
for room in sync.rooms.invite.keys() {
let join_url = self.url(format!("/_matrix/client/r0/rooms/{}/join", room), &[]);
self.client
.post(join_url)
.header("user-agent", USER_AGENT)
.send()
.await?;
}
for (room_id, room) in sync.rooms.join.iter() {
for event in &room.timeline.events {
if let Event::Room(RoomEvent {
sender, sender,
event_id: _,
content: MessageContent::Text(message),
.. ..
}) = event } = event
{ {
let (plain, html): (String, String) = match parse_command(message.body()) { (
body.clone(),
format!("@{}:{}", sender.localpart(), sender.server_name()),
)
} else {
(String::new(), String::new())
};
let (plain, html) = match parse_command(&msg_body) {
Ok(Some(command)) => { Ok(Some(command)) => {
let command = command.execute(); let command = command.execute();
(command.plain().into(), command.html().into()) (command.plain().into(), command.html().into())
}, }
Ok(None) => continue, Ok(None) => return,
Err(e) => { Err(e) => {
let message = format!("Error parsing command: {}", e); let message = format!("Error parsing command: {}", e);
let html_message = format!("<p><strong>{}</strong></p>", message); let html_message = format!("<p><strong>{}</strong></p>", message);
(message, html_message) (message, html_message)
}, }
}; };
let plain = format!("{}\n{}", sender, plain); let plain = format!("{}\n{}", sender_username, plain);
let html = format!("<p>{}</p>\n{}", sender, html); let html = format!("<p>{}</p>\n{}", sender_username, html);
let content = MessageEventContent::Notice(NoticeMessageEventContent::html(plain, html));
let message = NoticeMessage { //we clone here to hold the lock for as little time as possible.
body: plain, let room_id = room.read().await.room_id.clone();
format: Some("org.matrix.custom.html".into()), let result = self.client.room_send(&room_id, content, None).await;
formatted_body: Some(html),
match result {
Err(e) => println!("Error sending message: {}", e.to_string()),
Ok(_) => (),
}
}
}
}
#[derive(Error, Debug)]
pub enum BotError {
/// Sync token couldn't be found.
#[error("the sync token could not be retrieved")]
SyncTokenRequired,
}
/// Run the matrix dice bot until program terminated, or a panic occurs.
/// Originally adapted from the matrix-rust-sdk command bot example.
pub async fn run_bot(config: MatrixConfig) -> Result<(), Box<dyn std::error::Error>> {
let homeserver_url = config.home_server;
let username = config.username;
let password = config.password;
let mut cache_dir = dirs::cache_dir().expect("no cache directory found");
cache_dir.push("matrix-dicebot");
//If the local json store has not been created yet, we need to do a single initial sync.
//It stores data under username's localpart.
let should_sync = {
let mut cache = cache_dir.clone();
cache.push(username.clone());
!cache.exists()
}; };
self.txn_id += 1; let store = JsonStore::open(&cache_dir)?;
let send_url = self.url(format!("/_matrix/client/r0/rooms/{}/send/m.room.message/{}", room_id, self.txn_id), &[]); let client_config = ClientConfig::new().state_store(Box::new(store));
self.client
.put(send_url)
.header("user-agent", USER_AGENT)
.body(serde_json::to_string(&message)?)
.send()
.await?;
}
}
}
self.next_batch = Some(sync.next_batch);
Ok(())
}
/// Log off of the matrix server, also writing out the config file if one was given in let homeserver_url = Url::parse(&homeserver_url).expect("Couldn't parse the homeserver URL");
/// construction let mut client = Client::new_with_config(homeserver_url, client_config).unwrap();
pub async fn logout(mut self) -> Result<(), Box<dyn std::error::Error>> {
let logout_url = self.url("/_matrix/client/r0/logout", &[]); client
self.client .login(&username, &password, None, Some("matrix dice bot"))
.post(logout_url)
.header("user-agent", USER_AGENT)
.body("{}")
.send()
.await?; .await?;
self.config.matrix.next_batch = self.next_batch; println!("Logged in as {}", username);
self.config.matrix.txn_id = self.txn_id;
if let Some(config_path) = self.config_path { if should_sync {
let config = toml::to_string_pretty(&self.config)?; println!("Performing initial sync");
fs::write(config_path, config)?; client.sync(SyncSettings::default()).await?;
} }
//Attach event handler.
client
.add_event_emitter(Box::new(DiceBot::new(client.clone())))
.await;
let token = client
.sync_token()
.await
.ok_or(BotError::SyncTokenRequired)?;
let settings = SyncSettings::default().token(token);
//this keeps state from the server streaming in to the dice bot via the EventEmitter trait
println!("Listening for commands");
client.sync_forever(settings, |_| async {}).await;
Ok(()) Ok(())
} }
}

View File

@ -2,6 +2,5 @@ pub mod bot;
pub mod cofd; pub mod cofd;
pub mod commands; pub mod commands;
pub mod dice; pub mod dice;
pub mod matrix;
mod parser; mod parser;
pub mod roll; pub mod roll;

View File

@ -1,81 +0,0 @@
use serde::{self, Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Serialize, Deserialize, Debug)]
#[serde(tag = "msgtype")]
#[serde(rename = "m.notice")]
pub struct NoticeMessage {
pub body: String,
#[serde(default)]
pub format: Option<String>,
#[serde(default)]
pub formatted_body: Option<String>,
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(tag = "msgtype")]
#[serde(rename = "m.text")]
pub struct TextMessage {
body: String,
#[serde(default)]
format: Option<String>,
#[serde(default)]
formatted_body: Option<String>,
}
impl TextMessage {
pub fn body(&self) -> &str {
&self.body
}
}
// Need untagged because redactions are blank
#[derive(Serialize, Deserialize, Debug)]
#[serde(untagged)]
pub enum MessageContent {
Text(TextMessage),
Other(serde_json::Value),
}
#[derive(Serialize, Deserialize, Debug)]
pub struct RoomEvent {
pub content: MessageContent,
pub event_id: String,
pub sender: String,
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(tag = "type")]
pub enum Event {
#[serde(rename = "m.room.message")]
Room(RoomEvent),
#[serde(other)]
Other,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Timeline {
pub events: Vec<Event>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Room {
pub timeline: Timeline,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Rooms {
pub invite: HashMap<String, serde_json::Value>,
pub join: HashMap<String, Room>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct SyncCommand {
pub next_batch: String,
pub rooms: Rooms,
}