25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

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