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 10 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
6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #!/bin/bash
  2. muttdir="$HOME/.config/mutt/"
  3. changePassword() { \
  4. 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>&- )
  5. dialog --title "Luke's mutt/offlineIMAP password wizard" --passwordbox "Enter the new password for the \"$1\" account." 10 60 2> /tmp/$1
  6. 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)
  7. shred -u /tmp/$1
  8. mv /tmp/$1.gpg ~/.config/mutt/credentials/ ;}
  9. chooseDetect() { for x in $(cat ~/.offlineimaprc | grep "^accounts =" | sed -e 's/accounts =\( \)//g;s/\(,\) /\n/g;'); do detectMailboxes $x; done && detectSuccess ;}
  10. detectWarning() { \
  11. dialog --title "Mailbox detect requirement" --yesno "In order for the mailbox detection system to work, you must have
  12. 1) already have added the email account with this wizard, and
  13. 2) already have run offlineimap at least once to synchronize your mail.
  14. This detection system only works if you already have begun your first sync. If you have, press 'yes' to continue." 12 70 ;}
  15. 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:
  16. gi for move to the Inbox.
  17. gs for move to Sent Mail.
  18. gd for move to Drafts
  19. gS for move to Spam
  20. ga for move to the Archive.
  21. gj for move to Junk
  22. gt for move to Trash
  23. 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 ;}
  24. formatShortcut() { \
  25. while read data; do
  26. echo "macro index,pager g$1 \"<change-folder>$data<enter>\" \"Go to $2.\"" >> "$muttdir"accounts/$3.muttrc
  27. done ;}
  28. detectMailboxes() { \
  29. find ~/.mail/$1 -maxdepth 1 -mindepth 1 -type d | sed -e "s/.*\///g;s/^/=/g" > /tmp/$1_boxes
  30. oneline=$(cat /tmp/$1_boxes | tr "\n" " ")
  31. sed -i "/^mailboxes\|^set spoolfile\|^set record\|^set postponed/d" "$muttdir"accounts/$1.muttrc
  32. echo mailboxes $oneline >> "$muttdir"accounts/$1.muttrc
  33. sed -i "/^macro index,pager g/d" "$muttdir"accounts/$1.muttrc
  34. grep -vi /tmp/$1_boxes -e "trash\|drafts\|sent\|trash\|spam\|junk\|archive\|chat\|old\|new\|gmail\|sms\|call" | sort -n | sed 1q | formatShortcut i inbox $1
  35. grep -i /tmp/$1_boxes -e sent | formatShortcut s sent $1
  36. grep -i /tmp/$1_boxes -e draft | formatShortcut d drafts $1
  37. grep -i /tmp/$1_boxes -e trash | formatShortcut t trash $1
  38. grep -i /tmp/$1_boxes -e spam | formatShortcut S spam $1
  39. grep -i /tmp/$1_boxes -e archive | formatShortcut a archive $1
  40. spoolfile=$(grep -vi /tmp/$1_boxes -e "trash\|drafts\|sent\|trash\|spam\|junk\|archive\|chat\|old\|new\|gmail\|sms\|call" | sort -n | sed 1q | sed -e 's/=/+/g')
  41. record=$(grep -i /tmp/$1_boxes -e sent | sed -e 's/=/+/g')
  42. postponed=$(grep -i /tmp/$1_boxes -e draft | sed -e 's/=/+/g')
  43. echo "set spoolfile = \"$spoolfile\"" >> "$muttdir"accounts/$1.muttrc
  44. echo "set record = \"$record\"" >> "$muttdir"accounts/$1.muttrc
  45. echo "set postponed = \"$postponed\"" >> "$muttdir"accounts/$1.muttrc ;}
  46. # Get all accounts in ~/.offlineimaprc and load into variable `accounts`.
  47. getAccounts() { \
  48. cat ~/.offlineimaprc | grep "^accounts =" | sed -e 's/accounts =\( \)//g;s/\(,\) /\n/g;' | nl --number-format=ln > /tmp/numbered
  49. accounts=()
  50. while read n s ; do
  51. accounts+=($n "$s" off)
  52. done < /tmp/numbered ;}
  53. # Yields a menu of available accounts.
  54. inventory() { \
  55. getAccounts
  56. choices=$(dialog --separate-output --checklist "Select all desired email accounts with <SPACE>." 15 40 16 "${accounts[@]}" 2>&1 >/dev/tty)
  57. if [ -z "$choices" ];
  58. then
  59. clear
  60. else
  61. userchoices=$(IFS="|"; keys="${choices[*]}"; keys="${keys//|/\\|}"; grep -w "${keys}" /tmp/numbered | awk '{print $2}')
  62. fi ;}
  63. removeAccount() { sed -ie "
  64. /Account $1]/,/Account/{//!d}
  65. /Account $1]/d
  66. s/ $1\(,\|$\)//g
  67. s/=$1\(,\|$\)/=/g
  68. s/,$//g
  69. " ~/.offlineimaprc
  70. rm "$muttdir"accounts/$1.muttrc
  71. rm "$muttdir"credentials/$1.gpg
  72. rm -rf "$muttdir"accounts/$1
  73. sed -i '/$1.muttrc/d' "$muttdir"personal.muttrc ;}
  74. manual() { \
  75. imap=$( dialog --inputbox "Insert the IMAP server for your email provider (excluding the port number)" 10 60 3>&1 1>&2 2>&3 3>&- )
  76. iport=$(dialog --inputbox "What is your server's IMAP port number? (Usually 993)" 10 60 3>&1 1>&2 2>&3 3>&-)
  77. smtp=$( dialog --inputbox "Insert the SMTP server for your email provider (excluding the port number)" 10 60 3>&1 1>&2 2>&3 3>&- )
  78. sport=$( dialog --inputbox "What is your server's SMTP port number? (Usually 587 or 465)" 10 60 3>&1 1>&2 2>&3 3>&- ) ;}
  79. 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>&- )
  80. # Check to see if domain is in domain list
  81. serverinfo=$(cat "$muttdir"autoconf/domains.csv | grep -w ^${fulladdr##*@})
  82. if [ -z "$serverinfo" ];
  83. then
  84. manual
  85. else
  86. # Read in server data as variables
  87. IFS=, read service imap iport smtp sport <<EOF
  88. $serverinfo
  89. EOF
  90. fi
  91. 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>&- )
  92. 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>&- )
  93. login=$(dialog --title "Luke's mutt/offlineIMAP autoconfig" --inputbox "Enter your login for the \"$title\" account.\n(If left empty, the full email address will be used instead.)" 10 60 3>&1 1>&2 2>&3 3>&- )
  94. # Sets the repo type and other variables for the sed regex.
  95. if [[ "$service" == "gmail.com" ]];
  96. then
  97. type="Gmail"
  98. delet="remotehost"
  99. else
  100. type="IMAP"
  101. delet="Gmail]\/"
  102. fi
  103. if [[ -z "$login" ]];
  104. then
  105. login=$fulladdr
  106. fi
  107. # The replacements
  108. replacement="
  109. s/\$realname/$realname/g;
  110. s/\$title/$title/g;
  111. s/\$fulladdr/$fulladdr/g;
  112. s/\$imap/$imap/g;
  113. s/\$iport/$iport/g;
  114. s/\$smtp/$smtp/g;
  115. s/\$sport/$sport/g;
  116. s/\$type/$type/g;
  117. s/\$login/$login/g;
  118. /$delet/d"
  119. # Gets the first unused shortcut number in the muttrc and puts it in $idnum.
  120. cat "$muttdir"personal.muttrc | grep i[0-9] | awk '{print $3}' | sed -e 's/i//g' > /tmp/mutt_used
  121. echo -e "1\n2\n3\n4\n5\n6\n7\n8\n9" > /tmp/mutt_all_possible
  122. idnum=$(diff /tmp/mutt_all_possible /tmp/mutt_used | sed -n 2p | awk '{print $2}')
  123. addAccount \
  124. ;}
  125. addAccount() {
  126. # First, adding the encrypted password.
  127. dialog --title "Luke's mutt/offlineIMAP password wizard" --passwordbox "Enter the password for the \"$title\" account." 10 60 2> /tmp/$title
  128. 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)
  129. shred -u /tmp/$title
  130. mv /tmp/$title.gpg ~/.config/mutt/credentials/
  131. # Adding directory structure for cache.
  132. mkdir -p "$muttdir"accounts/$title/cache/bodies
  133. # Creating the offlineimaprc if it doesn't exist already.
  134. if [ ! -f ~/.offlineimaprc ]; then cp "$muttdir"autoconf/offlineimap_header ~/.offlineimaprc; fi
  135. cat "$muttdir"autoconf/offlineimap_profile | sed -e "$replacement" >> ~/.offlineimaprc
  136. mkdir -p ~/.mail/$title
  137. # Add the mutt profile.
  138. cat "$muttdir"autoconf/mutt_profile | sed -e "$replacement" > "$muttdir"accounts/$title.muttrc
  139. # Add a numbered shortcut in the muttrc
  140. echo "macro index,pager i$idnum '<sync-mailbox><enter-command>source "$muttdir"accounts/$title.muttrc<enter><change-folder>!<enter>'" >> "$muttdir"personal.muttrc
  141. # Add to offlineimaprc sync list.
  142. sed -i "s/^accounts =.*[a-zA-Z]$/&, $title/g;s/^accounts =$/accounts = $title/g" ~/.offlineimaprc
  143. # Makes account default if there is no default account.
  144. grep "$muttdir"personal.muttrc -e "^source .*accounts.*" >/dev/null && echo there || \
  145. echo "source ${muttdir}accounts/$title.muttrc" >> "$muttdir"personal.muttrc
  146. dialog --title "Finalizing your account." --msgbox "The account \"$title\" has been added. Now to finalize installation, do the following:
  147. 1) Run offlineimap to start the sync. This will start your mail sync.
  148. 2) After or while running offlineimap, choose the \"autodetect mailboxes\" option, which will finalize your config files based on the directory structure of the downloaded mailbox.
  149. After that, you will be able to open neomutt to your email account." 13 80 ;}
  150. # This is run when a user chooses to add an account.
  151. chooseAdd() { \
  152. mkdir -p "$muttdir"credentials/ "$muttdir"accounts/
  153. 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>&- )
  154. addloop
  155. while : ;
  156. do
  157. dialog --title "Luke's mutt/offlineIMAP password wizard" --yesno "Would you like to add another email account?" 5 60 || break
  158. addloop
  159. done ;}
  160. wipe () { rm $HOME/.offlineimaprc
  161. rm -rf "$muttdir"/accounts
  162. rm -f "$muttdir"credentials/*gpg
  163. rm "$muttdir"personal.muttrc ;}
  164. while : ;
  165. do
  166. choice=$(dialog --title "Luke's mutt/offlineIMAP wizard" --nocancel \
  167. --menu "What would you like to do?" 14 45 7 \
  168. 0 "List all email accounts configured." \
  169. 1 "Add an email account." \
  170. 2 "Auto-detect mailboxes for an account." \
  171. 3 "Change an account's password." \
  172. 4 "Remove an email account." \
  173. 5 "Remove all email accounts." \
  174. 6 "Exit this wizard." \
  175. 3>&1 1>&2 2>&3 3>&1 )
  176. case $choice in
  177. 0) dialog --title "Accounts detected" --msgbox "The following accounts have been detected:
  178. $(grep ~/.offlineimaprc -e "^accounts =" | sed 's/accounts =//g')
  179. " 6 60;;
  180. 1) chooseAdd;;
  181. 2) detectWarning && chooseDetect ;;
  182. 3) inventory && for i in $userchoices; do changePassword $i ; done;;
  183. 4) inventory && for i in $userchoices; do removeAccount $i ; done;;
  184. 5) (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) ;;
  185. 6) clear && break ;;
  186. esac
  187. done