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