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.
 
 
 
 

263 lines
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. #ifgoogleline
  133. grep "i[0-9]" "$muttdir/personal.muttrc" | awk '{print $3}' | sed -e 's/i//g' > "$tmpdir/mutt_used"
  134. printf "1\\n2\\n3\\n4\\n5\\n6\\n7\\n8\\n9" > "$tmpdir/mutt_all_possible"
  135. idnum=$(diff "$tmpdir/mutt_all_possible" "$tmpdir/mutt_used" | sed -n 2p | awk '{print $2}')
  136. getpass "$title" || userexit
  137. addaccount
  138. }
  139. getpass() { \
  140. dialog --title "Luke's mutt/offlineIMAP password wizard" --passwordbox "Enter the password for the \"$1\" account." 10 60 2> "$tmpdir/$1"
  141. "$GPG" -r "$gpgemail" --encrypt "$tmpdir/$1"
  142. shred -u "$tmpdir/$1"
  143. mv "$tmpdir/$1.gpg" "$creddir"
  144. }
  145. addaccount() { \
  146. getprofiles
  147. mkdir -p "$HOME/.config/offlineimap/" "$HOME/.config/msmtp"
  148. [ ! -f "$HOME/.config/offlineimap/config" ] && echo "$offlineimap_header" > "$HOME/.config/offlineimap/config"
  149. [ ! -f "$HOME/.config/msmtp/config" ] && echo "$msmtp_header" > "$HOME/.config/msmtp/config"
  150. echo "$offlineimap_profile" >> "$HOME/.config/offlineimap/config"
  151. echo "$msmtp_profile" >> "$HOME/.config/msmtp/config"
  152. echo "$mutt_profile" > "$accdir/$title.muttrc"
  153. }
  154. askgpg() { \
  155. 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
  156. while ! echo "$gpgemail" | grep "$emailre"; do
  157. 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
  158. done
  159. if "$GPG" -K | grep "<$gpgemail>"; then
  160. echo "$gpgemail" > "$creddir/gpgemail"
  161. else
  162. 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
  163. clear; exit
  164. fi
  165. }
  166. formatShortcut() { \
  167. while read data; do { echo "macro index,pager g$1 \"<change-folder>$data<enter>\" \"Go to $2.\" # autogenerated"
  168. echo "macro index,pager M$1 \"<save-message>$data<enter>\" \"Move mail to $2.\" # autogenerated"
  169. echo "macro index,pager C$1 \"<copy-message>$data<enter>\" \"Copy mail to $2.\" # autogenerated"; } >> "$muttdir/accounts/$3.muttrc"
  170. done ;}
  171. gen_delim() { \
  172. delim="="
  173. for i in $(seq $(( $1 - 1 )))
  174. do
  175. delim="$delim-"
  176. done
  177. echo $delim ;}
  178. detectMailboxes() { \
  179. ls -d "$maildir/$1/"* | sed "s/.*\///;s/^/=/" > "$tmpdir/$1_boxes"
  180. sidebar_width="$(sed -n -e '/^set sidebar_width/p' "$muttdir/muttrc" | awk -F'=' '{print $2}')"
  181. delim="$(gen_delim "$sidebar_width")"
  182. oneline="$(sed -e "s/^\|$/\"/g" "$tmpdir/$1_boxes" | tr "\n" " ")"
  183. oneline="=$1 $delim $oneline"
  184. sed -i "/^mailboxes\|^set record\|^set postponed\|^set trash\|^set spoolfile/d" "$muttdir/accounts/$1.muttrc"
  185. echo mailboxes "$oneline" >> "$muttdir/accounts/$1.muttrc"
  186. sed -i "/# autogenerated/d" "$muttdir/accounts/$1.muttrc"
  187. grep -i "$tmpdir/$1_boxes" -e inbox | sed 1q | formatShortcut i inbox "$1"
  188. grep -i "$tmpdir/$1_boxes" -e sent | sed 1q | formatShortcut s sent "$1"
  189. grep -i "$tmpdir/$1_boxes" -e draft | sed 1q | formatShortcut d drafts "$1"
  190. grep -i "$tmpdir/$1_boxes" -e trash | sed 1q | formatShortcut t trash "$1"
  191. grep -i "$tmpdir/$1_boxes" -e spam | sed 1q | formatShortcut S spam "$1"
  192. grep -i "$tmpdir/$1_boxes" -e junk | sed 1q | formatShortcut j junk "$1"
  193. grep -i "$tmpdir/$1_boxes" -e archive | sed 1q | formatShortcut a archive "$1"
  194. spoolfile=$(grep -i "$tmpdir/$1_boxes" -e inbox | sed -e 's/=/+/g' | sed 1q)
  195. record=$(grep -i "$tmpdir/$1_boxes" -e sent | sed -e 's/=/+/g' | sed 1q)
  196. postponed=$(grep -i "$tmpdir/$1_boxes" -e draft | sed -e 's/=/+/g' | sed 1q)
  197. trash=$(grep -i "$tmpdir/$1_boxes" -e trash | sed -e 's/=/+/g' | sed 1q)
  198. { echo "set spoolfile = \"$spoolfile\""; echo "set record = \"$record\""; echo "set postponed = \"$postponed\""; echo "set trash = \"$trash\""; } >> "$muttdir/accounts/$1.muttrc"
  199. }
  200. #wipe () { rm "$HOME/.config/offlineimap/config" "$accdir" "$creddir" "$muttdir/personal.muttrc" ;}
  201. [ -z "$gpgemail" ] && askgpg
  202. while : ;
  203. do
  204. choice=$(dialog --title "Luke's mutt/offlineIMAP wizard" --nocancel \
  205. --menu "What would you like to do?" 15 50 8 \
  206. 0 "Add email account (Begin installtion)" \
  207. 1 "Autodetect mailboxes (Finalize installation)" \
  208. 2 "Enable/disable autosync." \
  209. 3 "Change an account's password" \
  210. 4 "Remove an account" \
  211. 5 "Remove all accounts" \
  212. 6 "Change your GPG email" \
  213. 7 "Exit this wizard." \
  214. 3>&1 1>&2 2>&3 3>&1 )
  215. case $choice in
  216. 0) dialog --title "Accounts detected" --msgbox "The following accounts have been detected:
  217. $(grep ~/.offlineimaprc -e "^accounts =" | sed 's/accounts =//g')
  218. " 6 60;;
  219. 1) chooseadd ;;
  220. 2) detectWarning && chooseDetect;;
  221. 3) inventory && for i in $userchoices; do getpass "$i" ; done;;
  222. 4) inventory && for i in $userchoices; do removeAccount "$i" ; done;;
  223. 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) ;;
  224. 6) askgpg ;;
  225. 7) clear && break ;;
  226. *) echo "Error. Are you sure you have dialog installed?" >&2; exit 2
  227. esac
  228. done
  229. rm -rf "$tmpdir"