51 lines
1.6 KiB
Bash
Executable File
51 lines
1.6 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# ping-charles.sh — SMS Charles via T-Mobile email-to-SMS gateway.
|
|
# Usage: ping-charles.sh "short message" [severity]
|
|
# Relay: curl SMTP (Cloudron mail submission). Config: /home/reachableceo/.coordinate/secrets/ping-charles.env (0600)
|
|
# PING_SMTP_HOST=mail.knownelement.com:587
|
|
# PING_SMTP_USER=...
|
|
# PING_SMTP_PASS=...
|
|
# PING_FROM=...
|
|
# Until wired, messages queue in ~/.coordinate/outbox/ping-queue.txt (flushed on first successful send).
|
|
|
|
set -euo pipefail
|
|
DEST="8182807059@tmomail.net"
|
|
CONF="$HOME/.coordinate/secrets/ping-charles.env"
|
|
QUEUE="$HOME/.coordinate/outbox/ping-queue.txt"
|
|
MSG="${1:?usage: ping-charles.sh \"message\" [severity]}"
|
|
SEV="${2:-info}"
|
|
STAMP="$(date '+%m-%d %H:%M')"
|
|
LINE="[$SEV $STAMP MOPAC] $MSG"
|
|
|
|
if [ ! -f "$CONF" ]; then
|
|
mkdir -p "$(dirname "$QUEUE")"
|
|
echo "$LINE" >> "$QUEUE"
|
|
echo "ping queued (no relay config): $LINE" >&2
|
|
exit 0
|
|
fi
|
|
set -a; . "$CONF"; set +a
|
|
|
|
send_one() {
|
|
printf 'From: %s\nTo: %s\nSubject: MOPAC %s\nContent-Type: text/plain; charset=UTF-8\n\n%s\n' \
|
|
"$PING_FROM" "$DEST" "$SEV" "$1" \
|
|
| curl -sS --max-time 30 --url "smtp://$PING_SMTP_HOST" \
|
|
--mail-from "$PING_FROM" --mail-rcpt "$DEST" \
|
|
-u "$PING_SMTP_USER:$PING_SMTP_PASS" \
|
|
-T - && return 0 || return 1
|
|
}
|
|
|
|
if [ -f "$QUEUE" ] && [ -s "$QUEUE" ]; then
|
|
while IFS= read -r queued; do
|
|
send_one "$queued" && sed -i '1d' "$QUEUE" || break
|
|
done < /dev/null
|
|
fi
|
|
|
|
if send_one "$LINE"; then
|
|
echo "sms sent: $LINE" >&2
|
|
exit 0
|
|
else
|
|
mkdir -p "$(dirname "$QUEUE")"; echo "$LINE" >> "$QUEUE"
|
|
echo "sms send FAILED, queued: $LINE" >&2
|
|
exit 1
|
|
fi
|