This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

tools.mutt: fix email sending with timeouts and SSH fallback

+36 -23
+36 -23
tools/mutt/flake-module.nix
··· 63 63 msmtprc = pkgs.writeText "msmtprc" '' 64 64 defaults 65 65 logfile ~/.msmtp.log 66 + timeout 10 66 67 account prism 67 68 host neoprism.r 68 69 account c-base ··· 98 99 msmtp = pkgs.writeShellScriptBin "msmtp" '' 99 100 export NOTMUCH_CONFIG="${notmuchConfig}" 100 101 101 - # Detect which account to use based on arguments 102 + # Save message to temporary file first 103 + tmpfile=$(mktemp) 104 + trap 'rm -f "$tmpfile"' EXIT 105 + cat > "$tmpfile" 106 + 107 + # Detect which account to use based on From: header in the email 102 108 account="" 103 - for arg in "$@"; do 104 - if [[ "$arg" == "c-base" ]] || [[ "$arg" =~ @c-base\.org ]]; then 105 - account="c-base" 106 - break 107 - elif [[ "$arg" == "clan-infra" ]] || [[ "$arg" =~ @clan\.lol ]]; then 108 - account="clan-infra" 109 - break 110 - elif [[ "$arg" == "dedede" ]] || [[ "$arg" =~ @dedede\.org ]]; then 111 - account="dedede" 112 - break 113 - fi 114 - done 109 + from_line=$(grep -i "^From:" "$tmpfile" | head -1) 110 + if [[ "$from_line" =~ @c-base\.org ]]; then 111 + account="c-base" 112 + elif [[ "$from_line" =~ @clan\.lol ]]; then 113 + account="clan-infra" 114 + elif [[ "$from_line" =~ @dedede\.org ]]; then 115 + account="dedede" 116 + fi 115 117 116 118 # For external accounts (c-base, clan-infra, dedede), always send directly 117 119 if [ -n "$account" ]; then 118 - ${pkgs.coreutils}/bin/tee >(${pkgs.notmuch}/bin/notmuch insert +sent) | \ 119 - ${pkgs.msmtp}/bin/msmtp -C ${msmtprc} "$@" 120 - # For default account, try neoprism.r first, then SSH 121 - elif ping -W2 -c1 neoprism.r >/dev/null 2>&1; then 122 - ${pkgs.coreutils}/bin/tee >(${pkgs.notmuch}/bin/notmuch insert +sent) | \ 123 - ${pkgs.msmtp}/bin/msmtp -C ${msmtprc} "$@" 120 + if ${pkgs.msmtp}/bin/msmtp -C ${msmtprc} "$@" < "$tmpfile"; then 121 + ${pkgs.notmuch}/bin/notmuch insert +sent < "$tmpfile" 122 + exit 0 123 + else 124 + exit 1 125 + fi 126 + fi 127 + 128 + # For default account, try neoprism.r first, fallback to SSH 129 + if ${pkgs.msmtp}/bin/msmtp -C ${msmtprc} "$@" < "$tmpfile" 2>/dev/null; then 130 + ${pkgs.notmuch}/bin/notmuch insert +sent < "$tmpfile" 131 + exit 0 132 + fi 133 + 134 + # If direct connection failed, fall back to SSH tunnel 135 + echo "Direct connection failed, sending via SSH..." >&2 136 + if ${pkgs.openssh}/bin/ssh -o ConnectTimeout=10 -p 45621 neoprism.lassul.us 'msmtp -C /dev/null --host=localhost --port=25 --read-envelope-from --read-recipients' < "$tmpfile"; then 137 + ${pkgs.notmuch}/bin/notmuch insert +sent < "$tmpfile" 138 + exit 0 124 139 else 125 - # Fall back to SSH tunnel to neoprism, submit via SMTP to local daemon 126 - echo "neoprism.r not reachable, sending via SSH..." >&2 127 - ${pkgs.coreutils}/bin/tee >(${pkgs.notmuch}/bin/notmuch insert +sent) | \ 128 - ${pkgs.openssh}/bin/ssh -p 45621 neoprism.lassul.us 'msmtp -C /dev/null --host=localhost --port=25 --read-envelope-from --read-recipients' 140 + echo "Failed to send email" >&2 141 + exit 1 129 142 fi 130 143 ''; 131 144