#!/bin/bash # # Copyright (C) 2026 Danilo M. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2 as # published by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # Notifies when mail arrives: one notification per account per batch, naming # the newest senders and subjects. # # Watches the notmuch Xapian directory and, on each commit, asks notmuch what # changed since the revision it last saw. A commit is NOT the same as new # mail, since reading and tagging also commit, which is why the revision # counter does the work rather than a count delta. # # This is its own process rather than part of a bar: a bar restart would # otherwise stop notifications with nothing reporting it. (It began next to # waybar-mail.sh, which watched the same arrival edge and went with waybar.) # # Usage: # mail-notify.sh watch forever (what autostart runs) # mail-notify.sh --once process one tick and exit (for manual verification, one tick) set -u STATE="${MAIL_NOTIFY_STATE:-$HOME/.local/state/mail-notify.lastmod}" CONFIG="${MAIL_NOTIFY_CONFIG:-$HOME/.config/qtmaildir/qtmaildir.conf}" SCOPE='tag:unread and tag:inbox' OPEN="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")/mail-open.sh" # How many threads a notification body lists before eliding into "+N more". # Three matches the drawer's own --limit=3. ROWS=3 # dunst here resolves a themed icon NAME only through its icon_path, which # holds no mail icon, so a name renders nothing (dunst stores an empty # icon_path for it). Pass an absolute path, as this machine's other # notifiers do. This is the icon the user picked; ${XDG_DATA_HOME} keeps a # home path out of the committed file. MAIL_ICON="${XDG_DATA_HOME:-$HOME/.local/share}/icons/MB-Blueberry-Suru-GLOW/actions/24/mail-unread-multiple.svg" # Accounts in file order, one "keylabel" line each, read from stdin. # # Two things here are load-bearing, and both have already broken this # component once: # # The key runs to the closing bracket, NOT to the first dot. Real keys # contain dots, so splitting on the first one yields a notmuch tag matching # nothing and an account that silently never notifies. # # And this walks lines rather than matching a section body as "everything up # to the next [". Accounts have folders named like [Gmail]/Bozze, which ends # the body before its label and makes the account display its raw key. parse_accounts() { local line key label key="" label="" while IFS= read -r line || [[ -n "$line" ]]; do if [[ "$line" =~ ^\[account\.([^]]+)\] ]]; then [[ -n "$key" ]] && printf '%s\t%s\n' "$key" "${label:-$key}" key="${BASH_REMATCH[1]}" label="" continue fi # Any other section ends the current account. if [[ "$line" =~ ^\[ ]]; then [[ -n "$key" ]] && printf '%s\t%s\n' "$key" "${label:-$key}" key="" label="" continue fi [[ -n "$key" ]] || continue if [[ "$line" =~ ^[[:space:]]*label[[:space:]]*=[[:space:]]*(.*)$ ]]; then label="${BASH_REMATCH[1]}" # Trailing whitespace only; a label may contain spaces. label="${label%"${label##*[![:space:]]}"}" fi done [[ -n "$key" ]] && printf '%s\t%s\n' "$key" "${label:-$key}" return 0 } # Renders notmuch search JSON into notification body text. # $1 the JSON array from `notmuch search --format=json` # $2 the true total for this batch, which may exceed the rows present # # dunst has body-markup in its capabilities, so a subject containing < or & # would be parsed as markup and could vanish from the notification. Subjects # are attacker-controlled text arriving from the internet, so the three XML # characters are escaped here. This is the one place in this script where # untrusted text reaches a renderer. # # Malformed JSON prints nothing and succeeds. A notification with no body is # still worth sending: the summary already carries the account and the count. build_body() { local json="$1" total="$2" shown rowtext body rowtext="$(printf '%s' "$json" | jq -r ' .[] | "• " + ((.authors // "(unknown)") + " — " + (.subject // "(no subject)")) | gsub("[\r\n]+"; " ") | gsub("&"; "&") | gsub("<"; "<") | gsub(">"; ">") ' 2>/dev/null)" || return 0 [[ -n "$rowtext" ]] || return 0 shown="$(printf '%s\n' "$rowtext" | wc -l)" body="$(printf '%s\n' "$rowtext" | awk 'NR>1{print ""} 1')" printf '%s' "$body" if [[ "$total" -gt "$shown" ]]; then printf '\n+%d more' "$((total - shown))" fi printf '\n' } # One action per listed thread, "=