2022-01-16 13:52:23 +02:00
|
|
|
use std::{convert::TryFrom, convert::TryInto, sync::Arc, time::Instant};
|
2020-11-09 12:21:04 +01:00
|
|
|
|
2022-01-16 13:52:23 +02:00
|
|
|
use crate::{
|
|
|
|
error::{Error, Result},
|
|
|
|
pdu::PduBuilder,
|
|
|
|
server_server, Database, PduEvent,
|
|
|
|
};
|
2022-01-21 17:34:21 +02:00
|
|
|
use clap::Parser;
|
2022-01-18 13:53:17 +02:00
|
|
|
use regex::Regex;
|
2022-01-16 13:52:23 +02:00
|
|
|
use rocket::{
|
|
|
|
futures::{channel::mpsc, stream::StreamExt},
|
|
|
|
http::RawStr,
|
|
|
|
};
|
2020-12-04 18:16:17 -05:00
|
|
|
use ruma::{
|
2021-10-13 10:16:45 +02:00
|
|
|
events::{room::message::RoomMessageEventContent, EventType},
|
2022-01-16 13:52:23 +02:00
|
|
|
EventId, RoomId, RoomVersionId, UserId,
|
2020-12-04 18:16:17 -05:00
|
|
|
};
|
2021-10-13 10:16:45 +02:00
|
|
|
use serde_json::value::to_raw_value;
|
2021-07-13 15:44:25 +02:00
|
|
|
use tokio::sync::{MutexGuard, RwLock, RwLockReadGuard};
|
2021-07-29 08:36:01 +02:00
|
|
|
use tracing::warn;
|
2020-11-09 12:21:04 +01:00
|
|
|
|
|
|
|
pub enum AdminCommand {
|
2020-12-08 10:33:44 +01:00
|
|
|
RegisterAppservice(serde_yaml::Value),
|
2021-12-20 15:46:36 +01:00
|
|
|
UnregisterAppservice(String),
|
2020-12-08 10:33:44 +01:00
|
|
|
ListAppservices,
|
2021-12-26 20:00:31 +01:00
|
|
|
ListLocalUsers,
|
2022-01-09 20:07:50 +01:00
|
|
|
ShowMemoryUsage,
|
2021-10-13 10:16:45 +02:00
|
|
|
SendMessage(RoomMessageEventContent),
|
2020-11-09 12:21:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Admin {
|
|
|
|
pub sender: mpsc::UnboundedSender<AdminCommand>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Admin {
|
|
|
|
pub fn start_handler(
|
|
|
|
&self,
|
2021-07-14 07:07:08 +00:00
|
|
|
db: Arc<RwLock<Database>>,
|
2020-11-09 12:21:04 +01:00
|
|
|
mut receiver: mpsc::UnboundedReceiver<AdminCommand>,
|
|
|
|
) {
|
|
|
|
tokio::spawn(async move {
|
|
|
|
// TODO: Use futures when we have long admin commands
|
|
|
|
//let mut futures = FuturesUnordered::new();
|
|
|
|
|
2021-07-14 07:07:08 +00:00
|
|
|
let guard = db.read().await;
|
2020-11-09 12:21:04 +01:00
|
|
|
|
2021-11-27 00:30:28 +01:00
|
|
|
let conduit_user = UserId::parse(format!("@conduit:{}", guard.globals.server_name()))
|
|
|
|
.expect("@conduit:server_name is valid");
|
2021-07-14 07:07:08 +00:00
|
|
|
|
|
|
|
let conduit_room = guard
|
2020-11-09 12:21:04 +01:00
|
|
|
.rooms
|
|
|
|
.id_from_alias(
|
2021-11-27 00:30:28 +01:00
|
|
|
format!("#admins:{}", guard.globals.server_name())
|
|
|
|
.as_str()
|
|
|
|
.try_into()
|
|
|
|
.expect("#admins:server_name is a valid room alias"),
|
2020-11-09 12:21:04 +01:00
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
2021-07-13 15:44:25 +02:00
|
|
|
let conduit_room = match conduit_room {
|
|
|
|
None => {
|
|
|
|
warn!("Conduit instance does not have an #admins room. Logging to that room will not work. Restart Conduit after creating a user to fix this.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Some(r) => r,
|
|
|
|
};
|
2020-12-02 15:49:50 +01:00
|
|
|
|
2021-07-14 07:07:08 +00:00
|
|
|
drop(guard);
|
|
|
|
|
2021-10-13 10:16:45 +02:00
|
|
|
let send_message = |message: RoomMessageEventContent,
|
2021-07-13 15:44:25 +02:00
|
|
|
guard: RwLockReadGuard<'_, Database>,
|
|
|
|
mutex_lock: &MutexGuard<'_, ()>| {
|
|
|
|
guard
|
|
|
|
.rooms
|
|
|
|
.build_and_append_pdu(
|
|
|
|
PduBuilder {
|
|
|
|
event_type: EventType::RoomMessage,
|
2021-10-13 10:16:45 +02:00
|
|
|
content: to_raw_value(&message)
|
2021-07-13 15:44:25 +02:00
|
|
|
.expect("event is valid, we just created it"),
|
|
|
|
unsigned: None,
|
|
|
|
state_key: None,
|
|
|
|
redacts: None,
|
|
|
|
},
|
|
|
|
&conduit_user,
|
|
|
|
&conduit_room,
|
|
|
|
&guard,
|
|
|
|
mutex_lock,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
};
|
2020-12-08 10:33:44 +01:00
|
|
|
|
2020-11-09 12:21:04 +01:00
|
|
|
loop {
|
2021-03-03 22:38:31 +01:00
|
|
|
tokio::select! {
|
2020-11-09 12:21:04 +01:00
|
|
|
Some(event) = receiver.next() => {
|
2021-07-14 07:07:08 +00:00
|
|
|
let guard = db.read().await;
|
2021-08-03 11:10:58 +02:00
|
|
|
let mutex_state = Arc::clone(
|
2021-07-13 15:44:25 +02:00
|
|
|
guard.globals
|
2021-08-03 11:10:58 +02:00
|
|
|
.roomid_mutex_state
|
2021-07-13 15:44:25 +02:00
|
|
|
.write()
|
|
|
|
.unwrap()
|
|
|
|
.entry(conduit_room.clone())
|
|
|
|
.or_default(),
|
|
|
|
);
|
2021-08-03 11:10:58 +02:00
|
|
|
let state_lock = mutex_state.lock().await;
|
2021-07-14 07:07:08 +00:00
|
|
|
|
2020-11-09 12:21:04 +01:00
|
|
|
match event {
|
2021-12-26 20:00:31 +01:00
|
|
|
AdminCommand::ListLocalUsers => {
|
2022-01-16 21:22:57 +01:00
|
|
|
match guard.users.list_local_users() {
|
2022-01-15 17:10:23 +01:00
|
|
|
Ok(users) => {
|
|
|
|
let mut msg: String = format!("Found {} local user account(s):\n", users.len());
|
|
|
|
msg += &users.join("\n");
|
|
|
|
send_message(RoomMessageEventContent::text_plain(&msg), guard, &state_lock);
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
send_message(RoomMessageEventContent::text_plain(e.to_string()), guard, &state_lock);
|
|
|
|
}
|
|
|
|
}
|
2021-12-24 23:06:54 +01:00
|
|
|
}
|
2020-12-08 10:33:44 +01:00
|
|
|
AdminCommand::RegisterAppservice(yaml) => {
|
2021-07-14 07:07:08 +00:00
|
|
|
guard.appservice.register_appservice(yaml).unwrap(); // TODO handle error
|
2020-12-08 10:33:44 +01:00
|
|
|
}
|
2021-12-20 15:46:36 +01:00
|
|
|
AdminCommand::UnregisterAppservice(service_name) => {
|
|
|
|
guard.appservice.unregister_appservice(&service_name).unwrap(); // TODO: see above
|
|
|
|
}
|
2020-12-08 10:33:44 +01:00
|
|
|
AdminCommand::ListAppservices => {
|
2021-07-14 07:07:08 +00:00
|
|
|
if let Ok(appservices) = guard.appservice.iter_ids().map(|ids| ids.collect::<Vec<_>>()) {
|
2021-06-08 18:10:00 +02:00
|
|
|
let count = appservices.len();
|
|
|
|
let output = format!(
|
|
|
|
"Appservices ({}): {}",
|
|
|
|
count,
|
|
|
|
appservices.into_iter().filter_map(|r| r.ok()).collect::<Vec<_>>().join(", ")
|
|
|
|
);
|
2021-10-13 10:16:45 +02:00
|
|
|
send_message(RoomMessageEventContent::text_plain(output), guard, &state_lock);
|
2021-06-08 18:10:00 +02:00
|
|
|
} else {
|
2021-10-13 10:16:45 +02:00
|
|
|
send_message(RoomMessageEventContent::text_plain("Failed to get appservices."), guard, &state_lock);
|
2021-06-08 18:10:00 +02:00
|
|
|
}
|
2020-12-08 10:33:44 +01:00
|
|
|
}
|
2022-01-09 20:07:50 +01:00
|
|
|
AdminCommand::ShowMemoryUsage => {
|
|
|
|
if let Ok(response) = guard._db.memory_usage() {
|
|
|
|
send_message(RoomMessageEventContent::text_plain(response), guard, &state_lock);
|
|
|
|
} else {
|
2022-01-20 00:10:39 +01:00
|
|
|
send_message(RoomMessageEventContent::text_plain("Failed to get database memory usage.".to_owned()), guard, &state_lock);
|
2022-01-09 20:07:50 +01:00
|
|
|
}
|
|
|
|
}
|
2020-12-08 10:33:44 +01:00
|
|
|
AdminCommand::SendMessage(message) => {
|
2021-08-03 11:10:58 +02:00
|
|
|
send_message(message, guard, &state_lock);
|
2020-11-09 12:21:04 +01:00
|
|
|
}
|
|
|
|
}
|
2021-07-13 15:44:25 +02:00
|
|
|
|
2021-08-03 11:10:58 +02:00
|
|
|
drop(state_lock);
|
2020-11-09 12:21:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn send(&self, command: AdminCommand) {
|
2021-06-08 18:10:00 +02:00
|
|
|
self.sender.unbounded_send(command).unwrap();
|
2020-11-09 12:21:04 +01:00
|
|
|
}
|
|
|
|
}
|
2022-01-16 13:52:23 +02:00
|
|
|
|
2022-01-21 11:06:16 +02:00
|
|
|
// Parse chat messages from the admin room into an AdminCommand object
|
2022-01-16 13:52:23 +02:00
|
|
|
pub fn parse_admin_command(db: &Database, command_line: &str, body: Vec<&str>) -> AdminCommand {
|
2022-01-18 13:53:17 +02:00
|
|
|
let mut argv: Vec<_> = command_line.split_whitespace().skip(1).collect();
|
2022-01-16 13:52:23 +02:00
|
|
|
|
2022-01-18 13:53:17 +02:00
|
|
|
let command_name = match argv.get(0) {
|
|
|
|
Some(command) => *command,
|
2022-01-16 13:52:23 +02:00
|
|
|
None => {
|
2022-01-18 13:53:17 +02:00
|
|
|
let markdown_message = "No command given. Use `help` for a list of commands.";
|
2022-01-21 17:34:21 +02:00
|
|
|
let html_message = "No command given. Use <code>help</code> for a list of commands.";
|
2022-01-18 13:53:17 +02:00
|
|
|
|
2022-01-16 13:52:23 +02:00
|
|
|
return AdminCommand::SendMessage(RoomMessageEventContent::text_html(
|
2022-01-18 13:53:17 +02:00
|
|
|
markdown_message,
|
|
|
|
html_message,
|
2022-01-16 13:52:23 +02:00
|
|
|
));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-01-21 17:34:21 +02:00
|
|
|
// Replace `help command` with `command --help`
|
|
|
|
// Clap has a help subcommand, but it omits the long help description.
|
|
|
|
if argv[0] == "help" {
|
|
|
|
argv.remove(0);
|
|
|
|
argv.push("--help");
|
|
|
|
}
|
|
|
|
|
2022-01-18 13:53:17 +02:00
|
|
|
// Backwards compatibility with `register_appservice`-style commands
|
|
|
|
let command_with_dashes;
|
2022-01-21 17:34:21 +02:00
|
|
|
if argv[0].contains("_") {
|
|
|
|
command_with_dashes = argv[0].replace("_", "-");
|
2022-01-18 13:53:17 +02:00
|
|
|
argv[0] = &command_with_dashes;
|
|
|
|
}
|
2022-01-16 13:52:23 +02:00
|
|
|
|
2022-01-18 13:53:17 +02:00
|
|
|
match try_parse_admin_command(db, argv, body) {
|
2022-01-16 13:52:23 +02:00
|
|
|
Ok(admin_command) => admin_command,
|
|
|
|
Err(error) => {
|
2022-01-18 13:53:17 +02:00
|
|
|
let markdown_message = format!(
|
|
|
|
"Encountered an error while handling the `{}` command:\n\
|
|
|
|
```\n{}\n```",
|
2022-01-16 13:52:23 +02:00
|
|
|
command_name, error,
|
|
|
|
);
|
2022-01-21 17:34:21 +02:00
|
|
|
let html_message = format!(
|
|
|
|
"Encountered an error while handling the <code>{}</code> command:\n\
|
|
|
|
<pre>\n{}\n</pre>",
|
|
|
|
command_name, error,
|
|
|
|
);
|
2022-01-16 13:52:23 +02:00
|
|
|
|
|
|
|
AdminCommand::SendMessage(RoomMessageEventContent::text_html(
|
2022-01-18 13:53:17 +02:00
|
|
|
markdown_message,
|
|
|
|
html_message,
|
2022-01-16 13:52:23 +02:00
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-21 17:34:21 +02:00
|
|
|
#[derive(Parser)]
|
|
|
|
#[clap(name = "@conduit:example.com", version = env!("CARGO_PKG_VERSION"))]
|
2022-01-18 13:53:17 +02:00
|
|
|
enum AdminCommands {
|
2022-01-21 17:34:21 +02:00
|
|
|
#[clap(verbatim_doc_comment)]
|
2022-01-21 11:06:16 +02:00
|
|
|
/// Register an appservice using its registration YAML
|
2022-01-18 13:53:17 +02:00
|
|
|
///
|
2022-01-21 11:06:16 +02:00
|
|
|
/// This command needs a YAML generated by an appservice (such as a bridge),
|
|
|
|
/// which must be provided in a Markdown code-block below the command.
|
|
|
|
///
|
|
|
|
/// Registering a new bridge using the ID of an existing bridge will replace
|
|
|
|
/// the old one.
|
2022-01-18 13:53:17 +02:00
|
|
|
///
|
2022-01-21 17:34:21 +02:00
|
|
|
/// [add-yaml-block-to-usage]
|
2022-01-18 13:53:17 +02:00
|
|
|
RegisterAppservice,
|
2022-01-21 11:06:16 +02:00
|
|
|
|
|
|
|
/// Unregister an appservice using its ID
|
2022-01-21 17:34:21 +02:00
|
|
|
///
|
2022-01-21 11:06:16 +02:00
|
|
|
/// You can find the ID using the `list-appservices` command.
|
2022-01-21 17:34:21 +02:00
|
|
|
UnregisterAppservice {
|
|
|
|
/// The appservice to unregister
|
|
|
|
appservice_identifier: String,
|
|
|
|
},
|
2022-01-21 11:06:16 +02:00
|
|
|
|
|
|
|
/// List all the currently registered appservices
|
2022-01-18 13:53:17 +02:00
|
|
|
ListAppservices,
|
2022-01-21 11:06:16 +02:00
|
|
|
|
2022-01-22 14:29:50 +02:00
|
|
|
/// List users in the database
|
|
|
|
ListLocalUsers,
|
|
|
|
|
2022-01-18 13:53:17 +02:00
|
|
|
/// Get the auth_chain of a PDU
|
2022-01-21 17:34:21 +02:00
|
|
|
GetAuthChain {
|
|
|
|
/// An event ID (the $ character followed by the base64 reference hash)
|
|
|
|
event_id: Box<EventId>,
|
|
|
|
},
|
2022-01-21 11:06:16 +02:00
|
|
|
|
2022-01-18 13:53:17 +02:00
|
|
|
/// Parse and print a PDU from a JSON
|
2022-01-21 11:06:16 +02:00
|
|
|
///
|
|
|
|
/// The PDU event is only checked for validity and is not added to the
|
|
|
|
/// database.
|
2022-01-18 13:53:17 +02:00
|
|
|
ParsePdu,
|
2022-01-21 11:06:16 +02:00
|
|
|
|
2022-01-18 13:53:17 +02:00
|
|
|
/// Retrieve and print a PDU by ID from the Conduit database
|
2022-01-21 17:34:21 +02:00
|
|
|
GetPdu {
|
|
|
|
/// An event ID (a $ followed by the base64 reference hash)
|
|
|
|
event_id: Box<EventId>,
|
|
|
|
},
|
2022-01-21 11:06:16 +02:00
|
|
|
|
2022-01-18 13:53:17 +02:00
|
|
|
/// Print database memory usage statistics
|
|
|
|
DatabaseMemoryUsage,
|
2022-01-16 13:52:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn try_parse_admin_command(
|
|
|
|
db: &Database,
|
2022-01-18 13:53:17 +02:00
|
|
|
mut argv: Vec<&str>,
|
2022-01-16 13:52:23 +02:00
|
|
|
body: Vec<&str>,
|
|
|
|
) -> Result<AdminCommand> {
|
2022-01-18 13:53:17 +02:00
|
|
|
argv.insert(0, "@conduit:example.com:");
|
2022-01-21 17:34:21 +02:00
|
|
|
let command = match AdminCommands::try_parse_from(argv) {
|
2022-01-18 13:53:17 +02:00
|
|
|
Ok(command) => command,
|
|
|
|
Err(error) => {
|
2022-01-21 17:34:21 +02:00
|
|
|
let message = error
|
|
|
|
.to_string()
|
2022-01-18 13:53:17 +02:00
|
|
|
.replace("example.com", db.globals.server_name().as_str());
|
2022-01-21 17:34:21 +02:00
|
|
|
let html_message = usage_to_html(&message);
|
2022-01-18 13:53:17 +02:00
|
|
|
|
|
|
|
return Ok(AdminCommand::SendMessage(
|
2022-01-21 17:34:21 +02:00
|
|
|
RoomMessageEventContent::text_html(message, html_message),
|
2022-01-18 13:53:17 +02:00
|
|
|
));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let admin_command = match command {
|
|
|
|
AdminCommands::RegisterAppservice => {
|
2022-01-16 13:52:23 +02:00
|
|
|
if body.len() > 2 && body[0].trim() == "```" && body.last().unwrap().trim() == "```" {
|
|
|
|
let appservice_config = body[1..body.len() - 1].join("\n");
|
|
|
|
let parsed_config = serde_yaml::from_str::<serde_yaml::Value>(&appservice_config);
|
|
|
|
match parsed_config {
|
|
|
|
Ok(yaml) => AdminCommand::RegisterAppservice(yaml),
|
|
|
|
Err(e) => AdminCommand::SendMessage(RoomMessageEventContent::text_plain(
|
|
|
|
format!("Could not parse appservice config: {}", e),
|
|
|
|
)),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
AdminCommand::SendMessage(RoomMessageEventContent::text_plain(
|
|
|
|
"Expected code block in command body.",
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
2022-01-18 13:53:17 +02:00
|
|
|
AdminCommands::UnregisterAppservice {
|
|
|
|
appservice_identifier,
|
|
|
|
} => AdminCommand::UnregisterAppservice(appservice_identifier),
|
|
|
|
AdminCommands::ListAppservices => AdminCommand::ListAppservices,
|
2022-01-22 14:29:50 +02:00
|
|
|
AdminCommands::ListLocalUsers => AdminCommand::ListLocalUsers,
|
2022-01-18 13:53:17 +02:00
|
|
|
AdminCommands::GetAuthChain { event_id } => {
|
|
|
|
let event_id = Arc::<EventId>::from(event_id);
|
|
|
|
if let Some(event) = db.rooms.get_pdu_json(&event_id)? {
|
|
|
|
let room_id_str = event
|
|
|
|
.get("room_id")
|
|
|
|
.and_then(|val| val.as_str())
|
|
|
|
.ok_or_else(|| Error::bad_database("Invalid event in database"))?;
|
|
|
|
|
|
|
|
let room_id = <&RoomId>::try_from(room_id_str).map_err(|_| {
|
|
|
|
Error::bad_database("Invalid room id field in event in database")
|
|
|
|
})?;
|
|
|
|
let start = Instant::now();
|
|
|
|
let count = server_server::get_auth_chain(room_id, vec![event_id], db)?.count();
|
|
|
|
let elapsed = start.elapsed();
|
|
|
|
return Ok(AdminCommand::SendMessage(
|
|
|
|
RoomMessageEventContent::text_plain(format!(
|
|
|
|
"Loaded auth chain with length {} in {:?}",
|
|
|
|
count, elapsed
|
|
|
|
)),
|
|
|
|
));
|
2022-01-16 13:52:23 +02:00
|
|
|
} else {
|
2022-01-18 13:53:17 +02:00
|
|
|
AdminCommand::SendMessage(RoomMessageEventContent::text_plain("Event not found."))
|
2022-01-16 13:52:23 +02:00
|
|
|
}
|
|
|
|
}
|
2022-01-18 13:53:17 +02:00
|
|
|
AdminCommands::ParsePdu => {
|
2022-01-16 13:52:23 +02:00
|
|
|
if body.len() > 2 && body[0].trim() == "```" && body.last().unwrap().trim() == "```" {
|
|
|
|
let string = body[1..body.len() - 1].join("\n");
|
|
|
|
match serde_json::from_str(&string) {
|
|
|
|
Ok(value) => {
|
|
|
|
let event_id = EventId::parse(format!(
|
|
|
|
"${}",
|
|
|
|
// Anything higher than version3 behaves the same
|
|
|
|
ruma::signatures::reference_hash(&value, &RoomVersionId::V6)
|
|
|
|
.expect("ruma can calculate reference hashes")
|
|
|
|
))
|
|
|
|
.expect("ruma's reference hashes are valid event ids");
|
|
|
|
|
|
|
|
match serde_json::from_value::<PduEvent>(
|
|
|
|
serde_json::to_value(value).expect("value is json"),
|
|
|
|
) {
|
|
|
|
Ok(pdu) => {
|
|
|
|
AdminCommand::SendMessage(RoomMessageEventContent::text_plain(
|
|
|
|
format!("EventId: {:?}\n{:#?}", event_id, pdu),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
Err(e) => AdminCommand::SendMessage(
|
|
|
|
RoomMessageEventContent::text_plain(format!(
|
|
|
|
"EventId: {:?}\nCould not parse event: {}",
|
|
|
|
event_id, e
|
|
|
|
)),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => AdminCommand::SendMessage(RoomMessageEventContent::text_plain(
|
|
|
|
format!("Invalid json in command body: {}", e),
|
|
|
|
)),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
AdminCommand::SendMessage(RoomMessageEventContent::text_plain(
|
|
|
|
"Expected code block in command body.",
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
2022-01-18 13:53:17 +02:00
|
|
|
AdminCommands::GetPdu { event_id } => {
|
|
|
|
let mut outlier = false;
|
|
|
|
let mut pdu_json = db.rooms.get_non_outlier_pdu_json(&event_id)?;
|
|
|
|
if pdu_json.is_none() {
|
|
|
|
outlier = true;
|
|
|
|
pdu_json = db.rooms.get_pdu_json(&event_id)?;
|
|
|
|
}
|
|
|
|
match pdu_json {
|
|
|
|
Some(json) => {
|
|
|
|
let json_text =
|
|
|
|
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()
|
|
|
|
),
|
2022-01-16 13:52:23 +02:00
|
|
|
))
|
|
|
|
}
|
2022-01-18 13:53:17 +02:00
|
|
|
None => {
|
|
|
|
AdminCommand::SendMessage(RoomMessageEventContent::text_plain("PDU not found."))
|
|
|
|
}
|
2022-01-16 13:52:23 +02:00
|
|
|
}
|
|
|
|
}
|
2022-01-18 13:53:17 +02:00
|
|
|
AdminCommands::DatabaseMemoryUsage => AdminCommand::ShowMemoryUsage,
|
2022-01-16 13:52:23 +02:00
|
|
|
};
|
|
|
|
|
2022-01-18 13:53:17 +02:00
|
|
|
Ok(admin_command)
|
|
|
|
}
|
|
|
|
|
2022-01-21 17:34:21 +02:00
|
|
|
// Utility to turn clap's `--help` text to HTML.
|
|
|
|
fn usage_to_html(text: &str) -> String {
|
2022-01-18 13:53:17 +02:00
|
|
|
// For the conduit admin room, subcommands become main commands
|
|
|
|
let text = text.replace("SUBCOMMAND", "COMMAND");
|
|
|
|
let text = text.replace("subcommand", "command");
|
|
|
|
|
2022-01-21 17:34:21 +02:00
|
|
|
// Escape option names (e.g. `<element-id>`) since they look like HTML tags
|
|
|
|
let text = text.replace("<", "<").replace(">", ">");
|
2022-01-18 13:53:17 +02:00
|
|
|
|
2022-01-21 17:34:21 +02:00
|
|
|
// Italicize the first line (command name and version text)
|
|
|
|
let re = Regex::new("^(.*?)\n").expect("Regex compilation should not fail");
|
|
|
|
let text = re.replace_all(&text, "<em>$1</em>\n");
|
2022-01-18 13:53:17 +02:00
|
|
|
|
2022-01-21 17:34:21 +02:00
|
|
|
// Unmerge wrapped lines
|
|
|
|
let text = text.replace("\n ", " ");
|
2022-01-18 13:53:17 +02:00
|
|
|
|
2022-01-21 17:34:21 +02:00
|
|
|
// Wrap option names in backticks. The lines look like:
|
|
|
|
// -V, --version Prints version information
|
|
|
|
// And are converted to:
|
|
|
|
// <code>-V, --version</code>: Prints version information
|
|
|
|
// (?m) enables multi-line mode for ^ and $
|
|
|
|
let re = Regex::new("(?m)^ (([a-zA-Z_&;-]+(, )?)+) +(.*)$")
|
|
|
|
.expect("Regex compilation should not fail");
|
|
|
|
let text = re.replace_all(&text, "<code>$1</code>: $4");
|
|
|
|
|
|
|
|
// // Enclose examples in code blocks
|
|
|
|
// // (?ms) enables multi-line mode and dot-matches-all
|
|
|
|
// let re =
|
|
|
|
// Regex::new("(?ms)^Example:\n(.*?)\nUSAGE:$").expect("Regex compilation should not fail");
|
|
|
|
// let text = re.replace_all(&text, "EXAMPLE:\n<pre>$1</pre>\nUSAGE:");
|
|
|
|
|
|
|
|
let has_yaml_block_marker = text.contains("\n[add-yaml-block-to-usage]\n");
|
|
|
|
let text = text.replace("\n[add-yaml-block-to-usage]\n", "");
|
|
|
|
|
|
|
|
// Add HTML line-breaks
|
|
|
|
let text = text.replace("\n", "<br>\n");
|
|
|
|
|
|
|
|
let text = if !has_yaml_block_marker {
|
|
|
|
// Wrap the usage line in code tags
|
|
|
|
let re = Regex::new("(?m)^USAGE:<br>\n (@conduit:.*)<br>$")
|
|
|
|
.expect("Regex compilation should not fail");
|
|
|
|
re.replace_all(&text, "USAGE:<br>\n<code>$1</code><br>")
|
|
|
|
} else {
|
|
|
|
// Wrap the usage line in a code block, and add a yaml block example
|
|
|
|
// This makes the usage of e.g. `register-appservice` more accurate
|
|
|
|
let re = Regex::new("(?m)^USAGE:<br>\n (.*?)<br>\n<br>\n")
|
|
|
|
.expect("Regex compilation should not fail");
|
|
|
|
re.replace_all(
|
|
|
|
&text,
|
|
|
|
"USAGE:<br>\n<pre>$1\n```\nyaml content here\n```</pre>",
|
|
|
|
)
|
|
|
|
};
|
2022-01-18 13:53:17 +02:00
|
|
|
|
|
|
|
text.to_string()
|
|
|
|
}
|