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.
 
 
 
 

368 lines
16 KiB

  1. #!/bin/sh
  2. muttdir="$HOME/.config/mutt" # Main mutt config location
  3. accdir="$muttdir/accounts" # Directory for account settings
  4. maildir="$HOME/.local/share/mail" # Location of mail storage
  5. creddir="$HOME/.local/share/muttwizard" # Location of encrypted credentials
  6. bindir="$HOME/.config/mutt/bin" # Location of scripts run by mutt or the wizard
  7. namere="^[a-z_][a-z0-9_-]*$" # Regex to ensure viable username
  8. emailre=".\+@.\+\\..\+" # Regex to confirm valid email address
  9. mbsyncrc="$HOME/.mbsyncrc"
  10. [ ! -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
  11. gpgemail="$(grep "$emailre" "$creddir/gpgemail")" # Get previously set gpg email address
  12. tmpdir="$(mktemp -d)"
  13. GPG="gpg"; command -v gpg >/dev/null || GPG="gpg2" # Ensure proper gpg command
  14. # Get certificate location depending on OS. Linux is elsewhere condition.
  15. case "$(uname)" in
  16. "Darwin") sslcert="/usr/local/etc/openssl/cert.pem" ;;
  17. *) sslcert="/etc/ssl/certs/ca-certificates.crt" ;;
  18. esac
  19. getprofiles() { \
  20. unset msmtp_header msmtp_profile mutt_profile mbsync_profile
  21. printf "Creating profiles for \`%s\`..." "$title"
  22. msmtp_header="defaults
  23. auth on
  24. tls on
  25. tls_trust_file $sslcert
  26. logfile ~/.config/msmtp/msmtp.log
  27. "
  28. msmtp_profile="
  29. account $title
  30. host $smtp
  31. port $sport
  32. from $login
  33. user $login
  34. passwordeval \"$GPG -d --quiet --for-your-eyes-only --no-tty $creddir/$title.gpg | sed -e '\$a\\'\"
  35. "
  36. mbsync_profile="IMAPAccount $title
  37. Host $imap
  38. User $login
  39. Port $iport
  40. PassCmd \"gpg -q --for-your-eyes-only --no-tty -d ~/.local/share/muttwizard/$title.gpg\"
  41. SSLType IMAPS
  42. CertificateFile /etc/ssl/certs/ca-certificates.crt
  43. IMAPStore $title-remote
  44. Account $title
  45. MaildirStore $title-local
  46. Subfolders Verbatim
  47. Path ~/.local/share/mail/$title/
  48. Inbox ~/.local/share/mail/$title/INBOX
  49. Channel $title
  50. Expunge Both
  51. Master :$title-remote:
  52. Slave :$title-local:
  53. Patterns * !\"[Gmail]/All Mail\"
  54. Create Both
  55. SyncState *
  56. # End profile"
  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 = \"/usr/bin/msmtp -a $title\"
  63. set folder = \"$maildir/$title\"
  64. set header_cache = $accdir/$title/cache/headers
  65. set message_cachedir = $accdir/$title/cache/bodies
  66. set certificate_file = $accdir/$title/certificates
  67. source \"$bindir/getmuttpass $title |\"
  68. alias me $realname <$fulladdr>
  69. set mbox_type = Maildir
  70. set ssl_starttls = yes
  71. set ssl_force_tls = yes
  72. bind index,pager gg noop
  73. bind index,pager g noop
  74. bind index,pager M noop
  75. bind index,pager C noop
  76. bind index gg first-entry
  77. unmailboxes *
  78. set imap_check_subscribed
  79. "
  80. else
  81. mutt_profile="# vim: filetype=neomuttrc
  82. # muttrc file for account $title
  83. set realname = \"$realname\"
  84. set from = \"$fulladdr\"
  85. set sendmail = \"/usr/bin/msmtp -a $title\"
  86. set folder = \"imaps://$fulladdr@$imap:$iport\"
  87. set imap_user = \"$login\"
  88. set header_cache = $accdir/$title/cache/headers
  89. set message_cachedir = $accdir/$title/cache/bodies
  90. set certificate_file = $accdir/$title/certificates
  91. source \"$bindir/getmuttpass $title |\"
  92. alias me $realname <$fulladdr>
  93. set mbox_type = Maildir
  94. set ssl_starttls = yes
  95. set ssl_force_tls = yes
  96. bind index,pager gg noop
  97. bind index,pager g noop
  98. bind index,pager M noop
  99. bind index,pager C noop
  100. bind index gg first-entry
  101. unmailboxes *
  102. "
  103. fi
  104. printf "DONE.\\n"
  105. }
  106. addaccount() { \
  107. printf "Insert the \033[31memail address\033[0m that you want to autoconfigure for mutt/mbsync\\n\\nEmail: \033[36m\t"
  108. read -r fulladdr
  109. printf "\033[0m"
  110. while ! echo "$fulladdr" | grep "$emailre" >/dev/null; do
  111. printf "That is not a valid \033[31memail address\033[0m, please retype the desired email.\\n\\nEmail: \033[36m\t"
  112. read -r fulladdr
  113. printf "\033[0m"
  114. done
  115. domain="$(echo "$fulladdr" | sed "s/.*@//")"
  116. printf "\\nSearching for \033[32m%s\033[0m in \033[34m\`domains.csv\`\033[0m..." "$domain"
  117. serverinfo="$(grep "$domain" "$muttdir/domains.csv")"
  118. if [ -z "$serverinfo" ]; then
  119. 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"
  120. printf "Insert the IMAP server for your email provider (excluding the port number)\\n\033[36m\t"
  121. read -r imap
  122. printf "\033[0mWhat is your server's IMAP port number? (Usually something like 993)\\n\033[36m\t"
  123. read -r iport
  124. printf "\033[0mInsert the SMTP server for your email provider (excluding the port number)\\n\033[36m\t"
  125. read -r smtp
  126. printf "\033[0mWhat is your server's SMTP port number? (Usually 587 or 465)\\n\033[36m\t"
  127. read -r sport
  128. 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"
  129. else
  130. IFS=, read -r service imap iport smtp sport <<EOF
  131. $serverinfo
  132. EOF
  133. 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"
  134. fi
  135. cont
  136. printf "Enter the \033[35mfull name\033[0m you want to be identified by on this account.\\n\tReal name: "
  137. read -r realname
  138. 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: "
  139. read -r title
  140. while ! echo "$title" | grep "$namere" >/dev/null; do
  141. 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"
  142. read -r title
  143. printf "\033[0m"
  144. done
  145. 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"
  146. read -r login
  147. printf "\033[0m"
  148. [ -z "$login" ] && login="$fulladdr"
  149. grep "i[0-9]" "$muttdir/personal.muttrc" | awk '{print $3}' | sed -e 's/i//g' > "$tmpdir/mutt_used"
  150. seq 1 9 > "$tmpdir/mutt_all"
  151. idnum=$(diff "$tmpdir/mutt_all" "$tmpdir/mutt_used" | sed -n 2p | awk '{print $2}')
  152. getpass
  153. getprofiles
  154. mkdir -p "$accdir/$title/cache/bodies" "$HOME/.config/msmtp"
  155. [ ! -f "$HOME/.config/msmtp/config" ] && echo "$msmtp_header" > "$HOME/.config/msmtp/config"
  156. echo "$msmtp_profile" >> "$HOME/.config/msmtp/config"
  157. echo "$mutt_profile" > "$accdir/$title.muttrc"
  158. echo "$mbsync_profile" >> "$mbsyncrc"
  159. echo "macro index,pager i$idnum '<sync-mailbox><enter-command>source $accdir/$title.muttrc<enter><change-folder>!<enter>;<check-stats>'" >> "$muttdir/personal.muttrc"
  160. ! grep "^source.*.muttrc" "$muttdir/personal.muttrc" >/dev/null && echo "source $accdir/$title.muttrc" >> "$muttdir/personal.muttrc"
  161. trysync && finalize
  162. [ "$accounttype" = "offline" ] && sed -i "/IMAPAccount $title$/,/# End profile/d" "$mbsyncrc"
  163. }
  164. getpass() { \
  165. printf "Now enter your password for the \"%s\" account. Don't worry, this will be encrypted and only you with your GPG key can view it.\\n\tPassword: " "$title"
  166. stty -echo
  167. read -r password
  168. stty echo
  169. echo "$password" > "$tmpdir/$title"
  170. printf "Encrypting your password with %s..." "$GPG"
  171. "$GPG" -r "$gpgemail" --encrypt "$tmpdir/$title"
  172. printf "DONE\\nShredding all memory of your password for safety's sake..."
  173. unset password
  174. shred -u "$tmpdir/$title"
  175. mkdir -p "$creddir"
  176. mv "$tmpdir/$title.gpg" "$creddir/"
  177. printf "DONE.\\n"
  178. }
  179. askgpg() { \
  180. printf "To safely encrypt passwords, mutt-wizard requires that you have a GPG public/private key pair.\\n\\nPlease input the email address of your GPG key pair below.\\nEmail: \033[36m\t"
  181. read -r gpgemail
  182. printf "\033[0m"
  183. while ! echo "$gpgemail" | grep "$emailre" >/dev/null; do
  184. printf "That is not a valid email address. Please try again.\\nEmail: \033[36m\t"
  185. read -r gpgemail
  186. printf "\033[0m"
  187. done
  188. if "$GPG" -K | grep "<$gpgemail>" >/dev/null; then
  189. mkdir -p "$creddir"
  190. echo "$gpgemail" > "$creddir/gpgemail"
  191. else
  192. printf "You do not appear to have a private key associated with \033[33m%s\033[0m.\\nPlease generate a GPG key pair by running \`\033[32m%s --full-gen-key\033[0m\` and rerun the wizard.\\n" "$gpgemail" "$GPG"
  193. exit 1
  194. fi
  195. }
  196. formatShortcut() { \
  197. while read -r data; do { echo "macro index,pager g$1 \"<change-folder>$data<enter>\" \"Go to $2.\" # autogenerated"
  198. echo "macro index,pager M$1 \"<save-message>$data<enter>\" \"Move mail to $2.\" # autogenerated"
  199. echo "macro index,pager C$1 \"<copy-message>$data<enter>\" \"Copy mail to $2.\" # autogenerated"; } >> "$muttdir/accounts/$3.muttrc"
  200. done ;}
  201. trysync() { \
  202. ! 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
  203. printf "\033[32mYou must have an internet connection to continue.\033[0m\\nmutt-wizard will run mbsync briefly to (1) ensure that login details are functional and (2) allow mbsync to tell us what mailboxes your email account has.\\nAfter around 15 seconds, mutt-wizard will kill the process and continue.\\nYou can run mbsync manually to finish the mail sync later.\\n"
  204. cont
  205. mkdir -p "$maildir/$title"
  206. if mailboxes="$(mbsync -l "$title")" >/dev/null 2>&1; then
  207. printf "\033[32mMailboxes detected.\033[0m\\n"
  208. echo "$mailboxes" | xargs -I {} mkdir -p "$maildir/$title/{}"
  209. else
  210. printf "\033[31m\033[31mSync not successful.\033[0m Try running \`mbsync %s\` manually after double-checking your password and server settings.\\nThen select to finalize the account.\\n" "$title"; return 1
  211. fi
  212. }
  213. finalize() { \
  214. boxes="$(du -a "$maildir/$title/"* -d 0 | sed "s/^.*\//=/")"
  215. [ -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
  216. echo "$boxes" > "$tmpdir/title_boxes"
  217. printf "Setting up the mutt sidebar...\\n"
  218. sidebar_width="$(sed -n -e '/^set sidebar_width/p' "$muttdir/muttrc" | awk -F'=' '{print $2}')"
  219. delim="$(head -c "$sidebar_width" < /dev/zero | tr '\0' =)"
  220. printf "Setting default mailboxes for your Inbox, Sent, Drafts and Trash in mutt...\\n"
  221. oneline="$(sed -e "s/^\|$/\"/g" "$tmpdir/title_boxes" | tr "\n" " ")"
  222. oneline="=$title $delim $oneline"
  223. spoolfile=$(grep -i -m 1 "$tmpdir/title_boxes" -e inbox | sed -e 's/=/+/g')
  224. record=$(grep -i -m 1 "$tmpdir/title_boxes" -e sent | sed -e 's/=/+/g')
  225. postponed=$(grep -i -m 1 "$tmpdir/title_boxes" -e draft | sed -e 's/=/+/g')
  226. trash=$(grep -i -m 1 "$tmpdir/title_boxes" -e trash | sed -e 's/=/+/g')
  227. sed -i "/^mailboxes\|^set record\|^set postponed\|^set trash\|^set spoolfile/d" "$muttdir/accounts/$title.muttrc"
  228. { echo "set spoolfile = \"$spoolfile\""; echo "set record = \"$record\""; echo "set postponed = \"$postponed\""; echo "set trash = \"$trash\""; } >> "$muttdir/accounts/$title.muttrc"
  229. echo mailboxes "$oneline" >> "$muttdir/accounts/$title.muttrc"
  230. printf "Setting up your keyboard shortcuts for jumping between mailboxes...\\n"
  231. sed -i "/# autogenerated/d" "$muttdir/accounts/$title.muttrc"
  232. grep -i -m 1 inbox "$tmpdir/title_boxes" | formatShortcut i inbox "$title"
  233. grep -i -m 1 sent "$tmpdir/title_boxes" | formatShortcut s sent "$title"
  234. grep -i -m 1 draft "$tmpdir/title_boxes" | formatShortcut d drafts "$title"
  235. grep -i -m 1 trash "$tmpdir/title_boxes" | formatShortcut t trash "$title"
  236. grep -i -m 1 spam "$tmpdir/title_boxes" | formatShortcut S spam "$title"
  237. grep -i -m 1 junk "$tmpdir/title_boxes" | formatShortcut j junk "$title"
  238. grep -i -m 1 archive "$tmpdir/title_boxes" | formatShortcut a archive "$title"
  239. printf "All done.\\n"
  240. }
  241. 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
  242. 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
  243. return 0 ;}
  244. pick() { numbered="$(ls $accdir/*muttrc | sed "s/.*\///;s/\..*//" | nl)"
  245. [ "$(echo "$numbered" | wc -w)" = 0 ] && printf "No accounts to choose from.\\n" && return 1
  246. printf "Select an accounts to %s:\\n" "$1"
  247. echo "$numbered"
  248. printf "\033[36m\t"
  249. read -r input
  250. printf "\033[0m"
  251. [ -z "$input" ] && return 1
  252. title="$(echo "$numbered" | grep "$input" | awk '{print $2}')"
  253. [ -z "$title" ] && printf "Invalid response." && return 1
  254. return 0 ;}
  255. delete() { sed -i "/IMAPAccount $title$/,/# End profile/d" "$mbsyncrc"
  256. rm -rf "${accdir:?}/${title:?}" "$creddir/$title.gpg" "$accdir/$title.muttrc"
  257. sed -i "/$title.muttrc/d" "$muttdir/personal.muttrc"
  258. sed -i "/account $title/,/^\(\s*$\|account\)/d" "$HOME/.config/msmtp/config"
  259. #confirm "delete the local mail repository as well?" && rm -rf "${maildir:?}/${title:?}"
  260. }
  261. choosecron() { ! pgrep crond >/dev/null && echo "No cron manager running. Install/enable one and then select this option again." && return 1
  262. if crontab -l | grep mailsync >/dev/null; then
  263. echo "Active mail sync cronjob detected. Do you want to remove it?"
  264. printf "\033[36m\t"
  265. read -r rmyn
  266. printf "\033[0m"
  267. echo "$rmyn" | grep -i "^y\(es\)*$" >/dev/null && crontab -l | sed '/mailsync/d' | crontab - >/dev/null && echo "Mail sync turned off."
  268. else
  269. echo "How many minutes between each mail sync?"
  270. printf "\033[36m\t"
  271. read -r minnum
  272. printf "\033[0m"
  273. while ! echo "$minnum" | grep "^[0-9]\+$" >/dev/null; do
  274. printf "That doesn't look like a number. How many minutes between each mail sync?\\n\033[36m\t"
  275. read -r minnum
  276. printf "\033[0m"
  277. done
  278. (crontab -l; echo "*/$minnum * * * * export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus; export DISPLAY=:0; . \$HOME/.profile; $muttdir/bin/mailsync -f INBOX") | crontab - &&
  279. echo "Cronjob added. Mail will sync every $minnum minutes. Be sure you have your cron manager running."
  280. fi
  281. }
  282. cont() { printf "Press Enter to continue.\\n"
  283. stty -echo
  284. read -r null
  285. stty echo ; }
  286. addtype() { \
  287. ! 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
  288. while : ; do
  289. echo "Do you want to keep your mail for this account offline with mbsync? [yes/no]"
  290. read -r offnot
  291. case "$offnot" in
  292. [Yy]es) accounttype="offline" && break ;;
  293. [Nn]o) accounttype="online" && break ;;
  294. *) echo "I don't know what you mean. Try again or press ctrl-c to quit." ;;
  295. esac
  296. done
  297. addaccount
  298. }
  299. main() { \
  300. while : ; do
  301. [ -z "$gpgemail" ] && askgpg
  302. printf "Current GPG key email: %s\\n\033[34m" "$gpgemail"
  303. cat << "EOF"
  304. _ __ ___ _ _| |_| |_ __ _(_)______ _ _ __ __| |
  305. | '_ ` _ \| | | | __| __|___\ \ /\ / / |_ / _` | '__/ _` |
  306. | | | | | | |_| | |_| ||_____\ V V /| |/ / (_| | | | (_| |
  307. |_| |_| |_|\__,_|\__|\__| \_/\_/ |_/___\__,_|_| \__,_|
  308. EOF
  309. printf "\033[0mMade and maintained by Luke Smith <https://lukesmith.xyz>
  310. What would you like \033[32mmutt-wizard\033[0m to do?
  311. 1 Add an email account
  312. 2 Change an account's password
  313. 3 Remove an account
  314. 5 Change GPG key pair used for encryption
  315. 6 Delete all account data
  316. 7 Enable/disable autosync
  317. 0 Exit
  318. Input a number to continue or press ctrl-c.\\n"
  319. printf "\033[36m\t"
  320. read -r choice
  321. printf "\033[0m"
  322. case "$choice" in
  323. 1) addtype ;;
  324. 2) pick "change the password of" && confirm "change the password of" && getpass ;;
  325. 3) pick delete && confirm "delete the \`$title\` profile" && delete ;;
  326. 5) askgpg ;;
  327. 6) confirm "delete all account data" && wipe && rm -ri "$mbsyncrc" "$accdir" "$creddir" "$muttdir/personal.muttrc" && echo "All configs and account settings have been purged." ;;
  328. 7) choosecron ;;
  329. 0) break ;;
  330. *) printf "Invalid input.\\n"
  331. esac
  332. cont
  333. done
  334. }
  335. main
  336. rm -rf "$tmpdir"