updated README.md
[lace] / lace
1 #!/usr/bin/env bash
2
3 # forked from https://gitlab.com/uoou/dotfiles/-/tree/master/stow/bin/home/drew/.local/bin/lace
4 # https://raw.githubusercontent.com/frrobert2/lace/main/lace
5 # Shows both how many days ago and date
6 # Modified to work with Debian running Sed 3.7
7 # connectto is a timeout variable in seconds 
8 # it is the time lace will wait to connect to a site before it stops the connection
9 #  default is 30 seconds you can change it what you want
10 connectto=30
11 set -o pipefail
12 if [ -z "$XDG_CONFIG_HOME" ];then
13         config_place="$HOME/.config/lace"
14 else
15         config_place="$XDG_CONFIG_HOME/lace"
16 fi
17 [[ ! -d "$config_place" ]] && mkdir -p $config_place
18 subsfile="$config_place/subs"
19
20 red="[31m"
21 blue="[34m"
22 nc="[m"
23 esc=$(echo -e "\033")
24         if [[ "$1" == "strict" ]] || [[ "$1" == "--strict" ]] || [[ "$1" == "-strict" ]] || [[ "$1" == "-s" ]];then
25                 red="### "
26                 blue="### "
27                 nc=""
28                 esc=""
29         fi
30 tmpfile=$(mktemp /tmp/lace.XXXXXX)
31 tmpfile2=$(mktemp /tmp/lace.XXXXXX)
32 tmpfile3=$(mktemp /tmp/lace.XXXXXX)
33 function reldate() {
34 # stolen from https://unix.stackexchange.com/a/451216
35     local SEC_PER_MINUTE=$((60))
36     local   SEC_PER_HOUR=$((60*60))
37     local    SEC_PER_DAY=$((60*60*24))
38     local  SEC_PER_MONTH=$((60*60*24*30))
39     local   SEC_PER_YEAR=$((60*60*24*365))
40
41     local last_unix="$(date --date="@$1" +%s)"    # convert date to unix timestamp
42         local now_unix="$2"
43     local delta_s=$(( now_unix - last_unix ))
44
45     if (( delta_s <  SEC_PER_MINUTE * 2))
46     then
47         echo ""$((delta_s))" seconds ago"
48     elif (( delta_s <  SEC_PER_HOUR * 2))
49     then
50         echo ""$((delta_s / SEC_PER_MINUTE))" minutes ago"
51     elif (( delta_s <  SEC_PER_DAY * 2))
52     then
53         echo ""$((delta_s / SEC_PER_HOUR))" hours ago"
54     elif (( delta_s <  SEC_PER_MONTH * 2))
55     then
56         echo ""$((delta_s / SEC_PER_DAY))" days ago"
57     elif (( delta_s <  SEC_PER_YEAR * 2))
58     then
59         echo ""$((delta_s / SEC_PER_MONTH))" months ago"
60     else
61         echo ""$((delta_s / SEC_PER_YEAR))" years ago"
62     fi
63 }
64 #export -f reldate
65
66 function dohelp() {
67         echo -e "\nUsage:\n"
68         echo -e "\t$(basename "$0") - read subscriptions\n"
69         echo -e "\t$(basename "$0") nope - read subscriptions without pager\n"
70         echo -e "\t$(basename "$0") subs - list subscriptions\n"
71         echo -e "\t$(basename "$0") sub nickname url - add subscription, e.g.:"
72         echo -e "\t$(basename "$0") sub Drew gemini://friendo.monster/tiny.gmi\n"
73         echo -e "\t$(basename "$0") unsub nickname - remove subscription, e.g.:"
74         echo -e "\t$(basename "$0") unsub Drew\n"
75         echo -e "\t$(basename "$0") update - redownload script to location of current script (might need sudo, depending on location)\n"
76         exit
77 }
78
79 function timeline() {
80         while read f;do
81                 local user=$(awk '{print $2}' <<< "$f")
82                 local url=$(awk '{print $1}' <<< "$f")
83                 if [[ "$url" != *://* ]];then
84                         domain="$url"
85                         url="gemini://$url"
86                 else
87                                 domain=$(sed 's/gemini:\/\///' <<< "$url")
88                         fi
89                         if [[ "$url" != *.*/*.* ]] && [[ $url != */ ]];then
90                                 url="$url/"
91                         fi
92                         domain=$(sed 's/\/.*$//' <<< "$domain")
93                 timeout $connectto openssl s_client -crlf -quiet -connect "$domain:1965" <<<"$url" 2>/dev/null > $tmpfile3
94                         timeoutstatus=${PIPESTATUS[0]}
95                         if [ $timeoutstatus -ne 124 ];then
96                         local newuser=$(cat $tmpfile3 | grep -m 1 author: | awk -F ":" '{print $2}'  )
97                 local newavatar=$(cat $tmpfile3 | grep -m 1 avatar: | awk -F ":" '{print $2}'  )
98                         if [[ $newuser != "" ]]
99                         then
100         newuser=$(echo "$newuser" | xargs)
101         newavatar=$(echo "$newavatar" | xargs)
102                 user="$newavatar $newuser"
103         else
104         fav=$(timeout $connectto openssl s_client -crlf -quiet -connect "$domain:1965" <<<"gemini://$domain/favicon.txt" 2>/dev/null | sed -e '1d')
105 if [ ! -z "$fav" ];then
106         user="$fav $user"
107         fi
108                 
109                         fi
110
111                         cat $tmpfile3 | sed -e '1d' | sed -n '/##/,$p' | sed -e "s/^\(## .*\)$/\1\n${esc}$red$user${esc}$nc ($domain)/g" >> $tmpfile
112                         [[ ! -z $(sed -n '$p' $tmpfile) ]] && echo "" >> $tmpfile
113                         fi
114                 done <"$subsfile"
115                 cat $tmpfile | sed -r ':r;/(^|\n)$/!{$!{N;br}};s/\n/\v/g' | sed -rn '/^## .*/p' | sed -e 's/\v/\n/g' | sed -e "s/^## \(.*\)/date -d '\1' +%s/e" | sed -r ':r;/(^|\n)$/!{$!{N;br}};s/\n/\v/g' | sort -r | sed -e 's/\v/\n/g'  > $tmpfile3
116
117
118 cp $tmpfile3 $tmpfile
119         # removed segment of code       | sed "s/^\([0-9]\+\)$/{reldate \1 $now_unix;}/e" | sed "s/^\([0-9]* [a-z]* ago\)$/${esc}$blue\1${esc}$nc/g"
120
121         while read line; do
122         if [[ "$line" =~ ^[0-9]{10,}$ ]]; then
123                 reldate "$line" "$(date +%s)" >> $tmpfile2
124                 
125                 echo "$(date -d @$line)" >> $tmpfile2
126         else
127            echo "$line" >> $tmpfile2
128         fi
129         done<$tmpfile
130
131         cat $tmpfile2 | sed "s/^\([0-9]* [a-z]* ago\)$/${esc}$blue\1${esc}$nc/g" >$tmpfile
132         
133
134                 if [[ "$1" == "nope" ]];then
135                         cat $tmpfile && rm -f $tmpfile $tmpfile2 $tmpfile3
136                 else
137                         less -RisW $tmpfile && rm -f $tmpfile $tmpfile2 $tmpfile3
138                 fi
139         }
140
141         if [[ "$1" == "help" ]] || [[ "$1" == "--help" ]] || [[ "$1" == "-help" ]] || [[ "$1" == "-h" ]];then
142                 dohelp
143         elif [[ "$1" == "sub" ]];then
144                 [[ "$#" -ne 3 ]] && dohelp
145                 echo "$3 $2" >> $subsfile && echo "added $2 to subscriptions"
146 elif [[ "$1" == "unsub" ]];then
147         [[ "$#" -ne 2 ]] && dohelp
148         if grep " $2$" $subsfile 1>/dev/null;then
149                 sed -i "/ $2$/d" $subsfile &&
150                 echo "removed subscription to $2"
151         else
152                 echo "subscription \"$2\" does not exist"
153         fi
154 elif [[ "$1" == "subs" ]];then
155         awk '{print $2" ("$1")"}' $subsfile 
156 elif [[ "$1" == "update" ]];then
157         curl https://gitlab.com/frrobert2/lace/-/raw/dev/lace > "${BASH_SOURCE[0]}"
158 else
159         now_unix="$(date +'%s')"
160         if [[ "$1" == "nope" ]];then
161                 timeline nope
162         else
163                 timeline
164         fi
165 fi