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 7.1 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #!/bin/bash
  2. muttdir="$HOME/.config/mutt/"
  3. formatShortcut() { \
  4. while read data; do
  5. echo "macro index,pager g$1 \"<change-folder>$data<enter>\" \"Go to $2.\"" >> "$muttdir"accounts/$3.muttrc
  6. done ;}
  7. detectMailboxes() { \
  8. find ~/.mail/$1 -maxdepth 1 -mindepth 1 -type d | sed -e "s/.*\///g;s/^/=/g" > /tmp/$1_boxes
  9. oneline=$(cat /tmp/$1_boxes | tr "\n" " ")
  10. sed -i "/^mailboxes/d" "$muttdir"accounts/$1.muttrc
  11. echo mailboxes $oneline >> "$muttdir"accounts/$1.muttrc
  12. sed -i "/^macro index,pager g/d" "$muttdir"accounts/$1.muttrc
  13. grep -vi /tmp/$1_boxes -e "trash\|drafts\|sent\|trash\|spam\|junk\|archive" | sort -n | sed 1q | formatShortcut i inbox $1
  14. grep -i /tmp/$1_boxes -e sent | formatShortcut s sent $1
  15. grep -i /tmp/$1_boxes -e trash | formatShortcut t trash $1
  16. grep -i /tmp/$1_boxes -e spam | formatShortcut S spam $1
  17. grep -i /tmp/$1_boxes -e draft | formatShortcut d drafts $1
  18. grep -i /tmp/$1_boxes -e archive | formatShortcut a archive $1 ;}
  19. # Sees what accounts have been generated bny the wizard
  20. # by checking ~/.offlineimap and yields a menu of them.
  21. inventory() { \
  22. cat ~/.offlineimaprc | grep "^accounts =" | sed -e 's/accounts =\( \)//g;s/\(,\) /\n/g;' | nl --number-format=ln > /tmp/numbered
  23. accounts=()
  24. while read n s ; do
  25. accounts+=($n "$s" off)
  26. done < /tmp/numbered
  27. choices=$(dialog --separate-output --checklist "Choose an email account." 22 76 16 "${accounts[@]}" 2>&1 >/dev/tty)
  28. if [ -z "$choices" ];
  29. then
  30. clear
  31. else
  32. userchoices=$(IFS="|"; keys="${choices[*]}"; keys="${keys//|/\\|}"; grep -w "${keys}" /tmp/numbered | awk '{print $2}')
  33. fi ;}
  34. removeAccount() { sed -ie "
  35. /Account $1]/,/Account/{//!d}
  36. /Account $1]/d
  37. s/ $1\(,\|$\)//g
  38. s/=$1\(,\|$\)/=/g
  39. s/,$//g
  40. " ~/.offlineimaprc
  41. rm "$muttdir"accounts/$1.muttrc
  42. rm "$muttdir"credentials/$1.gpg
  43. rm -rf "$muttdir"accounts/$1
  44. sed -i '/$1.muttrc/d' "$muttdir"personal.muttrc ;}
  45. manual() { \
  46. imap=$( dialog --inputbox "Insert the IMAP server for your email provider (excluding the port number)" 10 60 3>&1 1>&2 2>&3 3>&- )
  47. iport=$(dialog --inputbox "What is your server's IMAP port number? (Usually 993)" 10 60 3>&1 1>&2 2>&3 3>&-)
  48. smtpserver=$( dialog --inputbox "Insert the SMTP server for your email provider (excluding the port number)" 10 60 3>&1 1>&2 2>&3 3>&- )
  49. sport=$( dialog --inputbox "What is your server's SMTP port number? (Usually 587 or 465)" 10 60 3>&1 1>&2 2>&3 3>&- ) ;}
  50. 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>&- )
  51. # Check to see if domain is in domain list
  52. serverinfo=$(cat "$muttdir"autoconf/domains.csv | grep -w ^${fulladdr##*@})
  53. if [ -z "$serverinfo" ];
  54. then
  55. manual
  56. else
  57. # Read in server data as variables
  58. IFS=, read service imap iport smtp sport spoolfile postponed record <<EOF
  59. $serverinfo
  60. EOF
  61. fi
  62. 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>&- )
  63. 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>&- )
  64. # Sets the repo type and other variables for the sed regex.
  65. if [[ "$service" == "gmail.com" ]];
  66. then
  67. type="Gmail"
  68. delet="remotehost"
  69. else
  70. type="IMAP"
  71. delet="Gmail]\/"
  72. fi
  73. # The replacements
  74. replacement="
  75. s/\$realname/$realname/g;
  76. s/\$title/$title/g;
  77. s/\$fulladdr/$fulladdr/g;
  78. s/\$imap/$imap/g;
  79. s/\$iport/$iport/g;
  80. s/\$smtp/$smtp/g;
  81. s/\$sport/$sport/g;
  82. s/\$spoolfile/$spoolfile/g;
  83. s/\$postponed/$postponed/g;
  84. s/\$record/$record/g;
  85. s/\$type/$type/g;
  86. /$delet/d"
  87. # Gets the first unused shortcut number in the muttrc and puts it in $idnum.
  88. cat "$muttdir"personal.muttrc | grep i[0-9] | awk '{print $3}' | sed -e 's/i//g' > /tmp/mutt_used
  89. echo -e "1\n2\n3\n4\n5\n6\n7\n8\n9" > /tmp/mutt_all_possible
  90. idnum=$(diff /tmp/mutt_all_possible /tmp/mutt_used | sed -n 2p | awk '{print $2}')
  91. addAccount \
  92. ;}
  93. addAccount() {
  94. # First, adding the encrypted password.
  95. dialog --title "Luke's mutt/offlineIMAP password wizard" --passwordbox "Enter the password for the \"$title\" account." 10 60 2> /tmp/$title
  96. gpg -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 GPG2 and you thus need to symlink /usr/bin/gpg to /usr/bin/gpg2." 7 60 && break)
  97. shred -u /tmp/$title
  98. mv /tmp/$title.gpg ~/.config/mutt/credentials/
  99. # Creating the offlineimaprc if it doesn't exist already.
  100. if [ ! -f ~/.offlineimaprc ]; then cp "$muttdir"autoconf/offlineimap_header ~/.offlineimaprc; fi
  101. cat "$muttdir"autoconf/offlineimap_profile | sed -e "$replacement" >> ~/.offlineimaprc
  102. # Add the mutt profile.
  103. cat "$muttdir"autoconf/mutt_profile | sed -e "$replacement" > "$muttdir"accounts/$title.muttrc
  104. # Add a numbered shortcut in the muttrc
  105. echo "macro index,pager i$idnum '<sync-mailbox><enter-command>source "$muttdir"accounts/$title.muttrc<enter><change-folder>!<enter>'" >> "$muttdir"personal.muttrc
  106. # Adding directory structure for cache.
  107. mkdir -p "$muttdir"accounts/$title/cache/bodies
  108. # Add to offlineimaprc sync list.
  109. sed -i "s/^accounts =.*[a-zA-Z]$/&, $title/g;s/^accounts =$/accounts = $title/g" ~/.offlineimaprc
  110. # Makes account default if there is no default account.
  111. grep "$muttdir"personal.muttrc -e "^source .*accounts.*" >/dev/null && echo there || \
  112. echo "source ${muttdir}accounts/$title.muttrc" >> "$muttdir"personal.muttrc ;}
  113. # This is run when a user chooses to add an account.
  114. addChosen() { \
  115. mkdir -p "$muttdir"credentials/ "$muttdir"accounts/
  116. 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>&- )
  117. addloop
  118. while : ;
  119. do
  120. dialog --title "Luke's mutt/offlineIMAP password wizard" --yesno "Would you like to add another email account?" 10 60 || break
  121. addloop
  122. done ;}
  123. wipe () { rm $HOME/.offlineimaprc
  124. rm -rf "$muttdir"/accounts
  125. rm -f "$muttdir"credentials/*gpg
  126. rm "$muttdir"personal.muttrc ;}
  127. while : ;
  128. do
  129. choice=$(dialog --title "Luke's mutt/offlineIMAP wizard" \
  130. --menu "What would you like to do?" 13 45 6 \
  131. 0 "List all email accounts configured." \
  132. 1 "Add an email account." \
  133. 2 "Auto-detect mailboxes for an account." \
  134. 3 "Remove an email account." \
  135. 4 "Remove all email accounts." \
  136. 5 "Exit this wizard." \
  137. 3>&1 1>&2 2>&3 3>&1 )
  138. case $choice in
  139. 0) dialog --title "Accounts detected" --msgbox "The following accounts have been detected:
  140. $(grep ~/.offlineimaprc -e "^accounts =" | sed 's/accounts =//g')
  141. " 6 60;;
  142. 1) addChosen;;
  143. 2) inventory && for i in $userchoices; do detectMailboxes $1 ; done &&exit;;
  144. 3) inventory && for i in $userchoices; do removeAccount $i ; done;;
  145. 4) (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) ;;
  146. 5) clear && break
  147. esac
  148. done