summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorp4bl0 <r _at_ uzy .dot. me>2016-02-16 23:24:14 (CET)
committerp4bl0 <r _at_ uzy .dot. me>2016-02-16 23:24:14 (CET)
commit6bd329d7284140d043883294a5e11562d4a4f862 (patch)
tree19978f78a140777a887019fd4c5e7f7a63185fd2
parent439e77e832e16a82ad74ed0b342b42e0f796292c (diff)
downloadmyutils-6bd329d7284140d043883294a5e11562d4a4f862.zip
myutils-6bd329d7284140d043883294a5e11562d4a4f862.tar.gz
better interface
-rwxr-xr-xterss160
1 files changed, 119 insertions, 41 deletions
diff --git a/terss b/terss
index 3ac6bf3..ddf807e 100755
--- a/terss
+++ b/terss
@@ -1,5 +1,7 @@
#!/bin/bash
+set -o errexit
+
TERSSDIR=~/.terss
_terss_tput=$(which tput)
@@ -25,49 +27,67 @@ _terss_echo(){
echo $n "$style$color$*$_terss_clear"
}
+_terss_log(){
+ _terss_echo -n white "[terss] "
+ _terss_echo white_b "$*"
+}
+
+function terss_firstrun(){
+ echo 'This is the first time you run terss, it needs to be configured.'
+ echo 'Please run "terss init"'
+ return 1
+}
+
+function terss_help(){
+ cat <<EOF
+terss is a microblogging software that includes a very simple RSS client.
+
+Usage:
+ terss init: (re)configure terss.
+ terss post MSG: post new message MSG.
+ terss update: update your local feed from your server.
+ terss follow USER URL: subscribe to USER who's feed is at URL.
+ terss unfollow USER: unsubscribe from USER's feed.
+ terss list [MAX]: list the last MAX message from your subscriptions
+ (default 10).
+EOF
+ return 0
+}
+
function terss_init(){
- echo -n "Username? "
- read USERNAME
- echo -n "Server (for SSH connection)? "
- read SERVER
- echo -n "Path of your feed (on the server)? "
- read FEED
- echo -n "Public URL of your feed (over HTTP)? "
- read URL
+ if [[ -f "$TERSSDIR/config" ]]; then
+ source "$TERSSDIR/config"
+ fi
+ read -e -p "Username? " -i "$USERNAME" USERNAME
+ read -e -p "Server (for SSH connection)? " -i "$SERVER" SERVER
+ read -e -p "Path of your feed (on the server)? " -i "$FEED" FEED
+ read -e -p "Public URL of your feed (over HTTP)? " -i "$URL" URL
mkdir -p "$TERSSDIR"
cat > "$TERSSDIR/config" <<EOF
USERNAME="$USERNAME"
SERVER="$SERVER"
FEED="$FEED"
+URL="$URL"
EOF
cat > "$TERSSDIR/feed" <<EOF
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
- <title>$USERNAME TeRSS feed</title>
+ <title>$USERNAME terss feed</title>
<link>$URL</link>
- <description>$USERNAME TeRSS feed</description>
+ <description>$USERNAME terss feed</description>
</channel>
</rss>
EOF
- echo "$URL" > "$TERSSDIR/subscriptions"
-}
-
-function terss_follow(){
- NAME="$1"
- URL="$2"
- echo -e "$NAME\t$URL" >> "$TERSSDIR/subscriptions"
-}
-
-function terss_unfollow(){
- NAME="$1"
- sed -i "\~^$NAME\t~d" "$TERSSDIR/subscriptions"
+ echo -e "$USERNAME\t$URL" > "$TERSSDIR/subscriptions"
+ echo
+ _terss_log "configuration complete."
+ return 0
}
function terss_post(){
- MSG="$1"
- # prétraitement MSG (@name to link)
- DATE="$(date -R)"
+ local MSG="$1"
+ local DATE="$(date -R)"
xml2 < "$TERSSDIR/feed" | grep -v -E '^/rss/channel/item' > "$TERSSDIR/tmp"
cat >>"$TERSSDIR/tmp" <<EOF
/rss/channel/item/title=$MSG
@@ -80,18 +100,54 @@ EOF
scp "$TERSSDIR/feed" "$SERVER:$FEED"
}
-function terss_fetch(){
- NAME="$1"
- URL="$2"
- wget -q "$URL" -O "$TERSSDIR/feeds/$NAME"
+function terss_update(){
+ if wget -q "$URL" -O "$TERSSDIR/tmp"; then
+ mv "$TERSSDIR/tmp" "$TERSSDIR/feed"
+ _terss_log "feed updated."
+ return 0
+ else
+ _terss_log "could not download feed from the following url:"
+ _terss_log "<$URL>."
+ return 1
+ fi
+}
+
+
+function terss_follow(){
+ local NAME="$1"
+ local URL="$2"
+ echo -e "$NAME\t$URL" >> "$TERSSDIR/subscriptions"
+ _terss_log "$NAME: subscription added."
+ return 0
+}
+
+function terss_unfollow(){
+ local NAME="$1"
+ if [[ $(grep -c -E "^$NAME " "$TERSSDIR/subscriptions") = "0" ]]; then
+ _terss_log "error: $NAME: no such subscription."
+ return 1
+ else
+ sed -i "\~^$NAME\t~d" "$TERSSDIR/subscriptions"
+ _terss_log "$NAME: subscription removed."
+ return 0
+ fi
+}
+
+function _terss_fetch(){
+ local NAME="$1"
+ local URL="$2"
+ if ! wget -q "$URL" -O "$TERSSDIR/feeds/$NAME"; then
+ _terss_log "error: could not access $NAME's feed at the following url:"
+ _terss_log "<$URL>."
+ return 1
+ fi
xml2 < "$TERSSDIR/feeds/$NAME" | grep -E '^/rss/channel/item' | \
sed 's~^/rss/channel/~~' > "$TERSSDIR/feeds/$NAME.tmp"
- MSG=
- DATE=
+ local MSG=
+ local DATE=
while read LINE; do
if [[ "$LINE" = "item" && ! -z "$MSG" && ! -z "$DATE" ]]; then
echo -e "$DATE\t$NAME\t$MSG"
-
elif [[ "${LINE%%=*}" = "item/title" ]]; then
MSG="${LINE#*=}"
elif [[ "${LINE%%=*}" = "item/pubDate" ]]; then
@@ -99,19 +155,22 @@ function terss_fetch(){
fi
done < "$TERSSDIR/feeds/$NAME.tmp" >> "$TERSSDIR/timeline.tmp"
echo -e "$DATE\t$NAME\t$MSG" >> "$TERSSDIR/timeline.tmp"
+ rm "$TERSSDIR/feeds/$NAME"{,.tmp}
}
function terss_list(){
- MAX="$1"
+ local MAX="$1"
mkdir -p "$TERSSDIR/feeds"
- rm "$TERSSDIR/timeline.tmp"
+ rm -f "$TERSSDIR/timeline.tmp"
touch "$TERSSDIR/timeline.tmp"
local IFS=$'\t'
while read SUB; do
SUB=($SUB)
- terss_fetch "${SUB[0]}" "${SUB[1]}"
+ _terss_fetch "${SUB[0]}" "${SUB[1]}"
done < "$TERSSDIR/subscriptions"
sort -n -r -k1 < "$TERSSDIR/timeline.tmp" | head -"$MAX" > "$TERSSDIR/timeline"
+ rm "$TERSSDIR/timeline.tmp"
+ echo
while read MSG; do
MSG=($MSG)
_terss_echo -n green_u "${MSG[1]}"
@@ -119,13 +178,32 @@ function terss_list(){
_terss_echo white_b " ${MSG[2]}"
echo
done < "$TERSSDIR/timeline"
+ rm "$TERSSDIR/timeline"
}
-. "$TERSSDIR/config"
+#####
+
+if [[ ! -f "$TERSSDIR/config" ]]; then
+ if [[ "$1" != "init" ]]; then
+ terss_firstrun >&2
+ else
+ terss_init
+ fi
+fi
+
+source "$TERSSDIR/config"
+if [[ -z "$USERNAME" || -z "$SERVER" || -z "$FEED" ]]; then
+ echo 'Configuration seems invalid, please run "terss init"' >&2
+ exit
+fi
+
case "$1" in
- init) terss_init ;;
- follow) terss_follow "$2" "$3" ;;
- unfollow) terss_unfollow "$2" ;;
- post) shift; terss_post "$*" ;;
- list) terss_list "${2:-20}" ;;
+ f|follow) terss_follow "$2" "$3" ;;
+ u|unfollow) terss_unfollow "$2" ;;
+ p|post) shift; terss_post "$*" ;;
+ up|update) terss_update ;;
+ l|list) terss_list "${2:-10}" ;;
+ *) terss_help ;;
esac
+
+exit 1
Pablo Rauzy — generated by cgit