aboutsummaryrefslogtreecommitdiff
path: root/scripts/dot-local/bin/mailsync
blob: 802944430dc466ae42d08325244c7b01014209d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env sh

# Run only if not already running in other instance
pgrep mbsync >/dev/null && {
  echo "mbsync is already running."
  exit
}

notify()
{
  notify-send --app-name="Mail" -- "$1" "$2"
}

# export GPG_TTY="$(tty)"
# export DISPLAY=":0"
# export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/"$(id -u)"/bus
export MAILDIR="$XDG_DATA_HOME/mail"
lastrun="$XDG_CACHE_HOME/.mailsynclastrun"
# export PYTHONWARNINGS="ignore::UserWarning"

# afew --move-mails --all
mbsync -q mit

new=$(find "$MAILDIR/mit/INBOX/new/" "$MAILDIR/mit/INBOX/cur/" -type f -newer "$lastrun" 2>/dev/null)
newcount=$(echo "$new" | sed '/^\s*$/d' | wc -l)

case 1 in
  $((newcount > 5)))
    echo "$newcount new mail for mit."
    notify "New Mail!" "$newcount new mail(s)."
    ;;
  $((newcount > 0)))
    echo "$newcount new mail for mit."
    for file in $new; do
      # Extract and decode subject and sender from mail.
      subject=$(awk '/^Subject: / && ++n == 1,/^.*: / && ++i == 2' "$file" | head -n-1 |
        perl -CS -MEncode -ne 'print decode("MIME-Header", $_)' |
        sed 's/^Subject: //' | tr -d '\n\t')
      from="$(sed -n "/^From:/ s|From: *|| p" "$file" |
        perl -CS -MEncode -ne 'print decode("MIME-Header", $_)')"
      from="${from% *}"
      from="${from%\"}"
      from="${from#\"}"
      notify "$from:" "$subject"
    done
    ;;
  *) echo "No new mail for mit." ;;
esac

# notmuch new
# afew --tag --new

#Create a touch file that indicates the time of the last run of mailsync
touch "$lastrun"