選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 

149 行
5.3 KiB

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