Add 'euph.rooms.<name>.password' config option
This commit is contained in:
parent
e40948567a
commit
6e6fddc0b1
7 changed files with 65 additions and 12 deletions
|
|
@ -18,6 +18,7 @@ Procedure when bumping the version number:
|
||||||
- Config file
|
- Config file
|
||||||
- `ephemeral` config option
|
- `ephemeral` config option
|
||||||
- `data_dir` config option
|
- `data_dir` config option
|
||||||
|
- `euph.rooms.<name>.password` config option
|
||||||
|
|
||||||
## v0.3.0 - 2022-08-22
|
## v0.3.0 - 2022-08-22
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
use std::collections::HashMap;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
|
|
@ -5,11 +6,22 @@ use serde::Deserialize;
|
||||||
|
|
||||||
use crate::macros::ok_or_return;
|
use crate::macros::ok_or_return;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Default, Deserialize)]
|
||||||
|
pub struct EuphRoom {
|
||||||
|
pub password: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Default, Deserialize)]
|
||||||
|
pub struct Euph {
|
||||||
|
pub rooms: HashMap<String, EuphRoom>,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Default, Deserialize)]
|
#[derive(Debug, Default, Deserialize)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
pub data_dir: Option<PathBuf>,
|
pub data_dir: Option<PathBuf>,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub ephemeral: bool,
|
pub ephemeral: bool,
|
||||||
|
pub euph: Euph,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
|
|
@ -23,4 +35,8 @@ impl Config {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn euph_room(&self, name: &str) -> EuphRoom {
|
||||||
|
self.euph.rooms.get(name).cloned().unwrap_or_default()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -58,6 +58,7 @@ enum Event {
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct State {
|
struct State {
|
||||||
name: String,
|
name: String,
|
||||||
|
password: Option<String>,
|
||||||
vault: EuphVault,
|
vault: EuphVault,
|
||||||
|
|
||||||
conn_tx: Option<ConnTx>,
|
conn_tx: Option<ConnTx>,
|
||||||
|
|
@ -231,7 +232,14 @@ impl State {
|
||||||
async fn on_packet(&mut self, packet: &ParsedPacket) -> anyhow::Result<()> {
|
async fn on_packet(&mut self, packet: &ParsedPacket) -> anyhow::Result<()> {
|
||||||
let data = ok_or_return!(&packet.content, Ok(()));
|
let data = ok_or_return!(&packet.content, Ok(()));
|
||||||
match data {
|
match data {
|
||||||
Data::BounceEvent(_) => {}
|
Data::BounceEvent(_) => {
|
||||||
|
if let Some(password) = &self.password {
|
||||||
|
// Try to authenticate with the configured password, but no
|
||||||
|
// promises if it doesn't work. In particular, we only ever
|
||||||
|
// try this password once.
|
||||||
|
self.on_auth(password.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
Data::DisconnectEvent(d) => {
|
Data::DisconnectEvent(d) => {
|
||||||
warn!("e&{}: disconnected for reason {:?}", self.name, d.reason);
|
warn!("e&{}: disconnected for reason {:?}", self.name, d.reason);
|
||||||
}
|
}
|
||||||
|
|
@ -411,7 +419,10 @@ pub struct Room {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Room {
|
impl Room {
|
||||||
pub fn new(vault: EuphVault) -> (Self, mpsc::UnboundedReceiver<EuphRoomEvent>) {
|
pub fn new(
|
||||||
|
vault: EuphVault,
|
||||||
|
password: Option<String>,
|
||||||
|
) -> (Self, mpsc::UnboundedReceiver<EuphRoomEvent>) {
|
||||||
let (canary_tx, canary_rx) = oneshot::channel();
|
let (canary_tx, canary_rx) = oneshot::channel();
|
||||||
let (event_tx, event_rx) = mpsc::unbounded_channel();
|
let (event_tx, event_rx) = mpsc::unbounded_channel();
|
||||||
let (euph_room_event_tx, euph_room_event_rx) = mpsc::unbounded_channel();
|
let (euph_room_event_tx, euph_room_event_rx) = mpsc::unbounded_channel();
|
||||||
|
|
@ -419,6 +430,7 @@ impl Room {
|
||||||
|
|
||||||
let state = State {
|
let state = State {
|
||||||
name: vault.room().to_string(),
|
name: vault.room().to_string(),
|
||||||
|
password,
|
||||||
vault,
|
vault,
|
||||||
conn_tx: None,
|
conn_tx: None,
|
||||||
last_msg_id: None,
|
last_msg_id: None,
|
||||||
|
|
|
||||||
|
|
@ -110,19 +110,21 @@ async fn main() -> anyhow::Result<()> {
|
||||||
let mut config = Config::load(&config_path);
|
let mut config = Config::load(&config_path);
|
||||||
set_data_dir(&mut config, args.data_dir);
|
set_data_dir(&mut config, args.data_dir);
|
||||||
set_ephemeral(&mut config, args.ephemeral);
|
set_ephemeral(&mut config, args.ephemeral);
|
||||||
|
let config = Box::leak(Box::new(config));
|
||||||
|
|
||||||
let vault = if config.ephemeral {
|
let vault = if config.ephemeral {
|
||||||
vault::launch_in_memory()?
|
vault::launch_in_memory()?
|
||||||
} else {
|
} else {
|
||||||
let data_dir = config
|
let data_dir = config
|
||||||
.data_dir
|
.data_dir
|
||||||
|
.clone()
|
||||||
.unwrap_or_else(|| dirs.data_dir().to_path_buf());
|
.unwrap_or_else(|| dirs.data_dir().to_path_buf());
|
||||||
println!("Data dir: {}", data_dir.to_string_lossy());
|
println!("Data dir: {}", data_dir.to_string_lossy());
|
||||||
vault::launch(&data_dir.join("vault.db"))?
|
vault::launch(&data_dir.join("vault.db"))?
|
||||||
};
|
};
|
||||||
|
|
||||||
match args.command.unwrap_or_default() {
|
match args.command.unwrap_or_default() {
|
||||||
Command::Run => run(&vault, args.measure_widths).await?,
|
Command::Run => run(config, &vault, args.measure_widths).await?,
|
||||||
Command::Export(args) => export::export(&vault, args).await?,
|
Command::Export(args) => export::export(&vault, args).await?,
|
||||||
Command::Gc => {
|
Command::Gc => {
|
||||||
println!("Cleaning up and compacting vault");
|
println!("Cleaning up and compacting vault");
|
||||||
|
|
@ -141,7 +143,7 @@ async fn main() -> anyhow::Result<()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn run(vault: &Vault, measure_widths: bool) -> anyhow::Result<()> {
|
async fn run(config: &'static Config, vault: &Vault, measure_widths: bool) -> anyhow::Result<()> {
|
||||||
let (logger, logger_rx) = Logger::init(log::Level::Debug);
|
let (logger, logger_rx) = Logger::init(log::Level::Debug);
|
||||||
info!(
|
info!(
|
||||||
"Welcome to {} {}",
|
"Welcome to {} {}",
|
||||||
|
|
@ -151,7 +153,7 @@ async fn run(vault: &Vault, measure_widths: bool) -> anyhow::Result<()> {
|
||||||
|
|
||||||
let mut terminal = Terminal::new()?;
|
let mut terminal = Terminal::new()?;
|
||||||
terminal.set_measuring(measure_widths);
|
terminal.set_measuring(measure_widths);
|
||||||
Ui::run(&mut terminal, vault.clone(), logger, logger_rx).await?;
|
Ui::run(config, &mut terminal, vault.clone(), logger, logger_rx).await?;
|
||||||
drop(terminal); // So the vault can print again
|
drop(terminal); // So the vault can print again
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,7 @@ use tokio::sync::mpsc::{self, UnboundedReceiver, UnboundedSender};
|
||||||
use tokio::task;
|
use tokio::task;
|
||||||
use toss::terminal::Terminal;
|
use toss::terminal::Terminal;
|
||||||
|
|
||||||
|
use crate::config::Config;
|
||||||
use crate::euph::EuphRoomEvent;
|
use crate::euph::EuphRoomEvent;
|
||||||
use crate::logger::{LogMsg, Logger};
|
use crate::logger::{LogMsg, Logger};
|
||||||
use crate::macros::{ok_or_return, some_or_return};
|
use crate::macros::{ok_or_return, some_or_return};
|
||||||
|
|
@ -66,6 +67,7 @@ impl Ui {
|
||||||
const POLL_DURATION: Duration = Duration::from_millis(100);
|
const POLL_DURATION: Duration = Duration::from_millis(100);
|
||||||
|
|
||||||
pub async fn run(
|
pub async fn run(
|
||||||
|
config: &'static Config,
|
||||||
terminal: &mut Terminal,
|
terminal: &mut Terminal,
|
||||||
vault: Vault,
|
vault: Vault,
|
||||||
logger: Logger,
|
logger: Logger,
|
||||||
|
|
@ -93,7 +95,7 @@ impl Ui {
|
||||||
let mut ui = Self {
|
let mut ui = Self {
|
||||||
event_tx: event_tx.clone(),
|
event_tx: event_tx.clone(),
|
||||||
mode: Mode::Main,
|
mode: Mode::Main,
|
||||||
rooms: Rooms::new(vault, event_tx.clone()),
|
rooms: Rooms::new(config, vault, event_tx.clone()),
|
||||||
log_chat: ChatState::new(logger),
|
log_chat: ChatState::new(logger),
|
||||||
key_bindings_list: None,
|
key_bindings_list: None,
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ use tokio::sync::{mpsc, oneshot};
|
||||||
use toss::styled::Styled;
|
use toss::styled::Styled;
|
||||||
use toss::terminal::Terminal;
|
use toss::terminal::Terminal;
|
||||||
|
|
||||||
|
use crate::config;
|
||||||
use crate::euph::{self, EuphRoomEvent};
|
use crate::euph::{self, EuphRoomEvent};
|
||||||
use crate::macros::{ok_or_return, some_or_return};
|
use crate::macros::{ok_or_return, some_or_return};
|
||||||
use crate::store::MsgStore;
|
use crate::store::MsgStore;
|
||||||
|
|
@ -47,6 +48,8 @@ pub enum RoomStatus {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct EuphRoom {
|
pub struct EuphRoom {
|
||||||
|
config: config::EuphRoom,
|
||||||
|
|
||||||
ui_event_tx: mpsc::UnboundedSender<UiEvent>,
|
ui_event_tx: mpsc::UnboundedSender<UiEvent>,
|
||||||
|
|
||||||
vault: EuphVault,
|
vault: EuphVault,
|
||||||
|
|
@ -62,8 +65,13 @@ pub struct EuphRoom {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl EuphRoom {
|
impl EuphRoom {
|
||||||
pub fn new(vault: EuphVault, ui_event_tx: mpsc::UnboundedSender<UiEvent>) -> Self {
|
pub fn new(
|
||||||
|
config: config::EuphRoom,
|
||||||
|
vault: EuphVault,
|
||||||
|
ui_event_tx: mpsc::UnboundedSender<UiEvent>,
|
||||||
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
config,
|
||||||
ui_event_tx,
|
ui_event_tx,
|
||||||
vault: vault.clone(),
|
vault: vault.clone(),
|
||||||
room: None,
|
room: None,
|
||||||
|
|
@ -94,7 +102,7 @@ impl EuphRoom {
|
||||||
if self.room.is_none() {
|
if self.room.is_none() {
|
||||||
let store = self.chat.store().clone();
|
let store = self.chat.store().clone();
|
||||||
let name = store.room().to_string();
|
let name = store.room().to_string();
|
||||||
let (room, euph_room_event_rx) = euph::Room::new(store);
|
let (room, euph_room_event_rx) = euph::Room::new(store, self.config.password.clone());
|
||||||
|
|
||||||
self.room = Some(room);
|
self.room = Some(room);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ use tokio::sync::mpsc;
|
||||||
use toss::styled::Styled;
|
use toss::styled::Styled;
|
||||||
use toss::terminal::Terminal;
|
use toss::terminal::Terminal;
|
||||||
|
|
||||||
|
use crate::config::Config;
|
||||||
use crate::euph::EuphRoomEvent;
|
use crate::euph::EuphRoomEvent;
|
||||||
use crate::vault::Vault;
|
use crate::vault::Vault;
|
||||||
|
|
||||||
|
|
@ -33,6 +34,8 @@ enum State {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Rooms {
|
pub struct Rooms {
|
||||||
|
config: &'static Config,
|
||||||
|
|
||||||
vault: Vault,
|
vault: Vault,
|
||||||
ui_event_tx: mpsc::UnboundedSender<UiEvent>,
|
ui_event_tx: mpsc::UnboundedSender<UiEvent>,
|
||||||
|
|
||||||
|
|
@ -43,8 +46,13 @@ pub struct Rooms {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Rooms {
|
impl Rooms {
|
||||||
pub fn new(vault: Vault, ui_event_tx: mpsc::UnboundedSender<UiEvent>) -> Self {
|
pub fn new(
|
||||||
|
config: &'static Config,
|
||||||
|
vault: Vault,
|
||||||
|
ui_event_tx: mpsc::UnboundedSender<UiEvent>,
|
||||||
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
config,
|
||||||
vault,
|
vault,
|
||||||
ui_event_tx,
|
ui_event_tx,
|
||||||
state: State::ShowList,
|
state: State::ShowList,
|
||||||
|
|
@ -54,9 +62,13 @@ impl Rooms {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_or_insert_room(&mut self, name: String) -> &mut EuphRoom {
|
fn get_or_insert_room(&mut self, name: String) -> &mut EuphRoom {
|
||||||
self.euph_rooms
|
self.euph_rooms.entry(name.clone()).or_insert_with(|| {
|
||||||
.entry(name.clone())
|
EuphRoom::new(
|
||||||
.or_insert_with(|| EuphRoom::new(self.vault.euph(name), self.ui_event_tx.clone()))
|
self.config.euph_room(&name),
|
||||||
|
self.vault.euph(name),
|
||||||
|
self.ui_event_tx.clone(),
|
||||||
|
)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Remove rooms that are not running any more and can't be found in the db.
|
/// Remove rooms that are not running any more and can't be found in the db.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue