aboutsummaryrefslogtreecommitdiff
path: root/scripts/signal-receive.sh
blob: 8c3ef01a550cb334d8df6dc16bdefc1c30a8953c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env bash
# signal-receive.sh — drain the Signal pager account's inbound queue.
#
# The pager identity (+15045173983) lives on velox (primary) and any linked
# device (ratio). The Signal protocol expects a registered account to receive
# regularly; when it goes quiet, signal-cli prints a staleness warning
# ("Messages have been last received N days ago") and the account drifts toward
# an unhealthy state. This script pulls anything queued and exits, keeping the
# account warm. Run by the signal-receive systemd user timer (scripts/systemd/)
# on every machine holding the account — the roam-sync-shaped fix for the
# receive-staleness caveat on the pager channel.
#
# Draining the queue is also what surfaces Craig's replies: a reply he types in
# Signal arrives as a data message here, so a warm account is a prerequisite for
# the read-replies half of the pager (see docs/design/…-signal-pager-runbook.org).
#
# The units stow via the shared dotfiles `common` package, so the timer may land
# on a machine that does not hold the account. That is fine: the script no-ops
# cleanly (exit 0) when the account is not registered locally, rather than
# erroring every cadence.
#
# Usage: signal-receive.sh [account] [timeout-seconds]
#   account         defaults to the pager identity below
#   timeout-seconds seconds to wait for new messages (default 10)
#
# Exit: 0 on a clean receive (including "nothing queued" and "account not on this
# machine"), non-zero only if signal-cli itself errors, so a real failure is
# visible in `systemctl --user status signal-receive`.

set -euo pipefail

account="${1:-+15045173983}"
timeout="${2:-10}"

if ! command -v signal-cli >/dev/null 2>&1; then
    echo "signal-receive: signal-cli not on PATH — nothing to do" >&2
    exit 0
fi

# No-op cleanly where the account isn't registered (a machine that stows the
# common units but isn't a pager device). Only a genuine receive error should
# surface as a failure.
if ! signal-cli listAccounts 2>/dev/null | grep -q "$account"; then
    echo "signal-receive: $account not registered on this machine — nothing to do" >&2
    exit 0
fi

# `receive` prints envelopes to stdout and returns 0 once the queue drains or
# the timeout elapses. --send-read-receipts tells Craig's phone his replies were
# read by the pager, matching normal Signal behavior.
exec signal-cli -a "$account" receive --timeout "$timeout" --send-read-receipts