repo: actpub
action: blob
revision: 
path_from: ap-announce
revision_from: refs/heads/master:
path_to: 
revision_to: 
git.thebackupbox.net
actpub
git clone git://git.thebackupbox.net/actpub

blob of:

actpub

/ ap-announce

blob_plain of this file

refs/heads/master:/ap-announce

 #!/usr/bin/env bash

 if [ ! "$1" ];then
   printf "usage: ap-announce \n" >&2
   exit 1
 fi

 type="Announce"
 object="$1"

 ### common stuff below
 HTTPSIG_KEYID="https://thebackupbox.net/~epoch/#main-key"
 HTTPSIG_HEADERS="(request-target) host date digest content-type"
 HTTPSIG_ALGO="rsa-sha256"
 CONTENT_TYPE="application/activity+json"
 timestamp="$(date +%s)"
 http_date="$(rfc7231date "${timestamp}")"
 ap_date="$(ap-date "--date=@${timestamp}")"
 context="https://www.w3.org/ns/activitystreams"
 uuid="$(uuidgen -t)"
 id="https://thebackupbox.net/~epoch/outbox/${uuid}"
 acct="$(ap-whoami)"
 actor="$(ap-getactoruri "${acct}")"
 to="https://www.w3.org/ns/activitystreams#Public"

 POST_DATA='{
   "@context":	"'"${context}"'",
   "id":		"'"${id}"'",
   "type":	"'"${type}"'",
   "actor":	"'"${actor}"'",
   "published":	"'"${ap_date}"'",
   "object":	"'"${object}"'",
   "to": [
     "'"${to}"'"
   ]
 }'

 HTTP_DIGEST="SHA-256=$(printf "%s" "$POST_DATA" | openssl sha256 | cut '-d ' -f2- | xxd -r -p | base64)"
 printf "ap-announce: digest: %s\n" "${HTTP_DIGEST}"

 printf "%s" "$POST_DATA" | jq . || exit $(printf "BAD POST_DATA:\n\n%s\n\n" "$POST_DATA" >&2 ; echo 1)

 printf 'LAST CHANCE BEFORE ANNOYING TO CHANGE THINGS HAPPEN\n'
 printf '/!\\ DOES THIS LOOK ALL GOOD? /!\\\n'
 read confirm
 [ "$confirm" == "yes" ] || exit "$(printf "KK. NOT POSTING\n" >&2 ; echo 1)"

 #echo "DISABLING THIS FOR NOW"
 #exit 0

 ### create the outbox entry.

 mkdir -p ~/.cache/ap/outbox
 cache_file=~/.cache/ap/outbox/"${uuid}"
 printf "%s" "$POST_DATA" > "${cache_file}"
 printf "ap-announce: local cache file of create activity: %s\n" "${cache_file}" >&2
 ap-upload-to-outbox "${cache_file}"

 ### send a notice about it to everyone

 for target_inbox in $(ap-getfollowers "${acct}" | xargs -n1 ap-getendpoint | sort | uniq);do
   host="$(uricut -d <<< "${target_inbox}")"
   target_inbox_path="/$(uricut -p <<< "${target_inbox}")"

   SIGNED_STRING="(request-target): post ${target_inbox_path}
 host: $host
 date: $http_date
 digest: $HTTP_DIGEST
 content-type: $CONTENT_TYPE"

   HTTPSIG_SIGNATURE="$(openssl dgst -sha256 -sign ~/.config/ap/private.pem <(printf "%s" "${SIGNED_STRING}") | base64 | tr -d '\n')"
   HTTP_SIGNATURE='keyId="'"${HTTPSIG_KEYID}"'",algorithm="'"${HTTPSIG_ALGO}"'",headers="'"${HTTPSIG_HEADERS}"'",signature="'"${HTTPSIG_SIGNATURE}"'"'

   printf "doing a curl to inbox: %s\n\n" "$target_inbox"
   curl -i \
     --data-raw "${POST_DATA}" \
     -H "Accept: application/activity+json" \
     -H "Signature: $HTTP_SIGNATURE" \
     -H "Date: $http_date" \
     -H "Digest: $HTTP_DIGEST" \
     -H "Content-Type: $CONTENT_TYPE" \
     -g "$target_inbox"
   echo '[DONE]' >&2
 done
 echo '[DONE DONE]' >&2