summaryrefslogtreecommitdiffstats
path: root/.local/bin
diff options
context:
space:
mode:
Diffstat (limited to '.local/bin')
-rwxr-xr-x.local/bin/mailsync76
-rwxr-xr-x.local/bin/openfile10
2 files changed, 86 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"
diff --git a/.local/bin/openfile b/.local/bin/openfile
new file mode 100755
index 0000000..cf3c6c3
--- /dev/null
+++ b/.local/bin/openfile
@@ -0,0 +1,10 @@
1#!/bin/sh
2
3# Helps open a file with xdg-open from mutt in a external program without weird side effects.
4tempdir="${XDG_CACHE_HOME:-$HOME/.cache}/mutt-wizard/files"
5file="$tempdir/$(basename "$1")"
6[ "$(uname)" = "Darwin" ] && opener="open" || opener="setsid -f xdg-open"
7mkdir -p "$tempdir"
8cp -f "$1" "$file"
9$opener "$file" >/dev/null 2>&1
10find "${tempdir:?}" -mtime +1 -type f -delete