Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

348 righe
16 KiB

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