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;
|
2021-05-27 15:38:38 +00:00
|
|
|
pub mod rooms;
|
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-05-26 22:20:53 +00:00
|
|
|
|
|
|
|
fn message_plain(&self, username: &str) -> String;
|
2021-01-30 14:17:34 +00:00
|
|
|
}
|
|
|
|
|
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-05-26 22:20:53 +00:00
|
|
|
Ok(resp) => format!("<p>{}</p>", resp.html).replace("\n", "<br/>"),
|
|
|
|
Err(e) => format!("<p>{}: <strong>{}</strong></p>", username, e).replace("\n", "<br/>"),
|
2021-01-30 14:17:34 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-26 22:20:53 +00:00
|
|
|
|
|
|
|
fn message_plain(&self, username: &str) -> String {
|
|
|
|
let message = match self {
|
|
|
|
Ok(resp) => format!("{}", resp.html),
|
|
|
|
Err(e) => format!("{}", e),
|
|
|
|
};
|
|
|
|
|
|
|
|
format!(
|
|
|
|
"{}:\n{}",
|
|
|
|
username,
|
|
|
|
html2text::from_read(message.as_bytes(), message.len())
|
|
|
|
)
|
|
|
|
}
|
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
|
2021-05-30 22:53:56 +00:00
|
|
|
/// go back to Matrix, if the command was executed, whether or not the
|
|
|
|
/// command was successful.
|
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(),
|
2021-05-28 14:35:14 +00:00
|
|
|
false => ctx.message_body,
|
2021-05-22 22:17:33 +00:00
|
|
|
};
|
|
|
|
|
2021-05-28 14:35:14 +00:00
|
|
|
let dots = match command.len() {
|
2021-05-22 22:17:33 +00:00
|
|
|
_len if _len > 30 => "[...]",
|
|
|
|
_ => "",
|
|
|
|
};
|
|
|
|
|
2021-05-28 14:35:14 +00:00
|
|
|
let command = command.substring(0, 30);
|
|
|
|
|
2021-05-22 22:17:33 +00:00
|
|
|
match result {
|
|
|
|
Ok(_) => {
|
|
|
|
info!(
|
|
|
|
"[{}] {} <{}{}> - success",
|
2021-05-29 22:29:30 +00:00
|
|
|
ctx.origin_room.display_name, ctx.username, command, dots
|
2021-05-22 22:17:33 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
error!(
|
|
|
|
"[{}] {} <{}{}> - {}",
|
2021-05-29 22:29:30 +00:00
|
|
|
ctx.origin_room.display_name, ctx.username, command, dots, e
|
2021-05-22 22:17:33 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
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,
|
2021-05-29 22:29:30 +00:00
|
|
|
matrix_client: matrix_sdk::Client::new(homeserver).unwrap(),
|
|
|
|
origin_room: secure_room!(),
|
|
|
|
active_room: secure_room!(),
|
2021-05-22 22:48:47 +00:00
|
|
|
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,
|
2021-05-29 22:29:30 +00:00
|
|
|
matrix_client: matrix_sdk::Client::new(homeserver).unwrap(),
|
|
|
|
origin_room: secure_room!(),
|
|
|
|
active_room: secure_room!(),
|
2021-05-22 22:48:47 +00:00
|
|
|
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,
|
2021-05-29 22:29:30 +00:00
|
|
|
matrix_client: matrix_sdk::Client::new(homeserver).unwrap(),
|
|
|
|
origin_room: dummy_room!(),
|
|
|
|
active_room: dummy_room!(),
|
2021-05-22 22:48:47 +00:00
|
|
|
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,
|
2021-05-29 22:29:30 +00:00
|
|
|
matrix_client: matrix_sdk::Client::new(homeserver).unwrap(),
|
|
|
|
origin_room: dummy_room!(),
|
|
|
|
active_room: dummy_room!(),
|
2021-05-22 22:48:47 +00:00
|
|
|
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-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-29 22:29:30 +00:00
|
|
|
matrix_client: matrix_sdk::Client::new(homeserver).unwrap(),
|
|
|
|
origin_room: dummy_room!(),
|
|
|
|
active_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
|
|
|
}
|
|
|
|
}
|