summaryrefslogtreecommitdiff
path: root/bin/movemymail
blob: 1b92e820b7e430a63a505b9333c65c75a1b4d7a9 (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
55
56
57
58
59
60
61
62
#!/bin/bash

. $HOME/.shenv

if [ -e "$HOME/.nomovemymail" ]; then
    echo >&2 "no movemymail"
    exit 1
fi

# if using notmuch on this machine, sync its database to maildir & git
if [ -f "$HOME/.fmail/.notmuch/xapian/iamglass" ]; then
    mr -d "$HOME/lib/nmbug-spw" autoci

    # ensure all drafts are in the drafts folder
    floating_drafts=$(notmuch search --output=files -- tag:draft and not folder:drafts and not tag:deleted)
    if [ ! -z "$floating_drafts" ]; then
        # TODO for each entry in $floating_drafts, if it contains the
        # substring ".fmail/annex", remove it from $floating_drafts
        # (mdmv used to do this, but now I plan to upload it to
        # Debian, it doesn't)
        chronic mdmv $floating_drafts $HOME/.fmail/drafts
    fi

    # ensure all spam is in the spam folder to train FastMail's spam filter
    floating_spam=$(notmuch search --output=files -- tag:spam and not folder:spam)
    if [ ! -z "$floating_spam" ]; then
        # TODO for each entry in $floating_drafts, if it contains the
        # substring ".fmail/annex", remove it from $floating_drafts
        # (mdmv used to do this, but now I plan to upload it to
        # Debian, it doesn't)
        chronic mdmv $floating_spam $HOME/.fmail/spam
    fi

    # ensure all deleted messages are in the trash folder
    floating_deleted=$(notmuch search --output=files -- tag:deleted and not folder:trash)
    if [ ! -z "$floating_deleted" ]; then
        # TODO for each entry in $floating_drafts, if it contains the
        # substring ".fmail/annex", remove it from $floating_drafts
        # (mdmv used to do this, but now I plan to upload it to
        # Debian, it doesn't)
        chronic mdmv $floating_deleted $HOME/.fmail/trash
    fi
fi

if offline; then
    echo >&2 ERROR: We\'re offline\; cannot sync mail
    exit 1
else
    # this shouldn't be necessary because there is
    # /etc/network/if-up.d/postfix, but it's cheap so just call again
    # here
    postqueue -f

    if pgrep -u $USER mbsync 2>/dev/null; then
        (killall --user $USER mbsync 2>/dev/null && sleep 5) || true
        killall -9 --user $USER mbsync 2>/dev/null || true
    fi
    mbsync fastmail
fi

# to confirm queue was flushed before I close laptop lid
mailq