nntp.dcgi - gophercgis - Collection of gopher CGI/DCGI for geomyidae
 (HTM) hg clone https://bitbucket.org/iamleot/gophercgis
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       nntp.dcgi
       ---
            1 #!/bin/sh
            2 
            3 . ../common/config.sh
            4 
            5 CGI="${CGI_BASEDIR}/nntp/nntp.dcgi"
            6 CGI_BODY="${CGI_BASEDIR}/nntp/nntp_body.cgi"
            7 NNTP_MESSAGES=100               # default number to over (+1)
            8 NNTP_SERVER="news.gmane.org"
            9 NNTP_PORT="119"
           10 NNTP_CMD="nc ${NNTP_SERVER} ${NNTP_PORT}"
           11 
           12 
           13 case "$2" in
           14 */[0-9]*/[0-9]*)
           15         # OVER (scan) of articles num1...num2
           16         group="${2%%/*}"
           17         nums="${2#*/}" ; num1="${nums%%/*}" ; num2="${nums##*/}"
           18         ;;
           19 *)
           20         # OVER (scan) of last NNTP_MESSAGES
           21         group="$2"
           22         ;;
           23 esac
           24 
           25 
           26 #
           27 # GROUP and print the information about the selected group, i.e.:
           28 # `211 <numbers> <first> <last> <group_name>'
           29 #
           30 nntp_group()
           31 {
           32         g="$1"
           33 
           34         { echo "group ${g}"; echo "quit"; } |
           35                 ${NNTP_CMD} |
           36                 sed -ne '/^211/p'
           37 }
           38 
           39 
           40 #
           41 # OVER and gph-ize the output similar to mailx(1).
           42 #
           43 nntp_over()
           44 {
           45         g="$1"
           46         range="$2"
           47 
           48         { echo "group ${g}"; echo "over ${range}"; echo "quit"; } |
           49                 ${NNTP_CMD} |
           50                 awk '
           51                         BEGIN {
           52                                 FS = "\t"
           53                                 over = 1
           54                         }
           55 
           56                         /^\.\r$/ {
           57                                 over = 0
           58                                 next
           59                         }
           60 
           61                         NR > 3 && over {
           62                                 id = $1
           63                                 subject = $2
           64                                 from = $3
           65                                 date = $4
           66                                 
           67                                 gsub("\\|", "\\|", subject)
           68                                 gsub("\\|", "\\|", from)
           69                                 gsub("\\|", "\\|", date)
           70 
           71                                 # Ignore seconds and TZ
           72                                 sub(/:[0-9][0-9] [+-][0-9]+$/, "", date)
           73 
           74                                 printf("[0| %d  %-17s  %-22s  %s|%s/%d|server|port]\n",
           75                                     id, substr(from, 1, 17), substr(date, 1, 22), subject,
           76                                     "'"${CGI_BODY}"?"${g}"'", id)
           77                         }
           78                 ' | tail -r
           79                 
           80 }
           81 
           82 
           83 #
           84 # <group>: show ID, From:, Date: and Subject: of the most recent NNTP_MESSAGES
           85 # <group>/<num1>/<num2>: show ID, From:, Date: and Subject: from <num1> to
           86 #                        <num2>
           87 #
           88 if [ "${group}" ]; then
           89         set -- `nntp_group "${group}"`
           90         n="$2"; first="$3"; last="$4"
           91 
           92         : ${num1:=$((last - NNTP_MESSAGES))}
           93         : ${num2:=${last}}
           94 
           95         [ "${num1}" -le 0 ] && num1=1
           96 
           97         echo "t"
           98         echo "t${group} (${num1}-${num2})"
           99         echo "t"
          100         nntp_over "${group}" "${num1}-${num2}"
          101         echo "t"
          102 
          103         if [ "${num2}" -gt "${NNTP_MESSAGES}" ]; then
          104                 prev_num1=$((num1 - NNTP_MESSAGES))
          105                 [ "${prev_num1}" -le 0 ] && prev_num1=1
          106                 prev_num2=$((num2 - NNTP_MESSAGES))
          107 
          108                 echo "[1|<< Older articles|${CGI}?${group}/${prev_num1}/${prev_num2}|server|port]"
          109         fi
          110 
          111         if [ "${num2}" -lt "${last}" ]; then 
          112                 new_num1=$((num1 + NNTP_MESSAGES))
          113                 new_num2=$((num2 + NNTP_MESSAGES))
          114                 [ "${new_num2}" -gt "${last}" ] && new_num2="${last}"
          115 
          116                 echo "[1|>> Newer articles|${CGI}?${group}/${new_num1}/${new_num2}|server|port]"
          117         fi
          118 
          119         exit 0
          120 fi