dotfiles

Settings and scripts
git clone git://git.konyahin.xyz/dotfiles
Log | Files | Refs | Submodules | LICENSE

mfilter (1409B)


      1 #!/usr/bin/env sh
      2 
      3 # MIT/X Consortium License
      4 # © 2023 Anton Konyahin <me@konyahin.xyz>
      5 #
      6 # Depends on mblaze: https://git.vuxu.org/mblaze/
      7 
      8 set -eu
      9 
     10 help () {
     11     cat <<EOF
     12 $0 [-hcp] [maildir]
     13    -h show this help
     14    -c filter old mails (by default we look at new)
     15    -p print all rules without filtering
     16 You should specify maildir with -c and -p options. You can use only one flag at time.
     17 EOF
     18     exit 0
     19 }
     20 
     21 if [ -z "${1:-}" ]; then
     22     help
     23 fi
     24 
     25 case "$1" in
     26     "-h" )
     27 	help
     28 	;;
     29     "-p" )
     30 	PRINT_ONLY=yes
     31 	MAIL_DIR=${2:-}
     32 	;;
     33     "-c" )
     34 	MAIL_DIR=$2
     35 	MLIST_FILTER=-C
     36 	MAIL_DEST=cur
     37 	;;
     38     * )
     39 	MAIL_DIR=$1
     40 	MLIST_FILTER=-N
     41 	MAIL_DEST=new
     42 	;;
     43 esac
     44 
     45 if [ -z "$MAIL_DIR" ]; then
     46     echo "You should specify maildir path"
     47     exit 1
     48 fi
     49 
     50 FILTER_FILE="$MAIL_DIR/.filter"
     51 if [ ! -e "$FILTER_FILE" ]; then
     52     echo "You should have .filter file in maildir"
     53     exit 1
     54 fi
     55 
     56 move () {
     57     if [ -n "${PRINT_ONLY:-}" ]; then
     58 	printf "%s\t-> %s\n" "$1" "$2"
     59     else
     60 	mlist "$MLIST_FILTER" "$MAIL_DIR/INBOX" |
     61 	    mpick -t "\"To\" =~~ \"$1\" ||
     62 	      	    \"Cc\" =~~ \"$1\" ||
     63              	    \"From\" =~~ \"$1\"" |
     64 	    mrefile -v "$MAIL_DIR/$2" |
     65 	    xargs -r -n1 -I {} mv {} "$MAIL_DIR/$2/$MAIL_DEST"
     66     fi
     67 }
     68 
     69 # if file doesn't end with new line, we need second check
     70 while read -r line || [ -n "$line" ]; do
     71     if [ -n "$line" ]; then
     72 	# shellcheck disable=SC2086
     73 	move $line
     74     fi
     75 done < "$FILTER_FILE"