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

150 lines
5.0 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)..."
sudo pacman -S --noconfirm --needed isync msmtp notmuch alot
# ── 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 ──────────────────────────────────────────────────────────────────────
log "Writing ~/.config/alot/config..."
mkdir -p ~/.config/alot
cat > ~/.config/alot/config << EOF
[accounts]
[[main]]
realname = $FULL_NAME
address = $EMAIL
sendmail_command = msmtp -a main
sent_box = maildir://$MAILDIR/Sent
draft_box = maildir://$MAILDIR/Drafts
[bindings]
[[search]]
esc = exit
[[thread]]
esc = exit
[[taglist]]
esc = exit
[[bufferlist]]
esc = exit
EOF
# ── 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"