2024-08-03 05:34:38 +00:00
|
|
|
use std::{
|
2024-08-12 00:05:18 +00:00
|
|
|
fmt::Write,
|
2024-08-03 05:34:38 +00:00
|
|
|
panic::AssertUnwindSafe,
|
|
|
|
sync::{Arc, Mutex},
|
|
|
|
time::SystemTime,
|
|
|
|
};
|
2024-06-23 07:10:29 +00:00
|
|
|
|
2024-07-05 07:52:05 +00:00
|
|
|
use clap::{CommandFactory, Parser};
|
2024-08-03 05:34:38 +00:00
|
|
|
use conduit::{
|
|
|
|
debug, error,
|
|
|
|
log::{
|
|
|
|
capture,
|
|
|
|
capture::Capture,
|
|
|
|
fmt::{markdown_table, markdown_table_head},
|
|
|
|
},
|
|
|
|
trace,
|
|
|
|
utils::string::{collect_stream, common_prefix},
|
|
|
|
Error, Result,
|
|
|
|
};
|
2024-07-10 10:43:41 +00:00
|
|
|
use futures_util::future::FutureExt;
|
|
|
|
use ruma::{
|
|
|
|
events::{
|
|
|
|
relation::InReplyTo,
|
|
|
|
room::message::{Relation::Reply, RoomMessageEventContent},
|
|
|
|
},
|
|
|
|
OwnedEventId,
|
2024-05-09 15:59:08 -07:00
|
|
|
};
|
2024-07-22 22:45:25 +00:00
|
|
|
use service::{
|
2024-08-03 02:20:06 +00:00
|
|
|
admin::{CommandInput, CommandOutput, HandlerFuture, HandlerResult},
|
2024-07-22 22:45:25 +00:00
|
|
|
Services,
|
|
|
|
};
|
2024-08-03 05:34:38 +00:00
|
|
|
use tracing::Level;
|
2024-07-22 22:45:25 +00:00
|
|
|
|
2024-07-27 00:11:41 +00:00
|
|
|
use crate::{admin, admin::AdminCommand, Command};
|
2024-05-09 15:59:08 -07:00
|
|
|
|
|
|
|
#[must_use]
|
2024-07-27 07:17:07 +00:00
|
|
|
pub(super) fn complete(line: &str) -> String { complete_command(AdminCommand::command(), line) }
|
2024-07-05 07:52:05 +00:00
|
|
|
|
|
|
|
#[must_use]
|
2024-08-03 02:20:06 +00:00
|
|
|
pub(super) fn handle(services: Arc<Services>, command: CommandInput) -> HandlerFuture {
|
2024-07-27 07:17:07 +00:00
|
|
|
Box::pin(handle_command(services, command))
|
|
|
|
}
|
2024-05-09 15:59:08 -07:00
|
|
|
|
2024-06-10 06:02:17 +00:00
|
|
|
#[tracing::instrument(skip_all, name = "admin")]
|
2024-08-03 02:20:06 +00:00
|
|
|
async fn handle_command(services: Arc<Services>, command: CommandInput) -> HandlerResult {
|
2024-07-27 07:17:07 +00:00
|
|
|
AssertUnwindSafe(Box::pin(process_command(services, &command)))
|
2024-07-10 10:43:41 +00:00
|
|
|
.catch_unwind()
|
|
|
|
.await
|
|
|
|
.map_err(Error::from_panic)
|
|
|
|
.or_else(|error| handle_panic(&error, command))
|
|
|
|
}
|
|
|
|
|
2024-08-03 05:34:38 +00:00
|
|
|
async fn process_command(services: Arc<Services>, input: &CommandInput) -> CommandOutput {
|
|
|
|
let (command, args, body) = match parse(&services, input) {
|
|
|
|
Err(error) => return error,
|
|
|
|
Ok(parsed) => parsed,
|
|
|
|
};
|
|
|
|
|
|
|
|
let context = Command {
|
|
|
|
services: &services,
|
|
|
|
body: &body,
|
|
|
|
timer: SystemTime::now(),
|
|
|
|
};
|
|
|
|
|
|
|
|
process(&context, command, &args)
|
2024-07-27 07:17:07 +00:00
|
|
|
.await
|
2024-08-03 05:34:38 +00:00
|
|
|
.and_then(|content| reply(content, input.reply_id.clone()))
|
2024-07-10 10:43:41 +00:00
|
|
|
}
|
|
|
|
|
2024-08-03 02:20:06 +00:00
|
|
|
fn handle_panic(error: &Error, command: CommandInput) -> HandlerResult {
|
2024-07-10 10:43:41 +00:00
|
|
|
let link = "Please submit a [bug report](https://github.com/girlbossceo/conduwuit/issues/new). 🥺";
|
|
|
|
let msg = format!("Panic occurred while processing command:\n```\n{error:#?}\n```\n{link}");
|
|
|
|
let content = RoomMessageEventContent::notice_markdown(msg);
|
|
|
|
error!("Panic while processing command: {error:?}");
|
|
|
|
Ok(reply(content, command.reply_id))
|
|
|
|
}
|
2024-06-23 04:33:50 +00:00
|
|
|
|
2024-07-10 10:43:41 +00:00
|
|
|
fn reply(mut content: RoomMessageEventContent, reply_id: Option<OwnedEventId>) -> Option<RoomMessageEventContent> {
|
|
|
|
content.relates_to = reply_id.map(|event_id| Reply {
|
2024-06-10 06:02:17 +00:00
|
|
|
in_reply_to: InReplyTo {
|
2024-06-15 23:52:55 +00:00
|
|
|
event_id,
|
2024-06-10 06:02:17 +00:00
|
|
|
},
|
|
|
|
});
|
2024-05-09 15:59:08 -07:00
|
|
|
|
2024-07-10 10:43:41 +00:00
|
|
|
Some(content)
|
2024-05-09 15:59:08 -07:00
|
|
|
}
|
|
|
|
|
2024-07-27 07:17:07 +00:00
|
|
|
// Parse and process a message from the admin room
|
2024-08-03 05:34:38 +00:00
|
|
|
async fn process(context: &Command<'_>, command: AdminCommand, args: &[String]) -> CommandOutput {
|
|
|
|
let filter: &capture::Filter =
|
2024-08-04 06:47:37 +00:00
|
|
|
&|data| data.level() <= Level::DEBUG && data.our_modules() && data.scope.contains(&"admin");
|
2024-08-03 05:34:38 +00:00
|
|
|
let logs = Arc::new(Mutex::new(
|
|
|
|
collect_stream(|s| markdown_table_head(s)).expect("markdown table header"),
|
|
|
|
));
|
|
|
|
|
|
|
|
let capture = Capture::new(
|
|
|
|
&context.services.server.log.capture,
|
|
|
|
Some(filter),
|
|
|
|
capture::fmt(markdown_table, logs.clone()),
|
|
|
|
);
|
|
|
|
|
|
|
|
let capture_scope = capture.start();
|
|
|
|
let result = Box::pin(admin::process(command, context)).await;
|
|
|
|
drop(capture_scope);
|
|
|
|
|
|
|
|
debug!(
|
|
|
|
ok = result.is_ok(),
|
|
|
|
elapsed = ?context.timer.elapsed(),
|
|
|
|
command = ?args,
|
|
|
|
"command processed"
|
|
|
|
);
|
|
|
|
|
2024-08-12 00:05:18 +00:00
|
|
|
let mut output = String::new();
|
|
|
|
|
|
|
|
// Prepend the logs only if any were captured
|
2024-08-03 05:34:38 +00:00
|
|
|
let logs = logs.lock().expect("locked");
|
2024-08-12 00:05:18 +00:00
|
|
|
if logs.lines().count() > 2 {
|
|
|
|
writeln!(&mut output, "{logs}").expect("failed to format logs to command output");
|
|
|
|
}
|
|
|
|
drop(logs);
|
|
|
|
|
|
|
|
match result {
|
|
|
|
Ok(content) => {
|
|
|
|
write!(&mut output, "{}", content.body()).expect("failed to format command result to output");
|
|
|
|
},
|
|
|
|
Err(error) => write!(&mut output, "Command failed with error:\n```\n{error:#?}\n```")
|
|
|
|
.expect("failed to format error to command output"),
|
2024-07-27 07:17:07 +00:00
|
|
|
};
|
|
|
|
|
2024-08-03 05:34:38 +00:00
|
|
|
Some(RoomMessageEventContent::notice_markdown(output))
|
2024-07-27 07:17:07 +00:00
|
|
|
}
|
2024-05-09 15:59:08 -07:00
|
|
|
|
2024-07-27 07:17:07 +00:00
|
|
|
// Parse chat messages from the admin room into an AdminCommand object
|
2024-08-03 05:34:38 +00:00
|
|
|
fn parse<'a>(
|
|
|
|
services: &Arc<Services>, input: &'a CommandInput,
|
|
|
|
) -> Result<(AdminCommand, Vec<String>, Vec<&'a str>), CommandOutput> {
|
|
|
|
let lines = input.command.lines().filter(|line| !line.trim().is_empty());
|
|
|
|
let command_line = lines.clone().next().expect("command missing first line");
|
|
|
|
let body = lines.skip(1).collect();
|
|
|
|
match parse_command(command_line) {
|
|
|
|
Ok((command, args)) => Ok((command, args, body)),
|
|
|
|
Err(error) => {
|
|
|
|
let message = error
|
|
|
|
.to_string()
|
|
|
|
.replace("server.name", services.globals.server_name().as_str());
|
|
|
|
Err(Some(RoomMessageEventContent::notice_markdown(message)))
|
|
|
|
},
|
2024-07-28 00:24:09 +00:00
|
|
|
}
|
2024-08-03 05:34:38 +00:00
|
|
|
}
|
2024-07-28 00:24:09 +00:00
|
|
|
|
2024-08-03 05:34:38 +00:00
|
|
|
fn parse_command(line: &str) -> Result<(AdminCommand, Vec<String>)> {
|
|
|
|
let argv = parse_line(line);
|
|
|
|
let command = AdminCommand::try_parse_from(&argv)?;
|
|
|
|
Ok((command, argv))
|
2024-07-28 00:24:09 +00:00
|
|
|
}
|
|
|
|
|
2024-07-27 07:17:07 +00:00
|
|
|
fn complete_command(mut cmd: clap::Command, line: &str) -> String {
|
|
|
|
let argv = parse_line(line);
|
|
|
|
let mut ret = Vec::<String>::with_capacity(argv.len().saturating_add(1));
|
|
|
|
|
|
|
|
'token: for token in argv.into_iter().skip(1) {
|
|
|
|
let cmd_ = cmd.clone();
|
|
|
|
let mut choice = Vec::new();
|
2024-07-08 02:59:58 +00:00
|
|
|
|
2024-07-27 07:17:07 +00:00
|
|
|
for sub in cmd_.get_subcommands() {
|
|
|
|
let name = sub.get_name();
|
|
|
|
if *name == token {
|
|
|
|
// token already complete; recurse to subcommand
|
2024-07-05 07:52:05 +00:00
|
|
|
ret.push(token);
|
2024-07-27 07:17:07 +00:00
|
|
|
cmd.clone_from(sub);
|
|
|
|
continue 'token;
|
|
|
|
} else if name.starts_with(&token) {
|
|
|
|
// partial match; add to choices
|
|
|
|
choice.push(name);
|
2024-07-05 07:52:05 +00:00
|
|
|
}
|
2024-07-27 07:17:07 +00:00
|
|
|
}
|
2024-07-05 07:52:05 +00:00
|
|
|
|
2024-07-27 07:17:07 +00:00
|
|
|
if choice.len() == 1 {
|
|
|
|
// One choice. Add extra space because it's complete
|
|
|
|
let choice = *choice.first().expect("only choice");
|
|
|
|
ret.push(choice.to_owned());
|
|
|
|
ret.push(String::new());
|
|
|
|
} else if choice.is_empty() {
|
|
|
|
// Nothing found, return original string
|
|
|
|
ret.push(token);
|
|
|
|
} else {
|
|
|
|
// Find the common prefix
|
|
|
|
ret.push(common_prefix(&choice).into());
|
2024-07-05 07:52:05 +00:00
|
|
|
}
|
|
|
|
|
2024-07-27 07:17:07 +00:00
|
|
|
// Return from completion
|
|
|
|
return ret.join(" ");
|
2024-07-05 07:52:05 +00:00
|
|
|
}
|
|
|
|
|
2024-07-27 07:17:07 +00:00
|
|
|
// Return from no completion. Needs a space though.
|
|
|
|
ret.push(String::new());
|
|
|
|
ret.join(" ")
|
|
|
|
}
|
2024-07-05 07:52:05 +00:00
|
|
|
|
2024-07-27 07:17:07 +00:00
|
|
|
// Parse chat messages from the admin room into an AdminCommand object
|
|
|
|
fn parse_line(command_line: &str) -> Vec<String> {
|
|
|
|
let mut argv = command_line
|
|
|
|
.split_whitespace()
|
|
|
|
.map(str::to_owned)
|
|
|
|
.collect::<Vec<String>>();
|
2024-05-09 15:59:08 -07:00
|
|
|
|
2024-07-27 07:17:07 +00:00
|
|
|
// Remove any escapes that came with a server-side escape command
|
|
|
|
if !argv.is_empty() && argv[0].ends_with("admin") {
|
|
|
|
argv[0] = argv[0].trim_start_matches('\\').into();
|
|
|
|
}
|
2024-06-13 22:22:21 +00:00
|
|
|
|
2024-07-27 07:17:07 +00:00
|
|
|
// First indice has to be "admin" but for console convenience we add it here
|
|
|
|
if !argv.is_empty() && !argv[0].ends_with("admin") && !argv[0].starts_with('@') {
|
|
|
|
argv.insert(0, "admin".to_owned());
|
|
|
|
}
|
2024-06-10 06:02:17 +00:00
|
|
|
|
2024-07-27 07:17:07 +00:00
|
|
|
// Replace `help command` with `command --help`
|
|
|
|
// Clap has a help subcommand, but it omits the long help description.
|
|
|
|
if argv.len() > 1 && argv[1] == "help" {
|
|
|
|
argv.remove(1);
|
|
|
|
argv.push("--help".to_owned());
|
|
|
|
}
|
2024-05-09 15:59:08 -07:00
|
|
|
|
2024-07-27 07:17:07 +00:00
|
|
|
// Backwards compatibility with `register_appservice`-style commands
|
|
|
|
if argv.len() > 1 && argv[1].contains('_') {
|
|
|
|
argv[1] = argv[1].replace('_', "-");
|
|
|
|
}
|
2024-05-09 15:59:08 -07:00
|
|
|
|
2024-07-27 07:17:07 +00:00
|
|
|
// Backwards compatibility with `register_appservice`-style commands
|
|
|
|
if argv.len() > 2 && argv[2].contains('_') {
|
|
|
|
argv[2] = argv[2].replace('_', "-");
|
|
|
|
}
|
2024-05-09 15:59:08 -07:00
|
|
|
|
2024-07-27 07:17:07 +00:00
|
|
|
// if the user is using the `query` command (argv[1]), replace the database
|
|
|
|
// function/table calls with underscores to match the codebase
|
|
|
|
if argv.len() > 3 && argv[1].eq("query") {
|
|
|
|
argv[3] = argv[3].replace('_', "-");
|
2024-05-09 15:59:08 -07:00
|
|
|
}
|
2024-07-27 07:17:07 +00:00
|
|
|
|
|
|
|
trace!(?command_line, ?argv, "parse");
|
|
|
|
argv
|
2024-05-09 15:59:08 -07:00
|
|
|
}
|