Dotfiles/setup/modules/optional-Modules/apps/mail-notmuch.sh

156 lines
5.9 KiB
Bash
Executable File

#!/bin/bash
set -euo pipefail
source "$(dirname "${BASH_SOURCE[0]}")/../../lib/logging.sh"
log "Installing mail stack (isync, msmtp, notmuch, alot, w3m)..."
sudo pacman -S --noconfirm --needed isync msmtp notmuch alot w3m
# ── Credentials ───────────────────────────────────────────────────────────────
read -rp "Full name : " FULL_NAME
read -rp "Email address : " EMAIL
read -rp "IMAP host (e.g. mail.example.com) : " IMAP_HOST
read -rp "IMAP port [993] : " IMAP_PORT; IMAP_PORT="${IMAP_PORT:-993}"
read -rp "IMAP username [$EMAIL] : " IMAP_USER; IMAP_USER="${IMAP_USER:-$EMAIL}"
read -rsp "IMAP password : " IMAP_PASS; echo
read -rp "SMTP host (e.g. mail.example.com) : " SMTP_HOST
read -rp "SMTP port [587] : " SMTP_PORT; SMTP_PORT="${SMTP_PORT:-587}"
MAILDIR="$HOME/Mail"
mkdir -p "$MAILDIR"
# ── mbsync ────────────────────────────────────────────────────────────────────
log "Writing ~/.mbsyncrc..."
cat > ~/.mbsyncrc << EOF
IMAPAccount main
Host $IMAP_HOST
Port $IMAP_PORT
User $IMAP_USER
Pass $IMAP_PASS
SSLType IMAPS
CertificateFile /etc/ssl/certs/ca-certificates.crt
IMAPStore main-remote
Account main
MaildirStore main-local
SubFolders Verbatim
Path $MAILDIR/
Inbox $MAILDIR/INBOX
Channel main
Far :main-remote:
Near :main-local:
Patterns *
Create Both
SyncState *
Expunge Both
EOF
chmod 600 ~/.mbsyncrc
# ── msmtp ─────────────────────────────────────────────────────────────────────
log "Writing ~/.msmtprc..."
cat > ~/.msmtprc << EOF
defaults
tls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
logfile ~/.msmtp.log
account main
host $SMTP_HOST
port $SMTP_PORT
auth on
user $IMAP_USER
password $IMAP_PASS
from $EMAIL
account default : main
EOF
chmod 600 ~/.msmtprc
# ── notmuch ───────────────────────────────────────────────────────────────────
log "Configuring notmuch..."
notmuch config set user.name "$FULL_NAME"
notmuch config set user.email "$EMAIL"
notmuch config set database.path "$MAILDIR"
notmuch config set maildir.synchronize_flags true
notmuch config set new.tags "unread;inbox"
# post-new hook: tag sent mail, remove inbox from trash
mkdir -p "$MAILDIR/.notmuch/hooks"
cat > "$MAILDIR/.notmuch/hooks/post-new" << 'EOF'
#!/bin/bash
notmuch tag +sent -inbox -- folder:Sent
notmuch tag +trash -inbox -unread -- folder:Trash
notmuch tag +draft -inbox -- folder:Drafts
notmuch tag +spam -inbox -unread -- folder:Spam folder:Junk
EOF
chmod +x "$MAILDIR/.notmuch/hooks/post-new"
# ── alot ──────────────────────────────────────────────────────────────────────
# The bindings section lives in ~/Dotfiles/alot/config (symlinked by shell-setup.sh).
# Write only the account block, which contains machine-specific paths/identity.
log "Writing account details into ~/Dotfiles/alot/config..."
ALOT_CFG="$HOME/Dotfiles/alot/config"
# Replace the [[main]] account block in-place (sed removes old block, cat appends new one)
python3 - "$ALOT_CFG" "$FULL_NAME" "$EMAIL" "$MAILDIR" << 'PYEOF'
import sys, re
path, name, email, maildir = sys.argv[1:]
block = f"""[accounts]
[[main]]
realname = {name}
address = {email}
sendmail_command = msmtp -a main
sent_box = maildir://{maildir}/Sent
draft_box = maildir://{maildir}/Drafts
"""
with open(path) as f:
text = f.read()
text = re.sub(r'\[accounts\].*?(?=\n\[|\Z)', block, text, flags=re.DOTALL)
with open(path, 'w') as f:
f.write(text)
PYEOF
# ── mailcap (HTML email rendering via w3m) ────────────────────────────────────
log "Writing ~/.mailcap..."
grep -qxF "text/html; w3m -dump -o document_charset=%{charset} '%s'; nametemplate=%s.html; copiousoutput" ~/.mailcap 2>/dev/null \
|| echo "text/html; w3m -dump -o document_charset=%{charset} '%s'; nametemplate=%s.html; copiousoutput" >> ~/.mailcap
# ── systemd timer for periodic sync ───────────────────────────────────────────
log "Installing mbsync systemd user timer (every 5 min)..."
mkdir -p ~/.config/systemd/user
cat > ~/.config/systemd/user/mbsync.service << EOF
[Unit]
Description=Sync mail with mbsync
After=network-online.target
[Service]
Type=oneshot
ExecStart=/usr/bin/mbsync -a
ExecStartPost=/usr/bin/notmuch new
EOF
cat > ~/.config/systemd/user/mbsync.timer << EOF
[Unit]
Description=Run mbsync every 5 minutes
[Timer]
OnBootSec=2min
OnUnitActiveSec=5min
Unit=mbsync.service
[Install]
WantedBy=timers.target
EOF
systemctl --user daemon-reload
systemctl --user enable --now mbsync.timer
# ── initial sync ──────────────────────────────────────────────────────────────
log "Running initial mail sync..."
mbsync -a
notmuch new
log "Mail setup complete. Syncs automatically every 5 min via systemd timer."
log "Open alot with: alot | Check timer: systemctl --user status mbsync.timer"