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.
 
 
 
 

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