mirror of
https://gitlab.com/famedly/conduit.git
synced 2025-01-14 21:46:29 +03:00
Refactor admin commands to use structopt
This commit is contained in:
parent
13ae036ca0
commit
e378bc4a2c
3 changed files with 175 additions and 138 deletions
|
@ -18,7 +18,7 @@ First, go into the #admins room of your homeserver. The first person that
|
||||||
registered on the homeserver automatically joins it. Then send a message into
|
registered on the homeserver automatically joins it. Then send a message into
|
||||||
the room like this:
|
the room like this:
|
||||||
|
|
||||||
@conduit:your.server.name: register_appservice
|
@conduit:your.server.name: register-appservice
|
||||||
```
|
```
|
||||||
paste
|
paste
|
||||||
the
|
the
|
||||||
|
@ -31,7 +31,7 @@ the room like this:
|
||||||
```
|
```
|
||||||
|
|
||||||
You can confirm it worked by sending a message like this:
|
You can confirm it worked by sending a message like this:
|
||||||
`@conduit:your.server.name: list_appservices`
|
`@conduit:your.server.name: list-appservices`
|
||||||
|
|
||||||
The @conduit bot should answer with `Appservices (1): your-bridge`
|
The @conduit bot should answer with `Appservices (1): your-bridge`
|
||||||
|
|
||||||
|
@ -46,9 +46,9 @@ could help.
|
||||||
|
|
||||||
To remove an appservice go to your admin room and execute
|
To remove an appservice go to your admin room and execute
|
||||||
|
|
||||||
```@conduit:your.server.name: unregister_appservice <name>```
|
```@conduit:your.server.name: unregister-appservice <name>```
|
||||||
|
|
||||||
where `<name>` one of the output of `list_appservices`.
|
where `<name>` one of the output of `list-appservices`.
|
||||||
|
|
||||||
### Tested appservices
|
### Tested appservices
|
||||||
|
|
||||||
|
|
|
@ -83,6 +83,9 @@ thread_local = "1.1.3"
|
||||||
# used for TURN server authentication
|
# used for TURN server authentication
|
||||||
hmac = "0.11.0"
|
hmac = "0.11.0"
|
||||||
sha-1 = "0.9.8"
|
sha-1 = "0.9.8"
|
||||||
|
# used for conduit's CLI and admin room command parsing
|
||||||
|
structopt = { version = "0.3.25", default-features = false }
|
||||||
|
pulldown-cmark = "0.9.1"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["conduit_bin", "backend_sqlite", "backend_rocksdb"]
|
default = ["conduit_bin", "backend_sqlite", "backend_rocksdb"]
|
||||||
|
|
|
@ -5,6 +5,7 @@ use crate::{
|
||||||
pdu::PduBuilder,
|
pdu::PduBuilder,
|
||||||
server_server, Database, PduEvent,
|
server_server, Database, PduEvent,
|
||||||
};
|
};
|
||||||
|
use regex::Regex;
|
||||||
use rocket::{
|
use rocket::{
|
||||||
futures::{channel::mpsc, stream::StreamExt},
|
futures::{channel::mpsc, stream::StreamExt},
|
||||||
http::RawStr,
|
http::RawStr,
|
||||||
|
@ -14,6 +15,7 @@ use ruma::{
|
||||||
EventId, RoomId, RoomVersionId, UserId,
|
EventId, RoomId, RoomVersionId, UserId,
|
||||||
};
|
};
|
||||||
use serde_json::value::to_raw_value;
|
use serde_json::value::to_raw_value;
|
||||||
|
use structopt::StructOpt;
|
||||||
use tokio::sync::{MutexGuard, RwLock, RwLockReadGuard};
|
use tokio::sync::{MutexGuard, RwLock, RwLockReadGuard};
|
||||||
use tracing::warn;
|
use tracing::warn;
|
||||||
|
|
||||||
|
@ -146,78 +148,98 @@ impl Admin {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_admin_command(db: &Database, command_line: &str, body: Vec<&str>) -> AdminCommand {
|
pub fn parse_admin_command(db: &Database, command_line: &str, body: Vec<&str>) -> AdminCommand {
|
||||||
let mut parts = command_line.split_whitespace().skip(1);
|
let mut argv: Vec<_> = command_line.split_whitespace().skip(1).collect();
|
||||||
|
|
||||||
let command_name = match parts.next() {
|
let command_name = match argv.get(0) {
|
||||||
Some(command) => command,
|
Some(command) => *command,
|
||||||
None => {
|
None => {
|
||||||
let message = "No command given. Use <code>help</code> for a list of commands.";
|
let markdown_message = "No command given. Use `help` for a list of commands.";
|
||||||
|
let html_message = markdown_to_html(&markdown_message);
|
||||||
|
|
||||||
return AdminCommand::SendMessage(RoomMessageEventContent::text_html(
|
return AdminCommand::SendMessage(RoomMessageEventContent::text_html(
|
||||||
html_to_markdown(message),
|
markdown_message,
|
||||||
message,
|
html_message,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let args: Vec<_> = parts.collect();
|
// Backwards compatibility with `register_appservice`-style commands
|
||||||
|
let command_with_dashes;
|
||||||
|
if command_line.contains("_") {
|
||||||
|
command_with_dashes = command_name.replace("_", "-");
|
||||||
|
argv[0] = &command_with_dashes;
|
||||||
|
}
|
||||||
|
|
||||||
match try_parse_admin_command(db, command_name, args, body) {
|
match try_parse_admin_command(db, argv, body) {
|
||||||
Ok(admin_command) => admin_command,
|
Ok(admin_command) => admin_command,
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
let message = format!(
|
let markdown_message = format!(
|
||||||
"Encountered error while handling <code>{}</code> command:\n\
|
"Encountered an error while handling the `{}` command:\n\
|
||||||
<pre>{}</pre>",
|
```\n{}\n```",
|
||||||
command_name, error,
|
command_name, error,
|
||||||
);
|
);
|
||||||
|
let html_message = markdown_to_html(&markdown_message);
|
||||||
|
|
||||||
AdminCommand::SendMessage(RoomMessageEventContent::text_html(
|
AdminCommand::SendMessage(RoomMessageEventContent::text_html(
|
||||||
html_to_markdown(&message),
|
markdown_message,
|
||||||
message,
|
html_message,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper for `RoomMessageEventContent::text_html`, which needs the content as
|
#[derive(StructOpt)]
|
||||||
// both markdown and HTML.
|
enum AdminCommands {
|
||||||
fn html_to_markdown(text: &str) -> String {
|
#[structopt(verbatim_doc_comment)]
|
||||||
text.replace("<p>", "")
|
/// Register a bridge using its registration YAML
|
||||||
.replace("</p>", "\n")
|
///
|
||||||
.replace("<pre>", "```\n")
|
/// This command needs a YAML generated by an appservice (such as a mautrix
|
||||||
.replace("</pre>", "\n```")
|
/// bridge), which must be provided in a code-block below the command.
|
||||||
.replace("<code>", "`")
|
///
|
||||||
.replace("</code>", "`")
|
/// Example:
|
||||||
.replace("<li>", "* ")
|
/// ````
|
||||||
.replace("</li>", "")
|
/// @conduit:example.com: register-appservice
|
||||||
.replace("<ul>\n", "")
|
/// ```
|
||||||
.replace("</ul>\n", "")
|
/// yaml content here
|
||||||
|
/// ```
|
||||||
|
/// ````
|
||||||
|
RegisterAppservice,
|
||||||
|
/// Unregister a bridge using its ID
|
||||||
|
UnregisterAppservice { appservice_identifier: String },
|
||||||
|
/// List all the currently registered bridges
|
||||||
|
ListAppservices,
|
||||||
|
/// Get the auth_chain of a PDU
|
||||||
|
GetAuthChain { event_id: Box<EventId> },
|
||||||
|
/// Parse and print a PDU from a JSON
|
||||||
|
ParsePdu,
|
||||||
|
/// Retrieve and print a PDU by ID from the Conduit database
|
||||||
|
GetPdu { event_id: Box<EventId> },
|
||||||
|
/// Print database memory usage statistics
|
||||||
|
DatabaseMemoryUsage,
|
||||||
}
|
}
|
||||||
|
|
||||||
const HELP_TEXT: &'static str = r#"
|
|
||||||
<p>The following commands are available:</p>
|
|
||||||
<ul>
|
|
||||||
<li><code>register_appservice</code>: Register a bridge using its registration YAML</li>
|
|
||||||
<li><code>unregister_appservice</code>: Unregister a bridge using its ID</li>
|
|
||||||
<li><code>list_appservices</code>: List all the currently registered bridges</li>
|
|
||||||
<li><code>get_auth_chain</code>: Get the `auth_chain` of a PDU</li>
|
|
||||||
<li><code>parse_pdu</code>: Parse and print a PDU from a JSON</li>
|
|
||||||
<li><code>get_pdu</code>: Retrieve and print a PDU by ID from the Conduit database</li>
|
|
||||||
<li><code>database_memory_usage</code>: Print database memory usage statistics</li>
|
|
||||||
<ul>
|
|
||||||
"#;
|
|
||||||
|
|
||||||
pub fn try_parse_admin_command(
|
pub fn try_parse_admin_command(
|
||||||
db: &Database,
|
db: &Database,
|
||||||
command: &str,
|
mut argv: Vec<&str>,
|
||||||
args: Vec<&str>,
|
|
||||||
body: Vec<&str>,
|
body: Vec<&str>,
|
||||||
) -> Result<AdminCommand> {
|
) -> Result<AdminCommand> {
|
||||||
let command = match command {
|
argv.insert(0, "@conduit:example.com:");
|
||||||
"help" => AdminCommand::SendMessage(RoomMessageEventContent::text_html(
|
let command = match AdminCommands::from_iter_safe(argv) {
|
||||||
html_to_markdown(HELP_TEXT),
|
Ok(command) => command,
|
||||||
HELP_TEXT,
|
Err(error) => {
|
||||||
)),
|
println!("Before:\n{}\n", error.to_string());
|
||||||
"register_appservice" => {
|
let markdown_message = usage_to_markdown(&error.to_string())
|
||||||
|
.replace("example.com", db.globals.server_name().as_str());
|
||||||
|
let html_message = markdown_to_html(&markdown_message);
|
||||||
|
|
||||||
|
return Ok(AdminCommand::SendMessage(
|
||||||
|
RoomMessageEventContent::text_html(markdown_message, html_message),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let admin_command = match command {
|
||||||
|
AdminCommands::RegisterAppservice => {
|
||||||
if body.len() > 2 && body[0].trim() == "```" && body.last().unwrap().trim() == "```" {
|
if body.len() > 2 && body[0].trim() == "```" && body.last().unwrap().trim() == "```" {
|
||||||
let appservice_config = body[1..body.len() - 1].join("\n");
|
let appservice_config = body[1..body.len() - 1].join("\n");
|
||||||
let parsed_config = serde_yaml::from_str::<serde_yaml::Value>(&appservice_config);
|
let parsed_config = serde_yaml::from_str::<serde_yaml::Value>(&appservice_config);
|
||||||
|
@ -233,19 +255,12 @@ pub fn try_parse_admin_command(
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
"unregister_appservice" => {
|
AdminCommands::UnregisterAppservice {
|
||||||
if args.len() == 1 {
|
appservice_identifier,
|
||||||
AdminCommand::UnregisterAppservice(args[0].to_owned())
|
} => AdminCommand::UnregisterAppservice(appservice_identifier),
|
||||||
} else {
|
AdminCommands::ListAppservices => AdminCommand::ListAppservices,
|
||||||
AdminCommand::SendMessage(RoomMessageEventContent::text_plain(
|
AdminCommands::GetAuthChain { event_id } => {
|
||||||
"Missing appservice identifier",
|
let event_id = Arc::<EventId>::from(event_id);
|
||||||
))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
"list_appservices" => AdminCommand::ListAppservices,
|
|
||||||
"get_auth_chain" => {
|
|
||||||
if args.len() == 1 {
|
|
||||||
if let Ok(event_id) = EventId::parse_arc(args[0]) {
|
|
||||||
if let Some(event) = db.rooms.get_pdu_json(&event_id)? {
|
if let Some(event) = db.rooms.get_pdu_json(&event_id)? {
|
||||||
let room_id_str = event
|
let room_id_str = event
|
||||||
.get("room_id")
|
.get("room_id")
|
||||||
|
@ -256,8 +271,7 @@ pub fn try_parse_admin_command(
|
||||||
Error::bad_database("Invalid room id field in event in database")
|
Error::bad_database("Invalid room id field in event in database")
|
||||||
})?;
|
})?;
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
let count =
|
let count = server_server::get_auth_chain(room_id, vec![event_id], db)?.count();
|
||||||
server_server::get_auth_chain(room_id, vec![event_id], db)?.count();
|
|
||||||
let elapsed = start.elapsed();
|
let elapsed = start.elapsed();
|
||||||
return Ok(AdminCommand::SendMessage(
|
return Ok(AdminCommand::SendMessage(
|
||||||
RoomMessageEventContent::text_plain(format!(
|
RoomMessageEventContent::text_plain(format!(
|
||||||
|
@ -265,15 +279,11 @@ pub fn try_parse_admin_command(
|
||||||
count, elapsed
|
count, elapsed
|
||||||
)),
|
)),
|
||||||
));
|
));
|
||||||
|
} else {
|
||||||
|
AdminCommand::SendMessage(RoomMessageEventContent::text_plain("Event not found."))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
AdminCommands::ParsePdu => {
|
||||||
|
|
||||||
AdminCommand::SendMessage(RoomMessageEventContent::text_plain(
|
|
||||||
"Usage: get_auth_chain <event-id>",
|
|
||||||
))
|
|
||||||
}
|
|
||||||
"parse_pdu" => {
|
|
||||||
if body.len() > 2 && body[0].trim() == "```" && body.last().unwrap().trim() == "```" {
|
if body.len() > 2 && body[0].trim() == "```" && body.last().unwrap().trim() == "```" {
|
||||||
let string = body[1..body.len() - 1].join("\n");
|
let string = body[1..body.len() - 1].join("\n");
|
||||||
match serde_json::from_str(&string) {
|
match serde_json::from_str(&string) {
|
||||||
|
@ -312,9 +322,7 @@ pub fn try_parse_admin_command(
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
"get_pdu" => {
|
AdminCommands::GetPdu { event_id } => {
|
||||||
if args.len() == 1 {
|
|
||||||
if let Ok(event_id) = EventId::parse(args[0]) {
|
|
||||||
let mut outlier = false;
|
let mut outlier = false;
|
||||||
let mut pdu_json = db.rooms.get_non_outlier_pdu_json(&event_id)?;
|
let mut pdu_json = db.rooms.get_non_outlier_pdu_json(&event_id)?;
|
||||||
if pdu_json.is_none() {
|
if pdu_json.is_none() {
|
||||||
|
@ -323,48 +331,74 @@ pub fn try_parse_admin_command(
|
||||||
}
|
}
|
||||||
match pdu_json {
|
match pdu_json {
|
||||||
Some(json) => {
|
Some(json) => {
|
||||||
let json_text = serde_json::to_string_pretty(&json)
|
let json_text =
|
||||||
.expect("canonical json is valid json");
|
serde_json::to_string_pretty(&json).expect("canonical json is valid json");
|
||||||
AdminCommand::SendMessage(
|
|
||||||
RoomMessageEventContent::text_html(
|
|
||||||
format!("{}\n```json\n{}\n```",
|
|
||||||
if outlier {
|
|
||||||
"PDU is outlier"
|
|
||||||
} else { "PDU was accepted"}, json_text),
|
|
||||||
format!("<p>{}</p>\n<pre><code class=\"language-json\">{}\n</code></pre>\n",
|
|
||||||
if outlier {
|
|
||||||
"PDU is outlier"
|
|
||||||
} else { "PDU was accepted"}, RawStr::new(&json_text).html_escape())
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
None => AdminCommand::SendMessage(RoomMessageEventContent::text_plain(
|
|
||||||
"PDU not found.",
|
|
||||||
)),
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
AdminCommand::SendMessage(RoomMessageEventContent::text_plain(
|
|
||||||
"Event ID could not be parsed.",
|
|
||||||
))
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
AdminCommand::SendMessage(RoomMessageEventContent::text_plain(
|
|
||||||
"Usage: get_pdu <eventid>",
|
|
||||||
))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
"database_memory_usage" => AdminCommand::ShowMemoryUsage,
|
|
||||||
_ => {
|
|
||||||
let message = format!(
|
|
||||||
"Unrecognized command <code>{}</code>, try <code>help</code> for a list of commands.",
|
|
||||||
command,
|
|
||||||
);
|
|
||||||
AdminCommand::SendMessage(RoomMessageEventContent::text_html(
|
AdminCommand::SendMessage(RoomMessageEventContent::text_html(
|
||||||
html_to_markdown(&message),
|
format!(
|
||||||
message,
|
"{}\n```json\n{}\n```",
|
||||||
|
if outlier {
|
||||||
|
"PDU is outlier"
|
||||||
|
} else {
|
||||||
|
"PDU was accepted"
|
||||||
|
},
|
||||||
|
json_text
|
||||||
|
),
|
||||||
|
format!(
|
||||||
|
"<p>{}</p>\n<pre><code class=\"language-json\">{}\n</code></pre>\n",
|
||||||
|
if outlier {
|
||||||
|
"PDU is outlier"
|
||||||
|
} else {
|
||||||
|
"PDU was accepted"
|
||||||
|
},
|
||||||
|
RawStr::new(&json_text).html_escape()
|
||||||
|
),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
None => {
|
||||||
|
AdminCommand::SendMessage(RoomMessageEventContent::text_plain("PDU not found."))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
AdminCommands::DatabaseMemoryUsage => AdminCommand::ShowMemoryUsage,
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(command)
|
Ok(admin_command)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn usage_to_markdown(text: &str) -> String {
|
||||||
|
// For the conduit admin room, subcommands become main commands
|
||||||
|
let text = text.replace("SUBCOMMAND", "COMMAND");
|
||||||
|
let text = text.replace("subcommand", "command");
|
||||||
|
|
||||||
|
// Put the first line (command name and version text) on its own paragraph
|
||||||
|
let re = Regex::new("^(.*?)\n").expect("Regex compilation should not fail");
|
||||||
|
let text = re.replace_all(&text, "*$1*\n\n");
|
||||||
|
|
||||||
|
// Wrap command names in backticks
|
||||||
|
// (?m) enables multi-line mode for ^ and $
|
||||||
|
let re = Regex::new("(?m)^ ([a-z-]+) +(.*)$").expect("Regex compilation should not fail");
|
||||||
|
let text = re.replace_all(&text, " `$1`: $2");
|
||||||
|
|
||||||
|
// Add * to list items
|
||||||
|
let re = Regex::new("(?m)^ (.*)$").expect("Regex compilation should not fail");
|
||||||
|
let text = re.replace_all(&text, "* $1");
|
||||||
|
|
||||||
|
// Turn section names to headings
|
||||||
|
let re = Regex::new("(?m)^([A-Z-]+):$").expect("Regex compilation should not fail");
|
||||||
|
let text = re.replace_all(&text, "#### $1");
|
||||||
|
|
||||||
|
text.to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn markdown_to_html(text: &str) -> String {
|
||||||
|
// CommonMark's spec allows HTML tags; however, CLI required arguments look
|
||||||
|
// very much like tags so escape them.
|
||||||
|
let text = text.replace("<", "<").replace(">", ">");
|
||||||
|
|
||||||
|
let mut html_output = String::new();
|
||||||
|
|
||||||
|
let parser = pulldown_cmark::Parser::new(&text);
|
||||||
|
pulldown_cmark::html::push_html(&mut html_output, parser);
|
||||||
|
|
||||||
|
html_output
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue