aboutsummaryrefslogtreecommitdiff
path: root/claude-templates/bin/page-signal
blob: 5a87c67d4148a6c4094ce1b7500aa30f01b24b70 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env bash
#
# page-signal — wrap signal-cli send for paging Craig via Signal.
#
# Defaults to --note-to-self for safety. Outbound (to another contact)
# requires an explicit --to <+number> on every call. No defaults, no batch.
#
# Usage:
#   page-signal "message"                  # note-to-self
#   page-signal --file path "message"      # with attachment
#   page-signal --to +15551234567 "msg"    # outbound (explicit recipient)
#   page-signal --quiet "message"          # suppress success output
#   page-signal --json "message"           # structured output (timestamp + status)
#   echo "msg" | page-signal               # message from stdin (no positional)
#
# Requires signal-cli installed and registered with an account.
#
# Use this when desktop notifications won't reach the user — page-signal
# fires cross-device. For routine completions and periodic status pings,
# stick with `notify`; Signal is for things that need attention while away
# from the desk.

set -euo pipefail

quiet=0
json=0
file=
recipient_args=(--note-to-self)
message=

usage() {
    sed -n '3,21p' "$0" | sed 's/^# \{0,1\}//'
}

while [ "$#" -gt 0 ]; do
    case "$1" in
        -h|--help)
            usage
            exit 0
            ;;
        --quiet)
            quiet=1
            shift
            ;;
        --json)
            json=1
            shift
            ;;
        --file)
            [ "$#" -ge 2 ] || { echo "page-signal: --file needs a path" >&2; exit 2; }
            file="$2"
            [ -f "$file" ] || { echo "page-signal: file not found: $file" >&2; exit 2; }
            shift 2
            ;;
        --to)
            [ "$#" -ge 2 ] || { echo "page-signal: --to needs a +E.164 number" >&2; exit 2; }
            case "$2" in
                +[0-9]*) ;;
                *) echo "page-signal: --to expects +E.164 (e.g. +15551234567)" >&2; exit 2 ;;
            esac
            recipient_args=("$2")
            shift 2
            ;;
        --)
            shift
            break
            ;;
        -*)
            echo "page-signal: unknown flag: $1 (use --help)" >&2
            exit 2
            ;;
        *)
            message="$1"
            shift
            ;;
    esac
done

if ! command -v signal-cli >/dev/null 2>&1; then
    echo "page-signal: signal-cli not found in PATH" >&2
    exit 3
fi

# Message: from positional arg, or from stdin if none given.
if [ -z "$message" ]; then
    if [ -t 0 ]; then
        echo "page-signal: no message (pass as arg or pipe via stdin)" >&2
        exit 2
    fi
    message="$(cat)"
fi

# Build the send command.
cmd=(signal-cli send -m "$message")
if [ -n "$file" ]; then
    cmd+=(-a "$file")
fi
cmd+=("${recipient_args[@]}")

# Send. Capture stderr so we can surface clean failure messages.
timestamp="$(date '+%Y-%m-%dT%H:%M:%S%z')"
if err="$("${cmd[@]}" 2>&1 >/dev/null)"; then
    status=ok
else
    status=fail
fi

if [ "$json" -eq 1 ]; then
    printf '{"status":"%s","timestamp":"%s","recipient":"%s"' \
        "$status" "$timestamp" "${recipient_args[0]}"
    if [ -n "$file" ]; then
        printf ',"attachment":"%s"' "$file"
    fi
    if [ "$status" = "fail" ]; then
        printf ',"error":"%s"' "$(echo "$err" | head -1 | sed 's/"/\\"/g')"
    fi
    printf '}\n'
elif [ "$quiet" -eq 0 ]; then
    if [ "$status" = "ok" ]; then
        echo "page-signal: sent to ${recipient_args[*]} at $timestamp"
    else
        echo "page-signal: FAIL — $err" >&2
    fi
fi

[ "$status" = "ok" ]