您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

203 行
9.3 KiB

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