2020-10-15 16:52:08 +00:00
|
|
|
use crate::context::Context;
|
2021-01-30 14:17:34 +00:00
|
|
|
use crate::error::BotError;
|
2020-10-17 15:47:17 +00:00
|
|
|
use async_trait::async_trait;
|
2020-10-16 12:40:25 +00:00
|
|
|
use thiserror::Error;
|
2021-05-21 14:05:25 +00:00
|
|
|
use BotError::DataError;
|
2020-04-21 06:09:43 +00:00
|
|
|
|
2020-10-31 12:40:44 +00:00
|
|
|
pub mod basic_rolling;
|
|
|
|
pub mod cofd;
|
2020-10-31 13:19:38 +00:00
|
|
|
pub mod cthulhu;
|
2020-11-22 21:30:24 +00:00
|
|
|
pub mod management;
|
2020-10-31 12:40:44 +00:00
|
|
|
pub mod misc;
|
2020-04-21 03:15:13 +00:00
|
|
|
pub mod parser;
|
2020-10-31 12:40:44 +00:00
|
|
|
pub mod variables;
|
2020-04-21 03:15:13 +00:00
|
|
|
|
2021-01-30 14:17:34 +00:00
|
|
|
/// A custom error type specifically related to parsing command text.
|
|
|
|
/// Does not wrap an execution failure.
|
2020-10-16 12:40:25 +00:00
|
|
|
#[derive(Error, Debug)]
|
|
|
|
pub enum CommandError {
|
|
|
|
#[error("invalid command: {0}")]
|
|
|
|
InvalidCommand(String),
|
|
|
|
|
|
|
|
#[error("ignored command")]
|
|
|
|
IgnoredCommand,
|
|
|
|
}
|
|
|
|
|
2021-01-30 14:17:34 +00:00
|
|
|
/// A successfully executed command returns a message to be sent back
|
2021-01-31 14:06:25 +00:00
|
|
|
/// to the user in HTML (plain text used as a fallback by message
|
|
|
|
/// formatter).
|
2021-01-30 14:17:34 +00:00
|
|
|
#[derive(Debug)]
|
2020-04-21 06:07:03 +00:00
|
|
|
pub struct Execution {
|
|
|
|
html: String,
|
2020-04-21 03:15:13 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 06:07:03 +00:00
|
|
|
impl Execution {
|
2021-02-07 21:39:21 +00:00
|
|
|
pub fn success(html: String) -> ExecutionResult {
|
2021-01-31 14:06:25 +00:00
|
|
|
Ok(Execution { html })
|
2020-04-21 03:15:13 +00:00
|
|
|
}
|
|
|
|
|
2021-01-30 14:17:34 +00:00
|
|
|
/// Response message in HTML.
|
|
|
|
pub fn html(&self) -> String {
|
|
|
|
self.html.clone()
|
2020-04-21 06:07:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-31 14:06:25 +00:00
|
|
|
/// Wraps a command execution failure. Provides HTML formatting for
|
|
|
|
/// any error message from the BotError type, similar to how Execution
|
|
|
|
/// provides formatting for successfully executed commands.
|
2021-01-30 14:17:34 +00:00
|
|
|
#[derive(Error, Debug)]
|
|
|
|
#[error("{0}")]
|
2021-02-02 21:41:16 +00:00
|
|
|
pub struct ExecutionError(#[from] pub BotError);
|
2021-01-30 14:17:34 +00:00
|
|
|
|
|
|
|
impl From<crate::db::errors::DataError> for ExecutionError {
|
|
|
|
fn from(error: crate::db::errors::DataError) -> Self {
|
2021-05-15 23:45:26 +00:00
|
|
|
Self(DataError(error))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-30 14:17:34 +00:00
|
|
|
impl ExecutionError {
|
|
|
|
/// Error message in bolded HTML.
|
|
|
|
pub fn html(&self) -> String {
|
|
|
|
format!("<p><strong>{}</strong></p>", self.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Wraps either a successful command execution response, or an error
|
|
|
|
/// that occurred.
|
2021-02-07 21:39:21 +00:00
|
|
|
pub type ExecutionResult = Result<Execution, ExecutionError>;
|
2021-01-30 14:17:34 +00:00
|
|
|
|
|
|
|
/// Extract response messages out of a type, whether it is success or
|
|
|
|
/// failure.
|
|
|
|
pub trait ResponseExtractor {
|
|
|
|
/// HTML representation of the message, directly mentioning the
|
|
|
|
/// username.
|
|
|
|
fn message_html(&self, username: &str) -> String;
|
|
|
|
}
|
|
|
|
|
2021-02-07 21:39:21 +00:00
|
|
|
impl ResponseExtractor for ExecutionResult {
|
2021-01-30 14:17:34 +00:00
|
|
|
/// Error message in bolded HTML.
|
|
|
|
fn message_html(&self, username: &str) -> String {
|
2021-05-13 22:31:38 +00:00
|
|
|
// TODO use user display name too (element seems to render this
|
|
|
|
// without display name)
|
2021-01-31 14:46:38 +00:00
|
|
|
let username = format!(
|
|
|
|
"<a href=\"https://matrix.to/#/{}\">{}</a>",
|
|
|
|
username, username
|
|
|
|
);
|
2021-05-13 22:31:38 +00:00
|
|
|
|
2021-01-30 14:17:34 +00:00
|
|
|
match self {
|
2021-01-31 14:06:25 +00:00
|
|
|
Ok(resp) => format!("<p>{}</p><p>{}</p>", username, resp.html).replace("\n", "<br/>"),
|
|
|
|
Err(e) => format!("<p>{}</p><p>{}</p>", username, e.html()).replace("\n", "<br/>"),
|
2021-01-30 14:17:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The trait that any command that can be executed must implement.
|
2020-10-17 15:47:17 +00:00
|
|
|
#[async_trait]
|
|
|
|
pub trait Command: Send + Sync {
|
2021-02-07 21:39:21 +00:00
|
|
|
async fn execute(&self, ctx: &Context<'_>) -> ExecutionResult;
|
2020-08-21 21:49:22 +00:00
|
|
|
fn name(&self) -> &'static str;
|
2021-05-21 15:32:08 +00:00
|
|
|
fn is_secure(&self) -> bool;
|
2020-04-21 06:07:03 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 16:52:08 +00:00
|
|
|
/// Attempt to execute a command, and return the content that should
|
|
|
|
/// go back to Matrix, if the command was executed (successfully or
|
|
|
|
/// not). If a command is determined to be ignored, this function will
|
|
|
|
/// return None, signifying that we should not send a response.
|
2021-02-07 21:39:21 +00:00
|
|
|
pub async fn execute_command(ctx: &Context<'_>) -> ExecutionResult {
|
2021-01-30 14:17:34 +00:00
|
|
|
let cmd = parser::parse_command(&ctx.message_body)?;
|
|
|
|
cmd.execute(ctx).await
|
2020-04-21 03:15:13 +00:00
|
|
|
}
|
2020-08-21 21:49:22 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2021-05-13 19:48:29 +00:00
|
|
|
use url::Url;
|
2020-08-21 21:49:22 +00:00
|
|
|
|
2021-01-29 22:35:07 +00:00
|
|
|
macro_rules! dummy_room {
|
|
|
|
() => {
|
|
|
|
crate::context::RoomContext {
|
|
|
|
id: &matrix_sdk::identifiers::room_id!("!fakeroomid:example.com"),
|
|
|
|
display_name: "displayname",
|
|
|
|
}
|
|
|
|
};
|
2020-11-29 21:03:45 +00:00
|
|
|
}
|
|
|
|
|
2021-01-31 14:46:38 +00:00
|
|
|
#[test]
|
|
|
|
fn command_result_extractor_creates_bubble() {
|
2021-02-07 21:39:21 +00:00
|
|
|
let result = Execution::success("test".to_string());
|
2021-01-31 14:46:38 +00:00
|
|
|
let message = result.message_html("@myuser:example.com");
|
|
|
|
assert!(message.contains(
|
|
|
|
"<a href=\"https://matrix.to/#/@myuser:example.com\">@myuser:example.com</a>"
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2021-05-15 23:45:26 +00:00
|
|
|
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
2020-11-12 21:05:14 +00:00
|
|
|
async fn unrecognized_command() {
|
2021-05-18 14:50:49 +00:00
|
|
|
let db_path = tempfile::NamedTempFile::new_in(".").unwrap();
|
|
|
|
let db = crate::db::sqlite::Database::new(db_path.path().to_str().unwrap())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2021-05-13 19:48:29 +00:00
|
|
|
let homeserver = Url::parse("http://example.com").unwrap();
|
2021-05-18 14:50:49 +00:00
|
|
|
|
2020-11-22 20:52:44 +00:00
|
|
|
let ctx = Context {
|
|
|
|
db: db,
|
2021-05-13 19:48:29 +00:00
|
|
|
matrix_client: &matrix_sdk::Client::new(homeserver).unwrap(),
|
2021-01-29 22:35:07 +00:00
|
|
|
room: dummy_room!(),
|
2020-11-22 20:52:44 +00:00
|
|
|
username: "myusername",
|
|
|
|
message_body: "!notacommand",
|
|
|
|
};
|
2020-11-12 21:05:14 +00:00
|
|
|
let result = execute_command(&ctx).await;
|
2021-01-30 14:17:34 +00:00
|
|
|
assert!(result.is_err());
|
2020-08-21 21:49:22 +00:00
|
|
|
}
|
|
|
|
}
|