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.
 
 
 
 

335 lines
16 KiB

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