#!/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. # # Opens qtmaildir on an account, or on a thread's newest unread message. # Shared by the drawer's mail page and mail-notify.sh, so both open mail the # same way. # # mail-open.sh [thread-id] # # A running qtmaildir takes the selectors over its socket and raises, so this # never opens a second window. # # The message is looked up at click time rather than when the row or the # notification was built: by then it may have been read, and an unread-only # lookup that finds nothing falls back to the whole thread instead of a miss. # Newest, because that is the message the row's date and the notification # are about. set -u key="${1:?usage: mail-open.sh [thread-id]}" tid="${2:-}" args=(--account "$key") # notmuch thread ids are hex; anything else never reaches a query. if [[ "$tid" =~ ^[0-9a-f]+$ ]]; then id="$(notmuch search --format=json --output=messages --sort=newest-first --limit=1 \ "thread:$tid and tag:unread and tag:inbox" 2>/dev/null | jq -r '.[0] // empty' 2>/dev/null)" if [[ -n "$id" ]]; then args+=(--message "$id") else args+=(--thread "$tid") fi fi exec "$HOME/bin/qtmaildir" "${args[@]}"