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.
 
 
 
 

309 lines
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. mwconfig="$muttdir/mutt-wizard.muttrc"
  13. muttrc="$muttdir/muttrc"
  14. getprofiles() { \
  15. unset msmtp_header msmtp_profile mutt_profile mbsync_profile
  16. printf "Creating profiles for \`%s\`..." "$title"
  17. msmtp_header="defaults
  18. auth on
  19. tls on
  20. tls_trust_file $sslcert
  21. logfile ~/.config/msmtp/msmtp.log
  22. "
  23. msmtp_profile="
  24. account $title
  25. host $smtp
  26. port $sport
  27. from $fulladdr
  28. user $login
  29. passwordeval \"pass mutt-wizard-$title\""
  30. mbsync_profile="IMAPStore $title-remote
  31. Host $imap
  32. Port $iport
  33. User $login
  34. PassCmd \"pass mutt-wizard-$title\"
  35. SSLType IMAPS
  36. CertificateFile /etc/ssl/certs/ca-certificates.crt
  37. MaildirStore $title-local
  38. Subfolders Verbatim
  39. Path ~/.local/share/mail/$title/
  40. Inbox ~/.local/share/mail/$title/INBOX
  41. Flatten .
  42. Channel $title
  43. Expunge Both
  44. Master :$title-remote:
  45. Slave :$title-local:
  46. Patterns * !\"[Gmail]/All Mail\"
  47. Create Both
  48. SyncState *
  49. # End profile
  50. "
  51. if [ "$accounttype" = "offline" ]; then
  52. mutt_profile="# vim: filetype=neomuttrc
  53. # muttrc file for account $title
  54. set realname = \"$realname\"
  55. set from = \"$fulladdr\"
  56. set sendmail = \"/usr/bin/msmtp -a $title\"
  57. alias me $realname <$fulladdr>
  58. set folder = \"$maildir/$title\"
  59. set header_cache = $accdir/$title/cache/headers
  60. set message_cachedir = $accdir/$title/cache/bodies
  61. set certificate_file = $accdir/$title/certificates
  62. set mbox_type = Maildir
  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>mailsync -V $title<enter>\" \"run mbsync to sync $title\"
  69. unmailboxes *
  70. "
  71. else
  72. mutt_profile="# vim: filetype=neomuttrc
  73. # muttrc file for account $title
  74. set realname = \"$realname\"
  75. set from = \"$fulladdr\"
  76. set sendmail = \"/usr/bin/msmtp -a $title\"
  77. alias me $realname <$fulladdr>
  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 mbox_type = Maildir
  85. set ssl_starttls = yes
  86. set ssl_force_tls = yes
  87. bind index,pager gg noop
  88. bind index,pager g noop
  89. bind index,pager M noop
  90. bind index,pager C noop
  91. bind index gg first-entry
  92. unmailboxes *
  93. "
  94. fi
  95. printf "DONE.\\n"
  96. }
  97. askinfo() { \
  98. printf "Insert the \033[31memail address\033[0m that you want to autoconfigure for mutt/mbsync\\n\tEmail: \033[36m"
  99. read -r fulladdr
  100. printf "\033[0m"
  101. while ! echo "$fulladdr" | grep "$emailre" >/dev/null; do
  102. printf "That is not a valid \033[31memail address\033[0m, please retype the desired email.\\n\\nEmail: \033[36m\t"
  103. read -r fulladdr
  104. printf "\033[0m"
  105. done
  106. domain="$(echo "$fulladdr" | sed "s/.*@//")"
  107. printf "\\nSearching for \033[32m%s\033[0m in \033[34m\`domains.csv\`\033[0m..." "$domain"
  108. serverinfo="$(grep "$domain" "$muttshare/domains.csv" 2>/dev/null)"
  109. if [ -z "$serverinfo" ]; then
  110. 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"
  111. printf "Insert the IMAP server for your email provider (excluding the port number)\\n\033[36m\t"
  112. read -r imap
  113. printf "\033[0mWhat is your server's IMAP port number? (Usually something like 993)\\n\033[36m\t"
  114. read -r iport
  115. printf "\033[0mInsert the SMTP server for your email provider (excluding the port number)\\n\033[36m\t"
  116. read -r smtp
  117. printf "\033[0mWhat is your server's SMTP port number? (Usually 587 or 465)\\n\033[36m\t"
  118. read -r sport
  119. 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"
  120. else
  121. IFS=, read -r service imap iport smtp sport <<EOF
  122. $serverinfo
  123. EOF
  124. 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"
  125. case "$service" in
  126. 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" ;;
  127. 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" ;;
  128. esac
  129. fi
  130. printf "Enter the \033[35mfull name\033[0m you want to be identified by on this account.\\n\tReal name: "
  131. read -r realname
  132. 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: "
  133. read -r title
  134. while ! echo "$title" | grep "$namere" >/dev/null; do
  135. 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"
  136. read -r title
  137. printf "\033[0m"
  138. done
  139. 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"
  140. read -r login
  141. printf "\033[0m"
  142. [ -z "$login" ] && login="$fulladdr"
  143. accounts="$(find "$accdir" -type f | grep -o "[0-9]-.*.muttrc" | sed "s/-/: /g;s/\..*//" | sort -n)"
  144. for x in $(seq 1 9); do echo "$accounts" | grep "$x" || { export idnum="$x"; break ;}; done
  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/$idnum-$title.muttrc"
  151. echo "$mbsync_profile" >> "$mbsyncrc"
  152. ! grep "^source.*.muttrc" "$muttrc" | grep -v "$mwconfig" >/dev/null && echo "source $accdir/$title.muttrc # mw-autogenerated" >> "$muttrc"
  153. echo "macro index,pager i$idnum '<sync-mailbox><enter-command>source $accdir/$idnum-$title.muttrc<enter><change-folder>!<enter>;<check-stats>' \"switch to $fulladdr\" # mw-autogenerated" >> "$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\" # mw-autogenerated"
  159. echo "macro index,pager M$1 \"<save-message>$data<enter>\" \"move mail to $2\" # mw-autogenerated"
  160. echo "macro index,pager C$1 \"<copy-message>$data<enter>\" \"copy mail to $2\" # mw-autogenerated"; } >> "$accdir/$3.muttrc"
  161. done ;}
  162. tryconnect() { mkdir -p "$maildir/$title"
  163. if mailboxes="$(mbsync -l "$title" | sed 's/\//./')" >/dev/null 2>&1; then
  164. [ "$accounttype" = "online" ] && sed -i "/IMAPStore $title-remote$/,/# End profile/d" "$mbsyncrc"
  165. printf "\033[32mMailboxes detected.\033[0m\\n"
  166. echo "$mailboxes" | xargs -I {} mkdir -p "$maildir/$title/{}"
  167. else
  168. 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"
  169. return 1
  170. fi ;}
  171. finalize() { \
  172. boxes="$(du -a "$maildir/$title/"* -d 0 | sed "s/^.*\//=/")"
  173. [ -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
  174. tmpdir="$(mktemp -d)"
  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 "/# mw-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" | nl)"
  204. echo "$numbered"
  205. [ "$(echo "$numbered" | wc -l)" = 0 ] && printf "No configured accounts.\\n" && return 1
  206. }
  207. pick() { printf "Select an accounts to %s:\\n" "$1"
  208. list
  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/[1-9]-$title.muttrc"
  218. sed -i "/$title.muttrc/d" "$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. mkdir -p "$muttdir" "$accdir" && echo "Required directories created."
  251. [ ! -f "$muttrc" ] && echo "# vim: filetype=neomuttrc" > "$muttrc" && echo "muttrc created."
  252. cp -v "$muttshare/mailcap" "$muttdir" && echo "Mailcap file added."
  253. cp -v "$muttshare/mutt-wizard.muttrc" "$muttdir" && echo "mutt-wizard config file added."
  254. ! grep "^source.*mutt-wizard.muttrc" "$muttrc" >/dev/null && echo "source $mwconfig" >> "$muttrc" && echo "muttrc will now source mutt-wizard."
  255. }
  256. purge() { confirm "delete all account data" || exit
  257. rm -rf "$mbsyncrc" "$accdir" "$mwconfig" "$HOME/.config/msmtp" "$mwconfig"
  258. echo "All configs and account settings have been purged."
  259. sed -i "\|$mwconfig|d" "$muttrc"
  260. }
  261. case "$1" in
  262. init) init ;;
  263. ls) list ;;
  264. add) asktype && askinfo && tryconnect && finalize || delete ;;
  265. pass) pick "change the password of" && getpass ;;
  266. delete) pick delete && confirm "delete the \`$title\` profile" && delete ;;
  267. purge) purge ;;
  268. cron) choosecron ;;
  269. *) cat << EOF
  270. mw: mutt-wizard, auto-configure email accounts for mutt
  271. including downloadable mail with \`isync\`.
  272. Allowed options:
  273. init Run once and first to copy required files
  274. add Add and autoconfigure an email address (9 max.)
  275. ls List configured accounts
  276. delete Pick an account to delete
  277. purge Delete all accounts and settings
  278. cron Enable or disable an autosync via cronjob
  279. all else Print this message
  280. NOTE: Once at least one account is added, you can run
  281. \`mbsync -a\` to begin downloading mail.
  282. EOF
  283. esac
  284. rm -rf "$tmpdir"