2021-08-04 21:13:51 +00:00
|
|
|
use crate::{location_info, settings::structs::Settings, LemmyError};
|
2021-03-01 17:24:11 +00:00
|
|
|
use anyhow::{anyhow, Context};
|
|
|
|
use deser_hjson::from_str;
|
2021-09-22 15:57:09 +00:00
|
|
|
use regex::{Regex, RegexBuilder};
|
2021-08-04 21:13:51 +00:00
|
|
|
use std::{env, fs, io::Error, sync::RwLock};
|
2021-03-01 17:24:11 +00:00
|
|
|
|
|
|
|
pub mod structs;
|
|
|
|
|
2021-09-28 15:34:18 +00:00
|
|
|
static DEFAULT_CONFIG_FILE: &str = "config/config.hjson";
|
2021-03-01 17:24:11 +00:00
|
|
|
|
|
|
|
lazy_static! {
|
2021-04-05 14:23:32 +00:00
|
|
|
static ref SETTINGS: RwLock<Settings> =
|
|
|
|
RwLock::new(Settings::init().expect("Failed to load settings file"));
|
2021-10-08 15:07:24 +00:00
|
|
|
static ref WEBFINGER_COMMUNITY_REGEX: Regex = Regex::new(&format!(
|
|
|
|
"^group:([a-z0-9_]{{3,}})@{}$",
|
|
|
|
Settings::get().hostname
|
|
|
|
))
|
|
|
|
.expect("compile webfinger regex");
|
|
|
|
static ref WEBFINGER_USER_REGEX: Regex = Regex::new(&format!(
|
|
|
|
"^acct:([a-z0-9_]{{3,}})@{}$",
|
|
|
|
Settings::get().hostname
|
|
|
|
))
|
|
|
|
.expect("compile webfinger regex");
|
2021-03-01 17:24:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Settings {
|
2021-05-26 13:39:38 +00:00
|
|
|
/// Reads config from configuration file.
|
2021-03-01 17:24:11 +00:00
|
|
|
///
|
|
|
|
/// Note: The env var `LEMMY_DATABASE_URL` is parsed in
|
|
|
|
/// `lemmy_db_queries/src/lib.rs::get_database_url_from_env()`
|
2021-09-22 15:57:09 +00:00
|
|
|
/// Warning: Only call this once.
|
|
|
|
pub fn init() -> Result<Self, LemmyError> {
|
2021-03-01 17:24:11 +00:00
|
|
|
// Read the config file
|
2021-10-08 15:07:24 +00:00
|
|
|
let config = from_str::<Settings>(&Self::read_config_file()?)?;
|
2021-03-01 17:24:11 +00:00
|
|
|
|
2021-08-04 21:13:51 +00:00
|
|
|
if config.hostname == "unset" {
|
2021-03-01 17:24:11 +00:00
|
|
|
return Err(anyhow!("Hostname variable is not set!").into());
|
|
|
|
}
|
|
|
|
|
2021-08-04 21:13:51 +00:00
|
|
|
Ok(config)
|
2021-03-01 17:24:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the config as a struct.
|
|
|
|
pub fn get() -> Self {
|
2021-03-01 12:56:07 +00:00
|
|
|
SETTINGS.read().expect("read config").to_owned()
|
2021-03-01 17:24:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_database_url(&self) -> String {
|
2021-08-04 21:13:51 +00:00
|
|
|
let conf = &self.database;
|
2021-03-01 17:24:11 +00:00
|
|
|
format!(
|
|
|
|
"postgres://{}:{}@{}:{}/{}",
|
2021-08-04 21:13:51 +00:00
|
|
|
conf.user, conf.password, conf.host, conf.port, conf.database,
|
2021-03-01 17:24:11 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_config_location() -> String {
|
2021-09-28 15:34:18 +00:00
|
|
|
env::var("LEMMY_CONFIG_LOCATION").unwrap_or_else(|_| DEFAULT_CONFIG_FILE.to_string())
|
2021-03-01 17:24:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn read_config_file() -> Result<String, Error> {
|
|
|
|
fs::read_to_string(Self::get_config_location())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns either "http" or "https", depending on tls_enabled setting
|
|
|
|
pub fn get_protocol_string(&self) -> &'static str {
|
2021-08-04 21:13:51 +00:00
|
|
|
if self.tls_enabled {
|
|
|
|
"https"
|
2021-03-01 17:24:11 +00:00
|
|
|
} else {
|
|
|
|
"http"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns something like `http://localhost` or `https://lemmy.ml`,
|
|
|
|
/// with the correct protocol and hostname.
|
|
|
|
pub fn get_protocol_and_hostname(&self) -> String {
|
2021-08-04 21:13:51 +00:00
|
|
|
format!("{}://{}", self.get_protocol_string(), self.hostname)
|
2021-03-01 17:24:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// When running the federation test setup in `api_tests/` or `docker/federation`, the `hostname`
|
|
|
|
/// variable will be like `lemmy-alpha:8541`. This method removes the port and returns
|
|
|
|
/// `lemmy-alpha` instead. It has no effect in production.
|
|
|
|
pub fn get_hostname_without_port(&self) -> Result<String, anyhow::Error> {
|
|
|
|
Ok(
|
|
|
|
self
|
2021-08-04 21:13:51 +00:00
|
|
|
.hostname
|
2021-03-01 17:24:11 +00:00
|
|
|
.split(':')
|
|
|
|
.collect::<Vec<&str>>()
|
|
|
|
.first()
|
|
|
|
.context(location_info!())?
|
|
|
|
.to_string(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-03-01 12:56:07 +00:00
|
|
|
pub fn save_config_file(data: &str) -> Result<String, LemmyError> {
|
2021-09-28 15:34:18 +00:00
|
|
|
fs::write(Settings::get_config_location(), data)?;
|
2021-03-01 17:24:11 +00:00
|
|
|
|
|
|
|
// Reload the new settings
|
|
|
|
// From https://stackoverflow.com/questions/29654927/how-do-i-assign-a-string-to-a-mutable-static-variable/47181804#47181804
|
2021-03-01 12:56:07 +00:00
|
|
|
let mut new_settings = SETTINGS.write().expect("write config");
|
2021-03-01 17:24:11 +00:00
|
|
|
*new_settings = match Settings::init() {
|
|
|
|
Ok(c) => c,
|
|
|
|
Err(e) => panic!("{}", e),
|
|
|
|
};
|
|
|
|
|
2021-03-01 12:56:07 +00:00
|
|
|
Ok(Self::read_config_file()?)
|
2021-03-01 17:24:11 +00:00
|
|
|
}
|
2021-09-22 15:57:09 +00:00
|
|
|
|
|
|
|
pub fn webfinger_community_regex(&self) -> Regex {
|
2021-10-08 15:07:24 +00:00
|
|
|
WEBFINGER_COMMUNITY_REGEX.to_owned()
|
2021-09-22 15:57:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn webfinger_username_regex(&self) -> Regex {
|
2021-10-08 15:07:24 +00:00
|
|
|
WEBFINGER_USER_REGEX.to_owned()
|
2021-09-22 15:57:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn slur_regex(&self) -> Regex {
|
|
|
|
let mut slurs = r"(fag(g|got|tard)?\b|cock\s?sucker(s|ing)?|ni((g{2,}|q)+|[gq]{2,})[e3r]+(s|z)?|mudslime?s?|kikes?|\bspi(c|k)s?\b|\bchinks?|gooks?|bitch(es|ing|y)?|whor(es?|ing)|\btr(a|@)nn?(y|ies?)|\b(b|re|r)tard(ed)?s?)".to_string();
|
|
|
|
if let Some(additional_slurs) = &self.additional_slurs {
|
|
|
|
slurs.push('|');
|
|
|
|
slurs.push_str(additional_slurs);
|
|
|
|
};
|
|
|
|
RegexBuilder::new(&slurs)
|
|
|
|
.case_insensitive(true)
|
|
|
|
.build()
|
|
|
|
.expect("compile regex")
|
|
|
|
}
|
2021-03-01 17:24:11 +00:00
|
|
|
}
|