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.
 
 
 
 

410 lines
17 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. [ ! -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
  10. gpgemail="$(grep "$emailre" "$creddir/gpgemail")" # Get previously set gpg email address
  11. tmpdir="$(mktemp -d)"
  12. GPG="gpg"; command -v gpg >/dev/null || GPG="gpg2" # Ensure proper gpg command
  13. # Get certificate location depending on OS. Linux is elsewhere condition.
  14. case "$(uname)" in
  15. "Darwin") sslcert="/usr/local/etc/openssl/cert.pem" ;;
  16. *) sslcert="/etc/ssl/certs/ca-certificates.crt" ;;
  17. esac
  18. getprofiles() { \
  19. unset msmtp_header msmtp_profile mutt_profile offlineimap_header offlineimap_profile
  20. printf "Creating profiles for \`%s\`..." "$title"
  21. msmtp_header="defaults
  22. auth on
  23. tls on
  24. tls_trust_file $sslcert
  25. logfile ~/.config/msmtp/msmtp.log
  26. "
  27. msmtp_profile="
  28. account $title
  29. host $smtp
  30. port $sport
  31. from $login
  32. user $login
  33. passwordeval \"$GPG -d --quiet --for-your-eyes-only --no-tty $creddir/$title.gpg | sed -e '\$a\\'\"
  34. "
  35. if [ "$accounttype" = "offline" ]; then
  36. mutt_profile="# vim: filetype=neomuttrc
  37. # muttrc file for account $title
  38. set realname = \"$realname\"
  39. set from = \"$fulladdr\"
  40. set sendmail = \"/usr/bin/msmtp -a $title\"
  41. set folder = \"$maildir/$title\"
  42. set header_cache = $accdir/$title/cache/headers
  43. set message_cachedir = $accdir/$title/cache/bodies
  44. set certificate_file = $accdir/$title/certificates
  45. source \"$bindir/getmuttpass $title |\"
  46. alias me $realname <$fulladdr>
  47. set mbox_type = Maildir
  48. set ssl_starttls = yes
  49. set ssl_force_tls = yes
  50. bind index,pager gg noop
  51. bind index,pager g noop
  52. bind index,pager M noop
  53. bind index,pager C noop
  54. bind index gg first-entry
  55. unmailboxes *
  56. set imap_check_subscribed
  57. "
  58. offlineimap_header="[general]
  59. accounts =
  60. starttls = yes
  61. ssl = true
  62. pythonfile = $bindir/imappwd.py
  63. "
  64. offlineimap_profile="
  65. [Account $title]
  66. localrepository = $title-local
  67. remoterepository = $title-remote
  68. [Repository $title-remote]
  69. auth_mechanisms = LOGIN
  70. type = $type
  71. remoteuser = $login
  72. remotepasseval = mailpasswd(\"$title\")
  73. remoteport = $iport
  74. sslcacertfile = $sslcert
  75. $ifgoogleline
  76. [Repository $title-local]
  77. type = Maildir
  78. localfolders = $maildir/$title
  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://$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/offlineIMAP\\n\\nEmail: "
  108. printf "\033[36m\t"
  109. read -r fulladdr
  110. printf "\033[0m"
  111. while ! echo "$fulladdr" | grep "$emailre" >/dev/null; do
  112. printf "That is not a valid \033[31memail address\033[0m, please retype the desired email.\\n\\nEmail: "
  113. printf "\033[36m\t"
  114. read -r fulladdr
  115. printf "\033[0m"
  116. done
  117. domain="$(echo "$fulladdr" | sed "s/.*@//")"
  118. printf "\\nSearching for \033[32m%s\033[0m in \033[34m\`domains.csv\`\033[0m..." "$domain"
  119. serverinfo="$(grep "$domain" "$muttdir/domains.csv")"
  120. if [ -z "$serverinfo" ]; then
  121. 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"
  122. printf "Insert the IMAP server for your email provider (excluding the port number)\\n"
  123. printf "\033[36m\t"
  124. read -r imap
  125. printf "\033[0m"
  126. printf "What is your server's IMAP port number? (Usually something like 993)\\n"
  127. printf "\033[36m\t"
  128. read -r iport
  129. printf "\033[0m"
  130. printf "Insert the SMTP server for your email provider (excluding the port number)\\n"
  131. printf "\033[36m\t"
  132. read -r smtp
  133. printf "\033[0m"
  134. printf "What is your server's SMTP port number? (Usually 587 or 465)\\n"
  135. printf "\033[36m\t"
  136. read -r sport
  137. printf "\033[0m"
  138. printf "\\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"
  139. else
  140. IFS=, read -r service imap iport smtp sport <<EOF
  141. $serverinfo
  142. EOF
  143. 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"
  144. fi
  145. cont
  146. printf "Enter the \033[35mfull name\033[0m you want to be identified by on this account.\\n\tReal name: "
  147. read -r realname
  148. 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: "
  149. read -r title
  150. while ! echo "$title" | grep "$namere" >/dev/null; do
  151. printf "\033[31mTry again\033[0m. Pick a nickname that is one word only including lowercase letters and _ or -.\\n\tAccount name: "
  152. printf "\033[36m\t"
  153. read -r title
  154. printf "\033[0m"
  155. done
  156. printf "If your account has a special username different from your address, insert it now. Otherwise leave this prompt totally blank.\\n\tLogin(?): "
  157. printf "\033[36m\t"
  158. read -r login
  159. printf "\033[0m"
  160. [ -z "$login" ] && login="$fulladdr"
  161. if [ "$service" = "gmail.com" ]; then
  162. type="Gmail"; ifgoogleline="folderfilter = lambda foldername: foldername not in ['[Gmail]/All Mail']
  163. ssl_version = tls1_2"
  164. printf "\033[1mGoogle\033[0m mail account detected. Remember to check the README to make sure of Google-specific settings you must enable.\\n"
  165. else
  166. type="IMAP"; ifgoogleline="remotehost = $imap"
  167. fi
  168. grep "i[0-9]" "$muttdir/personal.muttrc" | awk '{print $3}' | sed -e 's/i//g' > "$tmpdir/mutt_used"
  169. seq 1 9 > "$tmpdir/mutt_all"
  170. idnum=$(diff "$tmpdir/mutt_all" "$tmpdir/mutt_used" | sed -n 2p | awk '{print $2}')
  171. getpass
  172. getprofiles
  173. mkdir -p "$accdir/$title/cache/bodies" "$HOME/.config/msmtp"
  174. [ ! -f "$HOME/.config/msmtp/config" ] && echo "$msmtp_header" > "$HOME/.config/msmtp/config"
  175. echo "$msmtp_profile" >> "$HOME/.config/msmtp/config"
  176. echo "$mutt_profile" > "$accdir/$title.muttrc"
  177. if [ "$accounttype" = "offline" ]; then
  178. mkdir -p "$HOME/.config/offlineimap/"
  179. [ ! -f "$HOME/.config/offlineimap/config" ] && echo "$offlineimap_header" > "$HOME/.config/offlineimap/config"
  180. echo "$offlineimap_profile" >> "$HOME/.config/offlineimap/config"
  181. sed -i "s/^accounts =.*[a-zA-Z]$/&, $title/g;s/^accounts =\\s*$/accounts = $title/g" "$HOME/.config/offlineimap/config"
  182. fi
  183. echo "macro index,pager i$idnum '<sync-mailbox><enter-command>source \"$muttdir\"/accounts/$title.muttrc<enter><change-folder>!<enter>;<check-stats>'" >> "$muttdir/personal.muttrc"
  184. ! grep "^source.*.muttrc" "$muttdir/personal.muttrc" >/dev/null && echo "source $accdir/$title.muttrc" >> "$muttdir/personal.muttrc"
  185. trysync && finalize
  186. }
  187. getpass() { \
  188. 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"
  189. stty -echo
  190. read -r password
  191. stty echo
  192. echo "$password" > "$tmpdir/$title"
  193. printf "Encrypting your password with %s..." "$GPG"
  194. "$GPG" -r "$gpgemail" --encrypt "$tmpdir/$title"
  195. printf "DONE\\nShredding all memory of your password for safety's sake..."
  196. unset password
  197. shred -u "$tmpdir/$title"
  198. mkdir -p "$creddir"
  199. mv "$tmpdir/$title.gpg" "$creddir/"
  200. printf "DONE.\\n"
  201. }
  202. askgpg() { \
  203. 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: "
  204. printf "\033[36m\t"
  205. read -r gpgemail
  206. printf "\033[0m"
  207. while ! echo "$gpgemail" | grep "$emailre" >/dev/null; do
  208. printf "That is not a valid email address. Please try again.\\nEmail: "
  209. printf "\033[36m\t"
  210. read -r gpgemail
  211. printf "\033[0m"
  212. done
  213. if "$GPG" -K | grep "<$gpgemail>" >/dev/null; then
  214. mkdir -p "$creddir"
  215. echo "$gpgemail" > "$creddir/gpgemail"
  216. else
  217. 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"
  218. exit 1
  219. fi
  220. }
  221. formatShortcut() { \
  222. while read -r data; do { echo "macro index,pager g$1 \"<change-folder>$data<enter>\" \"Go to $2.\" # autogenerated"
  223. echo "macro index,pager M$1 \"<save-message>$data<enter>\" \"Move mail to $2.\" # autogenerated"
  224. echo "macro index,pager C$1 \"<copy-message>$data<enter>\" \"Copy mail to $2.\" # autogenerated"; } >> "$muttdir/accounts/$3.muttrc"
  225. done ;}
  226. trysync() { \
  227. ! ping -q -c 1 1.1.1.1 > /dev/null && printf "No internet connection detected.\\nTry rerunning offlineimap manually when connection is established, then select the option to detect mailboxes and finalize installation.\\n" && return 1
  228. printf "\033[32mYou must have an internet connection to continue.\033[0m\\nmutt-wizard will run offlineimap briefly to (1) ensure that login details are functional and (2) allow offlineimap to tell us what mailboxes your email account has.\\nAfter around 15 seconds, mutt-wizard will kill the process and continue.\\nYou can run offlineimap manually to finish the mail sync later.\\n"
  229. cont
  230. (sleep 15; killall offlineimap; killall offlineimap; killall offlineimap)>/dev/null 2>&1 &
  231. mkdir -p "$maildir"
  232. offlineimap -qoa "$title"
  233. if ls -d "$maildir/$title/"* >/dev/null 2>&1; then
  234. printf "\033[32mSync successful.\033[0m\\n"; return
  235. else
  236. printf "\033[31m\033[31mSync not successful.\033[0m Try running offlineimap manually after double-checking your password and server settings.\\nThen select to finalize the account.\\n"; return 1
  237. fi
  238. }
  239. finalize() { \
  240. boxes="$(du -a "$maildir/$title/"* -d 0 | sed "s/^.*\//=/")"
  241. [ -z "$boxes" ] && printf "\033[31mNo local mailboxes have been detected for %s.\033[0m\\nThis means that offlineimap has not been successfully run.\\nRun offlineimap, and if it has an error, be sure to check your password and server settings manually if needbe.\\n" "$title" && return
  242. echo "$boxes" > "$tmpdir/title_boxes"
  243. printf "Setting up the mutt sidebar...\\n"
  244. sidebar_width="$(sed -n -e '/^set sidebar_width/p' "$muttdir/muttrc" | awk -F'=' '{print $2}')"
  245. delim="$(head -c "$sidebar_width" < /dev/zero | tr '\0' =)"
  246. printf "Setting default mailboxes for your Inbox, Sent, Drafts and Trash in mutt...\\n"
  247. oneline="$(sed -e "s/^\|$/\"/g" "$tmpdir/title_boxes" | tr "\n" " ")"
  248. oneline="=$title $delim $oneline"
  249. spoolfile=$(grep -i "$tmpdir/title_boxes" -e inbox | sed -e 's/=/+/g' | sed 1q)
  250. record=$(grep -i "$tmpdir/title_boxes" -e sent | sed -e 's/=/+/g' | sed 1q)
  251. postponed=$(grep -i "$tmpdir/title_boxes" -e draft | sed -e 's/=/+/g' | sed 1q)
  252. trash=$(grep -i "$tmpdir/title_boxes" -e trash | sed -e 's/=/+/g' | sed 1q)
  253. sed -i "/^mailboxes\|^set record\|^set postponed\|^set trash\|^set spoolfile/d" "$muttdir/accounts/$title.muttrc"
  254. { echo "set spoolfile = \"$spoolfile\""; echo "set record = \"$record\""; echo "set postponed = \"$postponed\""; echo "set trash = \"$trash\""; } >> "$muttdir/accounts/$title.muttrc"
  255. echo mailboxes "$oneline" >> "$muttdir/accounts/$title.muttrc"
  256. printf "Setting up your keyboard shortcuts for jumping between mailboxes...\\n"
  257. sed -i "/# autogenerated/d" "$muttdir/accounts/$title.muttrc"
  258. grep -i "$tmpdir/title_boxes" -e inbox | sed 1q | formatShortcut i inbox "$title"
  259. grep -i "$tmpdir/title_boxes" -e sent | sed 1q | formatShortcut s sent "$title"
  260. grep -i "$tmpdir/title_boxes" -e draft | sed 1q | formatShortcut d drafts "$title"
  261. grep -i "$tmpdir/title_boxes" -e trash | sed 1q | formatShortcut t trash "$title"
  262. grep -i "$tmpdir/title_boxes" -e spam | sed 1q | formatShortcut S spam "$title"
  263. grep -i "$tmpdir/title_boxes" -e junk | sed 1q | formatShortcut j junk "$title"
  264. grep -i "$tmpdir/title_boxes" -e archive | sed 1q | formatShortcut a archive "$title"
  265. printf "All done.\\n"
  266. }
  267. wipe () { \
  268. printf "Are you \033[31;1mreally\033[0m sure you want to delete all email accounts?\\n" && 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
  269. printf "Are you really, really sure?" && 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
  270. #rm -rf "$HOME/.config/offlineimap/config" "$accdir" "$creddir" "$muttdir/personal.muttrc"
  271. echo deleted
  272. }
  273. pick() { \
  274. numbered="$(grep "^accounts *=" "$HOME/.config/offlineimap/config" | sed 's/accounts *= *//g;s/,/ /g;s/ \+/\n/g' | nl)"
  275. [ "$(echo "$numbered" | wc -w)" = 0 ] && printf "No accounts to delete.\\n" && return 1
  276. printf "Select an accounts to %s:\\n" "$1"
  277. echo "$numbered"
  278. printf "\033[36m\t"
  279. read -r input
  280. printf "\033[0m"
  281. [ -z "$input" ] && return 1
  282. title="$(echo "$numbered" | grep "$input" | awk '{print $2}')"
  283. [ -z "$title" ] && printf "Invalid response." && return 1
  284. [ -n "$2" ] && printf "Are you sure you want to %s the \`%s\` account?\\n" "$1" "$title" && read -r input && echo "$input" | grep -i "y\(es\)*" >/dev/null
  285. }
  286. delete() { sed -i "
  287. /Account $title]/,/Account/{//!d}
  288. /Account $title]/d
  289. s/ $title\(,\|$\)//g
  290. s/=$title\(,\|$\)/=/g
  291. s/,$//g
  292. " "$HOME/.config/offlineimap/config"
  293. rm -rf "${accdir:?}/${title:?}" "$creddir/$title.gpg" "$accdir/$title.muttrc"
  294. sed -i "/$title.muttrc/d" "$muttdir/personal.muttrc"
  295. # Delete from the line matching the account name, until the next account or empty line
  296. sed -i "/account $title/,/^\(\s*$\|account\)/d" "$HOME/.config/msmtp/config";}
  297. choosecron() { ! pgrep crond >/dev/null && echo "No cron manager running. Install/enable one and then select this option again." && return 1
  298. if crontab -l | grep mailsync >/dev/null; then
  299. echo "Active mail sync cronjob detected. Do you want to remove it?"
  300. printf "\033[36m\t"
  301. read -r rmyn
  302. printf "\033[0m"
  303. echo "$rmyn" | grep -i "^y\(es\)*$" >/dev/null && crontab -l | sed '/mailsync/d' | crontab - >/dev/null && echo "Mail sync turned off."
  304. else
  305. echo "How many minutes between each mail sync?"
  306. printf "\033[36m\t"
  307. read -r minnum
  308. printf "\033[0m"
  309. while ! echo "$minnum" | grep "^[0-9]\+$" >/dev/null; do
  310. printf "That doesn't look like a number. How many minutes between each mail sync?"
  311. printf "\033[36m\t"
  312. read -r minnum
  313. printf "\033[0m"
  314. done
  315. (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 - &&
  316. echo "Cronjob added. Mail will sync every $minnum minutes. Be sure you have your cron manager running."
  317. fi
  318. }
  319. cont() { [ -n "$1" ] && printf "%s\\n" "$1"
  320. printf "Press Enter to continue.\\n"
  321. stty -echo
  322. read -r null
  323. stty echo
  324. }
  325. addtype() { \
  326. while : ; do
  327. echo "Do you want to keep your mail for this account offline with offlineimap? [yes/no]"
  328. read -r offnot
  329. case "$offnot" in
  330. [Yy]es) accounttype="offline" && break ;;
  331. [Nn]o) accounttype="online" && break ;;
  332. *) echo "I don't know what you mean. Try again or press ctrl-c to quit." ;;
  333. esac
  334. done
  335. addaccount
  336. }
  337. main() { \
  338. while : ; do
  339. [ -z "$gpgemail" ] && askgpg
  340. printf "Current GPG key email: %s\\n\033[34m" "$gpgemail"
  341. cat << "EOF"
  342. _ __ ___ _ _| |_| |_ __ _(_)______ _ _ __ __| |
  343. | '_ ` _ \| | | | __| __|___\ \ /\ / / |_ / _` | '__/ _` |
  344. | | | | | | |_| | |_| ||_____\ V V /| |/ / (_| | | | (_| |
  345. |_| |_| |_|\__,_|\__|\__| \_/\_/ |_/___\__,_|_| \__,_|
  346. EOF
  347. printf "\033[0mMade and maintained by Luke Smith <https://lukesmith.xyz>
  348. What would you like \033[32mmutt-wizard\033[0m to do?
  349. \033[31m1 Add an email account\033[0m
  350. 2 Autodetect mailboxes
  351. 3 Change an account's password
  352. 4 Remove an account
  353. 5 Change GPG key pair used for encryption
  354. 6 Delete all account data
  355. 7 Enable/disable autosync
  356. 9 Edit this script
  357. 0 Exit
  358. Input a number to continue or press ctrl-c.\\n"
  359. printf "\033[36m\t"
  360. read -r choice
  361. printf "\033[0m"
  362. case "$choice" in
  363. 1) addtype ;;
  364. 2) pick finalize && finalize ;;
  365. 3) pick "change the password of" yes && getpass ;;
  366. 4) pick delete yes && delete ;;
  367. 5) askgpg ;;
  368. 6) wipe && printf "Account data purged." ;;
  369. 7) choosecron ;;
  370. 9) "$EDITOR" "$0"; exit ;;
  371. 0) break ;;
  372. *) printf "Invalid input.\\n"
  373. esac
  374. cont
  375. done
  376. }
  377. main
  378. rm -rf "$tmpdir"