aboutsummaryrefslogtreecommitdiff
path: root/dotfiles/.local/bin/mailsync
blob: d67db06de57a29db58ff570dbde7edd08a09c431 (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
#!/bin/sh
# Sync mail and give notification if there is new mail.

# Source the needed env variables so that it can function properly from cron
. ~/.cache/sessionenv

# Config files
notmuchrc="$HOME/.config/notmuchrc"
mbsyncrc="$HOME/.config/mbsyncrc"

# Run only if user logged in (prevent cron errors)
w | grep "^$USER\W" >/dev/null || exit

# Don't run if screen is locked (gpg key is locked too)
pidof swaylock && exit

# Check several times before giving up, useful when just waking up from sleep
# since internet is usually not available right away
wget -q --tries=3 --timeout=20 --spider 1.1.1.1 > /dev/null || exit

thereis=0

# Run mbsync. You can feed this script different settings.
if [ $# -eq 0 ]; then
	mbsync -c "$mbsyncrc" -a
else
	mbsync -c "$mbsyncrc" "$@"
fi

# Check all accounts/mailboxes for new mail. Notify if there is new content.
for mailbox in "$HOME/.local/share/mail/"*
do
	account="$(echo "$mailbox" | sed "s/.*\///")"
	newcount=$(find "$HOME/.local/share/mail/$account/INBOX/new/" "$HOME/.local/share/mail/$account/Inbox/new/" "$HOME/.local/share/mail/$account/inbox/new/" -type f -newer "$HOME/.cache/lastmailsync" 2> /dev/null | wc -l)
	if [ "$newcount" -gt "0" ]; then
    notify-send "📬 Email" "$newcount new mail(s) in \`$account\` mailbox."
    thereis=1
  fi
done

if [ $thereis = 1 ]; then
  mpv --really-quiet "$HOME/.local/share/soundalerts/mail.ogg"
  killall -43 waybar
fi

notmuch -c "$notmuchrc" new 2>/dev/null

# Create a touch file that indicates the time of the last run of mailsync
touch "$HOME/.cache/lastmailsync"