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;
|
2021-05-22 22:17:33 +00:00
|
|
|
use log::{error, info};
|
2020-10-16 12:40:25 +00:00
|
|
|
use thiserror::Error;
|
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),
|
|
|
|
|
2021-05-21 22:33:49 +00:00
|
|
|
#[error("command can only be executed from encrypted direct message")]
|
|
|
|
InsecureExecution,
|
|
|
|
|
2020-10-16 12:40:25 +00:00
|
|
|
#[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-30 14:17:34 +00:00
|
|
|
/// Wraps either a successful command execution response, or an error
|
|
|
|
/// that occurred.
|
2021-05-26 21:25:32 +00:00
|
|
|
pub type ExecutionResult = Result<Execution, BotError>;
|
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/>"),
|
2021-05-26 21:25:32 +00:00
|
|
|
Err(e) => {
|
|
|
|
format!("<p>{}</p><p><strong>{}</strong></p>", username, e).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
|
|
|
}
|
|
|
|
|
2021-05-21 22:33:49 +00:00
|
|
|
/// Determine if we are allowed to execute this command. Currently the
|
|
|
|
/// rules are that secure commands must be executed in secure rooms
|
|
|
|
/// (encrypted + direct), and anything else can be executed where
|
|
|
|
/// ever. Later, we can add stuff like admin/regular user power
|
|
|
|
/// separation, etc.
|
|
|
|
fn execution_allowed(cmd: &(impl Command + ?Sized), ctx: &Context<'_>) -> Result<(), CommandError> {
|
2021-05-26 21:12:21 +00:00
|
|
|
match cmd {
|
|
|
|
cmd if cmd.is_secure() && ctx.is_secure() => Ok(()),
|
|
|
|
cmd if cmd.is_secure() && !ctx.is_secure() => Err(CommandError::InsecureExecution),
|
|
|
|
_ => Ok(()),
|
2021-05-21 22:33:49 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-22 22:17:33 +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)?;
|
2021-05-21 22:33:49 +00:00
|
|
|
|
2021-05-22 22:17:33 +00:00
|
|
|
let result = match execution_allowed(cmd.as_ref(), ctx) {
|
2021-05-21 22:33:49 +00:00
|
|
|
Ok(_) => cmd.execute(ctx).await,
|
2021-05-26 21:25:32 +00:00
|
|
|
Err(e) => Err(e.into()),
|
2021-05-22 22:17:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
log_command(cmd.as_ref(), ctx, &result);
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Log result of an executed command.
|
|
|
|
fn log_command(cmd: &(impl Command + ?Sized), ctx: &Context, result: &ExecutionResult) {
|
|
|
|
use substring::Substring;
|
|
|
|
let command = match cmd.is_secure() {
|
|
|
|
true => cmd.name(),
|
|
|
|
false => ctx.message_body.substring(0, 30),
|
|
|
|
};
|
|
|
|
|
|
|
|
let dots = match ctx.message_body.len() {
|
|
|
|
_len if _len > 30 => "[...]",
|
|
|
|
_ => "",
|
|
|
|
};
|
|
|
|
|
|
|
|
match result {
|
|
|
|
Ok(_) => {
|
|
|
|
info!(
|
|
|
|
"[{}] {} <{}{}> - success",
|
|
|
|
ctx.room.display_name, ctx.username, command, dots
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
error!(
|
|
|
|
"[{}] {} <{}{}> - {}",
|
|
|
|
ctx.room.display_name, ctx.username, command, dots, e
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
2020-04-21 03:15:13 +00:00
|
|
|
}
|
2020-08-21 21:49:22 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2021-05-22 22:48:47 +00:00
|
|
|
use management::RegisterCommand;
|
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"),
|
2021-05-21 22:01:52 +00:00
|
|
|
display_name: "displayname".to_owned(),
|
|
|
|
secure: false,
|
2021-01-29 22:35:07 +00:00
|
|
|
}
|
|
|
|
};
|
2020-11-29 21:03:45 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 22:48:47 +00:00
|
|
|
macro_rules! secure_room {
|
|
|
|
() => {
|
|
|
|
crate::context::RoomContext {
|
|
|
|
id: &matrix_sdk::identifiers::room_id!("!fakeroomid:example.com"),
|
|
|
|
display_name: "displayname".to_owned(),
|
|
|
|
secure: true,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
|
|
|
async fn secure_context_secure_command_allows_execution() {
|
|
|
|
let db_path = tempfile::NamedTempFile::new_in(".").unwrap();
|
|
|
|
let db = crate::db::sqlite::Database::new(db_path.path().to_str().unwrap())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let homeserver = Url::parse("http://example.com").unwrap();
|
|
|
|
|
|
|
|
let ctx = Context {
|
2021-05-26 13:56:14 +00:00
|
|
|
account: crate::models::Account::default(),
|
2021-05-22 22:48:47 +00:00
|
|
|
db: db,
|
|
|
|
matrix_client: &matrix_sdk::Client::new(homeserver).unwrap(),
|
|
|
|
room: secure_room!(),
|
|
|
|
username: "myusername",
|
|
|
|
message_body: "!notacommand",
|
|
|
|
};
|
|
|
|
|
2021-05-26 15:28:59 +00:00
|
|
|
let cmd = RegisterCommand;
|
2021-05-22 22:48:47 +00:00
|
|
|
assert_eq!(execution_allowed(&cmd, &ctx).is_ok(), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
|
|
|
async fn secure_context_insecure_command_allows_execution() {
|
|
|
|
let db_path = tempfile::NamedTempFile::new_in(".").unwrap();
|
|
|
|
let db = crate::db::sqlite::Database::new(db_path.path().to_str().unwrap())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let homeserver = Url::parse("http://example.com").unwrap();
|
|
|
|
|
|
|
|
let ctx = Context {
|
2021-05-26 13:56:14 +00:00
|
|
|
account: crate::models::Account::default(),
|
2021-05-22 22:48:47 +00:00
|
|
|
db: db,
|
|
|
|
matrix_client: &matrix_sdk::Client::new(homeserver).unwrap(),
|
|
|
|
room: secure_room!(),
|
|
|
|
username: "myusername",
|
|
|
|
message_body: "!notacommand",
|
|
|
|
};
|
|
|
|
|
|
|
|
let cmd = variables::GetVariableCommand("".to_owned());
|
|
|
|
assert_eq!(execution_allowed(&cmd, &ctx).is_ok(), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
|
|
|
async fn insecure_context_insecure_command_allows_execution() {
|
|
|
|
let db_path = tempfile::NamedTempFile::new_in(".").unwrap();
|
|
|
|
let db = crate::db::sqlite::Database::new(db_path.path().to_str().unwrap())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let homeserver = Url::parse("http://example.com").unwrap();
|
|
|
|
|
|
|
|
let ctx = Context {
|
2021-05-26 13:56:14 +00:00
|
|
|
account: crate::models::Account::default(),
|
2021-05-22 22:48:47 +00:00
|
|
|
db: db,
|
|
|
|
matrix_client: &matrix_sdk::Client::new(homeserver).unwrap(),
|
|
|
|
room: dummy_room!(),
|
|
|
|
username: "myusername",
|
|
|
|
message_body: "!notacommand",
|
|
|
|
};
|
|
|
|
|
|
|
|
let cmd = variables::GetVariableCommand("".to_owned());
|
|
|
|
assert_eq!(execution_allowed(&cmd, &ctx).is_ok(), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
|
|
|
async fn insecure_context_secure_command_denies_execution() {
|
|
|
|
let db_path = tempfile::NamedTempFile::new_in(".").unwrap();
|
|
|
|
let db = crate::db::sqlite::Database::new(db_path.path().to_str().unwrap())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let homeserver = Url::parse("http://example.com").unwrap();
|
|
|
|
|
|
|
|
let ctx = Context {
|
2021-05-26 13:56:14 +00:00
|
|
|
account: crate::models::Account::default(),
|
2021-05-22 22:48:47 +00:00
|
|
|
db: db,
|
|
|
|
matrix_client: &matrix_sdk::Client::new(homeserver).unwrap(),
|
|
|
|
room: dummy_room!(),
|
|
|
|
username: "myusername",
|
|
|
|
message_body: "!notacommand",
|
|
|
|
};
|
|
|
|
|
2021-05-26 15:28:59 +00:00
|
|
|
let cmd = RegisterCommand;
|
2021-05-22 22:48:47 +00:00
|
|
|
assert_eq!(execution_allowed(&cmd, &ctx).is_err(), true);
|
|
|
|
}
|
|
|
|
|
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 {
|
2021-05-26 13:56:14 +00:00
|
|
|
account: crate::models::Account::default(),
|
2020-11-22 20:52:44 +00:00
|
|
|
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",
|
|
|
|
};
|
2021-05-22 22:48:47 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|