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.

mw 16 KiB

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