Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

260 rader
10 KiB

  1. #!/bin/sh
  2. muttdir="$HOME/.config/mutt" # Main mutt config location
  3. accdir="$muttdir/accounts" # Directory for account settings
  4. maildir="$HOME/.local/share/mail" # Location of mail storage
  5. creddir="$HOME/.local/share/muttwizard" # Location of encrypted credentials
  6. bindir="$HOME/.config/mutt/bin" # Location of scripts run by mutt or the wizard
  7. namere="^[a-z_][a-z0-9_-]*$" # Regex to ensure viable username
  8. emailre=".\+@.\+\\..\+" # Regex to confirm valid email address
  9. gpgemail="$(cat "$creddir/gpgemail")" # Get previously set gpg email address
  10. tmpdir="$(mktemp -d)"
  11. GPG="gpg"; command -v gpg >/dev/null || GPG="gpg2" # Ensure proper gpg command
  12. mkdir -p "$maildir" "$creddir" "$bindir"
  13. # Get certificate location depending on OS. Linux is elsewhere condition.
  14. case "$(uname)" in
  15. "Darwin") sslcert="/usr/local/etc/openssl/cert.pem" ;;
  16. *) sslcert="/etc/ssl/certs/ca-certificates.crt" ;;
  17. esac
  18. getprofiles() { \
  19. offlineimap_header="[general]
  20. accounts =
  21. starttls = yes
  22. ssl = true
  23. pythonfile = $bindir/imappwd.py
  24. "
  25. offlineimap_profile="
  26. [Account $title]
  27. localrepository = $title-local
  28. remoterepository = $title-remote
  29. [Repository $title-remote]
  30. auth_mechanisms = LOGIN
  31. type = $type
  32. remoteuser = $login
  33. remotepasseval = mailpasswd(\"$title\")
  34. remoteport = $iport
  35. sslline = $sslcert
  36. $ifgoogleline
  37. [Repository $title-local]
  38. type = Maildir
  39. localfolders = $maildir/$title
  40. "
  41. msmtp_header="defaults
  42. auth on
  43. tls on
  44. tls_trust_file $sslcert
  45. logfile ~/.msmtp.log
  46. "
  47. msmtp_profile="
  48. account $title
  49. host $smtp
  50. port $sport
  51. from $login
  52. user $login
  53. passwordeval \"$GPG -d --quiet --for-your-eyes-only --no-tty $creddir/$title.gpg | sed -e '\$a\\'\"
  54. "
  55. mutt_profile="# vim: filetype=neomuttrc
  56. # muttrc file for account $title
  57. set realname = \"$realname\"
  58. set from = \"$fulladdr\"
  59. set sendmail = \"/usr/bin/msmtp -a $title\"
  60. set folder = \"$maildir/$title\"
  61. set header_cache = $accdir/$title/cache/headers
  62. set message_cachedir = $accdir/$title/cache/bodies
  63. set certificate_file = $accdir/$title/certificates
  64. source \"$bindir/getmuttpass $title |\"
  65. alias me $realname <$fulladdr>
  66. set mbox_type = Maildir
  67. set ssl_starttls = yes
  68. set ssl_force_tls = yes
  69. bind index,pager gg noop
  70. bind index,pager g noop
  71. bind index,pager M noop
  72. bind index,pager C noop
  73. bind index gg first-entry
  74. unmailboxes *
  75. "
  76. offlineimap_profile="
  77. [Account $title]
  78. localrepository = $title-local
  79. remoterepository = $title-remote
  80. [Repository $title-remote]
  81. auth_mechanisms = LOGIN
  82. type = $type
  83. remoteuser = $login
  84. remotepasseval = mailpasswd(\"$title\")
  85. remotehost = $imap
  86. remoteport = $iport
  87. folderfilter = lambda foldername: foldername not in ['[Gmail]/All Mail']
  88. sslcacertfile = /etc/ssl/certs/ca-certificates.crt
  89. [Repository $title-local]
  90. type = Maildir
  91. localfolders = $maildir/$title
  92. "
  93. }
  94. userexit() { clear; exit ;}
  95. inventory() { \
  96. grep "^accounts =" "$HOME/.config/offlineimap/config" | sed 's/accounts =\( \)//g;s/\(,\) /\n/g;' | nl --number-format=ln > "$tmpdir/numbered"
  97. accounts=()
  98. while read n s ; do
  99. accounts+=($n "$s" off)
  100. done < "$tmpdir/numbered"
  101. choices=$(dialog --separate-output --checklist "Select all desired email accounts with <SPACE>." 15 40 16 "${accounts[@]}" 2>&1 >/dev/tty)
  102. if [ -z "$choices" ];
  103. then
  104. clear
  105. else
  106. userchoices=$(IFS="|"; keys="${choices[*]}"; keys="${keys//|/\\|}"; grep -w "${keys}" "$tmpdir/numbered" | awk '{print $2}')
  107. fi ;}
  108. chooseadd() { \
  109. 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>&- ) || userexit
  110. while ! echo "$fulladdr" | grep "emailre" >/dev/null; do
  111. fulladdr=$(dialog --no-cancel --title "Luke's mutt/offlineIMAP autoconfig" --inputbox "That's not a valid email address. Please input the entire address." 10 60 3>&1 1>&2 2>&3 3>&1) || userexit
  112. done
  113. domain="$(echo "$fulladdr" | sed "s/.*@//")"
  114. serverinfo="$(grep "$domain" "$muttdir/domains.csv")"
  115. if [ -z "$serverinfo" ]; then
  116. imap="$( dialog --inputbox "Insert the IMAP server for your email provider (excluding the port number)" 10 60 3>&1 1>&2 2>&3 3>&- )"
  117. iport="$(dialog --inputbox "What is your server's IMAP port number? (Usually 993)" 10 60 3>&1 1>&2 2>&3 3>&-)"
  118. smtp="$(dialog --inputbox "Insert the SMTP server for your email provider (excluding the port number)" 10 60 3>&1 1>&2 2>&3 3>&- )"
  119. sport="$(dialog --inputbox "What is your server's SMTP port number? (Usually 587 or 465)" 10 60 3>&1 1>&2 2>&3 3>&- )"
  120. else
  121. IFS=, read service imap iport smtp sport <<EOF
  122. $serverinfo
  123. EOF
  124. fi
  125. 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>&- ) || userexit
  126. 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>&1) || userexit
  127. while ! echo "$title" | grep "$namere" >/dev/null; do
  128. title=$(dialog --no-cancel --title "Luke's mutt/offlineIMAP autoconfig" --inputbox "Account title not valid. Give a username beginning with a letter, with only lowercase letters, - or _." 10 60 3>&1 1>&2 2>&3 3>&1) || userexit
  129. done
  130. login=$(dialog --title "Luke's mutt/offlineIMAP autoconfig" --inputbox "If you have a username for the \"$title\" account which is different from your email address, enter it here. Otherwise leave this prompt blank." 10 60 3>&1 1>&2 2>&3 3>&- ) || userexit
  131. [ -z "$login" ] && login="$fulladdr"
  132. # service
  133. # ifgoogleline
  134. grep "i[0-9]" "$muttdir/personal.muttrc" | awk '{print $3}' | sed -e 's/i//g' > "$tmpdir/mutt_used"
  135. printf "1\\n2\\n3\\n4\\n5\\n6\\n7\\n8\\n9" > "$tmpdir/mutt_all_possible"
  136. idnum=$(diff "$tmpdir/mutt_all_possible" "$tmpdir/mutt_used" | sed -n 2p | awk '{print $2}')
  137. getpass "$title" || userexit
  138. getprofiles
  139. mkdir -p "$HOME/.config/offlineimap/" "$HOME/.config/msmtp"
  140. [ ! -f "$HOME/.config/offlineimap/config" ] && echo "$offlineimap_header" > "$HOME/.config/offlineimap/config"
  141. [ ! -f "$HOME/.config/msmtp/config" ] && echo "$msmtp_header" > "$HOME/.config/msmtp/config"
  142. echo "$offlineimap_profile" >> "$HOME/.config/offlineimap/config"
  143. echo "$msmtp_profile" >> "$HOME/.config/msmtp/config"
  144. echo "$mutt_profile" > "$accdir/$title.muttrc"
  145. }
  146. getpass() { \
  147. dialog --title "Luke's mutt/offlineIMAP password wizard" --passwordbox "Enter the password for the \"$1\" account." 10 60 2> "$tmpdir/$1"
  148. "$GPG" -r "$gpgemail" --encrypt "$tmpdir/$1"
  149. shred -u "$tmpdir/$1"
  150. mv "$tmpdir/$1.gpg" "$creddir"
  151. }
  152. askgpg() { \
  153. gpgemail=$(dialog --title "Luke's mutt/offlineIMAP wizard" --inputbox "To safely encrypt passwords, mutt-wizard requires that you have a GPG public/private key pair.\n\nPlease insert the email address for your key pair here, or generate one first with \`$GPG --full-gen-key\` and come back." 12 60 3>&1 1>&2 2>&3 3>&- ) || userexit
  154. while ! echo "$gpgemail" | grep "$emailre"; do
  155. gpgemail=$(dialog --title "Luke's mutt/offlineIMAP wizard" --inputbox "That's not a valid email address. Please input the entire address." 10 60 3>&1 1>&2 2>&3 3>&1) || userexit
  156. done
  157. if "$GPG" -K | grep "<$gpgemail>"; then
  158. echo "$gpgemail" > "$creddir/gpgemail"
  159. else
  160. dialog --title "Luke's mutt/offlineIMAP wizard" --msgbox "You do not appear to have a private key with that email address.\n\nPlease be sure to create your key with \`$GPG --full-gen-key\` and come back." 8 70
  161. clear; exit
  162. fi
  163. }
  164. formatShortcut() { \
  165. while read data; do { echo "macro index,pager g$1 \"<change-folder>$data<enter>\" \"Go to $2.\" # autogenerated"
  166. echo "macro index,pager M$1 \"<save-message>$data<enter>\" \"Move mail to $2.\" # autogenerated"
  167. echo "macro index,pager C$1 \"<copy-message>$data<enter>\" \"Copy mail to $2.\" # autogenerated"; } >> "$muttdir/accounts/$3.muttrc"
  168. done ;}
  169. gen_delim() { \
  170. delim="="
  171. for i in $(seq $(( $1 - 1 )))
  172. do
  173. delim="$delim-"
  174. done
  175. echo $delim ;}
  176. detectMailboxes() { \
  177. ls -d "$maildir/$1/"* | sed "s/.*\///;s/^/=/" > "$tmpdir/$1_boxes"
  178. sidebar_width="$(sed -n -e '/^set sidebar_width/p' "$muttdir/muttrc" | awk -F'=' '{print $2}')"
  179. delim="$(gen_delim "$sidebar_width")"
  180. oneline="$(sed -e "s/^\|$/\"/g" "$tmpdir/$1_boxes" | tr "\n" " ")"
  181. oneline="=$1 $delim $oneline"
  182. sed -i "/^mailboxes\|^set record\|^set postponed\|^set trash\|^set spoolfile/d" "$muttdir/accounts/$1.muttrc"
  183. echo mailboxes "$oneline" >> "$muttdir/accounts/$1.muttrc"
  184. sed -i "/# autogenerated/d" "$muttdir/accounts/$1.muttrc"
  185. grep -i "$tmpdir/$1_boxes" -e inbox | sed 1q | formatShortcut i inbox "$1"
  186. grep -i "$tmpdir/$1_boxes" -e sent | sed 1q | formatShortcut s sent "$1"
  187. grep -i "$tmpdir/$1_boxes" -e draft | sed 1q | formatShortcut d drafts "$1"
  188. grep -i "$tmpdir/$1_boxes" -e trash | sed 1q | formatShortcut t trash "$1"
  189. grep -i "$tmpdir/$1_boxes" -e spam | sed 1q | formatShortcut S spam "$1"
  190. grep -i "$tmpdir/$1_boxes" -e junk | sed 1q | formatShortcut j junk "$1"
  191. grep -i "$tmpdir/$1_boxes" -e archive | sed 1q | formatShortcut a archive "$1"
  192. spoolfile=$(grep -i "$tmpdir/$1_boxes" -e inbox | sed -e 's/=/+/g' | sed 1q)
  193. record=$(grep -i "$tmpdir/$1_boxes" -e sent | sed -e 's/=/+/g' | sed 1q)
  194. postponed=$(grep -i "$tmpdir/$1_boxes" -e draft | sed -e 's/=/+/g' | sed 1q)
  195. trash=$(grep -i "$tmpdir/$1_boxes" -e trash | sed -e 's/=/+/g' | sed 1q)
  196. { echo "set spoolfile = \"$spoolfile\""; echo "set record = \"$record\""; echo "set postponed = \"$postponed\""; echo "set trash = \"$trash\""; } >> "$muttdir/accounts/$1.muttrc"
  197. }
  198. #wipe () { rm "$HOME/.config/offlineimap/config" "$accdir" "$creddir" "$muttdir/personal.muttrc" ;}
  199. [ -z "$gpgemail" ] && askgpg
  200. while : ;
  201. do
  202. choice=$(dialog --title "Luke's mutt/offlineIMAP wizard" --nocancel \
  203. --menu "What would you like to do?" 15 50 8 \
  204. 0 "Add email account (Begin installtion)" \
  205. 1 "Autodetect mailboxes (Finalize installation)" \
  206. 2 "Enable/disable autosync." \
  207. 3 "Change an account's password" \
  208. 4 "Remove an account" \
  209. 5 "Remove all accounts" \
  210. 6 "Change your GPG email" \
  211. 7 "Exit this wizard." \
  212. 3>&1 1>&2 2>&3 3>&1 )
  213. case $choice in
  214. 0) dialog --title "Accounts detected" --msgbox "The following accounts have been detected:
  215. $(grep ~/.offlineimaprc -e "^accounts =" | sed 's/accounts =//g')
  216. " 6 60;;
  217. 1) chooseadd ;;
  218. 2) detectWarning && chooseDetect;;
  219. 3) inventory && for i in $userchoices; do getpass "$i" ; done;;
  220. 4) inventory && for i in $userchoices; do removeAccount "$i" ; done;;
  221. 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) ;;
  222. 6) askgpg ;;
  223. 7) clear && break ;;
  224. *) echo "Error. Are you sure you have dialog installed?" >&2; exit 2
  225. esac
  226. done
  227. rm -rf "$tmpdir"