#!/bin/bash
# notify.sh : the one outbound-message helper for your AI agents.
#
# Free from the AI Open House library: https://aiopenhouse.co/guests/#library
# Use it, change it, ship it. Creative Commons Attribution 4.0.
#
# WHY AN ALLOWLIST: an outbound text from this Mac comes from YOUR number and is
# indistinguishable from you typing it. An agent that can text an arbitrary number
# is an agent that can text a client. Keys only. Adding a row below is a deliberate
# act, not a fix.
#
# SETUP
#   1. chmod +x notify.sh
#   2. Put your own number in the allowlist below.
#   3. ./notify.sh text me "hello"   (macOS asks once for Messages permission)
#
# USAGE
#   notify.sh text <key> "message"    send an iMessage
#   notify.sh keys                    list allowed recipients
#   Add --dry to print what would be sent and send nothing.
#
# EXIT CODES: 0 sent (or dry run), 1 bad usage, 2 unknown recipient, 3 send failed.

set -uo pipefail
VERSION="1.0.0"
LOG="${NOTIFY_LOG:-$HOME/.notify-log.txt}"

# Every message opens with this, so anyone reading a text from your number knows
# a machine wrote it. Change the wording, keep the idea.
PREFIX='[agent speaking]'

# ---- recipient allowlist. EDIT THIS. Keys on the left, numbers on the right. ----
recipient_for() {
  case "$1" in
    me)     echo "+15555550123" ;;   # <- your own number goes here
    # spouse) echo "+15555550124" ;;  # uncomment and fill in to add a person
    *)      echo "" ;;
  esac
}
ALL_KEYS="me"

die()   { printf '%s\n' "$*" >&2; exit "${2:-1}"; }
stamp() { date "+%a %Y-%m-%d %-I:%M %p %Z"; }

usage() {
  cat >&2 <<USAGE
notify.sh v$VERSION
  notify.sh text <key> "message"   send an iMessage to an allowlisted recipient
  notify.sh keys                   list allowed recipients
  --dry                            print what would be sent, send nothing
USAGE
  exit 1
}

DRY=0
ARGS=()
for a in "$@"; do
  if [ "$a" = "--dry" ]; then DRY=1; else ARGS+=("$a"); fi
done
set -- "${ARGS[@]:-}"
[ $# -ge 1 ] || usage

case "${1:-}" in
  keys) printf 'allowed: %s\n' "$ALL_KEYS"; exit 0 ;;
  text) : ;;
  *)    usage ;;
esac

KEY="${2:-}"; MSG="${3:-}"
[ -n "$KEY" ] && [ -n "$MSG" ] || usage

TO="$(recipient_for "$KEY")"
[ -n "$TO" ] || die "unknown recipient key '$KEY'. Allowed: $ALL_KEYS" 2

BODY="$PREFIX
$MSG"

if [ "$DRY" = "1" ]; then
  printf 'DRY RUN\nto:   %s (%s)\nbody: %s\n' "$KEY" "$TO" "$BODY"
  exit 0
fi

# AppleScript does the sending, so there is no API and no third-party service.
# Needs Automation permission for Messages (macOS prompts once, on first run).
if osascript <<APPLESCRIPT >/dev/null 2>&1
tell application "Messages"
  set targetService to 1st account whose service type = iMessage
  set targetBuddy to participant "$TO" of targetService
  send "$(printf '%s' "$BODY" | sed 's/\\/\\\\/g; s/"/\\"/g')" to targetBuddy
end tell
APPLESCRIPT
then
  RESULT="sent"
else
  RESULT="failed"
fi

printf '%s\t%s\t%s\t%s\n' "$(stamp)" "$KEY" "$RESULT" "$MSG" >> "$LOG"
[ "$RESULT" = "sent" ] || die "send failed. Open Messages.app once, sign in, and check System Settings > Privacy & Security > Automation." 3
echo "sent to $KEY"
