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

324 行
14 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" 2>/dev/null)" # 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. # Get certificate location depending on OS. Linux is elsewhere condition.
  13. case "$(uname)" in
  14. "Darwin") sslcert="/usr/local/etc/openssl/cert.pem" ;;
  15. *) sslcert="/etc/ssl/certs/ca-certificates.crt" ;;
  16. esac
  17. getprofiles() { \
  18. printf "Creating profiles for \`%s\`..." "$title"
  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. sslcacertfile = $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 ~/.config/msmtp/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. printf "DONE.\\n"
  77. }
  78. addaccount() { \
  79. printf "Insert the \033[31memail address\033[0m that you want to autoconfigure for mutt/offlineIMAP\\n\\nEmail: "
  80. printf "\033[36m\t"
  81. read -r fulladdr
  82. printf "\033[0m"
  83. while ! echo "$fulladdr" | grep "$emailre" >/dev/null; do
  84. printf "That is not a valid \033[31memail address\033[0m, please retype the desired email.\\n\\nEmail: "
  85. printf "\033[36m\t"
  86. read -r fulladdr
  87. printf "\033[0m"
  88. done
  89. domain="$(echo "$fulladdr" | sed "s/.*@//")"
  90. printf "\\nSearching for \033[32m%s\033[0m in \033[34m\`domains.csv\`\033[0m..." "$domain"
  91. serverinfo="$(grep "$domain" "$muttdir/domains.csv")"
  92. if [ -z "$serverinfo" ]; then
  93. printf "Your email domain is not in mutt-wizard's database yet.\\nmutt-wizard will still autoconfigure everything, but you will have to manually type in your service's IMAP and SMTP server information.\\nYou can usually quickly find this by internet searching for it.\\n"
  94. printf "Insert the IMAP server for your email provider (excluding the port number)\\n"
  95. printf "\033[36m\t"
  96. read -r imap
  97. printf "\033[0m"
  98. printf "What is your server's IMAP port number? (Usually something like 993)\\n"
  99. printf "\033[36m\t"
  100. read -r iport
  101. printf "\033[0m"
  102. printf "Insert the SMTP server for your email provider (excluding the port number)\\n"
  103. printf "\033[36m\t"
  104. read -r smtp
  105. printf "\033[0m"
  106. printf "What is your server's SMTP port number? (Usually 587 or 465)\\n"
  107. printf "\033[36m\t"
  108. read -r sport
  109. printf "\033[0m"
  110. printf "\\nGreat! If you want to be helpful, copy the line below and you can add it to the \`domains.csv\` file on Github.\\nThis will make things easier for others who use your email provider.\\n\\n%s,%s,%s,%s,%s\\n\\nAlthough be sure to test to see if these settings work first! ;-)\\n" "$domain" "$imap" "$iport" "$smtp" "$sport"
  111. else
  112. IFS=, read -r service imap iport smtp sport <<EOF
  113. $serverinfo
  114. EOF
  115. printf "\\n\033[3;33mCongrats!\033[0m Server info has automatically be found, so you won't have to look anything up!\\n\t\033[1mIMAP server\033[0m: %s\\n\t\033[1mIMAP port\033[0m: %s\\n\t\033[1mSMTP server\033[0m: %s\\n\t\033[1mSMTP port\033[0m: %s\\nThis data will be used by the wizard.\\n" "$imap" "$iport" "$smtp" "$sport"
  116. fi
  117. printf "\\nPress enter to continue.\\n"
  118. stty -echo
  119. read -r
  120. stty echo
  121. printf "Enter the \033[35mfull name\033[0m you want to be identified by on this account.\\n\tReal name: "
  122. read -r realname
  123. printf "Enter a short, \033[36mone-word identifier\033[0m for this email account that will distinguish them from any other accounts you add.\\n\tAccount name: "
  124. read -r title
  125. while ! echo "$title" | grep "$namere" >/dev/null; do
  126. printf "\033[31mTry again\033[0m. Pick a nickname that is one word only including lowercase letters and _ or -.\\n\tAccount name: "
  127. printf "\033[36m\t"
  128. read -r title
  129. printf "\033[0m"
  130. done
  131. printf "If your account has a special username different from your address, insert it now. Otherwise leave this prompt totally blank.\\n\tLogin(?): "
  132. printf "\033[36m\t"
  133. read -r login
  134. printf "\033[0m"
  135. [ -z "$login" ] && login="$fulladdr"
  136. if [ "$service" = "gmail.com" ]; then
  137. type="Gmail"; ifgoogleline="folderfilter = lambda foldername: foldername not in ['[Gmail]/All Mail']"
  138. printf "\033[1mGoogle\033[0m mail account detected. Remember to check the README to make sure of Google-specific settings you must enable.\\n"
  139. else
  140. type="IMAP"; ifgoogleline="remotehost = $imap"
  141. fi
  142. grep "i[0-9]" "$muttdir/personal.muttrc" | awk '{print $3}' | sed -e 's/i//g' > "$tmpdir/mutt_used"
  143. seq 1 9 > "$tmpdir/mutt_all"
  144. idnum=$(diff "$tmpdir/mutt_all" "$tmpdir/mutt_used" | sed -n 2p | awk '{print $2}')
  145. getpass
  146. getprofiles
  147. mkdir -p "$accdir/$title/cache/bodies"
  148. mkdir -p "$HOME/.config/offlineimap/" "$HOME/.config/msmtp"
  149. [ ! -f "$HOME/.config/offlineimap/config" ] && echo "$offlineimap_header" > "$HOME/.config/offlineimap/config"
  150. [ ! -f "$HOME/.config/msmtp/config" ] && echo "$msmtp_header" > "$HOME/.config/msmtp/config"
  151. echo "$offlineimap_profile" >> "$HOME/.config/offlineimap/config"
  152. echo "$msmtp_profile" >> "$HOME/.config/msmtp/config"
  153. echo "$mutt_profile" > "$accdir/$title.muttrc"
  154. sed -i "s/^accounts =.*[a-zA-Z]$/&, $title/g;s/^accounts =\\s*$/accounts = $title/g" "$HOME/.config/offlineimap/config"
  155. echo "macro index,pager i$idnum '<sync-mailbox><enter-command>source \"$muttdir\"/accounts/$title.muttrc<enter><change-folder>!<enter>;<check-stats>'" >> "$muttdir/personal.muttrc"
  156. ! grep "^source.*.muttrc" "$muttdir/personal.muttrc" >/dev/null && echo "source $accdir/$title.muttrc" >> "$muttdir/personal.muttrc"
  157. trysync && finalize
  158. }
  159. getpass() { \
  160. printf "Now enter your password for the \"%s\" account. Don't worry, this will be encrypted and only you with your GPG key can view it.\\n\tPassword: " "$title"
  161. stty -echo
  162. read -r password
  163. stty echo
  164. echo "$password" > "$tmpdir/$title"
  165. printf "Encrypting your password with %s..." "$GPG"
  166. "$GPG" -r "$gpgemail" --encrypt "$tmpdir/$title"
  167. printf "DONE\\nShredding all memory of your password for safety's sake..."
  168. unset password
  169. shred -u "$tmpdir/$title"
  170. mkdir -p "$creddir"
  171. mv "$tmpdir/$title.gpg" "$creddir/"
  172. printf "DONE.\\n"
  173. }
  174. askgpg() { \
  175. printf "To safely encrypt passwords, mutt-wizard requires that you have a GPG public/private key pair.\\n\\nPlease input the email address of your GPG key pair below.\\nEmail: "
  176. printf "\033[36m\t"
  177. read -r gpgemail
  178. printf "\033[0m"
  179. while ! echo "$gpgemail" | grep "$emailre" >/dev/null; do
  180. printf "That is not a valid email address. Please try again.\\nEmail: "
  181. printf "\033[36m\t"
  182. read -r gpgemail
  183. printf "\033[0m"
  184. done
  185. if "$GPG" -K | grep "<$gpgemail>" >/dev/null; then
  186. mkdir -p "$creddir"
  187. echo "$gpgemail" > "$creddir/gpgemail"
  188. else
  189. printf "You do not appear to have a private key associated with %s.\\nPlease generate a GPG key pair by running \`%s --full-gen-key\` and rerun the wizard.\\n" "$gpgemail" "$GPG"
  190. exit 1
  191. fi
  192. }
  193. formatShortcut() { \
  194. while read -r data; do { echo "macro index,pager g$1 \"<change-folder>$data<enter>\" \"Go to $2.\" # autogenerated"
  195. echo "macro index,pager M$1 \"<save-message>$data<enter>\" \"Move mail to $2.\" # autogenerated"
  196. echo "macro index,pager C$1 \"<copy-message>$data<enter>\" \"Copy mail to $2.\" # autogenerated"; } >> "$muttdir/accounts/$3.muttrc"
  197. done ;}
  198. trysync() { \
  199. ! ping -q -c 1 1.1.1.1 > /dev/null && printf "No internet connection detected.\\nTry rerunning offlineimap manually when connection is established, then select the option to detect mailboxes and finalize installation.\\n" && return 1
  200. printf "\033[32mYou must have an internet connection to continue.\033[0m\\nmutt-wizard will run offlineimap briefly to (1) ensure that login details are functional and (2) allow offlineimap to tell us what mailboxes your email account has.\\nAfter around 15 seconds, mutt-wizard will kill the process and continue.\\nYou can run offlineimap manually to finish the mail sync later.\\nPress enter to continue.\\n."
  201. stty -echo
  202. read -r
  203. stty echo
  204. (sleep 15; killall offlineimap; killall offlineimap; killall offlineimap)>/dev/null 2>&1 &
  205. mkdir -p "$maildir"
  206. offlineimap -qoa "$title"
  207. if ls -d "$maildir/$title/"* >/dev/null 2>&1; then
  208. printf "\033[32mSync successful.\033[0m\\n"; return
  209. else
  210. printf "\033[31m\033[31mSync not successful.\033[0m Try running offlineimap manually after double-checking your password and server settings.\\nThen select to finalize the account.\\n"; return 1
  211. fi
  212. }
  213. finalize() { \
  214. boxes="$(du -a "$maildir/$title/"* -d 0 | sed "s/^.*\//=/")"
  215. [ -z "$boxes" ] && printf "\033[31mNo local mailboxes have been detected for %s.\033[0m\\nThis means that offlineimap has not been successfully run.\\nRun offlineimap, and if it has an error, be sure to check your password and server settings manually if needbe.\\n" "$title" && return
  216. echo "$boxes" > "$tmpdir/title_boxes"
  217. printf "Setting up the mutt sidebar...\\n"
  218. sidebar_width="$(sed -n -e '/^set sidebar_width/p' "$muttdir/muttrc" | awk -F'=' '{print $2}')"
  219. delim="$(head -c "$sidebar_width" < /dev/zero | tr '\0' =)"
  220. printf "Setting default mailboxes for your Inbox, Sent, Drafts and Trash in mutt...\\n"
  221. oneline="$(sed -e "s/^\|$/\"/g" "$tmpdir/title_boxes" | tr "\n" " ")"
  222. oneline="=$title $delim $oneline"
  223. spoolfile=$(grep -i "$tmpdir/title_boxes" -e inbox | sed -e 's/=/+/g' | sed 1q)
  224. record=$(grep -i "$tmpdir/title_boxes" -e sent | sed -e 's/=/+/g' | sed 1q)
  225. postponed=$(grep -i "$tmpdir/title_boxes" -e draft | sed -e 's/=/+/g' | sed 1q)
  226. trash=$(grep -i "$tmpdir/title_boxes" -e trash | sed -e 's/=/+/g' | sed 1q)
  227. sed -i "/^mailboxes\|^set record\|^set postponed\|^set trash\|^set spoolfile/d" "$muttdir/accounts/$title.muttrc"
  228. { echo "set spoolfile = \"$spoolfile\""; echo "set record = \"$record\""; echo "set postponed = \"$postponed\""; echo "set trash = \"$trash\""; } >> "$muttdir/accounts/$title.muttrc"
  229. echo mailboxes "$oneline" >> "$muttdir/accounts/$title.muttrc"
  230. printf "Setting up your keyboard shortcuts for jumping between mailboxes...\\n"
  231. sed -i "/# autogenerated/d" "$muttdir/accounts/$title.muttrc"
  232. grep -i "$tmpdir/title_boxes" -e inbox | sed 1q | formatShortcut i inbox "$title"
  233. grep -i "$tmpdir/title_boxes" -e sent | sed 1q | formatShortcut s sent "$title"
  234. grep -i "$tmpdir/title_boxes" -e draft | sed 1q | formatShortcut d drafts "$title"
  235. grep -i "$tmpdir/title_boxes" -e trash | sed 1q | formatShortcut t trash "$title"
  236. grep -i "$tmpdir/title_boxes" -e spam | sed 1q | formatShortcut S spam "$title"
  237. grep -i "$tmpdir/title_boxes" -e junk | sed 1q | formatShortcut j junk "$title"
  238. grep -i "$tmpdir/title_boxes" -e archive | sed 1q | formatShortcut a archive "$title"
  239. printf "All done.\\n"
  240. }
  241. wipe () { \
  242. printf "Are you \033[31;1mreally\033[0m sure you want to delete all email accounts?\\n" && read -r input && ! echo "$input" | grep -i "y\(es\)*" >/dev/null && printf "That doesn't seem like a yes to me.\\n\\n" && return 1
  243. printf "Are you really, really sure?" && read -r input && ! echo "$input" | grep -i "y\(es\)*" >/dev/null && printf "That doesn't seem like a yes to me.\\n\\n" && return 1
  244. #rm -rf "$HOME/.config/offlineimap/config" "$accdir" "$creddir" "$muttdir/personal.muttrc"
  245. echo deleted
  246. }
  247. pick() { \
  248. numbered="$(grep "^accounts *=" "$HOME/.config/offlineimap/config" | sed 's/accounts *= *//g;s/,/ /g;s/ \+/\n/g' | nl)"
  249. [ "$(echo "$numbered" | wc -w)" = 0 ] && printf "No accounts to delete.\\n" && return 1
  250. printf "Select an accounts to %s:\\n" "$1"
  251. echo "$numbered"
  252. printf "\033[36m\t"
  253. read -r input
  254. printf "\033[0m"
  255. [ -z "$input" ] && return 1
  256. title="$(echo "$numbered" | grep "$input" | awk '{print $2}')"
  257. [ -z "$title" ] && printf "Invalid response." && return 1
  258. [ -n "$2" ] && printf "Are you sure you want to %s the \`%s\` account?" "$1" "$title" && read -r input && echo "$input" | grep -i "y\(es\)*" >/dev/null
  259. }
  260. delete() { sed -i "
  261. /Account $title]/,/Account/{//!d}
  262. /Account $title]/d
  263. s/ $title\(,\|$\)//g
  264. s/=$title\(,\|$\)/=/g
  265. s/,$//g
  266. " "$HOME/.config/offlineimap/config"
  267. rm -rf "${accdir:?}/${title:?}" "$creddir/$title.gpg" "$accdir/$title.muttrc"
  268. sed -i "/$title.muttrc/d" "$muttdir/personal.muttrc"
  269. # Delete from the line matching the account name, until the next account or empty line
  270. sed -i "/account $title/,/^\(\s*$\|account\)/d" "$HOME/.config/msmtp/config";}
  271. main() { \
  272. while : ; do
  273. [ -z "$gpgemail" ] && askgpg
  274. printf "What would you like \033[32mmutt-wizard\033[0m to do?
  275. \033[31m1 Add an email account\033[0m
  276. 2 Autodetect mailboxes
  277. 3 Change an account's password
  278. 4 Remove an account
  279. 5 Change GPG key pair used for encryption
  280. 6 Delete all account data
  281. 0 Exit
  282. Input a number to continue or press ctrl-c.\\n"
  283. printf "\033[36m\t"
  284. read -r choice
  285. printf "\033[0m"
  286. case "$choice" in
  287. 1) addaccount ;;
  288. 2) pick finalize && finalize ;;
  289. 3) pick "change the password of" yes && getpass ;;
  290. 4) pick delete yes && delete ;;
  291. 5) askgpg ;;
  292. 6) wipe && printf "Account data purged." ;;
  293. 0) break ;;
  294. *) printf "Invalid input.\\n"
  295. esac
  296. done
  297. }
  298. main
  299. rm -rf "$tmpdir"