You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mutt-wizard.sh 12 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #!/usr/bin/env bash
  2. if [[ "$(uname)" == "Darwin" ]]
  3. then
  4. os=".macos"
  5. else
  6. os=""
  7. fi
  8. muttdir="$HOME/.config/mutt/"
  9. createMailboxes() { \
  10. tmpdir=$(mktemp -d)
  11. offlineimap --info -a $1 2&> "$tmpdir"/log
  12. sed -n '/^Folderlist/,/^Folderlist/p' "$tmpdir"/log |
  13. grep "^ " | sed -e "s/\//./g;s/(.*//g;s/^ //g" > "$tmpdir"/lognew
  14. while read box; do mkdir -p "$HOME/.mail/$1/$box"; done <"$tmpdir"/lognew ;}
  15. chooseSync() { (cat /var/run/crond.pid && testSync) || dialog --msgbox "No cronjob manager detected. Please install one and return to enable automatic mailsyncing" 10 60 ;}
  16. testSync() { (crontab -l | grep .config/mutt/etc/mailsync && removeSync) || addSync ;}
  17. addSync() { min=$(dialog --inputbox "How many minutes should be between mail syncs?" 8 60 3>&1 1>&2 2>&3 3>&-)
  18. (crontab -l; echo "*/$min * * * * eval \"export $(egrep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -u $LOGNAME -x i3)/environ)\"; $HOME/.config/mutt/etc/mailsync.sh") | crontab - &&
  19. dialog --msgbox "Cronjob successfully added. Remember you may need to restart or tell systemd/etc. to start your cron manager for this to take effect." 7 60 ;}
  20. removeSync() { ((crontab -l | sed -e '/.config\/mutt\/etc\/mailsync/d') | crontab - >/dev/null) && dialog --msgbox "Cronjob successfully removed. To reactivate, select this option again." 6 60 ;}
  21. changePassword() { \
  22. gpgemail=$( dialog --title "Luke's mutt/offlineIMAP password wizard" --inputbox "Insert the email address with which you originally created your GPG key pair. This is NOT necessarily the email you want to configure." 10 60 3>&1 1>&2 2>&3 3>&- )
  23. dialog --title "Luke's mutt/offlineIMAP password wizard" --passwordbox "Enter the new password for the \"$1\" account." 10 60 2> /tmp/$1
  24. gpg2 -r $gpgemail --encrypt /tmp/$1 || (dialog --title "GPG decryption failed." --msgbox "GPG decryption failed. This is either because you do not have a GPG key pair or because your distro uses GPG1 and you thus need to symlink /usr/bin/gpg2 to /usr/bin/gpg." 7 60 && break)
  25. shred -u /tmp/$1
  26. mv /tmp/$1.gpg ~/.config/mutt/credentials/
  27. dialog --title "Finalizing your account." --infobox "The account \"$title\"'s password has been changed. Now attempting to configure mail directories...
  28. This may take several seconds..." 10 70
  29. createMailboxes $title || (clear && exit)
  30. detectMailboxes $title
  31. dialog --title "Password changed." --msgbox "Your "$fulladdr" password has been changed. To start the download of your mail, you can manually run \`offlineimap -a $title\` in a terminal. The first sync may take some time depending on the amount of your mail." 8 60 ;}
  32. chooseDetect() { for x in $(cat ~/.offlineimaprc | grep "^accounts =" | sed -e 's/accounts =\( \)//g;s/\(,\) /\n/g;'); do detectMailboxes $x; done && detectSuccess ;}
  33. detectWarning() { \
  34. dialog --title "Mailbox detect requirement" --yesno "In order for the mailbox detection system to work, you must have
  35. 1) already have added the email account with this wizard, and
  36. 2) already have run offlineimap at least once to synchronize your mail.
  37. This detection system only works if you already have begun your first sync. If you have, press 'yes' to continue." 12 70 ;}
  38. detectSuccess() { dialog --title "Mailbox detect complete." --msgbox "The script has now smartly detected your different mailboxes and has enabled them in the sidebar and given you keyboard shortcuts as below:
  39. gi for move to the Inbox.
  40. gs for move to Sent Mail.
  41. gd for move to Drafts
  42. gS for move to Spam
  43. ga for move to the Archive.
  44. gj for move to Junk
  45. gt for move to Trash
  46. These shortcuts will only work if your email system does have that particular folder (i.e. if your email system has a Junk folder, but not a Trash folder, 'gt' will not work, etc." 20 60 ;}
  47. formatShortcut() { \
  48. while read data; do
  49. echo "macro index,pager g$1 \"<change-folder>$data<enter>\" \"Go to $2.\"" >> "$muttdir"accounts/$3.muttrc
  50. done ;}
  51. gen_delim() { \
  52. delim="="
  53. for i in `seq $(( $1 - 1 ))`
  54. do
  55. delim="$delim-"
  56. done
  57. echo $delim ;}
  58. detectMailboxes() { \
  59. find ~/.mail/$1 -maxdepth 1 -mindepth 1 -type d | sed -e "s/.*\///g;s/^/=/g" > /tmp/$1_boxes
  60. sidebar_width=$(sed -n -e '/^set sidebar_width/p' "$muttdir"/muttrc | awk -F'=' '{print $2}')
  61. delim=$(gen_delim $sidebar_width)
  62. oneline=$(cat /tmp/$1_boxes | sed -e "s/^\|$/\"/g" | tr "\n" " ")
  63. oneline="=$1 $delim $oneline"
  64. sed -i "/^mailboxes\|^set record\|^set postponed/d" "$muttdir"accounts/$1.muttrc
  65. echo mailboxes $oneline >> "$muttdir"accounts/$1.muttrc
  66. sed -i "/^macro index,pager g/d" "$muttdir"accounts/$1.muttrc
  67. grep -i /tmp/$1_boxes -e inbox | sed 1q | formatShortcut i inbox $1
  68. grep -i /tmp/$1_boxes -e sent | sed 1q | formatShortcut s sent $1
  69. grep -i /tmp/$1_boxes -e draft | sed 1q | formatShortcut d drafts $1
  70. grep -i /tmp/$1_boxes -e trash | sed 1q | formatShortcut t trash $1
  71. grep -i /tmp/$1_boxes -e spam | sed 1q | formatShortcut S spam $1
  72. grep -i /tmp/$1_boxes -e archive | sed 1q | formatShortcut a archive $1
  73. spoolfile=$(grep -i /tmp/$1_boxes -e inbox | sed -e 's/=/+/g' | sed 1q)
  74. record=$(grep -i /tmp/$1_boxes -e sent | sed -e 's/=/+/g' | sed 1q)
  75. postponed=$(grep -i /tmp/$1_boxes -e draft | sed -e 's/=/+/g' | sed 1q)
  76. trash=$(grep -i /tmp/$1_boxes -e trash | sed -e 's/=/+/g' | sed 1q)
  77. echo "set spoolfile = \"$spoolfile\"" >> "$muttdir"accounts/$1.muttrc
  78. echo "set record = \"$record\"" >> "$muttdir"accounts/$1.muttrc
  79. echo "set postponed = \"$postponed\"" >> "$muttdir"accounts/$1.muttrc
  80. echo "set trash = \"$trash\"" >> "$muttdir"accounts/$1.muttrc ;}
  81. # Get all accounts in ~/.offlineimaprc and load into variable `accounts`.
  82. getAccounts() { \
  83. cat ~/.offlineimaprc | grep "^accounts =" | sed -e 's/accounts =\( \)//g;s/\(,\) /\n/g;' | nl --number-format=ln > /tmp/numbered
  84. accounts=()
  85. while read n s ; do
  86. accounts+=($n "$s" off)
  87. done < /tmp/numbered ;}
  88. # Yields a menu of available accounts.
  89. inventory() { \
  90. getAccounts
  91. choices=$(dialog --separate-output --checklist "Select all desired email accounts with <SPACE>." 15 40 16 "${accounts[@]}" 2>&1 >/dev/tty)
  92. if [ -z "$choices" ];
  93. then
  94. clear
  95. else
  96. userchoices=$(IFS="|"; keys="${choices[*]}"; keys="${keys//|/\\|}"; grep -w "${keys}" /tmp/numbered | awk '{print $2}')
  97. fi ;}
  98. removeAccount() { sed -ie "
  99. /Account $1]/,/Account/{//!d}
  100. /Account $1]/d
  101. s/ $1\(,\|$\)//g
  102. s/=$1\(,\|$\)/=/g
  103. s/,$//g
  104. " ~/.offlineimaprc
  105. rm "$muttdir"accounts/$1.muttrc
  106. rm "$muttdir"credentials/$1.gpg
  107. rm -rf "$muttdir"accounts/$1
  108. sed -i "/$1.muttrc/d" "$muttdir"personal.muttrc ;}
  109. manual() { \
  110. imap=$( dialog --inputbox "Insert the IMAP server for your email provider (excluding the port number)" 10 60 3>&1 1>&2 2>&3 3>&- )
  111. iport=$(dialog --inputbox "What is your server's IMAP port number? (Usually 993)" 10 60 3>&1 1>&2 2>&3 3>&-)
  112. smtp=$( dialog --inputbox "Insert the SMTP server for your email provider (excluding the port number)" 10 60 3>&1 1>&2 2>&3 3>&- )
  113. sport=$( dialog --inputbox "What is your server's SMTP port number? (Usually 587 or 465)" 10 60 3>&1 1>&2 2>&3 3>&- ) ;}
  114. addloop() { fulladdr=$( dialog --title "Luke's mutt/offlineIMAP autoconfig" --inputbox "Insert the full email address for the account you want to configure." 10 60 3>&1 1>&2 2>&3 3>&- )
  115. # Check to see if domain is in domain list
  116. serverinfo=$(cat "$muttdir"autoconf/domains.csv | grep -w ^${fulladdr##*@})
  117. if [ -z "$serverinfo" ];
  118. then
  119. manual
  120. else
  121. # Read in server data as variables
  122. IFS=, read service imap iport smtp sport <<EOF
  123. $serverinfo
  124. EOF
  125. fi
  126. realname=$( dialog --title "Luke's mutt/offlineIMAP autoconfig" --inputbox "Enter the full name you'd like to be identified by on this email account." 10 60 3>&1 1>&2 2>&3 3>&- )
  127. title=$( dialog --title "Luke's mutt/offlineIMAP autoconfig" --inputbox "Give a short, one-word name for this email account that will differentiate it from other email accounts." 10 60 3>&1 1>&2 2>&3 3>&- )
  128. login=$(dialog --title "Luke's mutt/offlineIMAP autoconfig" --inputbox "If you have a username for the \"$title\" account which is different from your email address, enter it here. Otherwise leave this prompt blank." 10 60 3>&1 1>&2 2>&3 3>&- )
  129. # Sets the repo type and other variables for the sed regex.
  130. if [[ "$service" == "gmail.com" ]];
  131. then
  132. type="Gmail"
  133. delet="remotehost"
  134. else
  135. type="IMAP"
  136. delet="Gmail]\/"
  137. fi
  138. if [[ -z "$login" ]];
  139. then
  140. login=$fulladdr
  141. fi
  142. # The replacements
  143. replacement="
  144. s/\$realname/$realname/g;
  145. s/\$title/$title/g;
  146. s/\$fulladdr/$fulladdr/g;
  147. s/\$imap/$imap/g;
  148. s/\$iport/$iport/g;
  149. s/\$smtp/$smtp/g;
  150. s/\$sport/$sport/g;
  151. s/\$type/$type/g;
  152. s/\$login/$login/g;
  153. /$delet/d"
  154. # Gets the first unused shortcut number in the muttrc and puts it in $idnum.
  155. cat "$muttdir"personal.muttrc | grep i[0-9] | awk '{print $3}' | sed -e 's/i//g' > /tmp/mutt_used
  156. echo -e "1\n2\n3\n4\n5\n6\n7\n8\n9" > /tmp/mutt_all_possible
  157. idnum=$(diff /tmp/mutt_all_possible /tmp/mutt_used | sed -n 2p | awk '{print $2}')
  158. addAccount \
  159. ;}
  160. addAccount() {
  161. # First, adding the encrypted password.
  162. dialog --title "Luke's mutt/offlineIMAP password wizard" --passwordbox "Enter the password for the \"$title\" account." 10 60 2> /tmp/$title
  163. gpg2 -r $gpgemail --encrypt /tmp/$title || (dialog --title "GPG decryption failed." --msgbox "GPG decryption failed. This is either because you do not have a GPG key pair or because your distro uses GPG1 and you thus need to symlink /usr/bin/gpg2 to /usr/bin/gpg." 7 60 && break)
  164. shred -u /tmp/$title
  165. mv /tmp/$title.gpg ~/.config/mutt/credentials/
  166. # Adding directory structure for cache.
  167. mkdir -p "$muttdir"accounts/$title/cache/bodies
  168. # Creating the offlineimaprc if it doesn't exist already.
  169. if [ ! -f ~/.offlineimaprc ]; then cp "$muttdir"autoconf/offlineimap_header"$os" ~/.offlineimaprc; fi
  170. cat "$muttdir"autoconf/offlineimap_profile"$os" | sed -e "$replacement" >> ~/.offlineimaprc
  171. mkdir -p ~/.mail/$title
  172. # Add the mutt profile.
  173. cat "$muttdir"autoconf/mutt_profile | sed -e "$replacement" > "$muttdir"accounts/$title.muttrc
  174. # Add a numbered shortcut in the muttrc
  175. echo "macro index,pager i$idnum '<sync-mailbox><enter-command>source "$muttdir"accounts/$title.muttrc<enter><change-folder>!<enter>;<check-stats>'" >> "$muttdir"personal.muttrc
  176. # Add to offlineimaprc sync list.
  177. sed -i.bu "s/^accounts =.*[a-zA-Z]$/&, $title/g;s/^accounts =\s*$/accounts = $title/g" ~/.offlineimaprc && rm ~/.offlineimaprc.bu
  178. # Makes account default if there is no default account.
  179. grep "$muttdir"personal.muttrc -e "^source .*accounts.*" >/dev/null && echo there || \
  180. echo "source ${muttdir}accounts/$title.muttrc" >> "$muttdir"personal.muttrc
  181. dialog --title "Finalizing your account." --infobox "The account \"$title\" has been added. Now attempting to configure mail directories...
  182. This may take several seconds..." 10 70
  183. createMailboxes $title || (clear && exit)
  184. detectMailboxes $title
  185. dialog --title "Account added." --msgbox "Your "$fulladdr" account has been added. To start the download of your mail, you can manually run \`offlineimap -a $title\` in a terminal. The first sync may take some time depending on the amount of your mail." 8 60 ;}
  186. # This is run when a user chooses to add an account.
  187. chooseAdd() { \
  188. mkdir -p "$muttdir"credentials/ "$muttdir"accounts/
  189. gpgemail=$( dialog --title "Luke's mutt/offlineIMAP password wizard" --inputbox "Insert the email address with which you originally created your GPG key pair. This is NOT necessarily the email you want to configure." 10 60 3>&1 1>&2 2>&3 3>&- )
  190. addloop
  191. while : ;
  192. do
  193. dialog --title "Luke's mutt/offlineIMAP password wizard" --yesno "Would you like to add another email account?" 5 60 || break
  194. addloop
  195. done ;}
  196. wipe () { rm $HOME/.offlineimaprc
  197. rm -rf "$muttdir"/accounts
  198. rm -f "$muttdir"credentials/*gpg
  199. rm "$muttdir"personal.muttrc ;}
  200. while : ;
  201. do
  202. choice=$(dialog --title "Luke's mutt/offlineIMAP wizard" --nocancel \
  203. --menu "What would you like to do?" 15 45 8 \
  204. 0 "List all email accounts configured." \
  205. 1 "Add an email account." \
  206. 2 "Enable/disable autosync." \
  207. 3 "Redetect mailboxes." \
  208. 4 "Change an account's password." \
  209. 5 "Remove an email account." \
  210. 6 "Remove all email accounts." \
  211. 7 "Exit this wizard." \
  212. 3>&1 1>&2 2>&3 3>&1 )
  213. case $choice in
  214. 0) dialog --title "Accounts detected" --msgbox "The following accounts have been detected:
  215. $(grep ~/.offlineimaprc -e "^accounts =" | sed 's/accounts =//g')
  216. " 6 60;;
  217. 1) chooseAdd;;
  218. 2) chooseSync;;
  219. 3) detectWarning && chooseDetect;;
  220. 4) inventory && for i in $userchoices; do changePassword $i ; done;;
  221. 5) inventory && for i in $userchoices; do removeAccount $i ; done;;
  222. 6) (dialog --defaultno --title "Wipe all custom neomutt/offlineIMAP settings?" --yesno "Would you like to wipe all of the mutt/offlineIMAP settings generated by the system?" 6 60 && wipe) ;;
  223. 7) clear && break ;;
  224. *) echo "Unable to read response from dialog. Exiting." >&2; exit 2
  225. esac
  226. done