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.
 
 
 
 

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