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.
 
 
 
 

195 lines
8.4 KiB

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