summaryrefslogtreecommitdiffstats
path: root/.local/bin/mailsync
diff options
context:
space:
mode:
Diffstat (limited to '.local/bin/mailsync')
-rwxr-xr-x.local/bin/mailsync32
1 files changed, 20 insertions, 12 deletions
diff --git a/.local/bin/mailsync b/.local/bin/mailsync
index ae0801c..cc7010b 100755
--- a/.local/bin/mailsync
+++ b/.local/bin/mailsync
@@ -3,9 +3,15 @@
3# Sync mail and give notification if there is new mail. 3# Sync mail and give notification if there is new mail.
4 4
5# Run only if user logged in (prevent cron errors) 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 ;} 6pgrep -u "${USER:=$LOGNAME}" > /dev/null || {
7 echo "$USER not logged in; sync will not run."
8 exit
9}
7# Run only if not already running in other instance 10# 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 ;} 11pgrep -x mbsync > /dev/null && {
12 notify-send --app-name="mailsync" "mbsync is already running" "you should fix this"
13 exit
14}
9 15
10if [[ -f "$HOME/.local/share/.dnd" ]]; then 16if [[ -f "$HOME/.local/share/.dnd" ]]; then
11 echo "do not disturb is on" 17 echo "do not disturb is on"
@@ -13,8 +19,11 @@ if [[ -f "$HOME/.local/share/.dnd" ]]; then
13fi 19fi
14 20
15# Checks for internet connection and set notification script. 21# 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 ;} 22ping -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 || {
17command -v notify-send >/dev/null || echo "Note that \`libnotify\` or \`libnotify-send\` should be installed for pop-up mail notifications with this script." 23 echo "No internet connection detected."
24 exit
25}
26command -v notify-send > /dev/null || echo 'Note that `libnotify` or `libnotify-send` should be installed for pop-up mail notifications with this script.'
18 27
19# Required to display notifications if run as a cronjob: 28# Required to display notifications if run as a cronjob:
20export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus 29export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus
@@ -25,11 +34,11 @@ export DISPLAY=:0.0
25 34
26# Settings are different for MacOS (Darwin) systems. 35# Settings are different for MacOS (Darwin) systems.
27if [ "$(uname)" = "Darwin" ]; then 36if [ "$(uname)" = "Darwin" ]; then
28 notify() { osascript -e "display notification \"$2 in $1\" with title \"You've got Mail\" subtitle \"Account: $account\"" && sleep 2 ;} 37 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\"" ;} 38 messageinfo() { osascript -e "display notification with title \"📧 $from\" subtitle \"$subject\""; }
30else 39else
31 notify() { notify-send --icon="mail_new" --app-name="excerpt" "\`$1\`" "📬 $2 new mail(s)" ;} 40 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" ;} 41 messageinfo() { notify-send --icon="mail-mark-unread" --app-name="mutt" "📧 $from" "$subject"; }
33fi 42fi
34 43
35# Check account for new mail. Notify if there is new content. 44# Check account for new mail. Notify if there is new content.
@@ -44,7 +53,7 @@ syncandnotify() {
44 # Extract subject and sender from mail. 53 # 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:]]*$//') 54 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') 55 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 & 56 messageinfo &
48 done 57 done
49 fi 58 fi
50} 59}
@@ -62,15 +71,14 @@ fi
62# ( kill -46 "$(pidof "${STATUSBAR:-dwmblocks}")" >/dev/null 2>&1 ) 2>/dev/null 71# ( kill -46 "$(pidof "${STATUSBAR:-dwmblocks}")" >/dev/null 2>&1 ) 2>/dev/null
63 72
64# Parallelize multiple accounts 73# Parallelize multiple accounts
65for account in $accounts 74for account in $accounts; do
66do
67 syncandnotify & 75 syncandnotify &
68done 76done
69 77
70wait 78wait
71# ( kill -46 "$(pidof "${STATUSBAR:-dwmblocks}")" >/dev/null 2>&1 ) 2>/dev/null 79# ( kill -46 "$(pidof "${STATUSBAR:-dwmblocks}")" >/dev/null 2>&1 ) 2>/dev/null
72 80
73notmuch new 2>/dev/null 81notmuch new 2> /dev/null
74 82
75#Create a touch file that indicates the time of the last run of mailsync 83#Create a touch file that indicates the time of the last run of mailsync
76touch "$HOME/.config/mutt/.mailsynclastrun" 84touch "$HOME/.config/mutt/.mailsynclastrun"