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.
 
 
 
 

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