aboutsummaryrefslogtreecommitdiff
path: root/offlineimap/postsynchook.sh
blob: 7262a50663fa7cbcd15b83c61a0f83376fe91cc7 (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
#!/usr/bin/env bash

clean () {
    sed "s/^\[/\\\[/g" | sed "s/\"/'/g" | sed 's/\!/❕/g'
}

get_sender () {
    grep -o '^From: \(.*\)$' $1 | sed "s/^From: //g" | clean
}

get_subject () {
    grep -o '^Subject: \(.*\)$' $1 | sed "s/^Subject: //g" | perl -pe 'use MIME::Words(decode_mimewords); $_=decode_mimewords($_);' | clean
}

notify () {
    account=$1
    previous_messages=/tmp/offlineimap-$account-messages
    maildir="$HOME/.mail/$account/inbox/new"

    if [ ! -e $previous_messages ]; then
        touch $previous_messages
    fi

    for file in $maildir/*; do
        if [ ! -f $file ]; then
            continue
        fi

        filename=$(basename $file)
        if grep -Fxq $filename $previous_messages; then
            : echo "seen"
        else
            echo $filename >> $previous_messages
            sender="`get_sender $file`"
            subject=`get_subject $file`
            message="New message for $account"

            if [ -e /usr/local/bin/terminal-notifier ]; then
                terminal-notifier -title $subject -subtitle $sender -message $message -sender 'org.gnu.Emacs' -activate 'org.gnu.Emacs'
            fi
        fi
    done
}

mu index
notify $1