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.
 
 
 
 

357 line
16 KiB

  1. #!/bin/sh
  2. case "$(uname)" in
  3. Linux) prefix="/usr" ;;
  4. *) prefix="/usr/local" ;;
  5. esac
  6. command -V gpg >/dev/null 2>&1 && GPG="gpg" || GPG="gpg2"
  7. [ -z $PASSWORD_STORE_DIR ] && PASSWORD_STORE_DIR="$HOME/.password-store"
  8. [ -r "$PASSWORD_STORE_DIR/.gpg-id" ] &&
  9. "$GPG" --list-secret-keys $(cat "$PASSWORD_STORE_DIR/.gpg-id") >/dev/null 2>&1 || {
  10. 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"
  11. exit
  12. }
  13. ! command -v $prefix/bin/mbsync >/dev/null && printf "\`mbsync\` must be installed to run mutt-wizard.\\n" && exit
  14. configdir=${XDG_CONFIG_HOME:-$HOME/.config}
  15. # Main mutt config location
  16. muttdir="$configdir/mutt"
  17. # Directory for account settings
  18. accdir="$muttdir/accounts"
  19. # Location of mail storage
  20. maildir="${MAILDIR:-${XDG_DATA_HOME:-$HOME/.local/share}/mail}"
  21. # Regex to ensure viable username
  22. namere="^[a-z_][a-z0-9_-]*$"
  23. # Regex to confirm valid email address
  24. emailre=".\+@.\+\\..\+"
  25. muttshare="$prefix/share/mutt-wizard"
  26. if [[ -f $configdir/isync/mbsyncrc ]]; then
  27. mbsyncrc="$configdir/isync/mbsyncrc"
  28. elif [[ -f $configdir/mbsync/mbsyncrc ]]; then
  29. mbsyncrc="$configdir/mbsync/mbsyncrc"
  30. else
  31. mbsyncrc="$HOME/.mbsyncrc"
  32. fi
  33. mwconfig="$muttshare/mutt-wizard.muttrc"
  34. cachedir="${XDG_CACHE_HOME:-$HOME/.cache}/mutt-wizard"
  35. muttrc="$muttdir/muttrc"
  36. msmtprc="$configdir/msmtp/config"
  37. # This is later changed to `None` later in the script if using Protonmail
  38. ssltype="IMAPS"
  39. mbsyncbin="$prefix/bin/mbsync -c $mbsyncrc"
  40. msmtpbin="$prefix/bin/msmtp"
  41. for x in "/etc/ssl/certs/ca-certificates.crt" "/etc/pki/tls/certs/ca-bundle.crt" "/etc/ssl/ca-bundle.pem" "/etc/pki/tls/cacert.pem" "/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem" "/etc/ssl/cert.pem" "/usr/local/share/ca-certificates/"
  42. do
  43. [ -f "$x" ] && sslcert="$x" && break
  44. done || { echo "CA Certificate not found. Please install one or link it to /etc/ssl/certs/ca-certificates.crt" && exit 1 ;}
  45. getaccounts() { accounts="$(find "$accdir" -type f | grep -o "[0-9]-.*.muttrc" | sed "s/-/: /;s/\..*//" | sort -n)" ;}
  46. list() { getaccounts && [ -n "$accounts" ] && echo "$accounts" ;}
  47. getprofiles() { \
  48. unset msmtp_header msmtp_profile mutt_profile mbsync_profile
  49. printf "Creating profiles for \`%s\`..." "$title"
  50. msmtp_header="defaults
  51. auth on
  52. tls on
  53. tls_trust_file $sslcert
  54. logfile ${XDG_LOG_HOME:-$HOME}/msmtp.log
  55. "
  56. msmtp_profile="account $title
  57. host $smtp
  58. port $sport
  59. from $fulladdr
  60. user $login
  61. passwordeval \"pass mutt-wizard-$title\"
  62. $starttlsoff
  63. "
  64. mbsync_profile="IMAPStore $title-remote
  65. Host $imap
  66. Port $iport
  67. User $login
  68. PassCmd \"pass mutt-wizard-$title\"
  69. SSLType $ssltype
  70. CertificateFile $sslcert
  71. MaildirStore $title-local
  72. Subfolders Verbatim
  73. Path ~/.local/share/mail/$title/
  74. Inbox ~/.local/share/mail/$title/INBOX
  75. Flatten .
  76. Channel $title
  77. Expunge Both
  78. Master :$title-remote:
  79. Slave :$title-local:
  80. Patterns * !\"[Gmail]/All Mail\"
  81. Create Both
  82. SyncState *
  83. MaxMessages $maxmes
  84. # End profile
  85. "
  86. if [ "$accounttype" = "offline" ]; then
  87. mutt_profile="# vim: filetype=neomuttrc
  88. # muttrc file for account $title
  89. set realname = \"$realname\"
  90. set from = \"$fulladdr\"
  91. set sendmail = \"$msmtpbin -a $title\"
  92. alias me $realname <$fulladdr>
  93. set folder = \"$maildir/$title\"
  94. set header_cache = $cachedir/$title/headers
  95. set message_cachedir = $cachedir/$title/bodies
  96. set mbox_type = Maildir
  97. macro index gm \"<shell-escape>mailsync -V $title<enter>\" \"sync mail $title\"
  98. unmailboxes *
  99. "
  100. else
  101. mutt_profile="# vim: filetype=neomuttrc
  102. # muttrc file for account $title
  103. set realname = \"$realname\"
  104. set from = \"$fulladdr\"
  105. set sendmail = \"$msmtpbin -a $title\"
  106. alias me $realname <$fulladdr>
  107. set folder = \"imaps://$fulladdr@$imap:$iport\"
  108. set imap_user = \"$login\"
  109. set header_cache = $cachedir/$title/headers
  110. set message_cachedir = $cachedir/$title/bodies
  111. set imap_pass = \"\`pass mutt-wizard-$title\`\"
  112. set mbox_type = Maildir
  113. set ssl_starttls = yes
  114. set ssl_force_tls = yes
  115. bind index,pager gg noop
  116. bind index,pager g noop
  117. bind index,pager M noop
  118. bind index,pager C noop
  119. bind index gg first-entry
  120. unmailboxes *
  121. "
  122. fi
  123. printf "DONE.\\n"
  124. }
  125. askinfo() { \
  126. printf "Insert the \033[31memail address\033[0m that you want to autoconfigure for mutt/mbsync\\n\tEmail: \033[36m"
  127. read -r fulladdr
  128. printf "\033[0m"
  129. while ! echo "$fulladdr" | grep "$emailre" >/dev/null; do
  130. printf "That is not a valid \033[31memail address\033[0m, please retype the desired email.\\n\\nEmail: \033[36m\t"
  131. read -r fulladdr
  132. printf "\033[0m"
  133. done
  134. domain="$(echo "$fulladdr" | sed "s/.*@//")"
  135. printf "\\nSearching for \033[32m%s\033[0m in \033[34m\`domains.csv\`\033[0m..." "$domain"
  136. serverinfo="$(grep "^$domain" "$muttshare/domains.csv" 2>/dev/null)"
  137. if [ -z "$serverinfo" ]; then
  138. 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"
  139. printf "Insert the IMAP server for your email provider (excluding the port number)\\n\033[36m\t"
  140. read -r imap
  141. printf "\033[0mWhat is your server's IMAP port number? (Usually something like 993)\\n\033[36m\t"
  142. read -r iport
  143. printf "\033[0mInsert the SMTP server for your email provider (excluding the port number)\\n\033[36m\t"
  144. read -r smtp
  145. printf "\033[0mWhat is your server's SMTP port number? (Usually 587 or 465)\\n\033[36m\t"
  146. read -r sport
  147. 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"
  148. else
  149. IFS=, read -r service imap iport smtp sport <<EOF
  150. $serverinfo
  151. EOF
  152. printf "\\n\033[3;33mCongrats!\033[0m Server info has automatically been 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"
  153. case "$service" in
  154. 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" ;;
  155. protonmail.ch|protonmail.com|pm.me) printf "\033[31mREMEMBER: Protonmail users must install and configure Protonmail Bridge first for the sync to work:\\nhttps://protonmail.com/bridge/\\n\033[0m" && ssltype="None" ;;
  156. esac
  157. [ "$sport" = 465 ] && starttlsoff="tls_starttls off"
  158. fi
  159. printf "Enter the \033[35mfull name\033[0m you want to be identified by on this account.\\n\tReal name: "
  160. read -r realname
  161. 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: "
  162. read -r title
  163. while ! echo "$title" | grep "$namere" >/dev/null || ls "$accdir"/[0-9]"-$title.muttrc" >/dev/null 2>&1; do
  164. printf "\033[31mTry again\033[0m. Pick a nickname that is one word only including lowercase letters and _ or - and that you have \033[1mnot\033[0m used before.\\n\tAccount name: \033[36m\t"
  165. read -r title
  166. printf "\033[0m"
  167. done
  168. 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"
  169. read -r login
  170. printf "\033[0m"
  171. [ -z "$login" ] && login="$fulladdr"
  172. [ "$accounttype" = "offline" ] && printf "If you want to limit the number of messages kept offline to a number, enter that number below. If you do not want to limit your mail and would like \`mbsync\` to sync all mail, press enter without typing a number.\\n\t" && read -r maxmes
  173. echo "$maxmes" | grep "[1-9]" >/dev/null || maxmes="0"
  174. getpass
  175. getprofiles
  176. mkdir -p "$muttdir" "$accdir" "$cachedir/$title/bodies" "$configdir/msmtp"
  177. getaccounts
  178. for x in $(seq 1 9); do echo "$accounts" | grep "$x" >/dev/null 2>&1 || { export idnum="$x"; break ;}; done
  179. [ ! -f "$msmtprc" ] && echo "$msmtp_header" > "$msmtprc"
  180. echo "$msmtp_profile" >> "$msmtprc"
  181. command -V apt-get >/dev/null 2>&1 && ln -s "$msmtprc" "$msmtprc" 2>/dev/null
  182. case "$service" in
  183. protonmail.ch|protonmail.com|pm.me) protonfinger || return 1 ;;
  184. esac
  185. echo "$mutt_profile" > "$accdir/$idnum-$title.muttrc"
  186. echo "$mbsync_profile" >> "$mbsyncrc"
  187. notmuchauto
  188. [ ! -f "$muttrc" ] && echo "# vim: filetype=neomuttrc" > "$muttrc" && echo "muttrc created."
  189. ! grep "^source.*mutt-wizard.muttrc" "$muttrc" >/dev/null && echo "source $mwconfig # mw-autogenerated" >> "$muttrc"
  190. ! grep "^source.*.muttrc" "$muttrc" | grep -v "$mwconfig" >/dev/null && echo "source $accdir/$idnum-$title.muttrc # mw-autogenerated" >> "$muttrc"
  191. 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"
  192. }
  193. protonfinger() { printf "Getting Protonmail bridge fingerprint...\\n"
  194. fingerprint="$($msmtpbin --serverinfo --host=127.0.0.1 --port=1025 --tls --tls-certcheck=off)" || return 1
  195. sed -ibu "s/account $title/&\ntls_trust_file\ntls_fingerprint $fingerprint/" "$msmtprc" ; rm -f "$msmtprc"bu
  196. }
  197. getpass() { while : ; do pass rm -f "mutt-wizard-$title" >/dev/null 2>&1
  198. pass insert "mutt-wizard-$title" && break; done ;}
  199. formatShortcut() { \
  200. while read -r data; do { echo "macro index,pager g$1 \"<change-folder>$data<enter>\" \"go to $2\" # mw-autogenerated"
  201. echo "macro index,pager M$1 \"<save-message>$data<enter>\" \"move mail to $2\" # mw-autogenerated"
  202. echo "macro index,pager C$1 \"<copy-message>$data<enter>\" \"copy mail to $2\" # mw-autogenerated"; } >> "$accdir/$idnum-$title.muttrc"
  203. done ;}
  204. tryconnect() { mkdir -p "$maildir/$title"
  205. if mailboxes="$($mbsyncbin -l $title | sed 's/\//./')" >/dev/null 2>&1 && [ -n "$mailboxes" ]; then
  206. [ "$accounttype" = "online" ] && sed -ibu "/IMAPStore $title-remote$/,/# End profile/d" "$mbsyncrc" ; rm -f "$mbsyncrc"bu
  207. printf "\033[32mMailboxes detected.\033[0m\\n"
  208. echo "$mailboxes" | xargs -I {} mkdir -p "$maildir/$title/{}"
  209. return 0
  210. else
  211. printf "\033[31m\033[31mLog-on not successful.\033[0m\\nIt seems that either you inputted the wrong password or server settings, or there are other requirements for your account out of the control of mutt-wizard.\\n"
  212. return 1
  213. fi ;}
  214. finalize() { \
  215. boxes="$(find "$maildir/$title/" -mindepth 1 -maxdepth 1 | sed "s/\ /\\\ /g;s/^.*\//=/")"
  216. [ -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
  217. printf "Setting default mailboxes for your Inbox, Sent, Drafts and Trash in mutt...\\n"
  218. spoolfile=$(echo "$boxes" | grep -i -m 1 inbox | sed 's/=/+/g')
  219. record=$(echo "$boxes" | grep -i -m 1 sent | sed 's/=/+/g')
  220. postponed=$(echo "$boxes" | grep -i -m 1 draft | sed 's/=/+/g')
  221. trash=$(echo "$boxes" | grep -i -m 1 trash | sed 's/=/+/g')
  222. sed -ibu "/^mailboxes\|^set record\|^set postponed\|^set trash\|^set spoolfile/d" "$accdir/$idnum-$title.muttrc" ; rm -f "$accdir/$idnum-$title.muttrcbu"
  223. { echo "set spoolfile = \"$spoolfile\""; echo "set record = \"$record\""; echo "set postponed = \"$postponed\""; echo "set trash = \"$trash\""; } >> "$accdir/$idnum-$title.muttrc"
  224. echo "mailboxes =$title ===================== $(echo "$boxes" | sed -e "s/^\|$/\"/g" | tr "\n" " ")" >> "$accdir/$idnum-$title.muttrc"
  225. printf "Setting up your keyboard shortcuts for jumping between mailboxes...\\n"
  226. sed -ibu "/# mw-autogenerated/d" "$accdir/$idnum-$title.muttrc" ; rm -f "$accdir/$idnum-$title.muttrcbu"
  227. echo "$boxes" | grep -i inbox | head -n 1 | formatShortcut i inbox
  228. echo "$boxes" | grep -i sent | head -n 1 | formatShortcut s sent
  229. echo "$boxes" | grep -i draft | head -n 1 | formatShortcut d drafts
  230. echo "$boxes" | grep -i trash | head -n 1 | formatShortcut t trash
  231. echo "$boxes" | grep -i spam | head -n 1 | formatShortcut S spam
  232. echo "$boxes" | grep -i junk | head -n 1 | formatShortcut j junk
  233. echo "$boxes" | grep -i archive | head -n 1 | formatShortcut a archive
  234. [ "$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"
  235. command -V urlview >/dev/null 2>&1 && [ ! -f "$HOME/.urlview" ] && echo "COMMAND \$BROWSER" > "$HOME/.urlview"
  236. return 0
  237. }
  238. confirm() { printf "Do you want 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
  239. 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
  240. return 0 ;}
  241. pick() { printf "Select an accounts to %s:\\n" "$1"
  242. list
  243. read -r input
  244. [ -z "$input" ] && return 1
  245. title="$(echo "$accounts" | grep "$input" | awk '{print $2}')"
  246. [ -z "$title" ] && printf "Invalid response." && return 1
  247. return 0 ;}
  248. delete() { sed -ibu "/IMAPStore $title-remote$/,/# End profile/d" "$mbsyncrc" ; rm -rf "$mbsyncrc"bu
  249. rm -rf "${cachedir:?}/${title:?}" "$accdir/"[1-9]"-$title.muttrc"
  250. sed -ibu "/[0-9]-$title.muttrc/d" "$muttrc" ; rm -f "$muttrc"bu
  251. sed -ibu "/account $title/,/^\(\s*$\|account\)/d" "$msmtprc"; rm -f "$msmtprc"bu
  252. }
  253. choosecron() { ! pgrep cron >/dev/null && echo "No cron manager running. Install/enable one and then select this option again." && return 1
  254. if crontab -l | grep mailsync >/dev/null; then
  255. echo "Active mail sync cronjob detected. Do you want to remove it?"
  256. printf "\033[36m\t"
  257. read -r rmyn
  258. printf "\033[0m"
  259. echo "$rmyn" | grep -i "^y\(es\)*$" >/dev/null && crontab -l | sed '/mailsync/d' | crontab - >/dev/null && echo "Mail sync turned off."
  260. else
  261. echo "How many minutes between each mail sync?"
  262. printf "\033[36m\t"
  263. read -r minnum
  264. printf "\033[0m"
  265. while ! echo "$minnum" | grep "^[0-9]\+$" >/dev/null; do
  266. printf "That doesn't look like a number. How many minutes between each mail sync?\\n\033[36m\t"
  267. read -r minnum
  268. printf "\033[0m"
  269. done
  270. (crontab -l; echo "*/$minnum * * * * export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus; export DISPLAY=:0; $(type mailsync | cut -d' ' -f3)") | crontab - &&
  271. echo "Cronjob added. Mail will sync every $minnum minutes. Be sure you have your cron manager running."
  272. fi ;}
  273. asktype() { while : ; do
  274. printf "Do you want to keep your mail for this account offline with mbsync? [yes/no]\\n\t"
  275. read -r offnot
  276. case "$offnot" in
  277. [Yy][Ee][Ss]) accounttype="offline" && break ;;
  278. [Nn][Oo]) accounttype="online" && break ;;
  279. *) echo "Write out either yes or no completely. Try again or press ctrl-c to quit." ;;
  280. esac; done ;}
  281. purge() { confirm "delete all account data" || exit
  282. rm -rf "$mbsyncrc" "$accdir" "$configdir/msmtp" "$cachedir"
  283. crontab -l | sed '/mailsync/d' | crontab - >/dev/null
  284. echo "All configs and account settings have been purged."
  285. sed -ibu "/\# mw-autogenerated/d" "$muttrc" ; rm -f "$muttrc"bu
  286. }
  287. notmuchauto() { \
  288. [ -z "$NOTMUCH_CONFIG" ] && NOTMUCH_CONFIG="$HOME/.notmuch-config"
  289. [ -f "$NOTMUCH_CONFIG" ] && return 0
  290. nmbasic="[database]
  291. path=$maildir
  292. [user]
  293. name=$realname
  294. primary_email=$fulladdr
  295. [new]
  296. tags=unread;inbox;
  297. ignore=
  298. [search]
  299. exclude_tags=deleted;spam;
  300. [maildir]
  301. synchronize_flags=true
  302. [crypto]
  303. gpg_path=$GPG"
  304. echo "$nmbasic" > "$NOTMUCH_CONFIG" ;}
  305. trap 'echo -e "\033[0m\n"; exit' STOP INT ABRT KILL
  306. case "$1" in
  307. ls) list ;;
  308. add) asktype && askinfo && tryconnect && finalize || delete ;;
  309. pass) pick "change the password of" && getpass ;;
  310. delete) pick delete && confirm "delete the \`$title\` profile" && delete ;;
  311. purge) purge ;;
  312. cron) choosecron ;;
  313. *) cat << EOF
  314. mw: mutt-wizard, auto-configure email accounts for mutt
  315. including downloadable mail with \`isync\`.
  316. Allowed options:
  317. add Add and autoconfigure an email address (9 max.)
  318. ls List configured accounts
  319. delete Pick an account to delete
  320. purge Delete all accounts and settings
  321. cron Enable or disable an autosync via cronjob
  322. all else Print this message
  323. NOTE: Once at least one account is added, you can run
  324. \`mbsync -a\` to begin downloading mail.
  325. EOF
  326. esac