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

351 行
16 KiB

  1. #!/bin/sh
  2. command -V gpg >/dev/null 2>&1 && GPG="gpg" || GPG="gpg2"
  3. [ -z ${PASSWORD_STORE_DIR+x} ] && PASSWORD_STORE_DIR="$HOME/.password-store"
  4. [ -r "$PASSWORD_STORE_DIR/.gpg-id" ] &&
  5. "$GPG" --list-secret-keys $(cat "$PASSWORD_STORE_DIR/.gpg-id") >/dev/null 2>&1 || {
  6. printf "\`pass\` must be installed and initialized to encrypt passwords.\\nBe sure it is installed and run \`pass init <yourgpgemail>\`.\\nIf you don't have a GPG public private key pair, run \`%s --full-gen-key\` first.\\n" "$GPG"
  7. exit
  8. }
  9. ! command -v mbsync >/dev/null && printf "\`mbsync (isync package)\` must be installed to run mutt-wizard.\\n" && exit
  10. prefix="/usr/local"
  11. pass_prefix="mutt-wizard-"
  12. muttdir="${XDG_CONFIG_HOME:-$HOME/.config}/mutt" # Main mutt config location
  13. accdir="$muttdir/accounts" # Directory for account settings
  14. maildir="${XDG_DATA_HOME:-$HOME/.local/share}/mail" # Location of mail storage
  15. namere="^[a-z_][a-z0-9_-]*$" # Regex to ensure viable username
  16. emailre=".+@.+\..+" # Regex to confirm valid email address
  17. muttshare="$prefix/share/mutt-wizard"
  18. mbsyncrc="${MBSYNCRC:-$HOME/.mbsyncrc}"
  19. mwconfig="$muttshare/mutt-wizard.muttrc"
  20. cachedir="${XDG_CACHE_HOME:-$HOME/.cache}/mutt-wizard"
  21. muttrc="$muttdir/muttrc"
  22. msmtprc="${XDG_CONFIG_HOME:-$HOME/.config}/msmtp/config"
  23. msmtplog="${XDG_CONFIG_HOME:-$HOME/.config}/msmtp/msmtp.log"
  24. ssltype="IMAPS" # This is later changed to `None` later in the script if using Protonmail
  25. alias mbsync='mbsync -c "$mbsyncrc"'
  26. for x in "/etc/ssl/certs/ca-certificates.crt" "/etc/pki/tls/certs/ca-bundle.crt" "/etc/ssl/ca-bundle.pem" "/etc/pki/tls/cacert.pem" "/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem" "/etc/ssl/cert.pem" "/usr/local/share/ca-certificates/"
  27. do
  28. [ -f "$x" ] && sslcert="$x" && break
  29. done || { echo "CA Certificate not found. Please install one or link it to /etc/ssl/certs/ca-certificates.crt" && exit 1 ;}
  30. getaccounts() { accounts="$(find "$accdir" -type f | grep -o "[0-9]-.*.muttrc" | sed "s/-/: /;s/\..*//" | sort -n)" ;}
  31. list() { getaccounts && [ -n "$accounts" ] && echo "$accounts" ;}
  32. getprofiles() { \
  33. unset msmtp_header msmtp_profile mutt_profile mbsync_profile
  34. printf "Creating profiles for \`%s\`..." "$title"
  35. msmtp_header="defaults
  36. auth on
  37. tls on
  38. tls_trust_file $sslcert
  39. logfile $msmtplog
  40. "
  41. msmtp_profile="account $title
  42. host $smtp
  43. port $sport
  44. from $fulladdr
  45. user $login
  46. passwordeval \"pass $pass_prefix$title\"
  47. $starttlsoff
  48. "
  49. mbsync_profile="IMAPStore $title-remote
  50. Host $imap
  51. Port $iport
  52. User $login
  53. PassCmd \"pass $pass_prefix$title\"
  54. AuthMechs LOGIN
  55. SSLType $ssltype
  56. CertificateFile $sslcert
  57. MaildirStore $title-local
  58. Subfolders Verbatim
  59. Path ${XDG_DATA_HOME:-$HOME/.local/share}/mail/$title/
  60. Inbox ${XDG_DATA_HOME:-$HOME/.local/share}/mail/$title/INBOX
  61. Flatten .
  62. Channel $title
  63. Expunge Both
  64. Master :$title-remote:
  65. Slave :$title-local:
  66. Patterns * !\"[Gmail]/All Mail\"
  67. Create Both
  68. SyncState *
  69. MaxMessages $maxmes
  70. ExpireUnread no
  71. # End profile
  72. "
  73. if [ "$accounttype" = "offline" ]; then
  74. mutt_profile="# vim: filetype=neomuttrc
  75. # muttrc file for account $title
  76. set realname = \"$realname\"
  77. set from = \"$fulladdr\"
  78. set sendmail = \"msmtp -a $title\"
  79. alias me $realname <$fulladdr>
  80. set folder = \"$maildir/$title\"
  81. set header_cache = $cachedir/$title/headers
  82. set message_cachedir = $cachedir/$title/bodies
  83. set mbox_type = Maildir
  84. bind index,pager gg noop
  85. bind index,pager g noop
  86. bind index,pager M noop
  87. bind index,pager C noop
  88. bind index gg first-entry
  89. macro index o \"<shell-escape>mbsync -V $title<enter>\" \"run mbsync to sync $title\"
  90. unmailboxes *
  91. "
  92. else
  93. mutt_profile="# vim: filetype=neomuttrc
  94. # muttrc file for account $title
  95. set realname = \"$realname\"
  96. set from = \"$fulladdr\"
  97. set sendmail = \"msmtp -a $title\"
  98. alias me $realname <$fulladdr>
  99. set folder = \"imaps://$login@$imap:$iport\"
  100. set imap_user = \"$login\"
  101. set header_cache = $cachedir/$title/headers
  102. set message_cachedir = $cachedir/$title/bodies
  103. set imap_pass = \"\`pass $pass_prefix$title\`\"
  104. set mbox_type = Maildir
  105. set ssl_starttls = yes
  106. set ssl_force_tls = yes
  107. bind index,pager gg noop
  108. bind index,pager g noop
  109. bind index,pager M noop
  110. bind index,pager C noop
  111. bind index gg first-entry
  112. unmailboxes *
  113. "
  114. fi
  115. printf "DONE.\\n"
  116. }
  117. askinfo() { \
  118. printf "Insert the \033[31memail address\033[0m that you want to autoconfigure for mutt/mbsync\\n\tEmail: \033[36m"
  119. read -r fulladdr
  120. printf "\033[0m"
  121. while ! echo "$fulladdr" | grep -E "$emailre" >/dev/null; do
  122. printf "That is not a valid \033[31memail address\033[0m, please retype the desired email.\\n\\nEmail: \033[36m\t"
  123. read -r fulladdr
  124. printf "\033[0m"
  125. done
  126. domain="$(echo "$fulladdr" | sed "s/.*@//")"
  127. search_query=$domain
  128. case "$domain" in
  129. protonmail.com|protonmail.ch|pm.me)
  130. search_query='protonmail.com' ;;
  131. *)
  132. while : ; do
  133. printf "\nIs your email hosted with Protonmail? [yes/no] "
  134. read -r is_protonmail
  135. case $is_protonmail in
  136. [Yy][Ee][Ss]) search_query='protonmail.com' && break;;
  137. [Nn][Oo]) break;;
  138. *) printf 'Please answer Yes or No'
  139. esac; done;
  140. esac
  141. printf "\\nSearching for \033[32m%s\033[0m in \033[34m\`domains.csv\`\033[0m..." "$domain"
  142. serverinfo="$(grep "^$search_query" "$muttshare/domains.csv" 2>/dev/null)"
  143. if [ -z "$serverinfo" ]; then
  144. search_query=$(echo "$search_query" | sed "s/\.[^\.]*$/\.\\\*/")
  145. serverinfo="$(grep "^$search_query" "$muttshare/domains.csv" 2>/dev/null)"
  146. fi
  147. if [ -z "$serverinfo" ]; then
  148. 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"
  149. printf "Insert the IMAP server for your email provider (excluding the port number)\\n\033[36m\t"
  150. read -r imap
  151. printf "\033[0mWhat is your server's IMAP port number? (Usually something like 993)\\n\033[36m\t"
  152. read -r iport
  153. printf "\033[0mInsert the SMTP server for your email provider (excluding the port number)\\n\033[36m\t"
  154. read -r smtp
  155. printf "\033[0mWhat is your server's SMTP port number? (Usually 587 or 465)\\n\033[36m\t"
  156. read -r sport
  157. printf "\033[0m\\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"
  158. else
  159. IFS=, read -r service imap iport smtp sport <<EOF
  160. $serverinfo
  161. EOF
  162. printf "\\n\033[3;33mCongrats!\033[0m Server info has automatically been 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"
  163. case "$service" in
  164. gmail.com) printf "\033[31mREMEMBER: Gmail users must enable \"less secure\" (third-party) applications first for the sync to work:\\nhttps://support.google.com/accounts/answer/6010255\\n\033[0m" ;;
  165. protonmail.ch|protonmail.com|pm.me) printf "\033[31mREMEMBER: Protonmail users must install and configure Protonmail Bridge first for the sync to work:\\nhttps://protonmail.com/bridge/\\n\033[0m" && ssltype="None" ;;
  166. esac
  167. [ "$sport" = 465 ] && starttlsoff="tls_starttls off"
  168. fi
  169. printf "Enter the \033[35mfull name\033[0m you want to be identified by on this account.\\n\tReal name: "
  170. read -r realname
  171. 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: "
  172. read -r title
  173. while ! echo "$title" | grep "$namere" >/dev/null || ls "$accdir"/[0-9]"-$title.muttrc" >/dev/null 2>&1; do
  174. printf "\033[31mTry again\033[0m. Pick a nickname that is one word only including lowercase letters and _ or - and that you have \033[1mnot\033[0m used before.\\n\tAccount name: \033[36m\t"
  175. read -r title
  176. printf "\033[0m"
  177. done
  178. printf "If your account has a special username different from your address, insert it now. Otherwise leave this prompt totally blank.\\n\033[34mMost accounts will not have a separate login, so you should probably leave this blank.\033[0m\\n\tLogin(?): \033[36m"
  179. read -r login
  180. printf "\033[0m"
  181. [ -z "$login" ] && login="$fulladdr"
  182. [ "$accounttype" = "offline" ] && printf "If you want to limit the number of messages kept offline to a number, enter that number below. If you do not want to limit your mail and would like \`mbsync\` to sync all mail, press enter without typing a number.\\n\t" && read -r maxmes
  183. echo "$maxmes" | grep "[1-9]" >/dev/null || maxmes="0"
  184. getpass
  185. getprofiles
  186. mkdir -p "$muttdir" "$accdir" "$cachedir/$title/bodies" "${XDG_CONFIG_HOME:-$HOME/.config}/msmtp"
  187. getaccounts
  188. for x in $(seq 1 9); do echo "$accounts" | grep "$x" >/dev/null 2>&1 || { export idnum="$x"; break ;}; done
  189. [ ! -f "$msmtprc" ] && echo "$msmtp_header" > "$msmtprc"
  190. echo "$msmtp_profile" >> "$msmtprc"
  191. command -V apt-get >/dev/null 2>&1 && ln -s "$msmtprc" "$HOME/.msmtprc" 2>/dev/null
  192. case "$service" in
  193. protonmail.ch|protonmail.com|pm.me) protonfinger || return 1 ;;
  194. esac
  195. echo "$mutt_profile" > "$accdir/$idnum-$title.muttrc"
  196. mkdir -p "${mbsyncrc%/*}"
  197. echo "$mbsync_profile" >> "$mbsyncrc"
  198. notmuchauto
  199. [ ! -f "$muttrc" ] && echo "# vim: filetype=neomuttrc" > "$muttrc" && echo "muttrc created."
  200. ! grep "^source.*mutt-wizard.muttrc" "$muttrc" >/dev/null && echo "source $mwconfig # mw-autogenerated" >> "$muttrc"
  201. ! grep "^source.*.muttrc" "$muttrc" | grep -v "$mwconfig" >/dev/null && echo "source $accdir/$idnum-$title.muttrc # mw-autogenerated" >> "$muttrc"
  202. echo "macro index,pager i$idnum '<sync-mailbox><enter-command>source $accdir/$idnum-$title.muttrc<enter><change-folder>!<enter>;<check-stats>' \"switch to $fulladdr\" # mw-autogenerated" >> "$muttrc"
  203. }
  204. protonfinger() { printf "Getting Protonmail bridge fingerprint...\\n"
  205. fingerprint="$(msmtp --serverinfo --host=127.0.0.1 --port=1025 --tls --tls-certcheck=off | grep SHA256: | sed 's/^.*: //')"
  206. sed -ibu "s/account $title/&\ntls_trust_file\ntls_fingerprint $fingerprint/" "$msmtprc" ; rm -f "$msmtprc"bu
  207. }
  208. getpass() { while : ; do pass rm -f "$pass_prefix$title" >/dev/null 2>&1
  209. pass insert "$pass_prefix$title" && break; done ;}
  210. formatShortcut() { \
  211. while read -r data; do { echo "macro index,pager g$1 \"<change-folder>$data<enter>\" \"go to $2\" # mw-autogenerated"
  212. echo "macro index,pager M$1 \";<save-message>$data<enter>\" \"move mail to $2\" # mw-autogenerated"
  213. echo "macro index,pager C$1 \";<copy-message>$data<enter>\" \"copy mail to $2\" # mw-autogenerated"; } >> "$accdir/$idnum-$title.muttrc"
  214. done ;}
  215. tryconnect() { mkdir -p "$maildir/$title"
  216. if mailboxes="$(mbsync -l "$title" | sed 's/\//./')" >/dev/null 2>&1 && [ -n "$mailboxes" ]; then
  217. [ "$accounttype" = "online" ] && sed -ibu "/IMAPStore $title-remote$/,/# End profile/d" "$mbsyncrc" ; rm -f "$mbsyncrc"bu
  218. printf "\033[32mMailboxes detected.\033[0m\\n"
  219. echo "$mailboxes" | xargs -I {} mkdir -p "$maildir/$title/{}"
  220. return 0
  221. else
  222. printf "\033[31m\033[31mLog-on not successful.\033[0m\\nIt seems that either you inputted the wrong password or server settings, or there are other requirements for your account out of the control of mutt-wizard.\\n"
  223. return 1
  224. fi ;}
  225. finalize() { \
  226. boxes="$(find "$maildir/$title/" -mindepth 1 -type d | sed "s/\ /\\\ /g;s/^.*\//=/;/=\(cur\|new\|tmp\)$/d")"
  227. [ -z "$boxes" ] && printf "\033[31mNo local mailboxes have been detected for %s.\033[0m\\nThis means that mbsync has not been successfully run.\\nRun mbsync, and if it has an error, be sure to check your password and server settings manually if needbe.\\n" "$title" && return
  228. printf "Setting default mailboxes for your Inbox, Sent, Drafts and Trash in mutt...\\n"
  229. spoolfile=$(echo "$boxes" | grep -i -m 1 inbox | sed 's/=/+/g')
  230. record=$(echo "$boxes" | grep -i -m 1 sent | sed 's/=/+/g')
  231. postponed=$(echo "$boxes" | grep -i -m 1 draft | sed 's/=/+/g')
  232. trash=$(echo "$boxes" | grep -i -m 1 trash | sed 's/=/+/g')
  233. sed -ibu "/^mailboxes\|^set record\|^set postponed\|^set trash\|^set spoolfile/d" "$accdir/$idnum-$title.muttrc" ; rm -f "$accdir/$idnum-$title.muttrcbu"
  234. { echo "set spoolfile = \"$spoolfile\""; echo "set record = \"$record\""; echo "set postponed = \"$postponed\""; echo "set trash = \"$trash\""; } >> "$accdir/$idnum-$title.muttrc"
  235. echo "mailboxes $(echo "$boxes" | sed -e "s/^\|$/\"/g" | tr "\n" " ")" >> "$accdir/$idnum-$title.muttrc"
  236. printf "Setting up your keyboard shortcuts for jumping between mailboxes...\\n"
  237. sed -ibu "/# mw-autogenerated/d" "$accdir/$idnum-$title.muttrc" ; rm -f "$accdir/$idnum-$title.muttrcbu"
  238. echo "$boxes" | grep -i inbox | head -n 1 | formatShortcut i inbox
  239. echo "$boxes" | grep -i sent | head -n 1 | formatShortcut s sent
  240. echo "$boxes" | grep -i draft | head -n 1 | formatShortcut d drafts
  241. echo "$boxes" | grep -i trash | head -n 1 | formatShortcut t trash
  242. echo "$boxes" | grep -i spam | head -n 1 | formatShortcut S spam
  243. echo "$boxes" | grep -i junk | head -n 1 | formatShortcut j junk
  244. echo "$boxes" | grep -i archive | head -n 1 | formatShortcut a archive
  245. [ "$accounttype" = "offline" ] && printf "All done.\\n\033[33mYou should now be able to run \`\033[32mmbsync %s\033[33m\` to begin to download your mail.\033[0m\\n" "$title"
  246. command -V urlview >/dev/null 2>&1 && [ ! -f "$HOME/.urlview" ] && echo "COMMAND \$BROWSER" > "$HOME/.urlview"
  247. return 0
  248. }
  249. confirm() { printf "Do you want to %s? [yes/N]\\n\t" "$@" && read -r input && ! echo "$input" | grep -i "^yes$" >/dev/null && printf "That doesn't seem like a yes to me.\\n\\n" && return 1
  250. printf "Are you really, really sure you want to %s?\\n\t" "$@" && read -r input && ! echo "$input" | grep -i "^yes$" >/dev/null && printf "That doesn't seem like a yes to me.\\n\\n" && return 1
  251. return 0 ;}
  252. pick() { printf "Select an accounts to %s:\\n" "$1"
  253. list
  254. read -r input
  255. [ -z "$input" ] && return 1
  256. title="$(echo "$accounts" | grep "$input" | awk '{print $2}')"
  257. [ -z "$title" ] && printf "Invalid response." && return 1
  258. return 0 ;}
  259. delete() { sed -ibu "/IMAPStore $title-remote$/,/# End profile/d" "$mbsyncrc" ; rm -rf "$mbsyncrc"bu
  260. rm -rf "${cachedir:?}/${title:?}" "$accdir/"[1-9]"-$title.muttrc"
  261. sed -ibu "/[0-9]-$title.muttrc/d" "$muttrc" ; rm -f "$muttrc"bu
  262. sed -ibu "/account $title/,/^\(\s*$\|account\)/d" "$msmtprc"; rm -f "$msmtprc"bu
  263. }
  264. asktype() { while : ; do
  265. printf "Do you want to keep your mail for this account offline with mbsync? [yes/no]\\n\t"
  266. read -r offnot
  267. case "$offnot" in
  268. [Yy][Ee][Ss]) accounttype="offline" && break ;;
  269. [Nn][Oo]) accounttype="online" && break ;;
  270. *) echo "Write out either yes or no completely. Try again or press ctrl-c to quit." ;;
  271. esac; done ;}
  272. purge() { confirm "delete all account data" || exit
  273. rm -rf "$mbsyncrc" "$accdir" "${XDG_CONFIG_HOME:-$HOME/.config}/msmtp" "$cachedir"
  274. echo "All configs and account settings have been purged."
  275. sed -ibu "/\# mw-autogenerated/d" "$muttrc" ; rm -f "$muttrc"bu
  276. }
  277. syncwrapper() { mbsync "${1:--a}" &
  278. ( kill -46 "$(pidof "${STATUSBAR:-dwmblocks}")" >/dev/null 2>&1 ) 2>/dev/null
  279. wait
  280. ( kill -46 "$(pidof "${STATUSBAR:-dwmblocks}")" >/dev/null 2>&1 ) 2>/dev/null
  281. notmuch new
  282. }
  283. notmuchauto() { \
  284. [ -z "$NOTMUCH_CONFIG" ] && NOTMUCH_CONFIG="$HOME/.notmuch-config"
  285. [ -f "$NOTMUCH_CONFIG" ] && return 0
  286. nmbasic="[database]
  287. path=$maildir
  288. [user]
  289. name=$realname
  290. primary_email=$fulladdr
  291. [new]
  292. tags=unread;inbox;
  293. ignore=.mbsyncstate;.uidvalidity
  294. [search]
  295. exclude_tags=deleted;spam;
  296. [maildir]
  297. synchronize_flags=true
  298. [crypto]
  299. gpg_path=$GPG"
  300. echo "$nmbasic" > "$NOTMUCH_CONFIG" ;}
  301. trap 'echo -e "\033[0m\n"; exit' INT ABRT
  302. case "$1" in
  303. ls) list ;;
  304. add) asktype && askinfo && tryconnect && finalize || delete ;;
  305. pass) pick "change the password of" && getpass ;;
  306. delete) pick delete && confirm "delete the \`$title\` profile" && delete ;;
  307. sync) syncwrapper "$2" ;;
  308. purge) purge ;;
  309. *) cat << EOF
  310. mw: mutt-wizard, auto-configure email accounts for mutt
  311. including downloadable mail with \`isync\`.
  312. Allowed options:
  313. add Add and autoconfigure an email address (9 max.)
  314. ls List configured accounts
  315. delete Pick an account to delete
  316. purge Delete all accounts and settings
  317. sync Syncs mail and updates notmuch database
  318. all else Print this message
  319. NOTE: Once at least one account is added, you can run
  320. \`mbsync -a\` to begin downloading mail.
  321. EOF
  322. esac