Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. usage="usage: $(basename "$0") [-hn] ACCOUNT
  4.  
  5. Check an offlineimap account's mail directory for new messages and trigger notification
  6.  
  7. where:
  8. -h show this help text
  9. ACCOUNT the offlineimap account name"
  10.  
  11. : ${1?"Usage: $0 ACCOUNT"}
  12.  
  13. while getopts ':hs:' option; do
  14. case "$option" in
  15. h) echo "$usage"
  16. exit
  17. ;;
  18. \?) printf "illegal option: -%s\n" "$OPTARG" >&2
  19. echo "$usage" >&2
  20. exit 1
  21. ;;
  22. esac
  23. done
  24.  
  25. function clean {
  26. sed "s/^\[/\\\[/g" | sed "s/\"/'/g" | sed 's/\!/❕/g'
  27. }
  28.  
  29. function extract_sender {
  30. grep -o '^From: \(.*\)$' $1 | sed "s/^From: //g" | clean
  31. }
  32. function extract_subject {
  33. grep -o '^Subject: \(.*\)$' $1 | sed "s/^Subject: //g" | perl -pe 'use MIME::Words(decode_mimewords); $_=decode_mimewords($_);' | clean
  34. }
  35.  
  36. function offlineimap_notify {
  37. account=$1
  38. previousmessages=/tmp/offlineimap_${account}_messages
  39. newcount=0
  40. sender=''
  41. subject=''
  42.  
  43. # run OfflineIMAP once, with quiet interface
  44. # offlineimap -o -u quiet -a $account
  45.  
  46. maildirnew="$HOME/mail/$account/INBOX/new"
  47. if [ ! -e "$previousmessages" ] ; then
  48. touch "$previousmessages"
  49. fi
  50.  
  51. # check if the mails weren't already seen/reported before
  52. # and add them to a temporary list of previously seen/reported mails
  53. for file in $maildirnew/*
  54. do
  55. if [ ! -f $file ] ; then
  56. continue
  57. fi
  58.  
  59. filename=$(basename "$file")
  60. if grep -Fxq "$filename" "$previousmessages" ; then
  61. : echo "seen"
  62. else
  63. echo "$filename" >> "$previousmessages"
  64. newcount=$((newcount+1))
  65. if [ -z "$sender" ] ; then
  66. sender="`extract_sender $file`"
  67. fi
  68. if [ -z "$subject" ] ; then
  69. subject=`extract_subject $file`
  70. fi
  71. fi
  72. done
  73.  
  74.  
  75. if [ $newcount -gt 1 ] ; then
  76. title="$newcount new messages for $account 📬 "
  77. subtitle=`echo -e "$sender and others"`
  78. body=`echo -e "$subject"`
  79. else
  80. title="New message for $account ✉️"
  81. subtitle=`echo -e "$sender"`
  82. body=`echo -e "$subject"`
  83. fi
  84.  
  85. if [ $newcount -gt 0 ]
  86. then
  87. # osascript -e 'display notification "'"$body"'" with title "'"$title"'" subtitle "'"$subtitle"'"'
  88. terminal-notifier -title "${title}" -subtitle "${subtitle}" -message "$body" -sender 'org.gnu.Emacs'
  89. fi
  90. }
  91.  
  92. offlineimap_notify $1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement