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.

4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
5 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
5 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
5 年之前
5 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
5 年之前
5 年之前
4 年之前
4 年之前
4 年之前
5 年之前
4 年之前
5 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. #!/bin/sh
  2. prefix="/usr/local"
  3. pass_prefix="mw-"
  4. muttdir="${XDG_CONFIG_HOME:-$HOME/.config}/mutt" # Main mutt config location
  5. accdir="$muttdir/accounts" # Directory for account settings
  6. maildir="${XDG_DATA_HOME:-$HOME/.local/share}/mail" # Location of mail storage
  7. emailre=".+@.+\..+" # Regex to confirm valid email address
  8. muttshare="$prefix/share/mutt-wizard"
  9. mbsyncrc="${MBSYNCRC:-$HOME/.mbsyncrc}"
  10. mwconfig="$muttshare/mutt-wizard.muttrc"
  11. cachedir="${XDG_CACHE_HOME:-$HOME/.cache}/mutt-wizard"
  12. muttrc="$muttdir/muttrc"
  13. msmtprc="${XDG_CONFIG_HOME:-$HOME/.config}/msmtp/config"
  14. msmtplog="${XDG_CONFIG_HOME:-$HOME/.config}/msmtp/msmtp.log"
  15. marker="# mw-autogenerated"
  16. alias mbsync='mbsync -c "$mbsyncrc"'
  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" "/etc/ssl/cert.pem" "/usr/local/share/ca-certificates/"; do
  18. [ -f "$x" ] && sslcert="$x" && break
  19. done || { echo "CA Certificate not found. Please install one or link it to /etc/ssl/certs/ca-certificates.crt" && exit 1 ;}
  20. checkbasics() { command -V gpg >/dev/null 2>&1 && GPG="gpg" || GPG="gpg2"
  21. PASSWORD_STORE_DIR="${PASSWORD_STORE_DIR:-$HOME/.password-store}"
  22. [ -r "$PASSWORD_STORE_DIR/.gpg-id" ] &&
  23. "$GPG" --list-secret-keys "$(cat "$PASSWORD_STORE_DIR/.gpg-id")" >/dev/null 2>&1 || {
  24. echo "First run \`pass init <yourgpgemail>\` to set up a password archive."
  25. echo "(If you don't already have a GPG key pair, first run \`$GPG --full-gen-key\`.)"
  26. exit 1 ;} ;}
  27. getaccounts() { accounts="$(find -L "$accdir" -type f | grep -o "[0-9]-.*.muttrc" | sed "s/-/: /;s/\.muttrc$//" | sort -n)" ;}
  28. list() { getaccounts && [ -n "$accounts" ] && echo "$accounts" ;}
  29. getprofiles() { \
  30. unset msmtp_header msmtp_profile mutt_profile mbsync_profile
  31. case "$iport" in
  32. 1143) imapssl=None ;;
  33. 143) imapssl=STARTTLS ;;
  34. esac
  35. msmtp_header="defaults
  36. auth on
  37. tls on
  38. tls_trust_file $sslcert
  39. logfile $msmtplog
  40. "
  41. msmtp_profile="account $fulladdr
  42. host $smtp
  43. port ${sport:-587}
  44. from $fulladdr
  45. user $login
  46. passwordeval \"pass $pass_prefix$fulladdr\"
  47. $tlsline
  48. "
  49. mbsync_profile="IMAPStore $fulladdr-remote
  50. Host $imap
  51. Port ${iport:-993}
  52. User $login
  53. PassCmd \"pass $pass_prefix$fulladdr\"
  54. AuthMechs LOGIN
  55. SSLType ${imapssl:-IMAPS}
  56. CertificateFile $sslcert
  57. MaildirStore $fulladdr-local
  58. Subfolders Verbatim
  59. Path ${XDG_DATA_HOME:-$HOME/.local/share}/mail/$fulladdr/
  60. Inbox ${XDG_DATA_HOME:-$HOME/.local/share}/mail/$fulladdr/INBOX
  61. Flatten .
  62. Channel $fulladdr
  63. Expunge Both
  64. Master :$fulladdr-remote:
  65. Slave :$fulladdr-local:
  66. Patterns * !\"[Gmail]/All Mail\"
  67. Create Both
  68. SyncState *
  69. MaxMessages ${maxmes:-0}
  70. ExpireUnread no
  71. # End profile
  72. "
  73. if [ -z "${online+x}" ]; then
  74. mutt_profile="# vim: filetype=neomuttrc
  75. # muttrc file for account $fulladdr
  76. set realname = \"$realname\"
  77. set from = \"$fulladdr\"
  78. set sendmail = \"msmtp -a $fulladdr\"
  79. alias me $realname <$fulladdr>
  80. set folder = \"$maildir/$fulladdr\"
  81. set header_cache = $cachedir/$fulladdr/headers
  82. set message_cachedir = $cachedir/$fulladdr/bodies
  83. set mbox_type = Maildir
  84. bind index,pager gg noop
  85. bind index,pager g noop
  86. bind index,pager M noop
  87. bind index,pager C noop
  88. bind index gg first-entry
  89. macro index o \"<shell-escape>mw -y $fulladdr<enter>\" \"run mbsync to sync $fulladdr\"
  90. unmailboxes *
  91. unalternates *
  92. "
  93. else
  94. mutt_profile="# vim: filetype=neomuttrc
  95. # muttrc file for account $fulladdr
  96. set realname = \"$realname\"
  97. set from = \"$fulladdr\"
  98. set sendmail = \"msmtp -a $fulladdr\"
  99. alias me $realname <$fulladdr>
  100. set folder = \"imaps://$login@$imap:${iport:-993}\"
  101. set imap_user = \"$login\"
  102. set header_cache = $cachedir/$fulladdr/headers
  103. set message_cachedir = $cachedir/$fulladdr/bodies
  104. set imap_pass = \"\`pass $pass_prefix$fulladdr\`\"
  105. set mbox_type = Maildir
  106. set ssl_starttls = yes
  107. set ssl_force_tls = yes
  108. bind index,pager gg noop
  109. bind index,pager g noop
  110. bind index,pager M noop
  111. bind index,pager C noop
  112. bind index gg first-entry
  113. unmacro index o
  114. unmailboxes *
  115. "
  116. fi
  117. }
  118. parsedomains(){ serverinfo="$(grep "^${fulladdr#*@}" "$muttshare/domains.csv" 2>/dev/null)"
  119. [ -z "$serverinfo" ] && serverinfo="$(grep "$(echo "${fulladdr#*@}" | sed "s/\.[^\.]*$/\.\\\*/")" "$muttshare/domains.csv" 2>/dev/null)"
  120. IFS=, read -r service imapsugg iportsugg smtpsugg sportsugg <<EOF
  121. $serverinfo
  122. EOF
  123. imap="${imap:-$imapsugg}"
  124. smtp="${smtp:-$smtpsugg}"
  125. sport="${sport:-$sportsugg}"
  126. iport="${iport:-$iportsugg}"
  127. }
  128. delete() { if [ -z "${fulladdr+x}" ]; then
  129. echo "Select the account your would like to delete (by number):"
  130. list
  131. read -r input
  132. match="^$input:"
  133. else
  134. match=" $fulladdr$"
  135. getaccounts
  136. fi
  137. fulladdr="$(echo "$accounts" | grep "$match" | cut -f2 -d' ')"
  138. [ -z "$fulladdr" ] && echo "Invalid account name given." && return 1
  139. sed -ibu "/IMAPStore $fulladdr-remote$/,/# End profile/d" "$mbsyncrc" ; rm -f "$mbsyncrc"bu
  140. rm -rf "${cachedir:?}/${fulladdr:?}" "$accdir/"[1-9]"-$fulladdr.muttrc"
  141. sed -ibu "/[0-9]-$fulladdr.muttrc/d" "$muttrc" ; rm -f "$muttrc"bu
  142. sed -ibu "/account $fulladdr/,/^\(\s*$\|account\)/d" "$msmtprc"; rm -f "$msmtprc"bu
  143. pass rm -f "$pass_prefix$fulladdr" >/dev/null 2>&1
  144. # Get rid of those multiple newlines because I don't know awk well enough to do it by default lol.
  145. for file in "$msmtprc" "$mbsyncrc"; do
  146. tr '\n' '|' < "$file" | sed "s/||\+/||/g" | tr '|' '\n' >> "$file"bu
  147. mv -f "$file"bu "$file"
  148. done
  149. }
  150. askinfo() { \
  151. [ -z "$fulladdr" ] && echo "Give the full email address to add:" &&
  152. read -r fulladdr
  153. while ! echo "$fulladdr" | grep -qE "$emailre"; do
  154. echo "\`$fulladdr\` is not a valid email address. Please retype the address:"
  155. read -r fulladdr
  156. done
  157. grep -q "Channel\s*$fulladdr$" "$mbsyncrc" &&
  158. { echo "$fulladdr has already been added" && exit 1 ;}
  159. { [ -z "$imap" ] || [ -z "$smtp" ] ;} && parsedomains
  160. [ -z "$imap" ] && echo "Give your email server's IMAP address (excluding the port number):" &&
  161. read -r imap
  162. [ -z "$smtp" ] && echo "Give your email server's SMTP address (excluding the port number):" &&
  163. read -r smtp
  164. [ "$sport" = 465 ] && tlsline="tls_starttls off"
  165. [ -z "$realname" ] && realname="${fulladdr%%@*}"
  166. login="${login:-$fulladdr}"
  167. if [ -n "${password+x}" ]; then
  168. createpass
  169. else
  170. getpass
  171. fi
  172. }
  173. createpass() { echo "$password" > "$PASSWORD_STORE_DIR/$pass_prefix$fulladdr"
  174. "$GPG" -qer "$(cat "$PASSWORD_STORE_DIR/.gpg-id")" "$PASSWORD_STORE_DIR/$pass_prefix$fulladdr"
  175. rm -f "$PASSWORD_STORE_DIR/$pass_prefix$fulladdr" ;}
  176. writeinfo() { mkdir -p "$muttdir" "$accdir" "$cachedir/$fulladdr/bodies" "${XDG_CONFIG_HOME:-$HOME/.config}/msmtp" "$maildir/$fulladdr"
  177. # Get accounts and find the first missing account number (max. 9).
  178. getaccounts; for x in $(seq 1 9); do echo "$accounts" | grep -q "$x" || { export idnum="$x"; break ;}; done
  179. # Configure msmtprc for sending mail.
  180. [ ! -f "$msmtprc" ] && echo "$msmtp_header" > "$msmtprc"
  181. echo "$msmtp_profile" >> "$msmtprc"
  182. # On Ubuntu/Debian, a link is needed since they use an older version.
  183. command -V apt-get >/dev/null 2>&1 && ln -s "$msmtprc" "$HOME/.msmtprc" 2>/dev/null
  184. # Create the mbsync config file if local account.
  185. [ -z "${online+x}" ] && mkdir -p "${mbsyncrc%/*}" && echo "$mbsync_profile" >> "$mbsyncrc"
  186. # Create a muttrc for viewing mail.
  187. echo "$mutt_profile" > "$accdir/$idnum-$fulladdr.muttrc"
  188. [ ! -f "$muttrc" ] && echo "# vim: filetype=neomuttrc" > "$muttrc"
  189. ! grep -q "^source.*mutt-wizard.muttrc" "$muttrc" && echo "source $mwconfig $marker" >> "$muttrc"
  190. ! grep "^source.*.muttrc" "$muttrc" | grep -qv "$mwconfig" && echo "source $accdir/$idnum-$fulladdr.muttrc $marker" >> "$muttrc"
  191. echo "macro index,pager i$idnum '<sync-mailbox><enter-command>source $accdir/$idnum-$fulladdr.muttrc<enter><change-folder>!<enter>;<check-stats>' \"switch to $fulladdr\" $marker" >> "$muttrc"
  192. notmuchauto # Create a notmuch config file if not present already.
  193. }
  194. getpass() { while : ; do pass rm -f "$pass_prefix$fulladdr" >/dev/null 2>&1
  195. pass insert "$pass_prefix$fulladdr" && break; done ;}
  196. formatShortcut() { toappend="$toappend
  197. macro index,pager g$1 \"<change-folder>=$3<enter>\" \"go to $2\" $marker
  198. macro index,pager M$1 \";<save-message>=$3<enter>\" \"move mail to $2\" $marker
  199. macro index,pager C$1 \";<copy-message>=$3<enter>\" \"copy mail to $2\" $marker" >> "$accdir/$idnum-$fulladdr.muttrc" ;}
  200. setBox() { toappend="$toappend
  201. set $1 = \"+$2\" $marker" ;}
  202. getboxes() { [ -n "${force+x}" ] && mailboxes="INBOX
  203. Drafts
  204. Junk
  205. Trash
  206. Sent
  207. Archive" && return 0
  208. info="$(curl --location-trusted -s -m 5 --user "$login:$(pass $pass_prefix$fulladdr)" --url "${protocol:-imaps}://$imap:${iport:-993}")"
  209. [ -z "$info" ] && echo "Log-on not successful." && return 1
  210. mailboxes="$(echo "$info" | sed "s/.*\" //;s/\"//g" | tr -d ' ')"
  211. }
  212. finalize() { echo "$mailboxes" | xargs -I {} mkdir -p "$maildir/$fulladdr/{}/cur" "$maildir/$fulladdr/{}/tmp" "$maildir/$fulladdr/{}/new"
  213. sed -ibu "/$marker/d" "$accdir/$idnum-$fulladdr.muttrc" ; rm -f "$accdir/$idnum-$fulladdr.muttrcbu"
  214. toappend="mailboxes $(echo "$mailboxes" | sed "s/^/\"=/;s/$/\"/" | paste -sd ' ' - )"
  215. for x in $mailboxes; do
  216. case "$x" in
  217. *[Ss][Ee][Nn][Tt]*) setBox record "$x"; formatShortcut s sent "$x" ;;
  218. *[Dd][Rr][Aa][Ff][Tt][Ss]*) setBox postponed "$x"; formatShortcut d drafts "$x" ;;
  219. *[Tt][Rr][Aa][Ss][Hh]*) formatShortcut t trash "$x"; setBox trash "$x" ;;
  220. *[Jj][Uu][Nn][Kk]*) formatShortcut j junk "$x" ;;
  221. *[Aa][Rr][Cc][Hh][Ii][Vv][Ee]*) formatShortcut a archive "$x" ;;
  222. *[Ss][Pp][Aa][Mm]*) formatShortcut S spam "$x" ;;
  223. *[Ii][Nn][Bb][Oo][Xx]*) formatShortcut i inbox "$x"; setBox spoolfile "$x" ;;
  224. esac
  225. done
  226. echo "$toappend" >> "$accdir/$idnum-$fulladdr.muttrc"
  227. [ -z "${online+x}" ] && echo "$fulladdr (account #$idnum) added successfully."
  228. command -V urlview >/dev/null 2>&1 && [ ! -f "$HOME/.urlview" ] && echo "COMMAND \$BROWSER" > "$HOME/.urlview"
  229. return 0 ;}
  230. notmuchauto() { \
  231. [ -z "$NOTMUCH_CONFIG" ] && NOTMUCH_CONFIG="$HOME/.notmuch-config"
  232. [ -f "$NOTMUCH_CONFIG" ] && return 0
  233. nmbasic="[database]
  234. path=$maildir
  235. [user]
  236. name=$realname
  237. primary_email=$fulladdr
  238. [new]
  239. tags=unread;inbox;
  240. ignore=.mbsyncstate;.uidvalidity
  241. [search]
  242. exclude_tags=deleted;spam;
  243. [maildir]
  244. synchronize_flags=true
  245. [crypto]
  246. gpg_path=$GPG"
  247. echo "$nmbasic" > "$NOTMUCH_CONFIG" ;}
  248. togglecron() { cron="$(mktemp)"
  249. crontab -l > "$cron"
  250. if grep -q mailsync "$cron"; then
  251. echo "Removing automatic mailsync..."
  252. sed -ibu /mailsync/d "$cron"; rm -f "$cron"bu
  253. else
  254. echo "Adding automatic mailsync every ${cronmin:-10} minutes..."
  255. echo "*/${cronmin-10} * * * * $prefix/bin/mailsync" >> "$cron"
  256. fi &&
  257. crontab "$cron"; rm -f "$cron" ;}
  258. setact() { if [ -n "${action+x}" ] && [ "$action" != "$1" ]; then
  259. echo "Running $1 with $action..."
  260. echo "Incompatible options given. Only one action may be specified per run."
  261. return 1
  262. else
  263. action="$1"
  264. fi; }
  265. mwinfo() { cat << EOF
  266. mw: mutt-wizard, auto-configure email accounts for mutt
  267. including downloadable mail with \`isync\`.
  268. Main actions:
  269. -a your@email.com Add an email address
  270. -l List email addresses configured
  271. -d Remove an already added address
  272. -D your@email.com Force remove account without confirmation
  273. -y your@email.com Sync mail for account by name
  274. -Y Sync mail for all accounts
  275. -t number Toggle automatic mailsync every <number> minutes
  276. -T Toggle automatic mailsync
  277. Options allowed with -a:
  278. -u Account login name if not full address
  279. -n "Real name" to be on the email account
  280. -i IMAP server address
  281. -I IMAP server port
  282. -s SMTP server address
  283. -S SMTP server port
  284. -x Password for account (recommended to be in double quotes)
  285. -p Install for a Protonmail account.
  286. -o Configure address, but keep mail online.
  287. -f Assume typical English mailboxes without attempting log-on.
  288. NOTE: Once at least one account is added, you can run
  289. \`mbsync -a\` to begin downloading mail.
  290. To change an account's password, run \`pass edit ${pass_prefix}your@email.com\`.
  291. EOF
  292. }
  293. while getopts "fplhodTYD:y:i:I:s:S:u:a:n:x:m:t:" o; do case "${o}" in
  294. l) setact list || exit 1 ;;
  295. d) setact delete || exit 1 ;;
  296. D) setact delete || exit 1 ; fulladdr="$OPTARG" ;;
  297. y) setact sync || exit 1 ; fulladdr="$OPTARG" ;;
  298. Y) setact sync || exit 1 ;;
  299. a) setact add || exit 1 ; fulladdr="$OPTARG" ;;
  300. i) setact add || exit 1 ; imap="$OPTARG" ;;
  301. I) setact add || exit 1 ; iport="$OPTARG" ;;
  302. s) setact add || exit 1 ; smtp="$OPTARG" ;;
  303. S) setact add || exit 1 ; sport="$OPTARG" ;;
  304. u) setact add || exit 1 ; login="$OPTARG" ;;
  305. n) setact add || exit 1 ; realname="$OPTARG" ;;
  306. m) setact add || exit 1 ; maxmes="$OPTARG" ;;
  307. o) setact add || exit 1 ; online=True ;;
  308. f) setact add || exit 1 ; force=True ;;
  309. x) setact add || exit 1 ; password="$OPTARG" ;;
  310. t) setact toggle || exit 1 ; cronmin="$OPTARG" ;;
  311. T) setact toggle || exit 1 ;;
  312. p) echo "NOTE: Protonmail users must install and configure Protonmail Bridge first for the first sync to work."
  313. protocol="imap"
  314. imap="127.0.0.1"
  315. iport="1143"
  316. smtp="127.0.0.1"
  317. sport="1025"
  318. tlsline="tls_fingerprint $(msmtp --serverinfo --host=$smtp --port=$sport --tls --tls-certcheck=off | awk '/SHA256:/ {print $2}')"
  319. setact add || exit 1
  320. ;;
  321. *) mwinfo; exit 1 ;;
  322. esac done
  323. case "$action" in
  324. list) list ;;
  325. add) checkbasics && askinfo && getprofiles && writeinfo && getboxes && finalize || { delete ; exit 1 ;} ;;
  326. delete) delete ;;
  327. sync) mailsync $fulladdr ;;
  328. toggle) togglecron ;;
  329. *) mwinfo; exit 1 ;;
  330. esac