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.
 
 
 
 

293 lines
14 KiB

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