dotfiles

My personal shell configs and stuff
git clone git://git.alex.balgavy.eu/dotfiles.git
Log | Files | Refs | Submodules | README | LICENSE

notmuch-hook-old (3423B)


      1 #!/bin/sh
      2 notmuch new
      3 die() { printf '%s\n' "$1" >&2 && exit 1; }
      4 
      5 # arg: tag
      6 # returns: string in the format "-inbox +archive -draft -sent..."
      7 build_exclusive_tagstr() {
      8   [ $# -eq 0 ] && die "tag_exclusive needs an argument"
      9   exclusive_tags="inbox archive draft sent trash tickets spam"
     10   tag_str=""
     11   for tag in $exclusive_tags; do
     12     if [ "$1" = "$tag" ]; then
     13       tag_str="$tag_str +$tag"
     14     else
     15       tag_str="$tag_str -$tag"
     16     fi
     17   done
     18   printf '%s' "$tag_str"
     19 }
     20 
     21 # Change tags according to folders
     22 folders2tags() {
     23   notmuch tag --batch <<EOF
     24 # This will always be read mail
     25 $(build_exclusive_tagstr archive) -- folder:/Archive/
     26 $(build_exclusive_tagstr draft) -- folder:/Drafts/
     27 $(build_exclusive_tagstr sent) -- (from:alex@balgavy.eu or from:a.balgavy@gmail.com or from:a.balgavy@student.vu.nl) and (not folder:/Drafts/)
     28 $(build_exclusive_tagstr trash) -- folder:/Trash/ or folder:/Deleted/
     29 $(build_exclusive_tagstr tickets) -- folder:/Tickets/
     30 $(build_exclusive_tagstr spam) -- folder:/Spam/ or folder:/Junk/
     31 $(build_exclusive_tagstr inbox) -- folder:/Inbox/
     32 EOF
     33 }
     34 
     35 safeMove() { s=${1##*/}; s=${s%%,*}; mv -f "$1" "$2"/"$s"; }
     36 alias nms="notmuch search --exclude=false --output=files"
     37 
     38 # Move to folders according to tags
     39 tags2folders() {
     40   # Move a message file while removing its UID-part
     41   maildir="$HOME/.local/share/mail"
     42 
     43   for i in $(nms folder:/alex@balgavy.eu/ and tag:archive and not folder:/Archive/); do safeMove "$i" "$maildir/alex@balgavy.eu/Archive/cur"; done
     44   for i in $(nms folder:/alex@balgavy.eu/ and tag:draft and not folder:/Drafts/); do safeMove "$i" "$maildir/alex@balgavy.eu/Drafts/cur"; done
     45   for i in $(nms folder:/alex@balgavy.eu/ and tag:inbox and not folder:/Inbox/); do safeMove "$i" "$maildir/alex@balgavy.eu/Inbox/cur"; done
     46   for i in $(nms folder:/alex@balgavy.eu/ and tag:spam and not folder:/Spam/); do safeMove "$i" "$maildir/alex@balgavy.eu/Spam/cur"; done
     47   for i in $(nms folder:/alex@balgavy.eu/ and tag:tickets and not folder:/Tickets/); do safeMove "$i" "$maildir/alex@balgavy.eu/Tickets/cur"; done
     48   for i in $(nms folder:/alex@balgavy.eu/ and tag:trash and not folder:/Trash/); do safeMove "$i" "$maildir/alex@balgavy.eu/Trash/cur"; done
     49   for i in $(nms folder:/alex@balgavy.eu/ and tag:sent and not folder:/Sent/); do safeMove "$i" "$maildir/alex@balgavy.eu/Sent/cur"; done
     50 
     51   for i in $(nms folder:/a.balgavy@student.vu.nl/ and tag:archive and not folder:/Archive/); do safeMove "$i" "$maildir/a.balgavy@student.vu.nl/Archive/cur"; done
     52   for i in $(nms folder:/a.balgavy@student.vu.nl/ and tag:trash and not folder:/Deleted/); do safeMove "$i" "$maildir/a.balgavy@student.vu.nl/Deleted Items/cur"; done
     53   for i in $(nms folder:/a.balgavy@student.vu.nl/ and tag:draft and not folder:/Drafts/); do safeMove "$i" "$maildir/a.balgavy@student.vu.nl/Drafts/cur"; done
     54   for i in $(nms folder:/a.balgavy@student.vu.nl/ and tag:spam and not folder:/Junk/); do safeMove "$i" "$maildir/a.balgavy@student.vu.nl/Junk Email/cur"; done
     55   for i in $(nms folder:/a.balgavy@student.vu.nl/ and tag:sent and not folder:/Sent/); do safeMove "$i" "$maildir/a.balgavy@student.vu.nl/Sent Items/cur"; done
     56 }
     57 
     58 # When using notmuch, we set tags, so specify to move messages to folders based on tags
     59 if [ $# -gt 0 ] && [ "$1" = "tags2folders" ]; then tags2folders
     60 # Otherwise, in mutt or while retrieving new messages, want to set notmuch tags from folders
     61 else folders2tags; fi