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.

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