admin terminal console

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-06-10 06:02:17 +00:00
parent 5df7443437
commit 571ab6ac2b
17 changed files with 1190 additions and 681 deletions

316
Cargo.lock generated
View file

@ -130,7 +130,7 @@ checksum = "16e62a023e7c117e27523144c5d2459f4397fcc3cab0085af8e2224f643a0193"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.66",
]
[[package]]
@ -141,7 +141,7 @@ checksum = "c6fa2087f2753a7da8cc1c0dbfcf89579dd57458e36769de5ac750b4671737ca"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.66",
]
[[package]]
@ -389,7 +389,7 @@ dependencies = [
"regex",
"rustc-hash",
"shlex",
"syn",
"syn 2.0.66",
]
[[package]]
@ -510,6 +510,12 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "cfg_aliases"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd16c4719339c4530435d38e511904438d07cce7950afa3718a84ac36c10e89e"
[[package]]
name = "cfg_aliases"
version = "0.2.1"
@ -565,7 +571,7 @@ dependencies = [
"heck 0.5.0",
"proc-macro2",
"quote",
"syn",
"syn 2.0.66",
]
[[package]]
@ -574,6 +580,15 @@ version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4b82cf0babdbd58558212896d1a4272303a57bdb245c2bf1147185fb45640e70"
[[package]]
name = "clipboard-win"
version = "5.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "79f4473f5144e20d9aceaf2972478f06ddf687831eafeeb434fbaf0acc4144ad"
dependencies = [
"error-code",
]
[[package]]
name = "color_quant"
version = "1.1.0"
@ -681,7 +696,7 @@ dependencies = [
"itertools 0.13.0",
"libloading",
"log",
"nix",
"nix 0.29.0",
"parking_lot",
"rand",
"regex",
@ -781,10 +796,12 @@ dependencies = [
"reqwest",
"ruma",
"ruma-identifiers-validation",
"rustyline",
"serde",
"serde_json",
"serde_yaml",
"sha2",
"termimad",
"tokio",
"tracing",
"url",
@ -840,6 +857,15 @@ version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6051f239ecec86fde3410901ab7860d458d160371533842974fc61f96d15879b"
[[package]]
name = "coolor"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37e93977247fb916abeee1ff8c6594c9b421fd9c26c9b720a3944acb2a7de27b"
dependencies = [
"crossterm",
]
[[package]]
name = "core-foundation"
version = "0.9.4"
@ -874,6 +900,45 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "crokey"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b48209802ec5862bb034cb16719eec24d1c759e62921be7d3c899d0d85f3344b"
dependencies = [
"crokey-proc_macros",
"crossterm",
"once_cell",
"serde",
"strict",
]
[[package]]
name = "crokey-proc_macros"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "397d3c009d8df93c4b063ddaa44a81ee7098feb056f99b00896c36e2cee9a9f7"
dependencies = [
"crossterm",
"proc-macro2",
"quote",
"strict",
"syn 1.0.109",
]
[[package]]
name = "crossbeam"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1137cd7e7fc0fb5d3c5a8678be38ec56e819125d8d7907411fe24ccb943faca8"
dependencies = [
"crossbeam-channel",
"crossbeam-deque",
"crossbeam-epoch",
"crossbeam-queue",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-channel"
version = "0.5.13"
@ -883,12 +948,65 @@ dependencies = [
"crossbeam-utils",
]
[[package]]
name = "crossbeam-deque"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "613f8cc01fe9cf1a3eb3d7f488fd2fa8388403e97039e2f73692932e291a770d"
dependencies = [
"crossbeam-epoch",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-epoch"
version = "0.9.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e"
dependencies = [
"crossbeam-utils",
]
[[package]]
name = "crossbeam-queue"
version = "0.3.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df0346b5d5e76ac2fe4e327c5fd1118d6be7c51dfb18f9b7922923f287471e35"
dependencies = [
"crossbeam-utils",
]
[[package]]
name = "crossbeam-utils"
version = "0.8.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80"
[[package]]
name = "crossterm"
version = "0.27.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f476fe445d41c9e991fd07515a6f463074b782242ccf4a5b7b1d1012e70824df"
dependencies = [
"bitflags 2.5.0",
"crossterm_winapi",
"libc",
"mio",
"parking_lot",
"signal-hook",
"signal-hook-mio",
"winapi",
]
[[package]]
name = "crossterm_winapi"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "acdd7c62a3665c7f6830a51635d9ac9b23ed385797f70a83bb8bafe9c572ab2b"
dependencies = [
"winapi",
]
[[package]]
name = "crypto-common"
version = "0.1.6"
@ -924,7 +1042,7 @@ checksum = "f46882e17999c6cc590af592290432be3bce0428cb0d5f8b6715e4dc7b383eb3"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.66",
]
[[package]]
@ -993,7 +1111,7 @@ checksum = "487585f4d0c6655fe74905e2504d8ad6908e4db67f744eb140876906c2f3175d"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.66",
]
[[package]]
@ -1039,7 +1157,7 @@ dependencies = [
"heck 0.4.1",
"proc-macro2",
"quote",
"syn",
"syn 2.0.66",
]
[[package]]
@ -1048,6 +1166,12 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
[[package]]
name = "error-code"
version = "3.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a0474425d51df81997e2f90a21591180b38eccf27292d755f3e30750225c175b"
[[package]]
name = "fallible-iterator"
version = "0.3.0"
@ -1187,7 +1311,7 @@ checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.66",
]
[[package]]
@ -1480,7 +1604,7 @@ dependencies = [
"markup5ever",
"proc-macro2",
"quote",
"syn",
"syn 2.0.66",
]
[[package]]
@ -1766,7 +1890,7 @@ checksum = "1ec89e9337638ecdc08744df490b221a7399bf8d164eb52a665454e60e075ad6"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.66",
]
[[package]]
@ -1978,6 +2102,29 @@ dependencies = [
"typewit",
]
[[package]]
name = "lazy-regex"
version = "3.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d12be4595afdf58bd19e4a9f4e24187da2a66700786ff660a418e9059937a4c"
dependencies = [
"lazy-regex-proc_macros",
"once_cell",
"regex",
]
[[package]]
name = "lazy-regex-proc_macros"
version = "3.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44bcd58e6c97a7fcbaffcdc95728b393b8d98933bfadad49ed4097845b57ef0b"
dependencies = [
"proc-macro2",
"quote",
"regex",
"syn 2.0.66",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
@ -2151,6 +2298,15 @@ version = "0.3.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a"
[[package]]
name = "minimad"
version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a9c5d708226d186590a7b6d4a9780e2bdda5f689e0d58cd17012a298efd745d2"
dependencies = [
"once_cell",
]
[[package]]
name = "minimal-lexical"
version = "0.2.1"
@ -2174,6 +2330,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4a650543ca06a924e8b371db273b2756685faae30f8487da1b56505a8f78b0c"
dependencies = [
"libc",
"log",
"wasi",
"windows-sys 0.48.0",
]
@ -2184,6 +2341,18 @@ version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "650eef8c711430f1a879fdd01d4745a7deea475becfb90269c06775983bbf086"
[[package]]
name = "nix"
version = "0.28.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab2156c4fce2f8df6c499cc1c763e4394b7482525bf2a9701c9d79d215f519e4"
dependencies = [
"bitflags 2.5.0",
"cfg-if",
"cfg_aliases 0.1.1",
"libc",
]
[[package]]
name = "nix"
version = "0.29.0"
@ -2192,7 +2361,7 @@ checksum = "71e2746dc3a24dd78b3cfcb7be93368c6de9963d30f43a6a73998a9cf4b17b46"
dependencies = [
"bitflags 2.5.0",
"cfg-if",
"cfg_aliases",
"cfg_aliases 0.2.1",
"libc",
]
@ -2490,7 +2659,7 @@ dependencies = [
"proc-macro2",
"proc-macro2-diagnostics",
"quote",
"syn",
"syn 2.0.66",
]
[[package]]
@ -2583,7 +2752,7 @@ checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.66",
]
[[package]]
@ -2677,7 +2846,7 @@ checksum = "af066a9c399a26e020ada66a034357a868728e72cd426f3adcd35f80d88d88c8"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.66",
"version_check",
"yansi",
]
@ -2702,7 +2871,7 @@ dependencies = [
"itertools 0.12.1",
"proc-macro2",
"quote",
"syn",
"syn 2.0.66",
]
[[package]]
@ -3053,7 +3222,7 @@ dependencies = [
"quote",
"ruma-identifiers-validation",
"serde",
"syn",
"syn 2.0.66",
"toml",
]
@ -3250,6 +3419,25 @@ version = "1.0.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "955d28af4278de8121b7ebeb796b6a45735dc01436d898801014aced2773a3d6"
[[package]]
name = "rustyline"
version = "14.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7803e8936da37efd9b6d4478277f4b2b9bb5cdb37a113e8d63222e58da647e63"
dependencies = [
"bitflags 2.5.0",
"cfg-if",
"clipboard-win",
"libc",
"log",
"memchr",
"nix 0.28.0",
"unicode-segmentation",
"unicode-width",
"utf8parse",
"windows-sys 0.52.0",
]
[[package]]
name = "ryu"
version = "1.0.18"
@ -3478,7 +3666,7 @@ checksum = "500cbc0ebeb6f46627f50f3f5811ccf6bf00643be300b4c3eabc0ef55dc5b5ba"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.66",
]
[[package]]
@ -3607,6 +3795,27 @@ version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
[[package]]
name = "signal-hook"
version = "0.3.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8621587d4798caf8eb44879d42e56b9a93ea5dcd315a6487c357130095b62801"
dependencies = [
"libc",
"signal-hook-registry",
]
[[package]]
name = "signal-hook-mio"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "29ad2e15f37ec9a6cc544097b78a1ec90001e9f71b81338ca39f430adaca99af"
dependencies = [
"libc",
"mio",
"signal-hook",
]
[[package]]
name = "signal-hook-registry"
version = "1.4.2"
@ -3696,6 +3905,12 @@ version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3"
[[package]]
name = "strict"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f42444fea5b87a39db4218d9422087e66a85d0e7a0963a439b07bcdf91804006"
[[package]]
name = "string_cache"
version = "0.8.7"
@ -3737,6 +3952,17 @@ version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc"
[[package]]
name = "syn"
version = "1.0.109"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "syn"
version = "2.0.66"
@ -3768,7 +3994,7 @@ checksum = "c8af7666ab7b6390ab78131fb5b0fce11d6b7a6951602017c35fa82800708971"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.66",
]
[[package]]
@ -3782,6 +4008,22 @@ dependencies = [
"utf-8",
]
[[package]]
name = "termimad"
version = "0.29.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aab6c8572830b10362f27e242c7c5e749f062ec310b76a0d0b56670eca81f28e"
dependencies = [
"coolor",
"crokey",
"crossbeam",
"lazy-regex",
"minimad",
"serde",
"thiserror",
"unicode-width",
]
[[package]]
name = "thiserror"
version = "1.0.61"
@ -3799,7 +4041,7 @@ checksum = "46c3384250002a6d5af4d114f2845d37b57521033f30d5c3f46c4d70e1197533"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.66",
]
[[package]]
@ -3958,7 +4200,7 @@ checksum = "5f5ae998a069d4b5aba8ee9dad856af7d520c3699e6159b185c2acd48155d39a"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.66",
]
[[package]]
@ -4174,7 +4416,7 @@ source = "git+https://github.com/girlbossceo/tracing?branch=tracing-subscriber/e
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.66",
]
[[package]]
@ -4328,6 +4570,18 @@ dependencies = [
"tinyvec",
]
[[package]]
name = "unicode-segmentation"
version = "1.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d4c87d22b6e3f4a18d4d40ef354e97c90fcb14dd91d7dc0aa9d8a1172ebf7202"
[[package]]
name = "unicode-width"
version = "0.1.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0336d538f7abc86d282a4189614dfaa90810dfc2c6f6427eaf88e16311dd225d"
[[package]]
name = "unsafe-libyaml"
version = "0.2.11"
@ -4392,6 +4646,12 @@ version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be"
[[package]]
name = "utf8parse"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
[[package]]
name = "uuid"
version = "1.8.0"
@ -4456,7 +4716,7 @@ dependencies = [
"once_cell",
"proc-macro2",
"quote",
"syn",
"syn 2.0.66",
"wasm-bindgen-shared",
]
@ -4490,7 +4750,7 @@ checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.66",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
@ -4837,7 +5097,7 @@ checksum = "28cc31741b18cb6f1d5ff12f5b7523e3d6eb0852bbbad19d73905511d9849b95"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.66",
"synstructure",
]
@ -4858,7 +5118,7 @@ checksum = "15e934569e47891f7d9411f1a451d947a60e000ab3bd24fbb970f000387d1b3b"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.66",
]
[[package]]
@ -4878,7 +5138,7 @@ checksum = "0ea7b4a3637ea8669cedf0f1fd5c286a17f3de97b8dd5a70a6c167a1730e63a5"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.66",
"synstructure",
]
@ -4907,7 +5167,7 @@ checksum = "97cf56601ee5052b4417d90c8755c6683473c926039908196cf35d99f893ebe7"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.66",
]
[[package]]

View file

@ -414,6 +414,13 @@ features = [
"light",
]
[workspace.dependencies.rustyline]
version = "14.0.0"
default-features = false
[workspace.dependencies.termimad]
version = "0.29.2"
#
# Patches
#

View file

@ -1,24 +1,19 @@
use std::sync::Arc;
use clap::Parser;
use conduit::trace;
use regex::Regex;
use ruma::{
events::{
relation::InReplyTo,
room::message::{Relation::Reply, RoomMessageEventContent},
TimelineEventType,
},
OwnedRoomId, OwnedUserId, RoomId, ServerName, UserId,
ServerName,
};
use serde_json::value::to_raw_value;
use tokio::sync::MutexGuard;
use tracing::error;
extern crate conduit_service as service;
use conduit::{Error, Result};
pub(crate) use service::admin::{AdminRoomEvent, Service};
use service::{admin::HandlerResult, pdu::PduBuilder};
use conduit::Result;
use service::admin::HandlerResult;
pub(crate) use service::admin::{AdminEvent, Service};
use self::{fsck::FsckCommand, tester::TesterCommands};
use crate::{
@ -30,7 +25,7 @@ pub(crate) const PAGE_SIZE: usize = 100;
#[cfg_attr(test, derive(Debug))]
#[derive(Parser)]
#[command(name = "@conduit:server.name:", version = env!("CARGO_PKG_VERSION"))]
#[command(name = "admin", version = env!("CARGO_PKG_VERSION"))]
pub(crate) enum AdminCommand {
#[command(subcommand)]
/// - Commands for managing appservices
@ -73,85 +68,29 @@ pub(crate) enum AdminCommand {
}
#[must_use]
pub fn handle(event: AdminRoomEvent, room: OwnedRoomId, user: OwnedUserId) -> HandlerResult {
Box::pin(handle_event(event, room, user))
}
pub fn handle(event: AdminEvent) -> HandlerResult { Box::pin(handle_event(event)) }
async fn handle_event(event: AdminRoomEvent, admin_room: OwnedRoomId, server_user: OwnedUserId) -> Result<()> {
let (mut message_content, reply) = match event {
AdminRoomEvent::SendMessage(content) => (content, None),
AdminRoomEvent::ProcessMessage(room_message, reply_id) => {
// This future is ~8 KiB so it's better to start it off the stack.
(Box::pin(process_admin_message(room_message)).await, Some(reply_id))
#[tracing::instrument(skip_all, name = "admin")]
async fn handle_event(event: AdminEvent) -> Result<AdminEvent> { Ok(AdminEvent::Reply(process_event(event).await)) }
async fn process_event(event: AdminEvent) -> Option<RoomMessageEventContent> {
let (mut message_content, reply_id) = match event {
AdminEvent::Command(room_message, reply_id) => (Box::pin(process_admin_message(room_message)).await, reply_id),
AdminEvent::Notice(content) => (content, None),
AdminEvent::Reply(_) => return None,
};
message_content.relates_to = reply_id.map(|reply_id| Reply {
in_reply_to: InReplyTo {
event_id: reply_id.into(),
},
};
});
let mutex_state = Arc::clone(
services()
.globals
.roomid_mutex_state
.write()
.await
.entry(admin_room.clone())
.or_default(),
);
let state_lock = mutex_state.lock().await;
if let Some(reply) = reply {
message_content.relates_to = Some(Reply {
in_reply_to: InReplyTo {
event_id: reply.into(),
},
});
}
let response_pdu = PduBuilder {
event_type: TimelineEventType::RoomMessage,
content: to_raw_value(&message_content).expect("event is valid, we just created it"),
unsigned: None,
state_key: None,
redacts: None,
};
if let Err(e) = services()
.rooms
.timeline
.build_and_append_pdu(response_pdu, &server_user, &admin_room, &state_lock)
.await
{
handle_response_error(&e, &admin_room, &server_user, &state_lock).await?;
}
Ok(())
}
async fn handle_response_error(
e: &Error, admin_room: &RoomId, server_user: &UserId, state_lock: &MutexGuard<'_, ()>,
) -> Result<()> {
error!("Failed to build and append admin room response PDU: \"{e}\"");
let error_room_message = RoomMessageEventContent::text_plain(format!(
"Failed to build and append admin room PDU: \"{e}\"\n\nThe original admin command may have finished \
successfully, but we could not return the output."
));
let response_pdu = PduBuilder {
event_type: TimelineEventType::RoomMessage,
content: to_raw_value(&error_room_message).expect("event is valid, we just created it"),
unsigned: None,
state_key: None,
redacts: None,
};
services()
.rooms
.timeline
.build_and_append_pdu(response_pdu, server_user, admin_room, state_lock)
.await?;
Ok(())
Some(message_content)
}
// Parse and process a message from the admin room
#[tracing::instrument(name = "process")]
async fn process_admin_message(room_message: String) -> RoomMessageEventContent {
let mut lines = room_message.lines().filter(|l| !l.trim().is_empty());
let command_line = lines.next().expect("each string has at least one line");
@ -181,9 +120,13 @@ async fn process_admin_message(room_message: String) -> RoomMessageEventContent
// Parse chat messages from the admin room into an AdminCommand object
fn parse_admin_command(command_line: &str) -> Result<AdminCommand, String> {
// Note: argv[0] is `@conduit:servername:`, which is treated as the main command
let mut argv = command_line.split_whitespace().collect::<Vec<_>>();
// First indice has to be "admin" but for console convenience we add it here
if !argv.is_empty() && !argv[0].ends_with("admin") {
argv.insert(0, "admin");
}
// 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" {
@ -213,9 +156,11 @@ fn parse_admin_command(command_line: &str) -> Result<AdminCommand, String> {
argv[3] = &command_with_dashes_argv3;
}
trace!(?command_line, ?argv, "parse");
AdminCommand::try_parse_from(argv).map_err(|error| error.to_string())
}
#[tracing::instrument(skip_all, name = "command")]
async fn process_admin_command(command: AdminCommand, body: Vec<&str>) -> Result<RoomMessageEventContent> {
let reply_message_content = match command {
AdminCommand::Appservices(command) => appservice::process(command, body).await?,

View file

@ -354,10 +354,7 @@ pub(crate) async fn register_route(
.room_joined_count(&admin_room)?
== Some(1)
{
services()
.admin
.make_user_admin(&user_id, displayname)
.await?;
service::admin::make_user_admin(&user_id, displayname).await?;
warn!("Granting {user_id} admin privileges as the first user");
}

View file

@ -11,6 +11,7 @@ pub use server::Server;
pub use suppress::Suppress;
pub use tracing::Level;
pub use tracing_core::{Event, Metadata};
pub use tracing_subscriber::EnvFilter;
// Wraps for logging macros. Use these macros rather than extern tracing:: or
// log:: crates in project code. ::log and ::tracing can still be used if

View file

@ -58,6 +58,9 @@ brotli_compression = [
"conduit-router/brotli_compression",
"conduit-service/brotli_compression",
]
console = [
"conduit-service/console",
]
dev_release_log_level = [
"conduit-admin/dev_release_log_level",
"conduit-api/dev_release_log_level",

View file

@ -4,7 +4,11 @@ mod server;
extern crate conduit_core as conduit;
use std::{cmp, sync::Arc, time::Duration};
use std::{
cmp,
sync::{atomic::Ordering, Arc},
time::Duration,
};
use conduit::{debug_error, debug_info, error, trace, utils::available_parallelism, warn, Error, Result};
use server::Server;
@ -100,8 +104,13 @@ async fn async_main(server: &Arc<Server>) -> Result<(), Error> {
#[tracing::instrument(skip_all)]
async fn signal(server: Arc<Server>) {
use signal::unix;
let mut quit = unix::signal(unix::SignalKind::quit()).expect("SIGQUIT handler");
let mut term = unix::signal(unix::SignalKind::terminate()).expect("SIGTERM handler");
use unix::SignalKind;
const CONSOLE: bool = cfg!(feature = "console");
const RELOADING: bool = cfg!(all(unix, debug_assertions, not(CONSOLE)));
let mut quit = unix::signal(SignalKind::quit()).expect("SIGQUIT handler");
let mut term = unix::signal(SignalKind::terminate()).expect("SIGTERM handler");
loop {
trace!("Installed signal handlers");
let sig: &'static str;
@ -111,6 +120,16 @@ async fn signal(server: Arc<Server>) {
_ = term.recv() => { sig = "SIGTERM"; },
}
// Indicate the SIGINT is requesting a hot-reload.
if RELOADING && sig == "SIGINT" {
server.server.reloading.store(true, Ordering::Release);
}
// Indicate the signal is requesting a shutdown
if matches!(sig, "SIGQUIT" | "SIGTERM") || (!CONSOLE && sig == "SIGINT") {
server.server.stopping.store(true, Ordering::Release);
}
warn!("Received {sig}");
if let Err(e) = server.server.signal.send(sig) {
debug_error!("signal channel: {e}");

View file

@ -83,20 +83,23 @@ pub(crate) async fn stop(_server: Arc<Server>) -> Result<(), Error> {
#[tracing::instrument(skip_all)]
async fn signal(server: Arc<Server>, tx: Sender<()>, handle: axum_server::Handle) {
let sig: &'static str = server
.signal
.subscribe()
.recv()
.await
.expect("channel error");
loop {
let sig: &'static str = server
.signal
.subscribe()
.recv()
.await
.expect("channel error");
debug!("Received signal {}", sig);
if sig == "SIGINT" {
let reload = cfg!(unix) && cfg!(debug_assertions);
server.reloading.store(reload, Ordering::Release);
if !server.running() {
handle_shutdown(&server, &tx, &handle, sig).await;
break;
}
}
}
server.stopping.store(true, Ordering::Release);
async fn handle_shutdown(server: &Arc<Server>, tx: &Sender<()>, handle: &axum_server::Handle, sig: &str) {
debug!("Received signal {}", sig);
if let Err(e) = tx.send(()) {
error!("failed sending shutdown transaction to channel: {e}");
}

View file

@ -17,20 +17,24 @@ crate-type = [
]
[features]
element_hacks = []
brotli_compression = [
"reqwest/brotli",
]
console = [
"dep:rustyline",
"dep:termimad",
]
dev_release_log_level = []
element_hacks = []
gzip_compression = [
"reqwest/gzip",
]
release_max_log_level = [
"tracing/max_level_trace",
"tracing/release_max_level_info",
"log/max_level_trace",
"log/release_max_level_info",
]
gzip_compression = [
"reqwest/gzip",
]
brotli_compression = [
"reqwest/brotli",
]
sha256_media = [
"dep:sha2",
]
@ -57,11 +61,15 @@ regex.workspace = true
reqwest.workspace = true
ruma-identifiers-validation.workspace = true
ruma.workspace = true
rustyline.workspace = true
rustyline.optional = true
serde_json.workspace = true
serde.workspace = true
serde_yaml.workspace = true
sha2.optional = true
sha2.workspace = true
termimad.workspace = true
termimad.optional = true
tokio.workspace = true
tracing.workspace = true
url.workspace = true

View file

@ -1,540 +0,0 @@
use std::{collections::BTreeMap, future::Future, pin::Pin, sync::Arc};
use conduit::{Error, Result};
use ruma::{
api::client::error::ErrorKind,
events::{
room::{
canonical_alias::RoomCanonicalAliasEventContent,
create::RoomCreateEventContent,
guest_access::{GuestAccess, RoomGuestAccessEventContent},
history_visibility::{HistoryVisibility, RoomHistoryVisibilityEventContent},
join_rules::{JoinRule, RoomJoinRulesEventContent},
member::{MembershipState, RoomMemberEventContent},
message::RoomMessageEventContent,
name::RoomNameEventContent,
power_levels::RoomPowerLevelsEventContent,
preview_url::RoomPreviewUrlsEventContent,
topic::RoomTopicEventContent,
},
TimelineEventType,
},
EventId, OwnedRoomId, OwnedUserId, RoomId, RoomVersionId, UserId,
};
use serde_json::value::to_raw_value;
use tokio::{sync::Mutex, task::JoinHandle};
use tracing::{error, warn};
use crate::{pdu::PduBuilder, services};
pub type HandlerResult = Pin<Box<dyn Future<Output = Result<(), Error>> + Send>>;
pub type Handler = fn(AdminRoomEvent, OwnedRoomId, OwnedUserId) -> HandlerResult;
pub struct Service {
sender: loole::Sender<AdminRoomEvent>,
receiver: Mutex<loole::Receiver<AdminRoomEvent>>,
handler_join: Mutex<Option<JoinHandle<()>>>,
pub handle: Mutex<Option<Handler>>,
}
#[derive(Debug)]
pub enum AdminRoomEvent {
ProcessMessage(String, Arc<EventId>),
SendMessage(RoomMessageEventContent),
}
impl Service {
#[must_use]
pub fn build() -> Arc<Self> {
let (sender, receiver) = loole::unbounded();
Arc::new(Self {
sender,
receiver: Mutex::new(receiver),
handler_join: Mutex::new(None),
handle: Mutex::new(None),
})
}
pub async fn start_handler(self: &Arc<Self>) {
let self_ = Arc::clone(self);
let handle = services().server.runtime().spawn(async move {
self_
.handler()
.await
.expect("Failed to initialize admin room handler");
});
_ = self.handler_join.lock().await.insert(handle);
}
async fn handler(self: &Arc<Self>) -> Result<()> {
let receiver = self.receiver.lock().await;
let Ok(Some(admin_room)) = Self::get_admin_room() else {
return Ok(());
};
let server_user = &services().globals.server_user;
loop {
debug_assert!(!receiver.is_closed(), "channel closed");
tokio::select! {
event = receiver.recv_async() => match event {
Ok(event) => self.receive(event, &admin_room, server_user).await?,
Err(_e) => return Ok(()),
}
}
}
}
pub async fn close(&self) {
self.interrupt();
if let Some(handler_join) = self.handler_join.lock().await.take() {
if let Err(e) = handler_join.await {
error!("Failed to shutdown: {e:?}");
}
}
}
pub fn interrupt(&self) {
if !self.sender.is_closed() {
self.sender.close();
}
}
pub async fn send_message(&self, message_content: RoomMessageEventContent) {
self.send(AdminRoomEvent::SendMessage(message_content))
.await;
}
pub async fn process_message(&self, room_message: String, event_id: Arc<EventId>) {
self.send(AdminRoomEvent::ProcessMessage(room_message, event_id))
.await;
}
async fn receive(&self, event: AdminRoomEvent, room: &RoomId, user: &UserId) -> Result<(), Error> {
if let Some(handle) = self.handle.lock().await.as_ref() {
handle(event, room.into(), user.into()).await
} else {
Err(Error::Err("Admin module is not loaded.".into()))
}
}
async fn send(&self, message: AdminRoomEvent) {
debug_assert!(!self.sender.is_full(), "channel full");
debug_assert!(!self.sender.is_closed(), "channel closed");
self.sender.send(message).expect("message sent");
}
/// Gets the room ID of the admin room
///
/// Errors are propagated from the database, and will have None if there is
/// no admin room
pub fn get_admin_room() -> Result<Option<OwnedRoomId>> {
services()
.rooms
.alias
.resolve_local_alias(&services().globals.admin_alias)
}
/// Create the admin room.
///
/// Users in this room are considered admins by conduit, and the room can be
/// used to issue admin commands by talking to the server user inside it.
pub async fn create_admin_room(&self) -> Result<()> {
let room_id = RoomId::new(services().globals.server_name());
services().rooms.short.get_or_create_shortroomid(&room_id)?;
let mutex_state = Arc::clone(
services()
.globals
.roomid_mutex_state
.write()
.await
.entry(room_id.clone())
.or_default(),
);
let state_lock = mutex_state.lock().await;
// Create a user for the server
let server_user = &services().globals.server_user;
services().users.create(server_user, None)?;
let room_version = services().globals.default_room_version();
let mut content = match room_version {
RoomVersionId::V1
| RoomVersionId::V2
| RoomVersionId::V3
| RoomVersionId::V4
| RoomVersionId::V5
| RoomVersionId::V6
| RoomVersionId::V7
| RoomVersionId::V8
| RoomVersionId::V9
| RoomVersionId::V10 => RoomCreateEventContent::new_v1(server_user.clone()),
RoomVersionId::V11 => RoomCreateEventContent::new_v11(),
_ => {
warn!("Unexpected or unsupported room version {}", room_version);
return Err(Error::BadRequest(
ErrorKind::BadJson,
"Unexpected or unsupported room version found",
));
},
};
content.federate = true;
content.predecessor = None;
content.room_version = room_version;
// 1. The room create event
services()
.rooms
.timeline
.build_and_append_pdu(
PduBuilder {
event_type: TimelineEventType::RoomCreate,
content: to_raw_value(&content).expect("event is valid, we just created it"),
unsigned: None,
state_key: Some(String::new()),
redacts: None,
},
server_user,
&room_id,
&state_lock,
)
.await?;
// 2. Make conduit bot join
services()
.rooms
.timeline
.build_and_append_pdu(
PduBuilder {
event_type: TimelineEventType::RoomMember,
content: to_raw_value(&RoomMemberEventContent {
membership: MembershipState::Join,
displayname: None,
avatar_url: None,
is_direct: None,
third_party_invite: None,
blurhash: None,
reason: None,
join_authorized_via_users_server: None,
})
.expect("event is valid, we just created it"),
unsigned: None,
state_key: Some(server_user.to_string()),
redacts: None,
},
server_user,
&room_id,
&state_lock,
)
.await?;
// 3. Power levels
let mut users = BTreeMap::new();
users.insert(server_user.clone(), 100.into());
services()
.rooms
.timeline
.build_and_append_pdu(
PduBuilder {
event_type: TimelineEventType::RoomPowerLevels,
content: to_raw_value(&RoomPowerLevelsEventContent {
users,
..Default::default()
})
.expect("event is valid, we just created it"),
unsigned: None,
state_key: Some(String::new()),
redacts: None,
},
server_user,
&room_id,
&state_lock,
)
.await?;
// 4.1 Join Rules
services()
.rooms
.timeline
.build_and_append_pdu(
PduBuilder {
event_type: TimelineEventType::RoomJoinRules,
content: to_raw_value(&RoomJoinRulesEventContent::new(JoinRule::Invite))
.expect("event is valid, we just created it"),
unsigned: None,
state_key: Some(String::new()),
redacts: None,
},
server_user,
&room_id,
&state_lock,
)
.await?;
// 4.2 History Visibility
services()
.rooms
.timeline
.build_and_append_pdu(
PduBuilder {
event_type: TimelineEventType::RoomHistoryVisibility,
content: to_raw_value(&RoomHistoryVisibilityEventContent::new(HistoryVisibility::Shared))
.expect("event is valid, we just created it"),
unsigned: None,
state_key: Some(String::new()),
redacts: None,
},
server_user,
&room_id,
&state_lock,
)
.await?;
// 4.3 Guest Access
services()
.rooms
.timeline
.build_and_append_pdu(
PduBuilder {
event_type: TimelineEventType::RoomGuestAccess,
content: to_raw_value(&RoomGuestAccessEventContent::new(GuestAccess::Forbidden))
.expect("event is valid, we just created it"),
unsigned: None,
state_key: Some(String::new()),
redacts: None,
},
server_user,
&room_id,
&state_lock,
)
.await?;
// 5. Events implied by name and topic
let room_name = format!("{} Admin Room", services().globals.server_name());
services()
.rooms
.timeline
.build_and_append_pdu(
PduBuilder {
event_type: TimelineEventType::RoomName,
content: to_raw_value(&RoomNameEventContent::new(room_name))
.expect("event is valid, we just created it"),
unsigned: None,
state_key: Some(String::new()),
redacts: None,
},
server_user,
&room_id,
&state_lock,
)
.await?;
services()
.rooms
.timeline
.build_and_append_pdu(
PduBuilder {
event_type: TimelineEventType::RoomTopic,
content: to_raw_value(&RoomTopicEventContent {
topic: format!("Manage {}", services().globals.server_name()),
})
.expect("event is valid, we just created it"),
unsigned: None,
state_key: Some(String::new()),
redacts: None,
},
server_user,
&room_id,
&state_lock,
)
.await?;
// 6. Room alias
let alias = &services().globals.admin_alias;
services()
.rooms
.timeline
.build_and_append_pdu(
PduBuilder {
event_type: TimelineEventType::RoomCanonicalAlias,
content: to_raw_value(&RoomCanonicalAliasEventContent {
alias: Some(alias.clone()),
alt_aliases: Vec::new(),
})
.expect("event is valid, we just created it"),
unsigned: None,
state_key: Some(String::new()),
redacts: None,
},
server_user,
&room_id,
&state_lock,
)
.await?;
services()
.rooms
.alias
.set_alias(alias, &room_id, server_user)?;
// 7. (ad-hoc) Disable room previews for everyone by default
services()
.rooms
.timeline
.build_and_append_pdu(
PduBuilder {
event_type: TimelineEventType::RoomPreviewUrls,
content: to_raw_value(&RoomPreviewUrlsEventContent {
disabled: true,
})
.expect("event is valid we just created it"),
unsigned: None,
state_key: Some(String::new()),
redacts: None,
},
server_user,
&room_id,
&state_lock,
)
.await?;
Ok(())
}
/// Invite the user to the conduit admin room.
///
/// In conduit, this is equivalent to granting admin privileges.
pub async fn make_user_admin(&self, user_id: &UserId, displayname: String) -> Result<()> {
if let Some(room_id) = Self::get_admin_room()? {
let mutex_state = Arc::clone(
services()
.globals
.roomid_mutex_state
.write()
.await
.entry(room_id.clone())
.or_default(),
);
let state_lock = mutex_state.lock().await;
// Use the server user to grant the new admin's power level
let server_user = &services().globals.server_user;
// Invite and join the real user
services()
.rooms
.timeline
.build_and_append_pdu(
PduBuilder {
event_type: TimelineEventType::RoomMember,
content: to_raw_value(&RoomMemberEventContent {
membership: MembershipState::Invite,
displayname: None,
avatar_url: None,
is_direct: None,
third_party_invite: None,
blurhash: None,
reason: None,
join_authorized_via_users_server: None,
})
.expect("event is valid, we just created it"),
unsigned: None,
state_key: Some(user_id.to_string()),
redacts: None,
},
server_user,
&room_id,
&state_lock,
)
.await?;
services()
.rooms
.timeline
.build_and_append_pdu(
PduBuilder {
event_type: TimelineEventType::RoomMember,
content: to_raw_value(&RoomMemberEventContent {
membership: MembershipState::Join,
displayname: Some(displayname),
avatar_url: None,
is_direct: None,
third_party_invite: None,
blurhash: None,
reason: None,
join_authorized_via_users_server: None,
})
.expect("event is valid, we just created it"),
unsigned: None,
state_key: Some(user_id.to_string()),
redacts: None,
},
user_id,
&room_id,
&state_lock,
)
.await?;
// Set power level
let mut users = BTreeMap::new();
users.insert(server_user.clone(), 100.into());
users.insert(user_id.to_owned(), 100.into());
services()
.rooms
.timeline
.build_and_append_pdu(
PduBuilder {
event_type: TimelineEventType::RoomPowerLevels,
content: to_raw_value(&RoomPowerLevelsEventContent {
users,
..Default::default()
})
.expect("event is valid, we just created it"),
unsigned: None,
state_key: Some(String::new()),
redacts: None,
},
server_user,
&room_id,
&state_lock,
)
.await?;
// Send welcome message
services().rooms.timeline.build_and_append_pdu(
PduBuilder {
event_type: TimelineEventType::RoomMessage,
content: to_raw_value(&RoomMessageEventContent::text_html(
format!("## Thank you for trying out conduwuit!\n\nconduwuit is a fork of upstream Conduit which is in Beta. This means you can join and participate in most Matrix rooms, but not all features are supported and you might run into bugs from time to time.\n\nHelpful links:\n> Git and Documentation: https://github.com/girlbossceo/conduwuit\n> Report issues: https://github.com/girlbossceo/conduwuit/issues\n\nFor a list of available commands, send the following message in this room: `@conduit:{}: --help`\n\nHere are some rooms you can join (by typing the command):\n\nconduwuit room (Ask questions and get notified on updates):\n`/join #conduwuit:puppygock.gay`", services().globals.server_name()),
format!("<h2>Thank you for trying out conduwuit!</h2>\n<p>conduwuit is a fork of upstream Conduit which is in Beta. This means you can join and participate in most Matrix rooms, but not all features are supported and you might run into bugs from time to time.</p>\n<p>Helpful links:</p>\n<blockquote>\n<p>Git and Documentation: https://github.com/girlbossceo/conduwuit<br>Report issues: https://github.com/girlbossceo/conduwuit/issues</p>\n</blockquote>\n<p>For a list of available commands, send the following message in this room: <code>@conduit:{}: --help</code></p>\n<p>Here are some rooms you can join (by typing the command):</p>\n<p>conduwuit room (Ask questions and get notified on updates):<br><code>/join #conduwuit:puppygock.gay</code></p>\n", services().globals.server_name()),
))
.expect("event is valid, we just created it"),
unsigned: None,
state_key: None,
redacts: None,
},
server_user,
&room_id,
&state_lock,
).await?;
}
Ok(())
}
/// Checks whether a given user is an admin of this server
pub async fn user_is_admin(&self, user_id: &UserId) -> Result<bool> {
let Ok(Some(admin_room)) = Self::get_admin_room() else {
return Ok(false);
};
services().rooms.state_cache.is_joined(user_id, &admin_room)
}
}

View file

@ -0,0 +1,121 @@
#![cfg(feature = "console")]
use std::sync::Arc;
use conduit::{error, log, trace};
use ruma::events::room::message::RoomMessageEventContent;
use rustyline::{error::ReadlineError, history, Editor};
use termimad::MadSkin;
use tokio::{sync::Mutex, task::JoinHandle};
use crate::services;
pub struct Console {
join: Mutex<Option<JoinHandle<()>>>,
input: Mutex<Editor<(), history::MemHistory>>,
output: MadSkin,
}
impl Console {
#[must_use]
pub fn new() -> Arc<Self> {
use rustyline::config::{Behavior, BellStyle};
use termimad::{crossterm::style::Color, Alignment, CompoundStyle, LineStyle};
let config = rustyline::Config::builder()
.enable_signals(false)
.behavior(Behavior::PreferTerm)
.bell_style(BellStyle::Visible)
.auto_add_history(true)
.max_history_size(100)
.expect("valid history size")
.indent_size(4)
.tab_stop(4)
.build();
let history = history::MemHistory::with_config(config);
let input = Editor::with_history(config, history).expect("builder configuration succeeded");
let mut output = MadSkin::default_dark();
let code_style = CompoundStyle::with_fgbg(Color::AnsiValue(40), Color::AnsiValue(234));
output.inline_code = code_style.clone();
output.code_block = LineStyle {
left_margin: 0,
right_margin: 0,
align: Alignment::Left,
compound_style: code_style,
};
Arc::new(Self {
join: None.into(),
input: Mutex::new(input),
output,
})
}
#[allow(clippy::let_underscore_must_use)]
pub async fn start(self: &Arc<Self>) {
let mut join = self.join.lock().await;
if join.is_none() {
let self_ = Arc::clone(self);
_ = join.insert(services().server.runtime().spawn(self_.worker()));
}
}
pub fn interrupt(self: &Arc<Self>) { Self::handle_interrupt(); }
#[allow(clippy::let_underscore_must_use)]
pub async fn close(self: &Arc<Self>) {
if let Some(join) = self.join.lock().await.take() {
_ = join.await;
}
}
#[tracing::instrument(skip_all, name = "console")]
async fn worker(self: Arc<Self>) {
loop {
let mut input = self.input.lock().await;
let suppression = log::Suppress::new(&services().server);
let line = tokio::task::block_in_place(|| input.readline("uwu> "));
drop(suppression);
match line {
Ok(string) => self.handle(string).await,
Err(e) => match e {
ReadlineError::Eof => break,
ReadlineError::Interrupted => Self::handle_interrupt(),
ReadlineError::WindowResized => Self::handle_winch(),
_ => error!("console: {e:?}"),
},
}
}
self.join.lock().await.take();
}
async fn handle(&self, line: String) {
if line.is_empty() {
return;
}
match services().admin.command_in_place(line, None).await {
Ok(Some(content)) => self.output(content).await,
Err(e) => error!("processing command: {e}"),
_ => (),
}
}
async fn output(&self, output_content: RoomMessageEventContent) {
let output = self.output.term_text(output_content.body());
println!("{output}");
}
fn handle_interrupt() {
trace!("interrupted");
}
fn handle_winch() {
trace!("winch");
}
}

297
src/service/admin/create.rs Normal file
View file

@ -0,0 +1,297 @@
use std::{collections::BTreeMap, sync::Arc};
use conduit::{Error, Result};
use ruma::{
api::client::error::ErrorKind,
events::{
room::{
canonical_alias::RoomCanonicalAliasEventContent,
create::RoomCreateEventContent,
guest_access::{GuestAccess, RoomGuestAccessEventContent},
history_visibility::{HistoryVisibility, RoomHistoryVisibilityEventContent},
join_rules::{JoinRule, RoomJoinRulesEventContent},
member::{MembershipState, RoomMemberEventContent},
name::RoomNameEventContent,
power_levels::RoomPowerLevelsEventContent,
preview_url::RoomPreviewUrlsEventContent,
topic::RoomTopicEventContent,
},
TimelineEventType,
},
RoomId, RoomVersionId,
};
use serde_json::value::to_raw_value;
use tracing::warn;
use crate::{pdu::PduBuilder, services};
/// Create the admin room.
///
/// Users in this room are considered admins by conduit, and the room can be
/// used to issue admin commands by talking to the server user inside it.
pub async fn create_admin_room() -> Result<()> {
let room_id = RoomId::new(services().globals.server_name());
services().rooms.short.get_or_create_shortroomid(&room_id)?;
let mutex_state = Arc::clone(
services()
.globals
.roomid_mutex_state
.write()
.await
.entry(room_id.clone())
.or_default(),
);
let state_lock = mutex_state.lock().await;
// Create a user for the server
let server_user = &services().globals.server_user;
services().users.create(server_user, None)?;
let room_version = services().globals.default_room_version();
let mut content = match room_version {
RoomVersionId::V1
| RoomVersionId::V2
| RoomVersionId::V3
| RoomVersionId::V4
| RoomVersionId::V5
| RoomVersionId::V6
| RoomVersionId::V7
| RoomVersionId::V8
| RoomVersionId::V9
| RoomVersionId::V10 => RoomCreateEventContent::new_v1(server_user.clone()),
RoomVersionId::V11 => RoomCreateEventContent::new_v11(),
_ => {
warn!("Unexpected or unsupported room version {}", room_version);
return Err(Error::BadRequest(
ErrorKind::BadJson,
"Unexpected or unsupported room version found",
));
},
};
content.federate = true;
content.predecessor = None;
content.room_version = room_version;
// 1. The room create event
services()
.rooms
.timeline
.build_and_append_pdu(
PduBuilder {
event_type: TimelineEventType::RoomCreate,
content: to_raw_value(&content).expect("event is valid, we just created it"),
unsigned: None,
state_key: Some(String::new()),
redacts: None,
},
server_user,
&room_id,
&state_lock,
)
.await?;
// 2. Make conduit bot join
services()
.rooms
.timeline
.build_and_append_pdu(
PduBuilder {
event_type: TimelineEventType::RoomMember,
content: to_raw_value(&RoomMemberEventContent {
membership: MembershipState::Join,
displayname: None,
avatar_url: None,
is_direct: None,
third_party_invite: None,
blurhash: None,
reason: None,
join_authorized_via_users_server: None,
})
.expect("event is valid, we just created it"),
unsigned: None,
state_key: Some(server_user.to_string()),
redacts: None,
},
server_user,
&room_id,
&state_lock,
)
.await?;
// 3. Power levels
let mut users = BTreeMap::new();
users.insert(server_user.clone(), 100.into());
services()
.rooms
.timeline
.build_and_append_pdu(
PduBuilder {
event_type: TimelineEventType::RoomPowerLevels,
content: to_raw_value(&RoomPowerLevelsEventContent {
users,
..Default::default()
})
.expect("event is valid, we just created it"),
unsigned: None,
state_key: Some(String::new()),
redacts: None,
},
server_user,
&room_id,
&state_lock,
)
.await?;
// 4.1 Join Rules
services()
.rooms
.timeline
.build_and_append_pdu(
PduBuilder {
event_type: TimelineEventType::RoomJoinRules,
content: to_raw_value(&RoomJoinRulesEventContent::new(JoinRule::Invite))
.expect("event is valid, we just created it"),
unsigned: None,
state_key: Some(String::new()),
redacts: None,
},
server_user,
&room_id,
&state_lock,
)
.await?;
// 4.2 History Visibility
services()
.rooms
.timeline
.build_and_append_pdu(
PduBuilder {
event_type: TimelineEventType::RoomHistoryVisibility,
content: to_raw_value(&RoomHistoryVisibilityEventContent::new(HistoryVisibility::Shared))
.expect("event is valid, we just created it"),
unsigned: None,
state_key: Some(String::new()),
redacts: None,
},
server_user,
&room_id,
&state_lock,
)
.await?;
// 4.3 Guest Access
services()
.rooms
.timeline
.build_and_append_pdu(
PduBuilder {
event_type: TimelineEventType::RoomGuestAccess,
content: to_raw_value(&RoomGuestAccessEventContent::new(GuestAccess::Forbidden))
.expect("event is valid, we just created it"),
unsigned: None,
state_key: Some(String::new()),
redacts: None,
},
server_user,
&room_id,
&state_lock,
)
.await?;
// 5. Events implied by name and topic
let room_name = format!("{} Admin Room", services().globals.server_name());
services()
.rooms
.timeline
.build_and_append_pdu(
PduBuilder {
event_type: TimelineEventType::RoomName,
content: to_raw_value(&RoomNameEventContent::new(room_name))
.expect("event is valid, we just created it"),
unsigned: None,
state_key: Some(String::new()),
redacts: None,
},
server_user,
&room_id,
&state_lock,
)
.await?;
services()
.rooms
.timeline
.build_and_append_pdu(
PduBuilder {
event_type: TimelineEventType::RoomTopic,
content: to_raw_value(&RoomTopicEventContent {
topic: format!("Manage {}", services().globals.server_name()),
})
.expect("event is valid, we just created it"),
unsigned: None,
state_key: Some(String::new()),
redacts: None,
},
server_user,
&room_id,
&state_lock,
)
.await?;
// 6. Room alias
let alias = &services().globals.admin_alias;
services()
.rooms
.timeline
.build_and_append_pdu(
PduBuilder {
event_type: TimelineEventType::RoomCanonicalAlias,
content: to_raw_value(&RoomCanonicalAliasEventContent {
alias: Some(alias.clone()),
alt_aliases: Vec::new(),
})
.expect("event is valid, we just created it"),
unsigned: None,
state_key: Some(String::new()),
redacts: None,
},
server_user,
&room_id,
&state_lock,
)
.await?;
services()
.rooms
.alias
.set_alias(alias, &room_id, server_user)?;
// 7. (ad-hoc) Disable room previews for everyone by default
services()
.rooms
.timeline
.build_and_append_pdu(
PduBuilder {
event_type: TimelineEventType::RoomPreviewUrls,
content: to_raw_value(&RoomPreviewUrlsEventContent {
disabled: true,
})
.expect("event is valid we just created it"),
unsigned: None,
state_key: Some(String::new()),
redacts: None,
},
server_user,
&room_id,
&state_lock,
)
.await?;
Ok(())
}

139
src/service/admin/grant.rs Normal file
View file

@ -0,0 +1,139 @@
use std::{collections::BTreeMap, sync::Arc};
use conduit::Result;
use ruma::{
events::{
room::{
member::{MembershipState, RoomMemberEventContent},
message::RoomMessageEventContent,
power_levels::RoomPowerLevelsEventContent,
},
TimelineEventType,
},
UserId,
};
use serde_json::value::to_raw_value;
use super::Service;
use crate::{pdu::PduBuilder, services};
/// Invite the user to the conduit admin room.
///
/// In conduit, this is equivalent to granting admin privileges.
pub async fn make_user_admin(user_id: &UserId, displayname: String) -> Result<()> {
if let Some(room_id) = Service::get_admin_room()? {
let mutex_state = Arc::clone(
services()
.globals
.roomid_mutex_state
.write()
.await
.entry(room_id.clone())
.or_default(),
);
let state_lock = mutex_state.lock().await;
// Use the server user to grant the new admin's power level
let server_user = &services().globals.server_user;
// Invite and join the real user
services()
.rooms
.timeline
.build_and_append_pdu(
PduBuilder {
event_type: TimelineEventType::RoomMember,
content: to_raw_value(&RoomMemberEventContent {
membership: MembershipState::Invite,
displayname: None,
avatar_url: None,
is_direct: None,
third_party_invite: None,
blurhash: None,
reason: None,
join_authorized_via_users_server: None,
})
.expect("event is valid, we just created it"),
unsigned: None,
state_key: Some(user_id.to_string()),
redacts: None,
},
server_user,
&room_id,
&state_lock,
)
.await?;
services()
.rooms
.timeline
.build_and_append_pdu(
PduBuilder {
event_type: TimelineEventType::RoomMember,
content: to_raw_value(&RoomMemberEventContent {
membership: MembershipState::Join,
displayname: Some(displayname),
avatar_url: None,
is_direct: None,
third_party_invite: None,
blurhash: None,
reason: None,
join_authorized_via_users_server: None,
})
.expect("event is valid, we just created it"),
unsigned: None,
state_key: Some(user_id.to_string()),
redacts: None,
},
user_id,
&room_id,
&state_lock,
)
.await?;
// Set power level
let mut users = BTreeMap::new();
users.insert(server_user.clone(), 100.into());
users.insert(user_id.to_owned(), 100.into());
services()
.rooms
.timeline
.build_and_append_pdu(
PduBuilder {
event_type: TimelineEventType::RoomPowerLevels,
content: to_raw_value(&RoomPowerLevelsEventContent {
users,
..Default::default()
})
.expect("event is valid, we just created it"),
unsigned: None,
state_key: Some(String::new()),
redacts: None,
},
server_user,
&room_id,
&state_lock,
)
.await?;
// Send welcome message
services().rooms.timeline.build_and_append_pdu(
PduBuilder {
event_type: TimelineEventType::RoomMessage,
content: to_raw_value(&RoomMessageEventContent::text_html(
format!("## Thank you for trying out conduwuit!\n\nconduwuit is a fork of upstream Conduit which is in Beta. This means you can join and participate in most Matrix rooms, but not all features are supported and you might run into bugs from time to time.\n\nHelpful links:\n> Git and Documentation: https://github.com/girlbossceo/conduwuit\n> Report issues: https://github.com/girlbossceo/conduwuit/issues\n\nFor a list of available commands, send the following message in this room: `@conduit:{}: --help`\n\nHere are some rooms you can join (by typing the command):\n\nconduwuit room (Ask questions and get notified on updates):\n`/join #conduwuit:puppygock.gay`", services().globals.server_name()),
format!("<h2>Thank you for trying out conduwuit!</h2>\n<p>conduwuit is a fork of upstream Conduit which is in Beta. This means you can join and participate in most Matrix rooms, but not all features are supported and you might run into bugs from time to time.</p>\n<p>Helpful links:</p>\n<blockquote>\n<p>Git and Documentation: https://github.com/girlbossceo/conduwuit<br>Report issues: https://github.com/girlbossceo/conduwuit/issues</p>\n</blockquote>\n<p>For a list of available commands, send the following message in this room: <code>@conduit:{}: --help</code></p>\n<p>Here are some rooms you can join (by typing the command):</p>\n<p>conduwuit room (Ask questions and get notified on updates):<br><code>/join #conduwuit:puppygock.gay</code></p>\n", services().globals.server_name()),
))
.expect("event is valid, we just created it"),
unsigned: None,
state_key: None,
redacts: None,
},
server_user,
&room_id,
&state_lock,
).await?;
}
Ok(())
}

247
src/service/admin/mod.rs Normal file
View file

@ -0,0 +1,247 @@
pub mod console;
mod create;
mod grant;
use std::{future::Future, pin::Pin, sync::Arc};
use conduit::{Error, Result};
pub use create::create_admin_room;
pub use grant::make_user_admin;
use ruma::{
events::{room::message::RoomMessageEventContent, TimelineEventType},
EventId, OwnedRoomId, RoomId, UserId,
};
use serde_json::value::to_raw_value;
use tokio::{
sync::{Mutex, MutexGuard},
task::JoinHandle,
};
use tracing::error;
use crate::{pdu::PduBuilder, services};
pub type HandlerResult = Pin<Box<dyn Future<Output = Result<AdminEvent, Error>> + Send>>;
pub type Handler = fn(AdminEvent) -> HandlerResult;
pub struct Service {
sender: loole::Sender<AdminEvent>,
receiver: Mutex<loole::Receiver<AdminEvent>>,
handler_join: Mutex<Option<JoinHandle<()>>>,
pub handle: Mutex<Option<Handler>>,
#[cfg(feature = "console")]
pub console: Arc<console::Console>,
}
#[derive(Debug)]
pub enum AdminEvent {
Command(String, Option<Arc<EventId>>),
Reply(Option<RoomMessageEventContent>),
Notice(RoomMessageEventContent),
}
impl Service {
#[must_use]
pub fn build() -> Arc<Self> {
let (sender, receiver) = loole::unbounded();
Arc::new(Self {
sender,
receiver: Mutex::new(receiver),
handler_join: Mutex::new(None),
handle: Mutex::new(None),
#[cfg(feature = "console")]
console: console::Console::new(),
})
}
pub fn interrupt(&self) {
#[cfg(feature = "console")]
self.console.interrupt();
if !self.sender.is_closed() {
self.sender.close();
}
}
pub async fn close(&self) {
self.interrupt();
#[cfg(feature = "console")]
self.console.close().await;
if let Some(handler_join) = self.handler_join.lock().await.take() {
if let Err(e) = handler_join.await {
error!("Failed to shutdown: {e:?}");
}
}
}
pub async fn start_handler(self: &Arc<Self>) {
let self_ = Arc::clone(self);
let handle = services().server.runtime().spawn(async move {
self_
.handler()
.await
.expect("Failed to initialize admin room handler");
});
_ = self.handler_join.lock().await.insert(handle);
}
async fn handler(self: &Arc<Self>) -> Result<()> {
let receiver = self.receiver.lock().await;
let mut signals = services().server.signal.subscribe();
loop {
debug_assert!(!receiver.is_closed(), "channel closed");
tokio::select! {
event = receiver.recv_async() => match event {
Ok(event) => self.receive(event).await,
Err(_) => return Ok(()),
},
sig = signals.recv() => match sig {
Ok(sig) => self.handle_signal(sig).await,
Err(_) => continue,
},
}
}
}
pub async fn send_text(&self, body: &str) {
self.send_message(RoomMessageEventContent::text_plain(body))
.await;
}
pub async fn send_message(&self, message_content: RoomMessageEventContent) {
self.send(AdminEvent::Notice(message_content)).await;
}
pub async fn command(&self, command: String, event_id: Option<Arc<EventId>>) {
self.send(AdminEvent::Command(command, event_id)).await;
}
pub async fn command_in_place(
&self, command: String, event_id: Option<Arc<EventId>>,
) -> Result<Option<RoomMessageEventContent>> {
match self.handle(AdminEvent::Command(command, event_id)).await? {
AdminEvent::Reply(content) => Ok(content),
_ => Ok(None),
}
}
async fn send(&self, message: AdminEvent) {
debug_assert!(!self.sender.is_full(), "channel full");
debug_assert!(!self.sender.is_closed(), "channel closed");
self.sender.send(message).expect("message sent");
}
async fn receive(&self, event: AdminEvent) {
if let Ok(AdminEvent::Reply(content)) = self.handle(event).await {
handle_response(content).await;
}
}
async fn handle(&self, event: AdminEvent) -> Result<AdminEvent, Error> {
if let Some(handle) = self.handle.lock().await.as_ref() {
handle(event).await
} else {
Err(Error::Err("Admin module is not loaded.".into()))
}
}
async fn handle_signal(&self, #[allow(unused_variables)] sig: &'static str) {
#[cfg(feature = "console")]
if sig == "SIGINT" && services().server.running() {
self.console.start().await;
}
}
/// Checks whether a given user is an admin of this server
pub async fn user_is_admin(&self, user_id: &UserId) -> Result<bool> {
let Ok(Some(admin_room)) = Self::get_admin_room() else {
return Ok(false);
};
services().rooms.state_cache.is_joined(user_id, &admin_room)
}
/// Gets the room ID of the admin room
///
/// Errors are propagated from the database, and will have None if there is
/// no admin room
pub fn get_admin_room() -> Result<Option<OwnedRoomId>> {
services()
.rooms
.alias
.resolve_local_alias(&services().globals.admin_alias)
}
}
async fn handle_response(content: Option<RoomMessageEventContent>) {
if let Some(content) = content {
if let Err(e) = respond_to_room(content).await {
error!("{e}");
}
}
}
async fn respond_to_room(output_content: RoomMessageEventContent) -> Result<()> {
let Ok(Some(admin_room)) = Service::get_admin_room() else {
return Ok(());
};
let mutex_state = Arc::clone(
services()
.globals
.roomid_mutex_state
.write()
.await
.entry(admin_room.clone())
.or_default(),
);
let state_lock = mutex_state.lock().await;
let response_pdu = PduBuilder {
event_type: TimelineEventType::RoomMessage,
content: to_raw_value(&output_content).expect("event is valid, we just created it"),
unsigned: None,
state_key: None,
redacts: None,
};
let server_user = &services().globals.server_user;
if let Err(e) = services()
.rooms
.timeline
.build_and_append_pdu(response_pdu, server_user, &admin_room, &state_lock)
.await
{
handle_response_error(&e, &admin_room, server_user, &state_lock).await?;
}
Ok(())
}
async fn handle_response_error(
e: &Error, admin_room: &RoomId, server_user: &UserId, state_lock: &MutexGuard<'_, ()>,
) -> Result<()> {
error!("Failed to build and append admin room response PDU: \"{e}\"");
let error_room_message = RoomMessageEventContent::text_plain(format!(
"Failed to build and append admin room PDU: \"{e}\"\n\nThe original admin command may have finished \
successfully, but we could not return the output."
));
let response_pdu = PduBuilder {
event_type: TimelineEventType::RoomMessage,
content: to_raw_value(&error_room_message).expect("event is valid, we just created it"),
unsigned: None,
state_key: None,
redacts: None,
};
services()
.rooms
.timeline
.build_and_append_pdu(response_pdu, server_user, admin_room, state_lock)
.await?;
Ok(())
}

View file

@ -160,7 +160,9 @@ impl Data for KeyValueDatabase {
futures.push(self.userid_lastonetimekeyupdate.watch_prefix(&userid_bytes));
futures.push(Box::pin(async move {
let _result = services().server.signal.subscribe().recv().await;
while services().server.running() {
let _result = services().server.signal.subscribe().recv().await;
}
}));
if !services().server.running() {

View file

@ -190,7 +190,7 @@ pub(crate) async fn migrations(db: &KeyValueDatabase, config: &Config) -> Result
.insert(b"retroactively_fix_bad_data_from_roomuserid_joined", &[])?;
// Create the admin room and server user on first run
services().admin.create_admin_room().await?;
crate::admin::create_admin_room().await?;
warn!(
"Created new {} database with version {}",

View file

@ -498,7 +498,7 @@ impl Service {
{
services()
.admin
.process_message(body, pdu.event_id.clone())
.command(body, Some(pdu.event_id.clone()))
.await;
}
}