diff options
author | Yigit Sever | 2021-11-04 12:25:04 +0300 |
---|---|---|
committer | Yigit Sever | 2021-11-04 12:25:04 +0300 |
commit | a0bb023d782dd2d8703670d560b5ccd8d25d640e (patch) | |
tree | 1c11a649e2201e1458c4011894086f49da5ded15 /.local/bin/mailsync | |
parent | 93cdb13eada4e803fb4db71fb581d9e4a03d2119 (diff) | |
download | dotfiles-a0bb023d782dd2d8703670d560b5ccd8d25d640e.tar.gz dotfiles-a0bb023d782dd2d8703670d560b5ccd8d25d640e.tar.bz2 dotfiles-a0bb023d782dd2d8703670d560b5ccd8d25d640e.zip |
bins: shfmt, weasel suite, spark
Diffstat (limited to '.local/bin/mailsync')
-rwxr-xr-x | .local/bin/mailsync | 32 |
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) |
6 | pgrep -u "${USER:=$LOGNAME}" >/dev/null || { echo "$USER not logged in; sync will not run."; exit ;} | 6 | pgrep -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 |
8 | pgrep -x mbsync >/dev/null && { notify-send --app-name="mailsync" "mbsync is already running" "you should fix this" ; exit ;} | 11 | pgrep -x mbsync > /dev/null && { |
12 | notify-send --app-name="mailsync" "mbsync is already running" "you should fix this" | ||
13 | exit | ||
14 | } | ||
9 | 15 | ||
10 | if [[ -f "$HOME/.local/share/.dnd" ]]; then | 16 | if [[ -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 | |||
13 | fi | 19 | fi |
14 | 20 | ||
15 | # Checks for internet connection and set notification script. | 21 | # Checks for internet connection and set notification script. |
16 | ping -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 ;} | 22 | ping -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 || { |
17 | command -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 | } | ||
26 | command -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: |
20 | export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus | 29 | export 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. |
27 | if [ "$(uname)" = "Darwin" ]; then | 36 | if [ "$(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\""; } |
30 | else | 39 | else |
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"; } |
33 | fi | 42 | fi |
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 |
65 | for account in $accounts | 74 | for account in $accounts; do |
66 | do | ||
67 | syncandnotify & | 75 | syncandnotify & |
68 | done | 76 | done |
69 | 77 | ||
70 | wait | 78 | wait |
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 | ||
73 | notmuch new 2>/dev/null | 81 | notmuch 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 |
76 | touch "$HOME/.config/mutt/.mailsynclastrun" | 84 | touch "$HOME/.config/mutt/.mailsynclastrun" |