diff --git a/tasks.md b/tasks.md index 8f67545..256a0e3 100644 --- a/tasks.md +++ b/tasks.md @@ -346,8 +346,9 @@ Brukernavn@domene ruter til brukerens innboks. Alle domener (synops.no, sidelinja.org, vegard.info) ruter til samme bruker basert på username. - [x] 26.1 Username i auth_identities: legg til `username`-kolonne, populer fra Authentik `preferred_username` ved login. Unik constraint. Oppdater auth-callback i SvelteKit til å lagre username. -- [~] 26.2 msmtp oppsett: konfigurer utgående epost via SMTP-relay. Avsender: `vaktmester@synops.no`. Tilgjengelig som `synops-mail --send --to --subject ` CLI-verktøy. - > Påbegynt: 2026-03-18T19:05 +- [?] 26.2 msmtp oppsett: konfigurer utgående epost via SMTP-relay. Avsender: `vaktmester@synops.no`. Tilgjengelig som `synops-mail --send --to --subject ` CLI-verktøy. + > Spørsmål: Trenger SMTP-relay credentials. Hetzner-relay (mail.your-server.de:587) krever autentisering, og port 25 er blokkert utgående. Verktøyet (`synops-mail`) og msmtp-config er ferdig — mangler kun brukernavn/passord i `/srv/synops/config/msmtp/msmtprc`. Alternativer: (1) Hetzner Robot-konto credentials, (2) ekstern SMTP-relay (Brevo, Mailgun, etc.), (3) annen tilnærming? + > Kontekst: Alt er implementert og testet (CLI kompilerer, msmtp kobler til relay, auth feiler pga placeholder-credentials). Trenger bare ekte SMTP-bruker/passord. - [ ] 26.3 MX-records: sett opp MX for synops.no, sidelinja.org, vegard.info som peker til serveren. - [ ] 26.4 Postfix minimal: installer Postfix som lokal MTA kun for mottak. Ingen relay, ingen kø for utgående. Pipe innkommende epost til `synops-mail --receive`. - [ ] 26.5 `synops-mail --receive`: Rust CLI som leser raw epost fra stdin. Sjekk 1: avsender-epost matcher `auth_identities.email`? Sjekk 2: innhold starter med "Kjære vaktmester" (eller konfigurerbar frase)? Begge må matche. Opprett `content`-node i brukerens innboks med epostinnholdet. Alt annet → /dev/null, ingen bounce. diff --git a/tools/README.md b/tools/README.md index 9bf70d1..a4813f5 100644 --- a/tools/README.md +++ b/tools/README.md @@ -23,6 +23,7 @@ eller maskinrommet-API. Ligger i PATH via symlink eller direkte kall. | `synops-node` | Hent/vis en node med edges (UUID, --depth, --format json/md) | Ferdig | | `synops-ai` | AI-assistert generering av orkestreringsscript fra fritekst | Ferdig | | `synops-clip` | Hent og parse webartikler (Readability + Playwright-fallback, paywall-deteksjon) | Ferdig | +| `synops-mail` | Send epost via msmtp (vaktmester@synops.no) | Ferdig (venter SMTP-credentials) | ## Delt bibliotek diff --git a/tools/synops-mail/Cargo.lock b/tools/synops-mail/Cargo.lock new file mode 100644 index 0000000..4745f33 --- /dev/null +++ b/tools/synops-mail/Cargo.lock @@ -0,0 +1,186 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "anstream" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "824a212faf96e9acacdbd09febd34438f8f711fb84e09a8916013cd7815ca28d" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "940b3a0ca603d1eade50a4846a2afffd5ef57a9feac2c0e2ec2e14f9ead76000" + +[[package]] +name = "anstyle-parse" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52ce7f38b242319f7cabaa6813055467063ecdc9d355bbb4ce0c68908cd8130e" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" +dependencies = [ + "anstyle", + "once_cell_polyfill", + "windows-sys", +] + +[[package]] +name = "clap" +version = "4.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b193af5b67834b676abd72466a96c1024e6a6ad978a1f484bd90b85c94041351" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "714a53001bf66416adb0e2ef5ac857140e7dc3a0c48fb28b2f10762fc4b5069f" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1110bd8a634a1ab8cb04345d8d878267d57c3cf1b38d91b71af6686408bbca6a" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8d4a3bb8b1e0c1050499d1815f5ab16d04f0959b233085fb31653fbfc9d98f9" + +[[package]] +name = "colorchoice" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570" + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" + +[[package]] +name = "once_cell_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" + +[[package]] +name = "proc-macro2" +version = "1.0.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "syn" +version = "2.0.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "synops-mail" +version = "0.1.0" +dependencies = [ + "clap", +] + +[[package]] +name = "unicode-ident" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" + +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + +[[package]] +name = "windows-sys" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" +dependencies = [ + "windows-link", +] diff --git a/tools/synops-mail/Cargo.toml b/tools/synops-mail/Cargo.toml new file mode 100644 index 0000000..c8262fd --- /dev/null +++ b/tools/synops-mail/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "synops-mail" +version = "0.1.0" +edition = "2024" + +[[bin]] +name = "synops-mail" +path = "src/main.rs" + +[dependencies] +clap = { version = "4", features = ["derive", "env"] } diff --git a/tools/synops-mail/src/main.rs b/tools/synops-mail/src/main.rs new file mode 100644 index 0000000..9a009d1 --- /dev/null +++ b/tools/synops-mail/src/main.rs @@ -0,0 +1,134 @@ +// synops-mail — Send epost via msmtp. +// +// Sender epost via systemets msmtp-konfigurasjon. +// Avsender er alltid vaktmester@synops.no. +// +// Bruk: +// synops-mail --send --to bruker@eksempel.no --subject "Emne" +// echo "Brødtekst" | synops-mail --send --to bruker@eksempel.no --subject "Emne" +// synops-mail --send --to bruker@eksempel.no --subject "Emne" --body "Tekst her" +// +// Output: Bekreftelse til stdout ved suksess. +// Feil: stderr + exit code != 0. +// +// Konfigurering: +// msmtp-konfig: /srv/synops/config/msmtp/msmtprc +// Miljøvariabel MSMTP_CONFIG kan overstyre config-sti. +// +// Ref: docs/retninger/unix_filosofi.md + +use clap::Parser; +use std::io::{self, Read}; +use std::process::{Command, Stdio}; + +const DEFAULT_FROM: &str = "vaktmester@synops.no"; +const DEFAULT_FROM_NAME: &str = "Synops Vaktmester"; +const DEFAULT_CONFIG: &str = "/srv/synops/config/msmtp/msmtprc"; + +/// Send epost via msmtp. +#[derive(Parser)] +#[command(name = "synops-mail", about = "Send epost via msmtp")] +struct Cli { + /// Send-modus (påkrevd) + #[arg(long)] + send: bool, + + /// Mottaker-epostadresse + #[arg(long)] + to: String, + + /// Emne + #[arg(long)] + subject: String, + + /// Brødtekst (valgfritt — leses fra stdin om ikke oppgitt) + #[arg(long)] + body: Option, + + /// Avsender (default: vaktmester@synops.no) + #[arg(long, default_value = DEFAULT_FROM)] + from: String, + + /// msmtp config-fil (default: /srv/synops/config/msmtp/msmtprc) + #[arg(long, env = "MSMTP_CONFIG")] + config: Option, +} + +fn main() { + let cli = Cli::parse(); + + if !cli.send { + eprintln!("Feil: --send er påkrevd"); + std::process::exit(1); + } + + // Les body fra --body eller stdin + let body = match cli.body { + Some(b) => b, + None => { + let mut buf = String::new(); + io::stdin().read_to_string(&mut buf).unwrap_or_else(|e| { + eprintln!("Feil ved lesing fra stdin: {e}"); + std::process::exit(1); + }); + if buf.is_empty() { + eprintln!("Feil: Ingen --body og ingen data på stdin"); + std::process::exit(1); + } + buf + } + }; + + let config_path = cli.config.unwrap_or_else(|| DEFAULT_CONFIG.to_string()); + + // Bygg RFC 5322-melding + let message = format!( + "From: {from_name} <{from}>\r\n\ + To: {to}\r\n\ + Subject: {subject}\r\n\ + MIME-Version: 1.0\r\n\ + Content-Type: text/plain; charset=utf-8\r\n\ + Content-Transfer-Encoding: 8bit\r\n\ + \r\n\ + {body}", + from_name = DEFAULT_FROM_NAME, + from = cli.from, + to = cli.to, + subject = cli.subject, + body = body, + ); + + // Send via msmtp + let mut child = Command::new("msmtp") + .args(["--file", &config_path, "--from", &cli.from, &cli.to]) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .unwrap_or_else(|e| { + eprintln!("Kunne ikke starte msmtp: {e}"); + std::process::exit(1); + }); + + // Skriv melding til msmtp stdin + if let Some(mut stdin) = child.stdin.take() { + use std::io::Write; + stdin.write_all(message.as_bytes()).unwrap_or_else(|e| { + eprintln!("Feil ved skriving til msmtp: {e}"); + std::process::exit(1); + }); + } + + let output = child.wait_with_output().unwrap_or_else(|e| { + eprintln!("Feil ved venting på msmtp: {e}"); + std::process::exit(1); + }); + + if output.status.success() { + println!("Epost sendt til {}", cli.to); + } else { + let stderr = String::from_utf8_lossy(&output.stderr); + eprintln!("msmtp feilet (exit {}): {}", output.status, stderr); + std::process::exit(output.status.code().unwrap_or(1)); + } +}