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.
 
 
 
 

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