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.
 
 
 
 

302 line
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. 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. alias me $realname <$fulladdr>
  56. set folder = \"$maildir/$title\"
  57. set header_cache = $accdir/$title/cache/headers
  58. set message_cachedir = $accdir/$title/cache/bodies
  59. set certificate_file = $accdir/$title/certificates
  60. set mbox_type = Maildir
  61. bind index,pager gg noop
  62. bind index,pager g noop
  63. bind index,pager M noop
  64. bind index,pager C noop
  65. bind index gg first-entry
  66. macro index o \"<shell-escape>mailsync -V $title<enter>\" \"run mbsync to sync $title\"
  67. unmailboxes *
  68. "
  69. else
  70. mutt_profile="# vim: filetype=neomuttrc
  71. # muttrc file for account $title
  72. set realname = \"$realname\"
  73. set from = \"$fulladdr\"
  74. set sendmail = \"/usr/bin/msmtp -a $title\"
  75. alias me $realname <$fulladdr>
  76. set folder = \"imaps://$fulladdr@$imap:$iport\"
  77. set imap_user = \"$login\"
  78. set header_cache = $accdir/$title/cache/headers
  79. set message_cachedir = $accdir/$title/cache/bodies
  80. set certificate_file = $accdir/$title/certificates
  81. set imap_pass = \`pass mutt-wizard-$title\`
  82. set mbox_type = Maildir
  83. set ssl_starttls = yes
  84. set ssl_force_tls = yes
  85. bind index,pager gg noop
  86. bind index,pager g noop
  87. bind index,pager M noop
  88. bind index,pager C noop
  89. bind index gg first-entry
  90. unmailboxes *
  91. "
  92. fi
  93. printf "DONE.\\n"
  94. }
  95. askinfo() { \
  96. printf "Insert the \033[31memail address\033[0m that you want to autoconfigure for mutt/mbsync\\n\tEmail: \033[36m"
  97. read -r fulladdr
  98. printf "\033[0m"
  99. while ! echo "$fulladdr" | grep "$emailre" >/dev/null; do
  100. printf "That is not a valid \033[31memail address\033[0m, please retype the desired email.\\n\\nEmail: \033[36m\t"
  101. read -r fulladdr
  102. printf "\033[0m"
  103. done
  104. domain="$(echo "$fulladdr" | sed "s/.*@//")"
  105. printf "\\nSearching for \033[32m%s\033[0m in \033[34m\`domains.csv\`\033[0m..." "$domain"
  106. serverinfo="$(grep "$domain" "$muttshare/domains.csv" 2>/dev/null)"
  107. if [ -z "$serverinfo" ]; then
  108. 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"
  109. printf "Insert the IMAP server for your email provider (excluding the port number)\\n\033[36m\t"
  110. read -r imap
  111. printf "\033[0mWhat is your server's IMAP port number? (Usually something like 993)\\n\033[36m\t"
  112. read -r iport
  113. printf "\033[0mInsert the SMTP server for your email provider (excluding the port number)\\n\033[36m\t"
  114. read -r smtp
  115. printf "\033[0mWhat is your server's SMTP port number? (Usually 587 or 465)\\n\033[36m\t"
  116. read -r sport
  117. 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"
  118. else
  119. IFS=, read -r service imap iport smtp sport <<EOF
  120. $serverinfo
  121. EOF
  122. 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"
  123. case "$service" in
  124. 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" ;;
  125. 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" ;;
  126. esac
  127. fi
  128. printf "Enter the \033[35mfull name\033[0m you want to be identified by on this account.\\n\tReal name: "
  129. read -r realname
  130. 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: "
  131. read -r title
  132. while ! echo "$title" | grep "$namere" >/dev/null; do
  133. 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"
  134. read -r title
  135. printf "\033[0m"
  136. done
  137. 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"
  138. read -r login
  139. printf "\033[0m"
  140. [ -z "$login" ] && login="$fulladdr"
  141. tmpdir="$(mktemp -d)"
  142. grep "i[0-9]" "$muttdir/personal.muttrc" 2>/dev/null | awk '{print $3}' | sed -e 's/i//g' > "$tmpdir/mutt_used"
  143. seq 1 9 > "$tmpdir/mutt_all"
  144. idnum=$(diff "$tmpdir/mutt_all" "$tmpdir/mutt_used" | sed -n 2p | awk '{print $2}')
  145. getpass
  146. getprofiles
  147. mkdir -p "$accdir/$title/cache/bodies" "$HOME/.config/msmtp"
  148. [ ! -f "$HOME/.config/msmtp/config" ] && echo "$msmtp_header" > "$HOME/.config/msmtp/config"
  149. echo "$msmtp_profile" >> "$HOME/.config/msmtp/config"
  150. echo "$mutt_profile" > "$accdir/$title.muttrc"
  151. echo "$mbsync_profile" >> "$mbsyncrc"
  152. ! grep "^source.*.muttrc" "$muttdir/personal.muttrc" >/dev/null && echo "source $accdir/$title.muttrc" >> "$muttdir/personal.muttrc"
  153. 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"
  154. }
  155. getpass() { while : ; do pass rm -f "mutt-wizard-$title" >/dev/null 2>&1
  156. pass insert "mutt-wizard-$title" && break; done ;}
  157. formatShortcut() { \
  158. while read -r data; do { echo "macro index,pager g$1 \"<change-folder>$data<enter>\" \"go to $2\" # autogenerated"
  159. echo "macro index,pager M$1 \"<save-message>$data<enter>\" \"move mail to $2\" # autogenerated"
  160. echo "macro index,pager C$1 \"<copy-message>$data<enter>\" \"copy mail to $2\" # autogenerated"; } >> "$accdir/$3.muttrc"
  161. done ;}
  162. tryconnect() { ! 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
  163. mkdir -p "$maildir/$title"
  164. if mailboxes="$(mbsync -l "$title" | sed 's/\//./')" >/dev/null 2>&1; then
  165. [ "$accounttype" = "online" ] && sed -i "/IMAPStore $title-remote$/,/# End profile/d" "$mbsyncrc"
  166. printf "\033[32mMailboxes detected.\033[0m\\n"
  167. echo "$mailboxes" | xargs -I {} mkdir -p "$maildir/$title/{}"
  168. else
  169. 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"
  170. delete "$title"; return 1
  171. fi ;}
  172. finalize() { \
  173. boxes="$(du -a "$maildir/$title/"* -d 0 | sed "s/^.*\//=/")"
  174. [ -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
  175. echo "$boxes" > "$tmpdir/title_boxes"
  176. printf "Setting up the mutt sidebar...\\n"
  177. sidebar_width="$(sed -n -e '/^set sidebar_width/p' "$muttdir/muttrc" | awk -F'=' '{print $2}')"
  178. delim="$(head -c "$sidebar_width" < /dev/zero | tr '\0' =)"
  179. printf "Setting default mailboxes for your Inbox, Sent, Drafts and Trash in mutt...\\n"
  180. oneline="$(sed -e "s/^\|$/\"/g" "$tmpdir/title_boxes" | tr "\n" " ")"
  181. oneline="=$title $delim $oneline"
  182. spoolfile=$(grep -i -m 1 "$tmpdir/title_boxes" -e inbox | sed -e 's/=/+/g')
  183. record=$(grep -i -m 1 "$tmpdir/title_boxes" -e sent | sed -e 's/=/+/g')
  184. postponed=$(grep -i -m 1 "$tmpdir/title_boxes" -e draft | sed -e 's/=/+/g')
  185. trash=$(grep -i -m 1 "$tmpdir/title_boxes" -e trash | sed -e 's/=/+/g')
  186. sed -i "/^mailboxes\|^set record\|^set postponed\|^set trash\|^set spoolfile/d" "$accdir/$title.muttrc"
  187. { echo "set spoolfile = \"$spoolfile\""; echo "set record = \"$record\""; echo "set postponed = \"$postponed\""; echo "set trash = \"$trash\""; } >> "$accdir/$title.muttrc"
  188. echo mailboxes "$oneline" >> "$accdir/$title.muttrc"
  189. printf "Setting up your keyboard shortcuts for jumping between mailboxes...\\n"
  190. sed -i "/# autogenerated/d" "$accdir/$title.muttrc"
  191. grep -i -m 1 inbox "$tmpdir/title_boxes" | formatShortcut i inbox "$title"
  192. grep -i -m 1 sent "$tmpdir/title_boxes" | formatShortcut s sent "$title"
  193. grep -i -m 1 draft "$tmpdir/title_boxes" | formatShortcut d drafts "$title"
  194. grep -i -m 1 trash "$tmpdir/title_boxes" | formatShortcut t trash "$title"
  195. grep -i -m 1 spam "$tmpdir/title_boxes" | formatShortcut S spam "$title"
  196. grep -i -m 1 junk "$tmpdir/title_boxes" | formatShortcut j junk "$title"
  197. grep -i -m 1 archive "$tmpdir/title_boxes" | formatShortcut a archive "$title"
  198. [ "$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"
  199. }
  200. 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
  201. 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
  202. return 0 ;}
  203. list() { numbered="$(find "$accdir" -mindepth 1 -maxdepth 1 -type d -printf "%f\n" 2>/dev/null | nl)"
  204. [ "$(echo "$numbered" | wc -w)" = 0 ] && printf "No configured accounts.\\n" && return 1
  205. }
  206. pick() { list
  207. printf "Select an accounts to %s:\\n" "$1"
  208. echo "$numbered"
  209. printf "\033[36m\t"
  210. read -r input
  211. printf "\033[0m"
  212. [ -z "$input" ] && return 1
  213. title="$(echo "$numbered" | grep "$input" | awk '{print $2}')"
  214. [ -z "$title" ] && printf "Invalid response." && return 1
  215. return 0 ;}
  216. delete() { sed -i "/IMAPStore $title-remote$/,/# End profile/d" "$mbsyncrc"
  217. rm -rf "${accdir:?}/${title:?}" "$accdir/$title.muttrc"
  218. sed -i "/$title.muttrc/d" "$muttdir/personal.muttrc"
  219. sed -i "/account $title/,/^\(\s*$\|account\)/d" "$HOME/.config/msmtp/config"
  220. }
  221. choosecron() { ! pgrep crond >/dev/null && echo "No cron manager running. Install/enable one and then select this option again." && return 1
  222. if crontab -l | grep mailsync >/dev/null; then
  223. echo "Active mail sync cronjob detected. Do you want to remove it?"
  224. printf "\033[36m\t"
  225. read -r rmyn
  226. printf "\033[0m"
  227. echo "$rmyn" | grep -i "^y\(es\)*$" >/dev/null && crontab -l | sed '/mailsync/d' | crontab - >/dev/null && echo "Mail sync turned off."
  228. else
  229. echo "How many minutes between each mail sync?"
  230. printf "\033[36m\t"
  231. read -r minnum
  232. printf "\033[0m"
  233. while ! echo "$minnum" | grep "^[0-9]\+$" >/dev/null; do
  234. printf "That doesn't look like a number. How many minutes between each mail sync?\\n\033[36m\t"
  235. read -r minnum
  236. printf "\033[0m"
  237. done
  238. (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 - &&
  239. echo "Cronjob added. Mail will sync every $minnum minutes. Be sure you have your cron manager running."
  240. fi ;}
  241. asktype() { while : ; do
  242. printf "Do you want to keep your mail for this account offline with mbsync? [yes/no]\\n\t"
  243. read -r offnot
  244. case "$offnot" in
  245. [Yy][Ee][Ss]) accounttype="offline" && break ;;
  246. [Nn][Oo]) accounttype="online" && break ;;
  247. *) echo "Write out either yes or no completely. Try again or press ctrl-c to quit." ;;
  248. esac; done ;}
  249. init() { echo "Initializing a mutt-wizard configuration..."
  250. [ -f "$accdir" ] && echo "There is already a preexisting config. Purge the previous to reinit." && return 1
  251. mkdir -p "$muttdir" "$accdir"
  252. cp -v "$muttshare/mailcap" "$muttdir"
  253. cp -v "$muttshare/muttrc" "$muttdir"
  254. }
  255. case "$1" in
  256. init) init ;;
  257. ls) list ;;
  258. add) asktype && askinfo && tryconnect && finalize ;;
  259. pass) pick "change the password of" && getpass ;;
  260. delete) pick delete && confirm "delete the \`$title\` profile" && delete ;;
  261. purge) confirm "delete all account data" && rm -rf "$mbsyncrc" "$accdir" "$HOME/.config/msmtp" "$muttdir/personal.muttrc" && echo "All configs and account settings have been purged." ;;
  262. cron) choosecron ;;
  263. *) cat << EOF
  264. mw: mutt-wizard, auto-configure email accounts for mutt
  265. including downloadable mail with \`isync\`.
  266. Allowed options:
  267. init Run once and first to copy required files
  268. add Add and autoconfigure an email address (9 max.)
  269. ls List configured accounts
  270. delete Pick an account to delete
  271. purge Delete all accounts and settings
  272. cron Enable or disable an autosync via cronjob
  273. all else Print this message
  274. NOTE: Once at least one account is added, you can run
  275. \`mbsync -a\` to begin downloading mail.
  276. EOF
  277. esac
  278. rm -rf "$tmpdir"