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.
 
 
 
 

317 lines
15 KiB

  1. #!/bin/sh
  2. muttdir="$HOME/.config/mutt" # Main mutt config location
  3. bindir="$HOME/.config/mutt/bin" # Location of scripts run by mutt or the wizard
  4. [ ! -f "$muttdir/domains.csv" ] || [ ! -d "$bindir" ] && printf "Read the README. Be sure to put the repo in the right place before running.\\n" && exit 1
  5. [ ! -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
  6. accdir="$muttdir/accounts" # Directory for account settings
  7. maildir="$HOME/.local/share/mail" # Location of mail storage
  8. namere="^[a-z_][a-z0-9_-]*$" # Regex to ensure viable username
  9. emailre=".\+@.\+\\..\+" # Regex to confirm valid email address
  10. mbsyncrc="$HOME/.mbsyncrc"
  11. sslcert="/etc/ssl/certs/ca-certificates.crt"
  12. getprofiles() { \
  13. unset msmtp_header msmtp_profile mutt_profile mbsync_profile
  14. printf "Creating profiles for \`%s\`..." "$title"
  15. msmtp_header="defaults
  16. auth on
  17. tls on
  18. tls_trust_file $sslcert
  19. logfile ~/.config/msmtp/msmtp.log
  20. "
  21. msmtp_profile="
  22. account $title
  23. host $smtp
  24. port $sport
  25. from $fulladdr
  26. user $login
  27. passwordeval \"pass mutt-wizard-$title\""
  28. mbsync_profile="IMAPStore $title-remote
  29. Host $imap
  30. Port $iport
  31. User $login
  32. PassCmd \"pass mutt-wizard-$title\"
  33. SSLType IMAPS
  34. CertificateFile /etc/ssl/certs/ca-certificates.crt
  35. MaildirStore $title-local
  36. Subfolders Verbatim
  37. Path ~/.local/share/mail/$title/
  38. Inbox ~/.local/share/mail/$title/INBOX
  39. Flatten .
  40. Channel $title
  41. Expunge Both
  42. Master :$title-remote:
  43. Slave :$title-local:
  44. Patterns * !\"[Gmail]/All Mail\"
  45. Create Both
  46. SyncState *
  47. # End profile
  48. "
  49. if [ "$accounttype" = "offline" ]; then
  50. mutt_profile="# vim: filetype=neomuttrc
  51. # muttrc file for account $title
  52. set realname = \"$realname\"
  53. set from = \"$fulladdr\"
  54. set sendmail = \"/usr/bin/msmtp -a $title\"
  55. set folder = \"$maildir/$title\"
  56. set header_cache = $accdir/$title/cache/headers
  57. set message_cachedir = $accdir/$title/cache/bodies
  58. set certificate_file = $accdir/$title/certificates
  59. alias me $realname <$fulladdr>
  60. set mbox_type = Maildir
  61. set ssl_starttls = yes
  62. set ssl_force_tls = yes
  63. bind index,pager gg noop
  64. bind index,pager g noop
  65. bind index,pager M noop
  66. bind index,pager C noop
  67. bind index gg first-entry
  68. macro index o \"<shell-escape>$HOME/.config/mutt/bin/mailsync -V $title<enter>\" \"run mbsync to sync $title\"
  69. unmailboxes *
  70. set imap_check_subscribed
  71. "
  72. else
  73. mutt_profile="# vim: filetype=neomuttrc
  74. # muttrc file for account $title
  75. set realname = \"$realname\"
  76. set from = \"$fulladdr\"
  77. set sendmail = \"/usr/bin/msmtp -a $title\"
  78. set folder = \"imaps://$fulladdr@$imap:$iport\"
  79. set imap_user = \"$login\"
  80. set header_cache = $accdir/$title/cache/headers
  81. set message_cachedir = $accdir/$title/cache/bodies
  82. set certificate_file = $accdir/$title/certificates
  83. set imap_pass = \`pass mutt-wizard-$title\`
  84. set smtp_pass = \`pass mutt-wizard-$title\`
  85. alias me $realname <$fulladdr>
  86. set mbox_type = Maildir
  87. set ssl_starttls = yes
  88. set ssl_force_tls = yes
  89. bind index,pager gg noop
  90. bind index,pager g noop
  91. bind index,pager M noop
  92. bind index,pager C noop
  93. bind index gg first-entry
  94. unmailboxes *
  95. "
  96. fi
  97. printf "DONE.\\n"
  98. }
  99. addaccount() { \
  100. printf "Insert the \033[31memail address\033[0m that you want to autoconfigure for mutt/mbsync\\n\tEmail: \033[36m"
  101. read -r fulladdr
  102. printf "\033[0m"
  103. while ! echo "$fulladdr" | grep "$emailre" >/dev/null; do
  104. printf "That is not a valid \033[31memail address\033[0m, please retype the desired email.\\n\\nEmail: \033[36m\t"
  105. read -r fulladdr
  106. printf "\033[0m"
  107. done
  108. domain="$(echo "$fulladdr" | sed "s/.*@//")"
  109. printf "\\nSearching for \033[32m%s\033[0m in \033[34m\`domains.csv\`\033[0m..." "$domain"
  110. serverinfo="$(grep "$domain" "$muttdir/domains.csv" 2>/dev/null)"
  111. if [ -z "$serverinfo" ]; then
  112. 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"
  113. printf "Insert the IMAP server for your email provider (excluding the port number)\\n\033[36m\t"
  114. read -r imap
  115. printf "\033[0mWhat is your server's IMAP port number? (Usually something like 993)\\n\033[36m\t"
  116. read -r iport
  117. printf "\033[0mInsert the SMTP server for your email provider (excluding the port number)\\n\033[36m\t"
  118. read -r smtp
  119. printf "\033[0mWhat is your server's SMTP port number? (Usually 587 or 465)\\n\033[36m\t"
  120. read -r sport
  121. 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"
  122. else
  123. IFS=, read -r service imap iport smtp sport <<EOF
  124. $serverinfo
  125. EOF
  126. 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"
  127. case "$service" in
  128. 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" ;;
  129. 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" ;;
  130. esac
  131. fi
  132. printf "Enter the \033[35mfull name\033[0m you want to be identified by on this account.\\n\tReal name: "
  133. read -r realname
  134. 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: "
  135. read -r title
  136. while ! echo "$title" | grep "$namere" >/dev/null; do
  137. printf "\033[31mTry again\033[0m. Pick a nickname that is one word only including lowercase letters and _ or -.\\n\tAccount name: \033[36m\t"
  138. read -r title
  139. printf "\033[0m"
  140. done
  141. printf "If your account has a special username different from your address, insert it now. Otherwise leave this prompt totally blank.\\n\tLogin(?): \033[36m\t"
  142. read -r login
  143. printf "\033[0m"
  144. [ -z "$login" ] && login="$fulladdr"
  145. tmpdir="$(mktemp -d)"
  146. grep "i[0-9]" "$muttdir/personal.muttrc" 2>/dev/null | awk '{print $3}' | sed -e 's/i//g' > "$tmpdir/mutt_used"
  147. seq 1 9 > "$tmpdir/mutt_all"
  148. idnum=$(diff "$tmpdir/mutt_all" "$tmpdir/mutt_used" | sed -n 2p | awk '{print $2}')
  149. getpass
  150. getprofiles
  151. mkdir -p "$accdir/$title/cache/bodies" "$HOME/.config/msmtp"
  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/$title.muttrc"
  155. echo "$mbsync_profile" >> "$mbsyncrc"
  156. echo "macro index,pager i$idnum '<sync-mailbox><enter-command>source $accdir/$title.muttrc<enter><change-folder>!<enter>;<check-stats>' \"switch to $fulladdr\"" >> "$muttdir/personal.muttrc"
  157. ! grep "^source.*.muttrc" "$muttdir/personal.muttrc" >/dev/null && echo "source $accdir/$title.muttrc" >> "$muttdir/personal.muttrc"
  158. trysync && finalize
  159. }
  160. getpass() { pass rm -f "mutt-wizard-$title" >/dev/null 2>&1
  161. while ! pass ls | grep -o " mutt-wizard-$title$" >/dev/null 2>&1;do
  162. pass insert "mutt-wizard-$title"; done;}
  163. formatShortcut() { \
  164. while read -r data; do { echo "macro index,pager g$1 \"<change-folder>$data<enter>\" \"go to $2\" # autogenerated"
  165. echo "macro index,pager M$1 \"<save-message>$data<enter>\" \"move mail to $2\" # autogenerated"
  166. echo "macro index,pager C$1 \"<copy-message>$data<enter>\" \"copy mail to $2\" # autogenerated"; } >> "$accdir/$3.muttrc"
  167. done ;}
  168. trysync() { ! ping -q -c 1 1.1.1.1 > /dev/null && printf "No internet connection detected.\\nTry rerunning \`mbsync %s\` manually when connection is established, then select the option to detect mailboxes and finalize installation.\\n" "$title" && return 1
  169. mkdir -p "$maildir/$title"
  170. if mailboxes="$(mbsync -l "$title" | sed 's/\//./')" >/dev/null 2>&1; then
  171. [ "$accounttype" = "online" ] && sed -i "/IMAPStore $title-remote$/,/# End profile/d" "$mbsyncrc"
  172. printf "\033[32mMailboxes detected.\033[0m\\n"
  173. echo "$mailboxes" | xargs -I {} mkdir -p "$maildir/$title/{}"
  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. delete "$title"; return 1
  177. fi
  178. }
  179. finalize() { \
  180. boxes="$(du -a "$maildir/$title/"* -d 0 | sed "s/^.*\//=/")"
  181. [ -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
  182. echo "$boxes" > "$tmpdir/title_boxes"
  183. printf "Setting up the mutt sidebar...\\n"
  184. sidebar_width="$(sed -n -e '/^set sidebar_width/p' "$muttdir/muttrc" | awk -F'=' '{print $2}')"
  185. delim="$(head -c "$sidebar_width" < /dev/zero | tr '\0' =)"
  186. printf "Setting default mailboxes for your Inbox, Sent, Drafts and Trash in mutt...\\n"
  187. oneline="$(sed -e "s/^\|$/\"/g" "$tmpdir/title_boxes" | tr "\n" " ")"
  188. oneline="=$title $delim $oneline"
  189. spoolfile=$(grep -i -m 1 "$tmpdir/title_boxes" -e inbox | sed -e 's/=/+/g')
  190. record=$(grep -i -m 1 "$tmpdir/title_boxes" -e sent | sed -e 's/=/+/g')
  191. postponed=$(grep -i -m 1 "$tmpdir/title_boxes" -e draft | sed -e 's/=/+/g')
  192. trash=$(grep -i -m 1 "$tmpdir/title_boxes" -e trash | sed -e 's/=/+/g')
  193. sed -i "/^mailboxes\|^set record\|^set postponed\|^set trash\|^set spoolfile/d" "$accdir/$title.muttrc"
  194. { echo "set spoolfile = \"$spoolfile\""; echo "set record = \"$record\""; echo "set postponed = \"$postponed\""; echo "set trash = \"$trash\""; } >> "$accdir/$title.muttrc"
  195. echo mailboxes "$oneline" >> "$accdir/$title.muttrc"
  196. printf "Setting up your keyboard shortcuts for jumping between mailboxes...\\n"
  197. sed -i "/# autogenerated/d" "$accdir/$title.muttrc"
  198. grep -i -m 1 inbox "$tmpdir/title_boxes" | formatShortcut i inbox "$title"
  199. grep -i -m 1 sent "$tmpdir/title_boxes" | formatShortcut s sent "$title"
  200. grep -i -m 1 draft "$tmpdir/title_boxes" | formatShortcut d drafts "$title"
  201. grep -i -m 1 trash "$tmpdir/title_boxes" | formatShortcut t trash "$title"
  202. grep -i -m 1 spam "$tmpdir/title_boxes" | formatShortcut S spam "$title"
  203. grep -i -m 1 junk "$tmpdir/title_boxes" | formatShortcut j junk "$title"
  204. grep -i -m 1 archive "$tmpdir/title_boxes" | formatShortcut a archive "$title"
  205. [ "$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"
  206. }
  207. confirm() { printf "Are you \033[31;1mreally\033[0m sure you want to %s?\\n\t" "$@" && read -r input && ! echo "$input" | grep -i "y\(es\)*" >/dev/null && printf "That doesn't seem like a yes to me.\\n\\n" && return 1
  208. printf "Are you really, really sure you want to %s?\\n\t" "$@" && read -r input && ! echo "$input" | grep -i "y\(es\)*" >/dev/null && printf "That doesn't seem like a yes to me.\\n\\n" && return 1
  209. return 0 ;}
  210. pick() { numbered="$(find "$accdir" -mindepth 1 -maxdepth 1 -type d -printf "%f\n" | nl)"
  211. [ "$(echo "$numbered" | wc -w)" = 0 ] && printf "No accounts to choose from.\\n" && return 1
  212. printf "Select an accounts to %s:\\n" "$1"
  213. echo "$numbered"
  214. printf "\033[36m\t"
  215. read -r input
  216. printf "\033[0m"
  217. [ -z "$input" ] && return 1
  218. title="$(echo "$numbered" | grep "$input" | awk '{print $2}')"
  219. [ -z "$title" ] && printf "Invalid response." && return 1
  220. return 0 ;}
  221. delete() { sed -i "/IMAPStore $title-remote$/,/# End profile/d" "$mbsyncrc"
  222. rm -rf "${accdir:?}/${title:?}" "$accdir/$title.muttrc"
  223. sed -i "/$title.muttrc/d" "$muttdir/personal.muttrc"
  224. sed -i "/account $title/,/^\(\s*$\|account\)/d" "$HOME/.config/msmtp/config"
  225. }
  226. choosecron() { ! pgrep crond >/dev/null && echo "No cron manager running. Install/enable one and then select this option again." && return 1
  227. if crontab -l | grep mailsync >/dev/null; then
  228. echo "Active mail sync cronjob detected. Do you want to remove it?"
  229. printf "\033[36m\t"
  230. read -r rmyn
  231. printf "\033[0m"
  232. echo "$rmyn" | grep -i "^y\(es\)*$" >/dev/null && crontab -l | sed '/mailsync/d' | crontab - >/dev/null && echo "Mail sync turned off."
  233. else
  234. echo "How many minutes between each mail sync?"
  235. printf "\033[36m\t"
  236. read -r minnum
  237. printf "\033[0m"
  238. while ! echo "$minnum" | grep "^[0-9]\+$" >/dev/null; do
  239. printf "That doesn't look like a number. How many minutes between each mail sync?\\n\033[36m\t"
  240. read -r minnum
  241. printf "\033[0m"
  242. done
  243. (crontab -l; echo "*/$minnum * * * * export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus; export DISPLAY=:0; . \$HOME/.profile; $muttdir/bin/mailsync") | crontab - &&
  244. echo "Cronjob added. Mail will sync every $minnum minutes. Be sure you have your cron manager running."
  245. fi ;}
  246. addtype() { ! command -v mbsync >/dev/null && printf "You must have the \`mbsync\` command to add an account.\\nIt syncs mail and detects remote mailboxes.\\nYou can typically get it by installing \`isync\`.\\n" && return 1
  247. while : ; do
  248. printf "Do you want to keep your mail for this account offline with mbsync? [yes/no]\\n\t"
  249. read -r offnot
  250. case "$offnot" in
  251. [Yy]es) accounttype="offline" && break ;;
  252. [Nn]o) accounttype="online" && break ;;
  253. *) echo "I don't know what you mean. Try again or press ctrl-c to quit." ;;
  254. esac
  255. done
  256. addaccount ;}
  257. main() { while : ; do
  258. printf "Welcome to:\\n\033[34m"
  259. cat << "EOF"
  260. _ __ ___ _ _| |_| |_ __ _(_)______ _ _ __ __| |
  261. | '_ ` _ \| | | | __| __|___\ \ /\ / / |_ / _` | '__/ _` |
  262. | | | | | | |_| | |_| ||_____\ V V /| |/ / (_| | | | (_| |
  263. |_| |_| |_|\__,_|\__|\__| \_/\_/ |_/___\__,_|_| \__,_|
  264. EOF
  265. printf "\033[0mMade and maintained by Luke Smith <https://lukesmith.xyz>
  266. What would you like \033[32mmutt-wizard\033[0m to do?
  267. 1 Add an email account
  268. 2 Change an account's password
  269. 3 Remove an account
  270. 4 Purge all account data
  271. 5 Enable/disable autosync
  272. 0 Exit
  273. Input a number to continue or press ctrl-c.\\n"
  274. printf "\033[36m\t"
  275. read -r choice
  276. printf "\033[0m"
  277. case "$choice" in
  278. 1) addtype ;;
  279. 2) pick "change the password of" && confirm "change \`$title\`'s password" && getpass ;;
  280. 3) pick delete && confirm "delete the \`$title\` profile" && delete ;;
  281. 4) confirm "delete all account data" && rm -rf "$mbsyncrc" "$accdir" "$muttdir/personal.muttrc" && echo "All configs and account settings have been purged." ;;
  282. 5) choosecron ;;
  283. 0) break ;;
  284. *) printf "Invalid input.\\n"
  285. esac
  286. printf "Press Enter to continue.\\n"
  287. stty -echo
  288. read -r null
  289. stty echo
  290. done ;}
  291. main
  292. rm -rf "$tmpdir"