URI:
       pubsub_setup - pubsubhubbubblub - pubsubhubbub client implementation
  HTML git clone git://git.codemadness.org/pubsubhubbubblub
   DIR Log
   DIR Files
   DIR Refs
   DIR README
   DIR LICENSE
       ---
       pubsub_setup (3675B)
       ---
            1 #!/bin/sh
            2 
            3 usage() {
            4         printf "usage: %s [-s] [-u] <-b base_callback> <-f feedname> <-h hub> <-t topic>\n" "$0" >&2
            5         printf "or\n" >&2
            6         printf "usage: %s [-s] [-u] <-f feedname>\n" "$0" >&2
            7         exit 1
            8 }
            9 
           10 base=""
           11 feedname=""
           12 hub=""
           13 topic=""
           14 dosubscribe=0
           15 dounsubscribe=0
           16 while getopts "b:f:h:t:su" f; do
           17         case "${f}" in
           18         b) base="${OPTARG}";;
           19         f) feedname="${OPTARG}";;
           20         h) hub="${OPTARG}";;
           21         t) topic="${OPTARG}";;
           22         s) dosubscribe=1;;
           23         u) dounsubscribe=1;;
           24         esac
           25 done
           26 shift $(expr ${OPTIND} - 1)
           27 
           28 if test "${feedname}" != "" && test "${base}" = "" -o "${hub}" = "" -o "${topic}" = ""; then
           29         test -d "config/${feedname}" || usage # if specifying only -f then it must exist.
           30         # read settings.
           31         callback="$(cat "config/${feedname}/callback" 2>/dev/null)"
           32         hub="$(cat "config/${feedname}/hub" 2>/dev/null)"
           33         topic="$(cat "config/${feedname}/topic" 2>/dev/null)"
           34 
           35         test "${callback}" = "" -o "${hub}" = "" -o "${topic}" = "" && usage
           36 elif test "${base}" = "" -o "${feedname}" = "" -o "${hub}" = "" -o "${topic}" = ""; then
           37         usage
           38 fi
           39 
           40 # make sure it has a / at the end.
           41 base="${base%%/}/"
           42 
           43 # sha256sum, typically on Linux.
           44 shacmd="$(command -v sha256sum)"
           45 # sha256, typically on (OpenBSD)BSD.
           46 test "${shacmd}" = "" && shacmd=$(command -v sha256)
           47 if test "${shacmd}" = ""; then
           48         log_error "no sha256 or sha256sum tool found" >&2
           49         exit 1
           50 fi
           51 
           52 # sha()
           53 sha() {
           54         ${shacmd} | cut -f 1 -d ' '
           55 }
           56 
           57 # log(s)
           58 log() {
           59         printf '%s\n' "$1"
           60 }
           61 
           62 # log_error(s)
           63 log_error() {
           64         printf '%s\n' "$1" >&2
           65 }
           66 
           67 # subscribe(feedname, hub, topic, callback, mode, secret)
           68 subscribe() {
           69 #        if curl -s -f -H 'User-Agent:' -m 15 \
           70         # DEBUG
           71         if curl -v -f -H 'User-Agent:' -m 15 \
           72                 -L --max-redirs 3 \
           73                 --data-raw "hub.callback=$4" \
           74                 --data-raw "hub.lease_seconds=" \
           75                 --data-raw "hub.mode=$5" \
           76                 --data-raw "hub.secret=$6" \
           77                 --data-raw "hub.topic=$3" \
           78                 --data-raw "hub.verify=sync" \
           79                 "$2/subscribe"; then
           80                 log "$5 OK"
           81                 return 0
           82         else
           83                 log_error "$5 FAIL"
           84                 return 1
           85         fi
           86 }
           87 
           88 isnew=1
           89 test -d "config/${feedname}" && isnew=0
           90 
           91 mkdir -p "config/${feedname}"
           92 mkdir -p "feeds/${feedname}"
           93 mkdir -p "tmp/${feedname}"
           94 
           95 # create log if it does not exist.
           96 test -f "log" || touch "log"
           97 
           98 # generate random token if it does not exist.
           99 f="config/${feedname}/token"
          100 if ! test -f "${f}" && test "${isnew}" = "1"; then
          101         token="$(dd if=/dev/urandom count=10 bs=4096 2>/dev/null | sha)"
          102         echo "${token}" > "${f}"
          103 fi
          104 
          105 # generate random secret if it does not exist.
          106 f="config/${feedname}/secret"
          107 if ! test -f "${f}" && test "${isnew}" = "1"; then
          108         secret="$(dd if=/dev/urandom count=10 bs=4096 2>/dev/null | sha)"
          109         echo "${secret}" > "${f}"
          110 fi
          111 
          112 # read config.
          113 f="config/${feedname}/secret"
          114 secret=$(cat "${f}" 2>/dev/null)
          115 if test "${callback}" = ""; then
          116         f="config/${feedname}/token"
          117         token=$(cat "${f}" 2>/dev/null)
          118         callback="${base}${feedname}/${token}"
          119         printf '%s\n' "${callback}" > "config/${feedname}/callback"
          120 fi
          121 
          122 if test "$isnew" = "1"; then
          123         printf '%s\n' "${hub}" > "config/${feedname}/hub"
          124         printf '%s\n' "${topic}" > "config/${feedname}/topic"
          125 fi
          126 
          127 status=0
          128 if test "${dosubscribe}" = "1"; then
          129         f="config/${feedname}/ok"
          130         if test -f "${f}"; then
          131                 log_error "already registered? file exists: ${f}, skipping subscribing"
          132                 exit 1
          133         fi
          134 
          135         # register at hub. save state when successfully registered.
          136         if subscribe "${feedname}" "${hub}" "${topic}" "${callback}" "subscribe" "${secret}"; then
          137                 touch "config/${feedname}/ok"
          138         else
          139                 status=1
          140         fi
          141 fi
          142 
          143 if test "${dounsubscribe}" = "1"; then
          144         # unregister at hub. remove state when successfully registered.
          145         if subscribe "${feedname}" "${hub}" "${topic}" "${callback}" "unsubscribe" "${secret}"; then
          146                 rm -f "config/${feedname}/ok"
          147         else
          148                 status=1
          149         fi
          150 fi
          151 
          152 exit $status