Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
tiny does not update permissions of existing log files.
- Add new server configuration option `autoconnect` to disable automatically
connecting to a server on startup. (#443)
- Expand tildes and shell variables in `pem` (SASL authentication) and
`log_dir` config fields. (#192, #463)

# 2025/01/01: 0.13.0

Expand Down
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/tiny/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ log = "0.4"
serde = { version = "1.0.196", features = ["derive"] }
serde_yaml = "0.8"
shell-words = "1.1.0"
shellexpand = "3.1.2"
time = "0.1"
tokio = { version = "1.36", default-features = false, features = [] }
tokio-stream = { version = "0.1", features = [] }
Expand Down
4 changes: 3 additions & 1 deletion crates/tiny/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ servers:
# sasl:
# username: tiny_user
# password: hunter2
# pem: "/home/user/.config/tiny/oftc.pem"

# sasl:
# pem: "$HOME/.config/tiny/oftc.pem"

# nickserv_ident: hunter2

Expand Down
107 changes: 107 additions & 0 deletions crates/tiny/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use libtiny_client::SASLAuth as ClientSASLAuth;
use serde::{Deserialize, Deserializer};

use shellexpand::LookupError;
use std::env::VarError;
use std::fs;
use std::fs::File;
use std::io::{Read, Write};
Expand Down Expand Up @@ -314,6 +316,29 @@ impl Config<PassOrCmd> {
errors
}

pub(crate) fn expand_fields(
&mut self,
home_dir: impl Fn() -> Option<String>,
env_var: impl Fn(&str) -> Result<Option<String>, VarError>,
) -> Result<(), LookupError<VarError>> {
for server in &mut self.servers {
server.sasl_auth = match &mut server.sasl_auth {
None => None,
Some(other @ SASLAuth::Plain { .. }) => Some(other.clone()),
Some(SASLAuth::External { pem }) => Some(SASLAuth::External {
pem: expand_path(pem.to_path_buf(), &home_dir, &env_var)?,
}),
};
}

self.log_dir = match &self.log_dir {
None => None,
Some(dir) => Some(expand_path(dir.to_path_buf(), &home_dir, &env_var)?),
};

Ok(())
}

/// Runs password commands and updates the config with plain passwords obtained from the
/// commands.
pub(crate) fn read_passwords(self) -> Option<Config<String>> {
Expand Down Expand Up @@ -396,6 +421,16 @@ impl Config<PassOrCmd> {
}
}

fn expand_path(
path: PathBuf,
home_dir: &impl Fn() -> Option<String>,
env_var: &impl Fn(&str) -> Result<Option<String>, VarError>,
) -> Result<PathBuf, LookupError<VarError>> {
let path_str = path.to_string_lossy();
let expanded = shellexpand::full_with_context(&path_str, home_dir, env_var)?;
Ok(PathBuf::from(expanded.as_ref()))
}
Comment thread
daneofmanythings marked this conversation as resolved.

/// Returns tiny config file path. File may or may not exist.
///
/// Places to look: (in priority order)
Expand Down Expand Up @@ -559,4 +594,76 @@ mod tests {
])
);
}

#[test]
fn config_shell_expansion() {
let mut config: Config<PassOrCmd> = Config {
servers: vec![Server {
addr: "my_server".to_owned(),
alias: None,
port: 123,
tls: false,
pass: None,
autoconnect: true,
user: None,
realname: "".to_owned(),
nicks: vec!["".to_owned()],
join: vec![],
nickserv_ident: None,
sasl_auth: Some(SASLAuth::External {
pem: "~/a/$SASL/b".into(),
}),
}],
defaults: Defaults {
nicks: vec!["".to_owned()],
realname: "".to_owned(),
join: vec![],
tls: false,
},
log_dir: Some("~/b/$LOG/c".into()),
};
config
.expand_fields(
|| Some("/home/test".to_string()),
|s| match s {
"SASL" => Ok(Some("sasl_val".to_string())),
"LOG" => Ok(Some("log_val".to_string())),
_ => Err(VarError::NotPresent),
},
)
.unwrap();

assert_eq!(
config.servers[0].sasl_auth,
Some(SASLAuth::External {
pem: PathBuf::from("/home/test/a/sasl_val/b"),
})
);
assert_eq!(
config.log_dir,
Some(PathBuf::from("/home/test/b/log_val/c"))
);
}

#[test]
fn config_shell_expansion_var_fail() {
let mut config: Config<PassOrCmd> = Config {
servers: vec![],
defaults: Defaults {
nicks: vec!["nick".to_owned()],
realname: "real".to_owned(),
join: vec![],
tls: false,
},
log_dir: Some("~/logs/$MISSING/data".into()),
};
let err = config
.expand_fields(
|| Some("/home/test".to_string()),
|_| Err(VarError::NotPresent),
)
.unwrap_err();
assert_eq!(err.var_name, "MISSING");
assert_eq!(err.cause, VarError::NotPresent);
}
}
11 changes: 10 additions & 1 deletion crates/tiny/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fn main() {
println!("{yaml_err}");
exit(1);
}
Ok(config) => {
Ok(mut config) => {
let config_errors = config.validate();
if !config_errors.is_empty() {
println!(
Expand All @@ -54,6 +54,15 @@ fn main() {
exit(1);
}

if let Err(var_error) = config.expand_fields(
|| dirs::home_dir().and_then(|p| p.into_os_string().into_string().ok()),
|s| std::env::var(s).map(Some),
) {
println!("Config file error:");
println!("- Cannot expand variable: {var_error}");
exit(1);
};
Comment thread
osa1 marked this conversation as resolved.

let config = match config.read_passwords() {
None => exit(1),
Some(config) => config,
Expand Down
Loading