2020-10-15 16:52:08 +00:00
|
|
|
use crate::context::Context;
|
2020-10-17 15:47:17 +00:00
|
|
|
use async_trait::async_trait;
|
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
|
|
|
|
2020-10-16 12:40:25 +00:00
|
|
|
#[derive(Error, Debug)]
|
|
|
|
pub enum CommandError {
|
|
|
|
#[error("invalid command: {0}")]
|
|
|
|
InvalidCommand(String),
|
|
|
|
|
|
|
|
#[error("ignored command")]
|
|
|
|
IgnoredCommand,
|
|
|
|
}
|
|
|
|
|
2020-04-21 06:07:03 +00:00
|
|
|
pub struct Execution {
|
|
|
|
plain: String,
|
|
|
|
html: String,
|
2020-04-21 03:15:13 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 06:07:03 +00:00
|
|
|
impl Execution {
|
|
|
|
pub fn plain(&self) -> &str {
|
|
|
|
&self.plain
|
2020-04-21 03:15:13 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 06:07:03 +00:00
|
|
|
pub fn html(&self) -> &str {
|
|
|
|
&self.html
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-17 15:47:17 +00:00
|
|
|
#[async_trait]
|
|
|
|
pub trait Command: Send + Sync {
|
2020-11-03 20:14:15 +00:00
|
|
|
async fn execute(&self, ctx: &Context<'_>) -> Execution;
|
2020-08-21 21:49:22 +00:00
|
|
|
fn name(&self) -> &'static str;
|
2020-04-21 06:07:03 +00:00
|
|
|
}
|
|
|
|
|
2020-11-05 23:03:22 +00:00
|
|
|
#[derive(Debug)]
|
2020-10-15 16:52:08 +00:00
|
|
|
pub struct CommandResult {
|
|
|
|
pub plain: String,
|
|
|
|
pub html: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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.
|
2020-11-12 21:05:14 +00:00
|
|
|
pub async fn execute_command(ctx: &Context<'_>) -> CommandResult {
|
|
|
|
let res = parser::parse_command(&ctx.message_body);
|
2020-10-15 16:52:08 +00:00
|
|
|
|
|
|
|
let (plain, html) = match res {
|
2020-10-17 15:47:17 +00:00
|
|
|
Ok(cmd) => {
|
|
|
|
let execution = cmd.execute(ctx).await;
|
|
|
|
(execution.plain().into(), execution.html().into())
|
|
|
|
}
|
2020-10-15 16:52:08 +00:00
|
|
|
Err(e) => {
|
|
|
|
let message = format!("Error parsing command: {}", e);
|
|
|
|
let html_message = format!("<p><strong>{}</strong></p>", message);
|
|
|
|
(message, html_message)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let plain = format!("{}\n{}", ctx.username, plain);
|
|
|
|
let html = format!("<p>{}</p>\n{}", ctx.username, html);
|
|
|
|
|
2020-11-12 21:05:14 +00:00
|
|
|
CommandResult {
|
2020-10-15 16:52:08 +00:00
|
|
|
plain: plain,
|
|
|
|
html: html,
|
2020-11-12 21:05:14 +00:00
|
|
|
}
|
2020-04-21 03:15:13 +00:00
|
|
|
}
|
2020-08-21 21:49:22 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
2020-11-29 21:03:45 +00:00
|
|
|
/// Create a dummy room instance.
|
|
|
|
fn dummy_room() -> matrix_sdk::Room {
|
|
|
|
matrix_sdk::Room::new(
|
|
|
|
&matrix_sdk::identifiers::room_id!("!fakeroomid:example.com"),
|
|
|
|
&matrix_sdk::identifiers::user_id!("@fakeuserid:example.com"),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-11-12 21:05:14 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn unrecognized_command() {
|
|
|
|
let db = crate::db::Database::new_temp().unwrap();
|
2020-11-22 20:52:44 +00:00
|
|
|
let ctx = Context {
|
|
|
|
db: db,
|
|
|
|
matrix_client: &matrix_sdk::Client::new("http://example.com").unwrap(),
|
2020-11-29 21:03:45 +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;
|
|
|
|
assert!(result.plain.contains("Error"));
|
2020-08-21 21:49:22 +00:00
|
|
|
}
|
|
|
|
}
|