Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

435 строки
12 KiB

  1. #!/bin/sh
  2. set -a
  3. prefix="/usr/local"
  4. maildir="${XDG_DATA_HOME:-$HOME/.local/share}/mail"
  5. muttshare="$prefix/share/mutt-wizard"
  6. cachedir="${XDG_CACHE_HOME:-$HOME/.cache}/mutt-wizard"
  7. muttrc="${XDG_CONFIG_HOME:-$HOME/.config}/mutt/muttrc"
  8. accdir="${XDG_CONFIG_HOME:-$HOME/.config}/mutt/accounts"
  9. msmtprc="${XDG_CONFIG_HOME:-$HOME/.config}/msmtp/config"
  10. msmtplog="${XDG_STATE_HOME:-$HOME/.local/state}/msmtp/msmtp.log"
  11. mbsyncrc="${MBSYNCRC:-$HOME/.mbsyncrc}"
  12. mpoprc="${XDG_CONFIG_HOME:-$HOME/.config}/mpop/config"
  13. mpoptemp="$muttshare/mpop-temp"
  14. mbsynctemp="$muttshare/mbsync-temp"
  15. mutttemp="$muttshare/mutt-temp"
  16. msmtptemp="$muttshare/msmtp-temp"
  17. onlinetemp="$muttshare/online-temp"
  18. notmuchtemp="$muttshare/notmuch-temp"
  19. # With the use of templates, it's impossible to use parameter substitution.
  20. # Therefore, some default variables that might be otherwise overwritten are set
  21. # here.
  22. iport="993"
  23. sport="465"
  24. imapssl="IMAPS"
  25. tlsline="tls_starttls off"
  26. maxmes="0"
  27. alias mbsync='mbsync -c "$mbsyncrc"'
  28. # mbsync now requires "Far/Near" rather than "Master/Slave", but Ubuntu/Debian
  29. # have the older version.
  30. if command -V apt-get >/dev/null 2>&1; then
  31. master="Master"
  32. slave="Slave"
  33. else
  34. master="Far"
  35. slave="Near"
  36. fi
  37. for x in "/etc/ssl/certs/ca-certificates.crt" \
  38. "/etc/pki/tls/certs/ca-bundle.crt" "/etc/ssl/cert.pem" \
  39. "/etc/ssl/ca-bundle.pem" "/etc/pki/tls/cacert.pem" \
  40. "/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem" \
  41. "/usr/local/share/ca-certificates/"; do
  42. [ -f "$x" ] && sslcert="$x" && break
  43. done || { echo "CA Certificate not found. Please install one or link it to /etc/ssl/certs/ca-certificates.crt" && exit 1; }
  44. checkbasics() {
  45. command -V gpg >/dev/null 2>&1 && GPG="gpg" || GPG="gpg2"
  46. PASSWORD_STORE_DIR="${PASSWORD_STORE_DIR:-$HOME/.password-store}"
  47. [ -r "$PASSWORD_STORE_DIR/.gpg-id" ] || {
  48. echo "First run \`pass init <yourgpgemail>\` to set up a password archive."
  49. echo "(If you don't already have a GPG key pair, first run \`$GPG --full-generate-key\`.)"
  50. exit 1
  51. }
  52. }
  53. getaccounts() { accounts="$(find -L "$accdir" -type f 2>/dev/null | grep -o "\S*.muttrc" | sed "s|.*/\([0-9]-\)*||;s/\.muttrc$//" | nl)"; }
  54. list() { getaccounts && [ -n "$accounts" ] && echo "$accounts" || exit 1; }
  55. prepmsmtp() {
  56. mkdir -p "${msmtprc%/*}" "${msmtplog%/*}"
  57. ln -s "$msmtprc" "$HOME/.msmtprc" 2>/dev/null
  58. envsubst <"$msmtptemp" >>"$msmtprc"
  59. }
  60. prepmbsync() {
  61. mkdir -p "${mbsyncrc%/*}"
  62. [ -f "$mbsyncrc" ] && echo >>"$mbsyncrc"
  63. envsubst <"$mbsynctemp" >>"$mbsyncrc"
  64. }
  65. prepmpop() {
  66. mkdir -p "${mpoprc%/*}"
  67. envsubst <"$mpoptemp" >>"$mpoprc"
  68. }
  69. prepmutt() {
  70. mkdir -p "${muttrc%/*}" "$accdir"
  71. envsubst <"$mutttemp" >"$accdir/$fulladdr.muttrc"
  72. [ ! -f "$muttrc" ] && echo "# vim: filetype=neomuttrc" >"$muttrc"
  73. ! grep -q "^source.*mutt-wizard.muttrc" "$muttrc" && echo "source $muttshare/mutt-wizard.muttrc" >>"$muttrc"
  74. ! grep "^source.*.muttrc" "$muttrc" | grep -qv "$muttshare/mutt-wizard.muttrc" && echo "source $accdir/$fulladdr.muttrc" >>"$muttrc"
  75. echo "macro index,pager i$idnum '<sync-mailbox><enter-command>source $accdir/$fulladdr.muttrc<enter><change-folder>!<enter>;<check-stats>' \"switch to $fulladdr\"" >>"$muttrc"
  76. }
  77. getprofiles() {
  78. safename="$(echo $fulladdr | sed 's/@/_/g')"
  79. case "$type" in
  80. online)
  81. folder="imaps://$login@$imap:$iport"
  82. extra="$(envsubst <"$onlinetemp")"
  83. ;;
  84. pop) prepmpop ;;
  85. *)
  86. case "$iport" in
  87. 1143) imapssl=None ;;
  88. 143) imapssl=STARTTLS ;;
  89. esac
  90. prepmbsync
  91. ;;
  92. esac
  93. prepmsmtp
  94. prepmutt
  95. prepnotmuch
  96. }
  97. parsedomains() {
  98. serverinfo="$(grep "^${fulladdr#*@}" "$muttshare/domains.csv" 2>/dev/null)"
  99. [ -z "$serverinfo" ] && serverinfo="$(grep "$(echo "${fulladdr#*@}" | sed "s/\.[^\.]*$/\.\\\*/")" "$muttshare/domains.csv" 2>/dev/null)"
  100. IFS=, read -r service imapsugg iportsugg smtpsugg sportsugg <<EOF
  101. $serverinfo
  102. EOF
  103. imap="${imap:-$imapsugg}"
  104. smtp="${smtp:-$smtpsugg}"
  105. sport="${sport:-$sportsugg}"
  106. iport="${iport:-$iportsugg}"
  107. }
  108. delete() {
  109. if [ -z "${fulladdr+x}" ]; then
  110. echo "Select the account you would like to delete (by number):"
  111. list || exit 1
  112. read -r input
  113. match="^\s*$input\s\+"
  114. else
  115. match="\s\+$fulladdr$"
  116. getaccounts
  117. fi
  118. fulladdr="$(echo "$accounts" | grep "$match" | grep -o "\S*@\S*")"
  119. [ -z "$fulladdr" ] && echo "$fulladdr is not a valid account name." && return 1
  120. sed -ibu "/IMAPStore $fulladdr-remote$/,/# End profile/d" "$mbsyncrc" 2>/dev/null
  121. rm -f "$mbsyncrc"bu
  122. rm -rf "${cachedir:?}/${fulladdr:?}" "$accdir/$fulladdr.muttrc" "$accdir/"[0-9]-"$fulladdr.muttrc"
  123. sed -ibu "/\([0-9]-\)\?$fulladdr.muttrc/d" "$muttrc" 2>/dev/null
  124. rm -f "$muttrc"bu
  125. sed -ibu "/account $fulladdr$/,/^\(\s*$\|account\)/d" "$msmtprc" 2>/dev/null
  126. rm -f "$msmtprc"bu
  127. sed -ibu "/account $fulladdr$/,/^\(\s*$\|account\)/d" "$mpoprc" 2>/dev/null
  128. rm -f "$mpoprc"bu
  129. pass rm -f "$passprefix$fulladdr" >/dev/null 2>&1
  130. [ -n "${purge+x}" ] && safename="$(echo $fulladdr | sed 's/@/_/g')" && rm -rf "${cachedir:?}/${safename:?}" "${maildir:?}/${fulladdr:?}"
  131. }
  132. askinfo() {
  133. [ -z "$fulladdr" ] && echo "Give the full email address to add:" &&
  134. read -r fulladdr
  135. while ! echo "$fulladdr" | grep -qE "^.+@.+\.[A-Za-z]+$"; do
  136. echo "$fulladdr is not a valid email address. Please retype the address:"
  137. read -r fulladdr
  138. done
  139. folder="$maildir/$fulladdr"
  140. getaccounts
  141. echo "$accounts" | grep -q "\s$fulladdr$" 2>/dev/null &&
  142. { echo "$fulladdr has already been added" && exit 1; }
  143. { [ -z "$imap" ] || [ -z "$smtp" ]; } && parsedomains
  144. [ -z "$imap" ] && echo "Give your email server's IMAP address (excluding the port number):" &&
  145. read -r imap
  146. [ -z "$smtp" ] && echo "Give your email server's SMTP address (excluding the port number):" &&
  147. read -r smtp
  148. case $sport in
  149. 587) tlsline="# tls_starttls" ;;
  150. esac
  151. [ -z "$realname" ] && realname="${fulladdr%%@*}"
  152. [ -z "$passprefix" ] && passprefix=""
  153. hostname="${fulladdr#*@}"
  154. login="${login:-$fulladdr}"
  155. if [ -n "${password+x}" ]; then
  156. createpass
  157. else
  158. getpass
  159. fi
  160. }
  161. createpass() {
  162. echo "$password" >"$PASSWORD_STORE_DIR/$passprefix$fulladdr"
  163. "$GPG" -qe $(printf -- " -r %s" $(cat "$PASSWORD_STORE_DIR/.gpg-id")) "$PASSWORD_STORE_DIR/$passprefix$fulladdr"
  164. case "$(uname)" in
  165. Darwin | *BSD) rm -P "$PASSWORD_STORE_DIR/$passprefix$fulladdr" ;;
  166. *) shred -u "$PASSWORD_STORE_DIR/$passprefix$fulladdr" ;;
  167. esac
  168. rm -f "$PASSWORD_STORE_DIR/$passprefix$fulladdr"
  169. }
  170. errorexit() {
  171. echo "Log-on not successful."
  172. case "$imap" in
  173. imap.gmail.com)
  174. echo "This account with $service is using Google's Gmail servers, which disable all third-party applications without an application-specific password.
  175. Please be sure you are using OAUTH with your Gmail account, or better yet, stop using Gmail."
  176. ;;
  177. imap.mail.me.com)
  178. echo "This account with $service is using Apple's iCloud servers, which disable all non-Apple applications by default.
  179. Please be sure you either enable third-party applications, or create an app-specific password, or best of all, stop using Apple."
  180. ;;
  181. esac
  182. exit 1
  183. }
  184. getpass() { while :; do
  185. pass rm -f "$passprefix$fulladdr" >/dev/null 2>&1
  186. pass insert -f "$passprefix$fulladdr" && break
  187. done; }
  188. getboxes() {
  189. if [ -n "${force+x}" ]; then
  190. mailboxes="$(printf "INBOX\\nDrafts\\nJunk\\nTrash\\nSent\\nArchive")"
  191. else
  192. info="$(curl "$curlarg" --location-trusted -s -m 5 --user "$login:$(pass "$passprefix$fulladdr")" --url "${protocol:-imaps}://$imap:${iport:-993}")"
  193. [ -z "$info" ] && errorexit
  194. mailboxes="$(echo "$info" | grep -v HasChildren | sed "s/.*\" //;s/\"//g" | tr -d '\r')"
  195. fi
  196. [ "$type" = "pop" ] && mailboxes="INBOX"
  197. for x in $(
  198. sed -n "/^macro.* i[0-9] / s/\(^macro.* i\| .*\)//gp " "$muttrc" 2>/dev/null | sort -u
  199. echo 0
  200. ); do
  201. idnum=$((idnum + 1))
  202. [ "$idnum" -eq "$x" ] || break
  203. done
  204. toappend="mailboxes $(echo "$mailboxes" | sed "s/^/\"=/;s/$/\"/;s/'/\\\'/g" | paste -sd ' ' -)"
  205. }
  206. finalize() {
  207. echo "$toappend" >>"$accdir/$fulladdr.muttrc"
  208. [ "$type" != "online" ] && echo "$mailboxes" | xargs -I {} mkdir -p "$maildir/$fulladdr/{}/cur" "$maildir/$fulladdr/{}/tmp" "$maildir/$fulladdr/{}/new"
  209. mkdir -p "$cachedir/$safename/bodies"
  210. echo "$fulladdr (account #$idnum) added successfully."
  211. command -V urlview >/dev/null 2>&1 && [ ! -f "$HOME/.urlview" ] && echo "COMMAND \$BROWSER" >"$HOME/.urlview"
  212. return 0
  213. }
  214. prepnotmuch() {
  215. [ -z "$NOTMUCH_CONFIG" ] && NOTMUCH_CONFIG="$HOME/.notmuch-config"
  216. [ -f "$NOTMUCH_CONFIG" ] && return 0
  217. envsubst <"$notmuchtemp" >"$NOTMUCH_CONFIG"
  218. }
  219. togglecron() {
  220. cron="$(mktemp)"
  221. crontab -l >"$cron"
  222. if grep -q mailsync "$cron"; then
  223. echo "Removing automatic mailsync..."
  224. sed -ibu /mailsync/d "$cron"
  225. rm -f "$cron"bu
  226. else
  227. echo "Adding automatic mailsync every ${cronmin:-10} minutes..."
  228. echo "*/${cronmin:-10} * * * * $prefix/bin/mailsync" >>"$cron"
  229. fi &&
  230. crontab "$cron"
  231. rm -f "$cron"
  232. }
  233. setact() { if [ -n "${action+x}" ] && [ "$action" != "$1" ]; then
  234. echo "Running $1 with $action..."
  235. echo "Incompatible options given. Only one action may be specified per run."
  236. exit 1
  237. else
  238. action="$1"
  239. fi; }
  240. mwinfo() {
  241. cat <<EOF
  242. mw: mutt-wizard, auto-configure email accounts for mutt
  243. including downloadable mail with \`isync\`.
  244. Main actions:
  245. -a your@email.com Add an email address
  246. -l List email addresses configured
  247. -d Remove an already added address
  248. -D your@email.com Force remove account without confirmation
  249. -t number Toggle automatic mailsync every <number> minutes
  250. -T Toggle automatic mailsync
  251. -r Reorder account numbers
  252. Options allowed with -a:
  253. -u Account login name if not full address
  254. -n "Real name" to be on the email account
  255. -i IMAP/POP server address
  256. -I IMAP/POP server port
  257. -s SMTP server address
  258. -S SMTP server port
  259. -x Password for account (recommended to be in double quotes)
  260. -p Add for a POP server instead of IMAP.
  261. -P Pass Prefix (prefix of the file where password is stored)
  262. -X Delete an account's local email too when deleting.
  263. -o Configure address, but keep mail online.
  264. -f Assume typical English mailboxes without attempting log-on.
  265. -c Path to self-signed TLS certificate.
  266. NOTE: Once at least one account is added, you can run
  267. \`mbsync -a\` to begin downloading mail.
  268. To change an account's password, run \`pass edit '$passprefix'your@email.com\`.
  269. EOF
  270. }
  271. reorder() {
  272. tempfile="$(mktemp -u)"
  273. trap 'rm -f $tempfile' HUP INT QUIT TERM PWR EXIT
  274. echo "# Carefully reorder these accounts with the desired numbers in the first column.
  275. # DO NOT reorder rows or rename the accounts in the second column." >"$tempfile"
  276. sed -n "
  277. / i[0-9] / s?\(.* i\|'<sync.*/\|\.muttrc.*\)??g p
  278. " "$muttrc" >>"$tempfile"
  279. ${EDITOR:-vim} "$tempfile" || exit 1
  280. sed -i -e 's/#.*//' -e '/^$/d' "$tempfile"
  281. default="$(sort -n "$tempfile" | head -n 1)"
  282. default="${default#* }"
  283. sed -ibu "
  284. /.* i[0-9] .*.muttrc/d
  285. /^source.*accounts.*.muttrc/d
  286. " "$muttrc" 2>/dev/null
  287. rm -f "$muttrc"bu
  288. awk -v a="$accdir" -v d="$default" ' BEGIN { print "source "a"/"d".muttrc" }
  289. {
  290. print "macro index,pager i"$1" '\''<sync-mailbox><enter-command>source "a"/"$2".muttrc<enter><change-folder>!<enter>;<check-stats>'\'' \"switch to "$2"\""
  291. }
  292. ' "$tempfile" >>"$muttrc"
  293. }
  294. while getopts "rfpXlhodTYD:y:i:I:s:S:u:a:n:P:x:m:t:c:" o; do case "${o}" in
  295. l) setact list ;;
  296. r) setact reorder1 ;;
  297. d) setact delete ;;
  298. D)
  299. setact delete
  300. fulladdr="$OPTARG"
  301. ;;
  302. y)
  303. setact sync
  304. fulladdr="$OPTARG"
  305. ;;
  306. Y) setact sync ;;
  307. a)
  308. setact add
  309. fulladdr="$OPTARG"
  310. ;;
  311. i)
  312. setact add
  313. imap="$OPTARG"
  314. ;;
  315. I)
  316. setact add
  317. iport="$OPTARG"
  318. ;;
  319. s)
  320. setact add
  321. smtp="$OPTARG"
  322. ;;
  323. S)
  324. setact add
  325. sport="$OPTARG"
  326. ;;
  327. u)
  328. setact add
  329. login="$OPTARG"
  330. ;;
  331. n)
  332. setact add
  333. realname="$OPTARG"
  334. ;;
  335. P)
  336. setact add
  337. passprefix="$OPTARG"
  338. ;;
  339. m)
  340. setact add
  341. maxmes="$OPTARG"
  342. ;;
  343. o)
  344. setact add
  345. type="online"
  346. ;;
  347. p)
  348. setact add
  349. type="pop"
  350. protocol="pop3s"
  351. iport="${iport:-995}"
  352. ;;
  353. f)
  354. setact add
  355. force=True
  356. ;;
  357. x)
  358. setact add
  359. password="$OPTARG"
  360. ;;
  361. c)
  362. setact add
  363. sslcert="$OPTARG"
  364. curlarg="$curlarg -k"
  365. ;;
  366. X)
  367. setact delete
  368. purge=True
  369. ;;
  370. t)
  371. setact toggle
  372. cronmin="$OPTARG"
  373. ;;
  374. T) setact toggle ;;
  375. h) setact info ;;
  376. \?)
  377. echo "See \`$(basename $0) -h\` for possible options and help."
  378. exit 1
  379. ;;
  380. esac done
  381. [ -z "$action" ] && action="info"
  382. case "$action" in
  383. list) list ;;
  384. add) checkbasics && askinfo && getboxes && getprofiles && finalize ;;
  385. delete) delete ;;
  386. sync)
  387. echo "\`mw -y\` and \`mw -Y\` are now deprecated and will be removed in a future update. Please switch to using \`mailsync\`."
  388. mailsync $fulladdr
  389. ;;
  390. toggle) togglecron ;;
  391. reorder) reorder ;;
  392. info)
  393. mwinfo
  394. exit 1
  395. ;;
  396. esac