pubsub_setup: rework options, allow to specify options or use a config by feedname - pubsubhubbubblub - pubsubhubbub client implementation
HTML git clone git://git.codemadness.org/pubsubhubbubblub
DIR Log
DIR Files
DIR Refs
DIR README
DIR LICENSE
---
DIR commit 7d7c140fbc467e67745eb166c29f276b9d634a0a
DIR parent 584f6877875619a77ee4435d3e4621e560ae48eb
HTML Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date: Sat, 28 May 2022 14:02:38 +0200
pubsub_setup: rework options, allow to specify options or use a config by feedname
Diffstat:
M README | 11 +++++++----
M pubsub_setup | 102 ++++++++++++++++---------------
2 files changed, 60 insertions(+), 53 deletions(-)
---
DIR diff --git a/README b/README
@@ -123,15 +123,18 @@ Get the hub and feed URL:
Setup the feed structure for the CGI program and also subscribe to the hub using the -s option.
cd /var/www/pubsub-data
- pubsub_setup -s -b 'https://codemadness.org/pubsub/' \
- 'slashdot' 'http://pubsubhubbub.appspot.com/' 'http://rss.slashdot.org/Slashdot/slashdot'
+ pubsub_setup \
+ -b 'https://codemadness.org/pubsub/' \
+ -f 'slashdot' \
+ -h 'http://pubsubhubbub.appspot.com/' \
+ -t 'http://rss.slashdot.org/Slashdot/slashdot'
+ pubsub_setup -s -f 'slashdot'
Unsubscribe from a hub:
cd /var/www/pubsub-data
- pubsub_setup -u -b 'https://codemadness.org/pubsub/' \
- 'slashdot' 'http://pubsubhubbub.appspot.com/' 'http://rss.slashdot.org/Slashdot/slashdot'
+ pubsub_setup -u -f 'slashdot'
Monitor script example
DIR diff --git a/pubsub_setup b/pubsub_setup
@@ -1,30 +1,51 @@
#!/bin/sh
-while getopts "b:su" f; do
+usage() {
+ printf "usage: %s [-s] [-u] <-b base_callback> <-f feedname> <-h hub> <-t topic>\n" "$0" >&2
+ printf "or\n" >&2
+ printf "usage: %s [-s] [-u] <-f feedname>\n" "$0" >&2
+ exit 1
+}
+
+base=""
+feedname=""
+hub=""
+topic=""
+dosubscribe=0
+dounsubscribe=0
+while getopts "b:f:h:t:su" f; do
case "${f}" in
b) base="${OPTARG}";;
+ f) feedname="${OPTARG}";;
+ h) hub="${OPTARG}";;
+ t) topic="${OPTARG}";;
s) dosubscribe=1;;
u) dounsubscribe=1;;
esac
done
shift $(expr ${OPTIND} - 1)
-feedname="$1"
-hub="$2"
-topic="$3"
-if test "$1" = "" -o "$2" = "" -o "$3" = "" -o "$base" = ""; then
- printf "usage: %s <-b base_callback> [-s] [-u] <feedname> <hub> <topic>\n" "$0" >&2
- exit 1
+if test "${feedname}" != "" && test "${base}" = "" -o "${hub}" = "" -o "${topic}" = ""; then
+ test -d "config/${feedname}" || usage # if specifying only -f then it must exist.
+ # read settings.
+ callback="$(cat "config/${feedname}/callback" 2>/dev/null)"
+ hub="$(cat "config/${feedname}/hub" 2>/dev/null)"
+ topic="$(cat "config/${feedname}/topic" 2>/dev/null)"
+
+ test "${callback}" = "" -o "${hub}" = "" -o "${topic}" = "" && usage
+elif test "${base}" = "" -o "${feedname}" = "" -o "${hub}" = "" -o "${topic}" = ""; then
+ usage
fi
+
# make sure it has a / at the end.
base="${base%%/}/"
-# Linux
+# sha256sum, typically on Linux.
shacmd="$(command -v sha256sum)"
-# BSD
+# sha256, typically on (OpenBSD)BSD.
test "${shacmd}" = "" && shacmd=$(command -v sha256)
if test "${shacmd}" = ""; then
- echo "no sha256 or sha256sum tool found" >&2
+ log_error "no sha256 or sha256sum tool found" >&2
exit 1
fi
@@ -45,30 +66,21 @@ log_error() {
# subscribe(feedname, hub, topic, callback, mode, secret)
subscribe() {
- feedname="$1"
- hub="$2"
- topic="$3"
- callback="$4"
- mode="${5:-subscribe}"
- secret="$6"
- verify="async" # or "sync"
- lease_seconds=""
-
# if curl -s -f -H 'User-Agent:' -m 15 \
# DEBUG
if curl -v -f -H 'User-Agent:' -m 15 \
-L --max-redirs 3 \
- --data-raw "hub.callback=${callback}" \
- --data-raw "hub.lease_seconds=${lease_seconds}" \
- --data-raw "hub.mode=${mode}" \
- --data-raw "hub.secret=${secret}" \
- --data-raw "hub.topic=${topic}" \
- --data-raw "hub.verify=${verify}" \
- "${hub}/subscribe"; then
- log "${mode} OK"
+ --data-raw "hub.callback=$4" \
+ --data-raw "hub.lease_seconds=" \
+ --data-raw "hub.mode=$5" \
+ --data-raw "hub.secret=$6" \
+ --data-raw "hub.topic=$3" \
+ --data-raw "hub.verify=sync" \
+ "$2/subscribe"; then
+ log "$5 OK"
return 0
else
- log_error "${mode} FAIL"
+ log_error "$5 FAIL"
return 1
fi
}
@@ -83,40 +95,36 @@ mkdir -p "tmp/${feedname}"
# create log if it does not exist.
test -f "log" || touch "log"
-if test "${dosubscribe}" = "1"; then
- f="config/${feedname}/hub"
- if test -f "${f}"; then
- log_error "already registered? file exists: ${f}, skipping subscribing"
- exit 1
- fi
-fi
-
# generate random token if it does not exist.
f="config/${feedname}/token"
-if ! test -f "${f}" -a "${isnew}" = "1"; then
+if ! test -f "${f}" && test "${isnew}" = "1"; then
token="$(dd if=/dev/urandom count=10 bs=4096 2>/dev/null | sha)"
echo "${token}" > "${f}"
fi
# generate random secret if it does not exist.
f="config/${feedname}/secret"
-if ! test -f "${f}" -a "${isnew}" = "1"; then
+if ! test -f "${f}" && test "${isnew}" = "1"; then
secret="$(dd if=/dev/urandom count=10 bs=4096 2>/dev/null | sha)"
echo "${secret}" > "${f}"
fi
# read config.
-f="config/${feedname}/token"
-token=$(cat "${f}" 2>/dev/null)
-callback="$1/${token}"
f="config/${feedname}/secret"
secret=$(cat "${f}" 2>/dev/null)
+if test "${callback}" = ""; then
+ f="config/${feedname}/token"
+ token=$(cat "${f}" 2>/dev/null)
+ callback="${base}${feedname}/${token}"
+ printf '%s\n' "${callback}" > "config/${feedname}/callback"
+fi
-callback="${base}${feedname}/${token}"
+printf '%s\n' "${hub}" > "config/${feedname}/hub"
+printf '%s\n' "${topic}" > "config/${feedname}/topic"
status=0
if test "${dosubscribe}" = "1"; then
- f="config/${feedname}/hub"
+ f="config/${feedname}/ok"
if test -f "${f}"; then
log_error "already registered? file exists: ${f}, skipping subscribing"
exit 1
@@ -124,9 +132,7 @@ if test "${dosubscribe}" = "1"; then
# register at hub. save state when successfully registered.
if subscribe "${feedname}" "${hub}" "${topic}" "${callback}" "subscribe" "${secret}"; then
- printf '%s\n' "${callback}" > "config/${feedname}/callback"
- printf '%s\n' "${hub}" > "config/${feedname}/hub"
- printf '%s\n' "${topic}" > "config/${feedname}/topic"
+ touch "config/${feedname}/ok"
else
status=1
fi
@@ -135,9 +141,7 @@ fi
if test "${dounsubscribe}" = "1"; then
# unregister at hub. remove state when successfully registered.
if subscribe "${feedname}" "${hub}" "${topic}" "${callback}" "unsubscribe" "${secret}"; then
- rm -f "config/${feedname}/callback"
- rm -f "config/${feedname}/hub"
- rm -f "config/${feedname}/topic"
+ rm -f "config/${feedname}/ok"
else
status=1
fi