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.
 
 
 
 

343 lines
16 KiB

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