summaryrefslogtreecommitdiffstats
path: root/.local/bin/mailsync
diff options
context:
space:
mode:
authorYigit Sever2021-10-12 01:01:01 +0300
committerYigit Sever2021-10-12 01:01:01 +0300
commit22bafa21869fb47d8f6568a0e047c226926f20e0 (patch)
tree90b18965b589d95e27e166186ddc32ca8f92471b /.local/bin/mailsync
parent20e42aa0d070f0c09ab20bf4ab42c3495a807e1c (diff)
downloaddotfiles-22bafa21869fb47d8f6568a0e047c226926f20e0.tar.gz
dotfiles-22bafa21869fb47d8f6568a0e047c226926f20e0.tar.bz2
dotfiles-22bafa21869fb47d8f6568a0e047c226926f20e0.zip
mutt: free from the shackles
Diffstat (limited to '.local/bin/mailsync')
-rwxr-xr-x.local/bin/mailsync76
1 files changed, 76 insertions, 0 deletions
diff --git a/.local/bin/mailsync b/.local/bin/mailsync
new file mode 100755
index 0000000..ae0801c
--- /dev/null
+++ b/.local/bin/mailsync
@@ -0,0 +1,76 @@
1#!/bin/sh
2
3# Sync mail and give notification if there is new mail.
4
5# Run only if user logged in (prevent cron errors)
6pgrep -u "${USER:=$LOGNAME}" >/dev/null || { echo "$USER not logged in; sync will not run."; exit ;}
7# Run only if not already running in other instance
8pgrep -x mbsync >/dev/null && { notify-send --app-name="mailsync" "mbsync is already running" "you should fix this" ; exit ;}
9
10if [[ -f "$HOME/.local/share/.dnd" ]]; then
11 echo "do not disturb is on"
12 exit
13fi
14
15# Checks for internet connection and set notification script.
16ping -q -c 1 1.1.1.1 > /dev/null || ping -q -c 1 1.0.0.1 > /dev/null || ping -q -c 1 example.org || { echo "No internet connection detected."; exit ;}
17command -v notify-send >/dev/null || echo "Note that \`libnotify\` or \`libnotify-send\` should be installed for pop-up mail notifications with this script."
18
19# Required to display notifications if run as a cronjob:
20export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus
21export DISPLAY=:0.0
22
23# For individual configurations:
24[ -d "$HOME/.local/share/password-store" ] && export PASSWORD_STORE_DIR="$HOME/.local/share/password-store"
25
26# Settings are different for MacOS (Darwin) systems.
27if [ "$(uname)" = "Darwin" ]; then
28 notify() { osascript -e "display notification \"$2 in $1\" with title \"You've got Mail\" subtitle \"Account: $account\"" && sleep 2 ;}
29 messageinfo() { osascript -e "display notification with title \"📧 $from\" subtitle \"$subject\"" ;}
30else
31 notify() { notify-send --icon="mail_new" --app-name="excerpt" "\`$1\`" "📬 $2 new mail(s)" ;}
32 messageinfo() { notify-send --icon="mail-mark-unread" --app-name="mutt" "📧 $from" "$subject" ;}
33fi
34
35# Check account for new mail. Notify if there is new content.
36syncandnotify() {
37 acc="$(echo "$account" | sed "s/.*\///")"
38 if [ -z "$opts" ]; then mbsync "$acc"; else mbsync "$opts" "$acc"; fi
39 new=$(find "$HOME/.local/share/mail/$acc/INBOX/new/" "$HOME/.local/share/mail/$acc/Inbox/new/" "$HOME/.local/share/mail/$acc/inbox/new/" -type f -newer "$HOME/.config/mutt/.mailsynclastrun" 2> /dev/null)
40 newcount=$(echo "$new" | sed '/^\s*$/d' | wc -l)
41 if [ "$newcount" -gt "0" ]; then
42 notify "$acc" "$newcount" &
43 for file in $new; do
44 # Extract subject and sender from mail.
45 from=$(awk '/^From: / && ++n ==1,/^\<.*\>:/' "$file" | perl -CS -MEncode -ne 'print decode("MIME-Header", $_)' | awk '{ $1=""; if (NF>=3)$NF=""; print $0 }' | sed 's/^[[:blank:]]*[\"'\''\<]*//;s/[\"'\''\>]*[[:blank:]]*$//')
46 subject=$(awk '/^Subject: / && ++n == 1,/^\<.*\>: / && ++i == 2' "$file" | head -n 1 | perl -CS -MEncode -ne 'print decode("MIME-Header", $_)' | sed 's/^Subject: //' | sed 's/^{[[:blank:]]*[\"'\''\<]*//;s/[\"'\''\>]*[[:blank:]]*$//' | tr -d '\n')
47 messageinfo &
48 done
49 fi
50}
51
52# Sync accounts passed as argument or all.
53if [ "$#" -eq "0" ]; then
54 accounts="$(awk '/^Channel/ {print $2}' "$HOME/.mbsyncrc")"
55else
56 for arg in "$@"; do
57 [ "${arg%${arg#?}}" = '-' ] && opts="${opts:+${opts} }${arg}" && shift 1
58 done
59 accounts=$*
60fi
61
62# ( kill -46 "$(pidof "${STATUSBAR:-dwmblocks}")" >/dev/null 2>&1 ) 2>/dev/null
63
64# Parallelize multiple accounts
65for account in $accounts
66do
67 syncandnotify &
68done
69
70wait
71# ( kill -46 "$(pidof "${STATUSBAR:-dwmblocks}")" >/dev/null 2>&1 ) 2>/dev/null
72
73notmuch new 2>/dev/null
74
75#Create a touch file that indicates the time of the last run of mailsync
76touch "$HOME/.config/mutt/.mailsynclastrun"