rp

simple email tools
git clone https://git.parazyd.org/rp
Log | Files | Refs | README | LICENSE

rpabook (1193B)


      1 #!/bin/sh
      2 #
      3 # Copy me if you can
      4 # by parazyd
      5 #
      6 
      7 profile="${RPPROFILE:-$HOME/.rp/default}"
      8 
      9 ab="$profile/whitelist.abook"
     10 
     11 usage() {
     12 	cat <<EOF
     13 * Usage: $(basename $0) [-a < email] [-c pattern]
     14 	-a: add to abook
     15 	-c: completion (used with mutt)
     16 EOF
     17 	exit 1
     18 }
     19 
     20 while getopts "ach" opt; do
     21 	case "$opt" in
     22 	a)
     23 		email="$(cat)"
     24 		addr="$(echo "$email" | rpheaders From | cut -d'<' -f2 | tr -d '<>')"
     25 		name="$(echo "$email" | rpheaders From | cut -d'<' -f1 | sed 's/ $//')"
     26 		printf "%s <%s>\n" "$name" "$addr"
     27 		grep -q "^email=\"$addr\"" "$ab" && {
     28 			echo "* Address already in abook"
     29 			exit 0
     30 		}
     31 
     32 		if [ "$addr" = "$name" ]; then
     33 			name="$(echo "$name" | cut -d '@' -f1)"
     34 		fi
     35 
     36 		printf "email=\"%s\",%s" "$addr" "$name" >> "$ab"
     37 		exit 0
     38 		;;
     39 	c)
     40 		contact="$(grep -i "$2" "$ab")"
     41 		ret="$?"
     42 		if [ -t 1 ]; then
     43 			echo "$contact" | while read line; do
     44 				[ -n "$line" ] || break
     45 				email="$(echo "$line" | cut -d',' -f1 | cut -c 8- | sed 's/"$//')"
     46 				name="$(echo "$line" | cut -d',' -f2-)"
     47 				printf "%s <%s>\n" "$name" "$email"
     48 			done
     49 		else
     50 			echo "$contact" | sed -e 's/^email=//' -e 's/,/\t/' \
     51 				-e 's/"//' -e 's/"//'
     52 		fi
     53 		exit 0
     54 		;;
     55 	*)
     56 		usage
     57 		;;
     58 	esac
     59 done
     60 
     61 usage