cosmic/bin/rss

56 lines
2.1 KiB
Bash
Executable File

#!/bin/sh
run_user=$(id -u)
if [ "$run_user" -eq 0 ]; then
gopher_rss="/var/gopher/rss.xml"
######################################################################
########################## GOPHER VERSION ###########################
######################################################################
# Add header info to xml output
{
printf '<?xml version="1.0"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel>'
printf '<atom:link href="gopher://cosmic.voyage/0/rss.xml" rel="self" type="application/rss+xml" />'
printf "\\n<title>Cosmic Voyage</title>\\n"
printf "<link>gopher://cosmic.voyage</link>\\n"
printf "<description>Messages from the human stellar diaspora</description>\\n"
} > "${gopher_rss}"
xml_escape_script='s/&/\&amp;/g; s/</\&lt;/g; s/>/\&gt;/g; s/"/\&quot;/g; s/'"'"'/\&#39;/g'
# Loop through listings gophermap
loop=0
while read -r line; do
loop=$((loop+1))
if [ "$loop" -lt 20 ]; then
log=$(printf "%s" "$line" | awk -F'\t' '{print $2}')
title=$(printf "%s" "$line" | awk -F'\t' '{print $1}' | sed -e 's|^.||' -e "$xml_escape_script")
owner=$(stat -c %U "/var/gopher${log}")
# print item entry for each log
{
printf "<item>\\n"
printf " <title>%s</title>\\n" "$title"
printf " <author>%s@cosmic.voyage (%s)</author>\\n" "$owner" "$owner"
printf " <link>gopher://cosmic.voyage/0%s</link>\\n" "$(printf "%s" "$log" | sed 's|\ |%20|g')"
printf " <guid>gopher://cosmic.voyage/0%s</guid>\\n" "$(printf "%s" "$log" | sed 's|\ |%20|g')"
printf " <pubDate>%s GMT</pubDate>\\n" "$(date -d "$(stat -c %y "/var/gopher${log}")" +'%a, %d %b %Y %H:%M:%S')"
printf " <description><![CDATA[<pre>\\n"
sed "$xml_escape_script" "/var/gopher${log}"
printf "</pre>]]></description>\\n"
printf "</item>\\n"
} >> "${gopher_rss}"
fi
done < "/var/gopher/listing.gophermap"
# close up the footer
{
printf "</channel>\\n"
printf "</rss>\\n"
} >> "${gopher_rss}"
else
exec sudo "$0" "$@"
fi