💾 Archived View for rawtext.club › ~zilog › gabbr.txt captured on 2023-12-28 at 16:50:33.

View Raw

More Information

⬅️ Previous capture (2023-03-20)

➡️ Next capture (2024-02-05)

-=-=-=-=-=-=-

#!/bin/sh
# -------------------------------------------------------------
#  gabbr -- a RawText.club gab(1) client
# -------------------------------------------------------------
# $Log: gabbr,v $
# Revision 2.1  2021/05/01 19:49:27  zilog
# nGabbr becomes gabbr-2.x
#

# get/set RCS ID & Gabbr version:
RCS_ID="$Id: gabbr,v 2.1 2021/05/01 19:49:27 zilog Exp $"
GBR_VER="$(echo "$RCS_ID" |cut -d' ' -f3)"

# set initial # of messages to display:
MAXMSGS=5

# set formater for channel listings:
FORMAT='pr -t -o3 --columns=4'

# set PAGER:
if [ -x $(which less) ]
  then  PAGER='less -ReX'
  else  PAGER='more'
fi

## misc. functions:
clr_scrn() {
  clear
  echo ''
  echo '============================================================================='
}
gab_head() {
  gab -c |head -3 |grep -v '^



}
gab_chans() {
  gab -c |tail +4 |sort |tr -d ' *'
}
private_chans() {
  ls ${HOME}/.gab-*\! |cut -d'-' -f2
}
gab_list() {
  gab -l $MAXMSGS |fold -s |$PAGER
}
curr_chan() {
  cat $HOME/.gab_chan
}

## gabbr help message
gabbr_help() {
echo '
  gabbr is an interactive gab(1) client providing:

   - posting and/or deletion of messages
   - +/- adjustment of # of displayed messages
   - channel switching (public & private)
   - safe creation of new channels
   - block/unblock user toggle

  usage:  gabbr [-h|<chan>] ; defaults to cur. chan
  
   info:  gopher://rawtest.club/1/~zilog/Gabbr/
   help:  "gabbr" (gab) channel
 '
}

## menus:
main_menu() {
echo "
=============================================================================
 Gabbr-$GBR_VER   $(gab -n |sed 's/gab messages/msgs/')
============================================================================="
}

show_help() {
echo "
=============================================================================
  [G]  Goto a channel       [D]  Delete post(s)     [+/-]  chg # msgs shown
  [P]  Post a message       [R]  Redisplay msgs       [S]  Search for WORD 
  [N]  make NEW chan        [B]  (un)Block user       [Q]  Quit gabbr     
============================================================================="
}

list_chans() {
echo "
=============================================================================
$(gab_head)
$(gab_chans | $FORMAT)
 --------------------------- private channels -------------------------------
$(private_chans | $FORMAT)
=============================================================================
 $(gab -l 0 |sed -n '2p')
=============================================================================
"
}

grp_chan() {
  read -p 'earch - enter KEYWORD: ' KEYWRD
  while [ -n "$KEYWRD" ]
		do
      clr_scrn
      ( printf '=> searching%s for "%s"...\n\n- - -\n' "$(gab -l 0 |sed -n '2p' |cut -d: -f2)" "$KEYWRD"
        gab -l 5000 |grep -iw --color=always "$KEYWRD" |sed 's/$/\n- - -/' |fold -s ;) |$PAGER
      echo '============================================================================='
			read -n1 -p ' search again? [Ny]: ' REPLY
			echo
			case "$REPLY" in
				y|Y) echo ;read -p ' enter new KEYWORD: ' KEYWRD ;;
				  *) break ;;
			esac
  done
}

## primary gabbr functions:

# change to another channel:
chg_chan() {
  if [ $1 ]
    then KEYWORD="$1"
    else read -r -p 'oto: ' KEYWORD
  fi
  case "$KEYWORD" in
    (''|'?') list_chans
             read -r -p ' G> goto: ' KEYWORD ;;
         (*) ;;
  esac
  if [ -n "$KEYWORD" ]
    then
      CHAN="$( ( gab_chans ;private_chans ;) |grep -m1 "^$KEYWORD")"
      [ -n "$CHAN" ] && gab -c "$CHAN" >/dev/null || gab >/dev/null
    else
      gab >/dev/null
  fi
}

# create a NEW channel:
new_chan() {
  read -r -p 'ew chan[!]: ' NEWCHAN
  if [ -z "$NEWCHAN" ]
    then gab -c >/dev/null
    else gab -c "$NEWCHAN" >/dev/null
  fi
}

# post message in current channel:
post_msg() {
  read -r -p 'ost: ' GPOST
  [ -n "$GPOST" ] && gab -m "$GPOST"
}

# delete posts in current channel:
rm_posts() {
  CURRCHAN="$(curr_chan)"
  case "$CURRCHAN" in
    gab) CHANFILE="$HOME/.gab" ;;
      *) CHANFILE="$HOME/.gab-$CURRCHAN"
  esac
  NMSGS=$(cat $CHANFILE 2>/dev/null |wc -l)
  if [ -f $CHANFILE ] && [ $NMSGS -gt 0 ] ;then
    while [ $NMSGS -gt 0 ] ;do
      clear
      echo ''
      echo '============================================================================='
      echo " Your gab(1) '$CURRCHAN' channel posts:"
      echo '-----------------------------------------------------------------------------'
      tac $CHANFILE |cut -d'|' -f2- |nl |fold -s -w70 |sed '/^[[:alpha:],[:punct:]]/s/^/\t/'
      echo '-----------------------------------------------------------------------------'
      echo ' enter post # to remove; ENTER cancels:'
      echo '============================================================================='
      read -p ' G> post #: ' PSTNUM
      case $PSTNUM in
        ([0-9]*) printf "$((NMSGS - PSTNUM + 1)),d\nw\n" |\
     	          ed $CHANFILE >/dev/null ;;
            ('') break ;;
             (*) echo 'entry is invalid' ;;
      esac
      NMSGS=$(cat $CHANFILE |wc -l)
    done
  else
    echo ''
    echo " You currently have no posts in '$CURRCHAN'"
    echo ''
    echo " Returning to '$CURRCHAN'..'"
    sleep 2
  fi
}

# block/unblock other users:
blk_user() {
  BLKFILE="$HOME/.gab_block"
  echo ''
  echo ''
  if [ ! -f $BLKFILE ] || [ $(cat $BLKFILE |wc -l) -eq 0 ] ;then
    echo ' You have no blocked users on gab(1).'
  else
    echo " You're currently blocking: $(cat $BLKFILE |tr '\n' ' ')"
  fi
  echo ''
  read -p ' Enter USER to block/unblock or RETURN to cancel: ' BLKUSR
  case $BLKUSR in
   ('') printf '\n (un)block cancelled; returning to prevous channel..\n' ;;
    (*) if (grep -x $BLKUSR $BLKFILE >/dev/null)
          then printf '\n %s\n' "$(gab -u $BLKUSR)"
          else printf '\n %s\n' "$(gab -b $BLKUSR)"
        fi ;;
  esac
  sleep 3
}

# remove channel file(s):
rm_chans() {
  clear
  echo ''
  echo '============================================================================='
  echo ' Note: removing gab channel files only deletes *your* posts, no one elses.   '
  echo '============================================================================='
  echo '  You appear to have posts in the following gab(1) chans:                    '
  echo '-----------------------------------------------------------------------------'
  ls $HOME/.gab-* |cut -d'-' -f2 |pr -t -o4 --columns=4 -W72
  echo '-----------------------------------------------------------------------------'
  echo '============================================================================='
  read -r -p ' selectively delete? [N/y]: ' REPLY
  echo ''
  case "$REPLY" in
    y|Y) echo ' note:  pressing ↵ (ENTER) => keeps file; chan files are named ".gab-CHAN"'
	       echo ''
	       trap "main" 2
         cd $HOME && /usr/bin/rm -i -- .gab-*
				 gab -c gab >/dev/null
	       trap 2 ;;
      *) ;;
  esac
}

## the Gabbr command center:
gabbr_cmds() {
  read -r -N1 -p ' G> ' REPLY
  case "$REPLY" in
      ('+') export MAXMSGS=$((MAXMSGS+3)) ;;
      ('-') export MAXMSGS=$((MAXMSGS-3)) ;;
      (g|G) chg_chan ;;
      (n|N) new_chan ;;
      (p|P) post_msg ;;
      (d|D) rm_posts ;;
      (x|X) rm_chans ;;
      (b|B) blk_user ;;
      (s|S) grp_chan ;;
      (q|Q) echo '' ; exit 0 ;;
      (r|R) ;;
        (*) show_help && gabbr_cmds ;;
  esac
}

## main:
main() {
  case "$1" in
    -*) gabbr_help && exit 0 ;;
    '') ;;
     *) chg_chan "$1" ;;
  esac
  while : ;do
    clr_scrn
    gab_list
    main_menu
    gabbr_cmds
  done
}
main "$1"