2024-07-06 06:47:42 +00:00
|
|
|
use conduit::{utils::time, warn, Error, Result};
|
2024-04-20 19:13:18 -04:00
|
|
|
use ruma::events::room::message::RoomMessageEventContent;
|
|
|
|
|
2024-06-16 22:26:52 +00:00
|
|
|
use crate::services;
|
2024-04-20 19:13:18 -04:00
|
|
|
|
2024-06-16 22:26:52 +00:00
|
|
|
pub(super) async fn uptime(_body: Vec<&str>) -> Result<RoomMessageEventContent> {
|
2024-07-06 06:47:42 +00:00
|
|
|
let elapsed = services()
|
2024-05-09 15:59:08 -07:00
|
|
|
.server
|
2024-04-26 18:55:45 -07:00
|
|
|
.started
|
|
|
|
.elapsed()
|
2024-07-06 06:47:42 +00:00
|
|
|
.expect("standard duration");
|
|
|
|
|
|
|
|
let result = time::pretty(elapsed);
|
|
|
|
Ok(RoomMessageEventContent::notice_plain(format!("{result}.")))
|
2024-04-26 18:55:45 -07:00
|
|
|
}
|
|
|
|
|
2024-06-16 22:26:52 +00:00
|
|
|
pub(super) async fn show_config(_body: Vec<&str>) -> Result<RoomMessageEventContent> {
|
2024-04-20 19:13:18 -04:00
|
|
|
// Construct and send the response
|
|
|
|
Ok(RoomMessageEventContent::text_plain(format!("{}", services().globals.config)))
|
|
|
|
}
|
|
|
|
|
2024-06-16 22:26:52 +00:00
|
|
|
pub(super) async fn memory_usage(_body: Vec<&str>) -> Result<RoomMessageEventContent> {
|
2024-07-04 03:26:19 +00:00
|
|
|
let response0 = services().memory_usage().await?;
|
|
|
|
let response1 = services().db.db.memory_usage()?;
|
2024-05-09 15:59:08 -07:00
|
|
|
let response2 = conduit::alloc::memory_usage();
|
2024-04-20 19:13:18 -04:00
|
|
|
|
|
|
|
Ok(RoomMessageEventContent::text_plain(format!(
|
2024-07-04 03:26:19 +00:00
|
|
|
"Services:\n{response0}\nDatabase:\n{response1}\n{}",
|
2024-05-01 21:50:46 -04:00
|
|
|
if !response2.is_empty() {
|
2024-05-03 21:42:47 -04:00
|
|
|
format!("Allocator:\n {response2}")
|
2024-05-01 21:50:46 -04:00
|
|
|
} else {
|
2024-05-03 21:42:47 -04:00
|
|
|
String::new()
|
2024-05-01 21:50:46 -04:00
|
|
|
}
|
2024-04-20 19:13:18 -04:00
|
|
|
)))
|
|
|
|
}
|
|
|
|
|
2024-07-04 03:26:19 +00:00
|
|
|
pub(super) async fn clear_caches(_body: Vec<&str>) -> Result<RoomMessageEventContent> {
|
|
|
|
services().clear_cache().await;
|
2024-04-20 19:13:18 -04:00
|
|
|
|
|
|
|
Ok(RoomMessageEventContent::text_plain("Done."))
|
|
|
|
}
|
|
|
|
|
2024-06-16 22:26:52 +00:00
|
|
|
pub(super) async fn list_backups(_body: Vec<&str>) -> Result<RoomMessageEventContent> {
|
2024-04-20 19:13:18 -04:00
|
|
|
let result = services().globals.db.backup_list()?;
|
|
|
|
|
|
|
|
if result.is_empty() {
|
|
|
|
Ok(RoomMessageEventContent::text_plain("No backups found."))
|
|
|
|
} else {
|
|
|
|
Ok(RoomMessageEventContent::text_plain(result))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-16 22:26:52 +00:00
|
|
|
pub(super) async fn backup_database(_body: Vec<&str>) -> Result<RoomMessageEventContent> {
|
2024-05-09 15:59:08 -07:00
|
|
|
let mut result = services()
|
|
|
|
.server
|
|
|
|
.runtime()
|
|
|
|
.spawn_blocking(move || match services().globals.db.backup() {
|
|
|
|
Ok(()) => String::new(),
|
|
|
|
Err(e) => (*e).to_string(),
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2024-04-20 19:13:18 -04:00
|
|
|
|
|
|
|
if result.is_empty() {
|
|
|
|
result = services().globals.db.backup_list()?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(RoomMessageEventContent::text_plain(&result))
|
|
|
|
}
|
|
|
|
|
2024-06-16 22:26:52 +00:00
|
|
|
pub(super) async fn list_database_files(_body: Vec<&str>) -> Result<RoomMessageEventContent> {
|
2024-04-20 19:13:18 -04:00
|
|
|
let result = services().globals.db.file_list()?;
|
2024-06-16 23:09:54 +00:00
|
|
|
|
|
|
|
Ok(RoomMessageEventContent::notice_markdown(result))
|
2024-04-20 19:13:18 -04:00
|
|
|
}
|
2024-06-16 01:44:41 +00:00
|
|
|
|
2024-06-16 22:26:52 +00:00
|
|
|
pub(super) async fn admin_notice(_body: Vec<&str>, message: Vec<String>) -> Result<RoomMessageEventContent> {
|
2024-06-16 02:10:47 +00:00
|
|
|
let message = message.join(" ");
|
|
|
|
services().admin.send_text(&message).await;
|
|
|
|
|
|
|
|
Ok(RoomMessageEventContent::notice_plain("Notice was sent to #admins"))
|
|
|
|
}
|
|
|
|
|
2024-06-16 01:44:41 +00:00
|
|
|
#[cfg(conduit_mods)]
|
2024-06-16 22:26:52 +00:00
|
|
|
pub(super) async fn reload(_body: Vec<&str>) -> Result<RoomMessageEventContent> {
|
2024-06-16 01:44:41 +00:00
|
|
|
services().server.reload()?;
|
|
|
|
|
2024-06-16 19:44:46 +00:00
|
|
|
Ok(RoomMessageEventContent::notice_plain("Reloading server..."))
|
2024-06-16 01:44:41 +00:00
|
|
|
}
|
|
|
|
|
2024-06-16 19:46:32 +00:00
|
|
|
#[cfg(unix)]
|
2024-07-03 04:46:50 +00:00
|
|
|
pub(super) async fn restart(_body: Vec<&str>, force: bool) -> Result<RoomMessageEventContent> {
|
|
|
|
use conduit::utils::sys::current_exe_deleted;
|
|
|
|
|
|
|
|
if !force && current_exe_deleted() {
|
|
|
|
return Err(Error::Err(
|
|
|
|
"The server cannot be restarted because the executable was tampered with. If this is expected use --force \
|
|
|
|
to override."
|
|
|
|
.to_owned(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2024-06-16 19:46:32 +00:00
|
|
|
services().server.restart()?;
|
|
|
|
|
|
|
|
Ok(RoomMessageEventContent::notice_plain("Restarting server..."))
|
|
|
|
}
|
|
|
|
|
2024-06-16 22:26:52 +00:00
|
|
|
pub(super) async fn shutdown(_body: Vec<&str>) -> Result<RoomMessageEventContent> {
|
2024-06-16 01:44:41 +00:00
|
|
|
warn!("shutdown command");
|
|
|
|
services().server.shutdown()?;
|
|
|
|
|
2024-06-16 19:44:46 +00:00
|
|
|
Ok(RoomMessageEventContent::notice_plain("Shutting down server..."))
|
2024-06-16 01:44:41 +00:00
|
|
|
}
|