選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 

457 行
13 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. # TODO: oauth2 only for mbsync right now
  79. safename="$(echo $fulladdr | sed 's/@/_/g')"
  80. case "$type" in
  81. online)
  82. folder="imaps://$login@$imap:$iport"
  83. extra="$(envsubst <"$onlinetemp")"
  84. ;;
  85. pop) prepmpop ;;
  86. *)
  87. case "$iport" in
  88. 1143) imapssl=None ;;
  89. 143) imapssl=STARTTLS ;;
  90. esac
  91. prepmbsync
  92. ;;
  93. esac
  94. prepmsmtp
  95. prepmutt
  96. prepnotmuch
  97. }
  98. parsedomains() {
  99. serverinfo="$(grep "^${fulladdr#*@}" "$muttshare/domains.csv" 2>/dev/null)"
  100. [ -z "$serverinfo" ] && serverinfo="$(grep "$(echo "${fulladdr#*@}" | sed "s/\.[^\.]*$/\.\\\*/")" "$muttshare/domains.csv" 2>/dev/null)"
  101. IFS=, read -r service imapsugg iportsugg smtpsugg sportsugg <<EOF
  102. $serverinfo
  103. EOF
  104. imap="${imap:-$imapsugg}"
  105. smtp="${smtp:-$smtpsugg}"
  106. sport="${sport:-$sportsugg}"
  107. iport="${iport:-$iportsugg}"
  108. }
  109. delete() {
  110. if [ -z "${fulladdr+x}" ]; then
  111. echo "Select the account you would like to delete (by number):"
  112. list || exit 1
  113. read -r input
  114. match="^\s*$input\s\+"
  115. else
  116. match="\s\+$fulladdr$"
  117. getaccounts
  118. fi
  119. fulladdr="$(echo "$accounts" | grep "$match" | grep -o "\S*@\S*")"
  120. [ -z "$fulladdr" ] && echo "$fulladdr is not a valid account name." && return 1
  121. sed -ibu "/IMAPStore $fulladdr-remote$/,/# End profile/d" "$mbsyncrc" 2>/dev/null
  122. rm -f "$mbsyncrc"bu
  123. rm -rf "${cachedir:?}/${fulladdr:?}" "$accdir/$fulladdr.muttrc" "$accdir/"[0-9]-"$fulladdr.muttrc"
  124. sed -ibu "/\([0-9]-\)\?$fulladdr.muttrc/d" "$muttrc" 2>/dev/null
  125. rm -f "$muttrc"bu
  126. sed -ibu "/account $fulladdr$/,/^\(\s*$\|account\)/d" "$msmtprc" 2>/dev/null
  127. rm -f "$msmtprc"bu
  128. sed -ibu "/account $fulladdr$/,/^\(\s*$\|account\)/d" "$mpoprc" 2>/dev/null
  129. rm -f "$mpoprc"bu
  130. pass rm -f "$passprefix$fulladdr" >/dev/null 2>&1
  131. [ -n "${purge+x}" ] && safename="$(echo $fulladdr | sed 's/@/_/g')" && rm -rf "${cachedir:?}/${safename:?}" "${maildir:?}/${fulladdr:?}"
  132. }
  133. askinfo() {
  134. [ -z "$fulladdr" ] && echo "Give the full email address to add:" &&
  135. read -r fulladdr
  136. while ! echo "$fulladdr" | grep -qE "^.+@.+\.[A-Za-z]+$"; do
  137. echo "$fulladdr is not a valid email address. Please retype the address:"
  138. read -r fulladdr
  139. done
  140. folder="$maildir/$fulladdr"
  141. getaccounts
  142. echo "$accounts" | grep -q "\s$fulladdr$" 2>/dev/null &&
  143. { echo "$fulladdr has already been added" && exit 1; }
  144. { [ -z "$imap" ] || [ -z "$smtp" ]; } && parsedomains
  145. [ -z "$imap" ] && echo "Give your email server's IMAP address (excluding the port number):" &&
  146. read -r imap
  147. [ -z "$smtp" ] && echo "Give your email server's SMTP address (excluding the port number):" &&
  148. read -r smtp
  149. case $sport in
  150. 587) tlsline="# tls_starttls" ;;
  151. esac
  152. [ -z "$realname" ] && realname="${fulladdr%%@*}"
  153. [ -z "$passprefix" ] && passprefix=""
  154. hostname="${fulladdr#*@}"
  155. login="${login:-$fulladdr}"
  156. [ -f "$oauthtokenfile" ] ||
  157. printf "If you want to use OAUTH2 (for Microsoft or Google), input path to pre-created token file (see help). Otherwise, leave empty: " &&
  158. read -r oauthtokenfile
  159. if [ -f "$oauthtokenfile" ]; then
  160. authtype_msmtp=xoauth2
  161. authtype_mbsync=XOAUTH2
  162. else
  163. [ -n "$oauthtokenfile" ] && echo "Token file not found"
  164. authtype_msmtp=on
  165. authtype_mbsync=LOGIN
  166. if [ -n "${password+x}" ]; then
  167. insertpass
  168. else
  169. getpass
  170. fi
  171. fi
  172. pass_cmdline="$(pass_cmdline)"
  173. }
  174. insertpass() {
  175. printf "%s" "$password" | pass insert -fe "$PASSWORD_STORE_DIR/$passprefix$fulladdr"
  176. }
  177. errorexit() {
  178. echo "Log-on not successful."
  179. case "$imap" in
  180. imap.mail.me.com)
  181. echo "This account with $service is using Apple's iCloud servers, which disable all non-Apple applications by default.
  182. Please be sure you either enable third-party applications, or create an app-specific password, or best of all, stop using Apple."
  183. ;;
  184. esac
  185. exit 1
  186. }
  187. pass_cmdline() {
  188. if [ -f "$oauthtokenfile" ]; then
  189. # do not use pass insert to not clutter pass git history with token updates
  190. encrypt_pipe="$GPG -qe $(printf -- " -r %s" $(cat "$PASSWORD_STORE_DIR/.gpg-id"))"
  191. printf '%s ' /usr/share/neomutt/oauth2/mutt_oauth2.py --encryption-pipe "$encrypt_pipe" "$passprefix$fulladdr.tokens"
  192. else
  193. printf '%s ' pass "$passprefix$fulladdr"
  194. fi
  195. }
  196. getpass() { while :; do
  197. pass rm -f "$passprefix$fulladdr" >/dev/null 2>&1
  198. pass insert -f "$passprefix$fulladdr" && break
  199. done; }
  200. getboxes() {
  201. # TODO: add oauth2 curl
  202. # in the meantime, get box names after syncing from folder structure:
  203. #for d in "$maildir"/*
  204. #do
  205. # echo "$(basename "$d"):"
  206. # mailboxes="$(find "$d" -mindepth 1 -type d -not -name 'cur' -not -name 'new' -not -name 'tmp' -printf '="%P" ')"
  207. # printf "\tmailboxes %s\n\n" "$mailboxes"
  208. #done
  209. if [ -f "$oauthtokenfile" ] || [ -n "${force+x}" ]; then
  210. mailboxes="$(printf "INBOX\\nDrafts\\nJunk\\nTrash\\nSent\\nArchive")"
  211. else
  212. info="$(curl --location-trusted -s -m 5 --user "$login:$(pass show "$prefix$fulladdr")" --url "${protocol:-imaps}://$imap:${iport:-993}")"
  213. [ -z "$info" ] && errorexit
  214. mailboxes="$(echo "$info" | grep -v HasChildren | sed "s/.*\" //;s/\"//g" | tr -d '\r')"
  215. fi
  216. [ "$type" = "pop" ] && mailboxes="INBOX"
  217. for x in $(
  218. sed -n "/^macro.* i[0-9] / s/\(^macro.* i\| .*\)//gp " "$muttrc" 2>/dev/null | sort -u
  219. echo 0
  220. ); do
  221. idnum=$((idnum + 1))
  222. [ "$idnum" -eq "$x" ] || break
  223. done
  224. toappend="mailboxes $(echo "$mailboxes" | sed "s/^/\"=/;s/$/\"/;s/'/\\\'/g" | paste -sd ' ' -)"
  225. }
  226. finalize() {
  227. echo "$toappend" >>"$accdir/$fulladdr.muttrc"
  228. [ "$type" != "online" ] && echo "$mailboxes" | xargs -I {} mkdir -p "$maildir/$fulladdr/{}/cur" "$maildir/$fulladdr/{}/tmp" "$maildir/$fulladdr/{}/new"
  229. mkdir -p "$cachedir/$safename/bodies"
  230. echo "$fulladdr (account #$idnum) added successfully."
  231. command -V urlview >/dev/null 2>&1 && [ ! -f "$HOME/.urlview" ] && echo "COMMAND \$BROWSER" >"$HOME/.urlview"
  232. return 0
  233. }
  234. prepnotmuch() {
  235. [ -z "$NOTMUCH_CONFIG" ] && NOTMUCH_CONFIG="$HOME/.notmuch-config"
  236. [ -f "$NOTMUCH_CONFIG" ] && return 0
  237. envsubst <"$notmuchtemp" >"$NOTMUCH_CONFIG"
  238. }
  239. togglecron() {
  240. cron="$(mktemp)"
  241. crontab -l >"$cron"
  242. if grep -q mailsync "$cron"; then
  243. echo "Removing automatic mailsync..."
  244. sed -ibu /mailsync/d "$cron"
  245. rm -f "$cron"bu
  246. else
  247. echo "Adding automatic mailsync every ${cronmin:-10} minutes..."
  248. echo "*/${cronmin:-10} * * * * $prefix/bin/mailsync" >>"$cron"
  249. fi &&
  250. crontab "$cron"
  251. rm -f "$cron"
  252. }
  253. setact() { if [ -n "${action+x}" ] && [ "$action" != "$1" ]; then
  254. echo "Running $1 with $action..."
  255. echo "Incompatible options given. Only one action may be specified per run."
  256. exit 1
  257. else
  258. action="$1"
  259. fi; }
  260. mwinfo() {
  261. cat <<EOF
  262. mw: mutt-wizard, auto-configure email accounts for mutt
  263. including downloadable mail with \`isync\`.
  264. Main actions:
  265. -a your@email.com Add an email address
  266. -l List email addresses configured
  267. -d Remove an already added address
  268. -D your@email.com Force remove account without confirmation
  269. -t number Toggle automatic mailsync every <number> minutes
  270. -T Toggle automatic mailsync
  271. -r Reorder account numbers
  272. Options allowed with -a:
  273. -u Account login name if not full address
  274. -n "Real name" to be on the email account
  275. -i IMAP/POP server address
  276. -I IMAP/POP server port
  277. -s SMTP server address
  278. -S SMTP server port
  279. -x Password for account (recommended to be in double quotes)
  280. -o Registered OAUTH2 token file path. See mw(1) for more info.
  281. -p Add for a POP server instead of IMAP.
  282. -P Pass Prefix (prefix of the file where password is stored)
  283. -X Delete an account's local email too when deleting.
  284. -o Configure address, but keep mail online.
  285. -f Assume typical English mailboxes without attempting log-on.
  286. NOTE: Once at least one account is added, you can run
  287. \`mbsync -a\` to begin downloading mail.
  288. To change an account's password, run \`pass edit '$passprefix'your@email.com\`.
  289. EOF
  290. }
  291. reorder() {
  292. tempfile="$(mktemp -u)"
  293. trap 'rm -f $tempfile' HUP INT QUIT TERM PWR EXIT
  294. echo "# Carefully reorder these accounts with the desired numbers in the first column.
  295. # DO NOT reorder rows or rename the accounts in the second column." >"$tempfile"
  296. sed -n "
  297. / i[0-9] / s?\(.* i\|'<sync.*/\|\.muttrc.*\)??g p
  298. " "$muttrc" >>"$tempfile"
  299. ${EDITOR:-vim} "$tempfile" || exit 1
  300. sed -i -e 's/#.*//' -e '/^$/d' "$tempfile"
  301. default="$(sort -n "$tempfile" | head -n 1)"
  302. default="${default#* }"
  303. sed -ibu "
  304. /.* i[0-9] .*.muttrc/d
  305. /^source.*accounts.*.muttrc/d
  306. " "$muttrc" 2>/dev/null
  307. rm -f "$muttrc"bu
  308. awk -v a="$accdir" -v d="$default" ' BEGIN { print "source "a"/"d".muttrc" }
  309. {
  310. print "macro index,pager i"$1" '\''<sync-mailbox><enter-command>source "a"/"$2".muttrc<enter><change-folder>!<enter>;<check-stats>'\'' \"switch to "$2"\""
  311. }
  312. ' "$tempfile" >>"$muttrc"
  313. }
  314. while getopts "rfpXlhodTYD:y:i:I:s:S:u:a:n:P:x:O:m:t:" o; do case "${o}" in
  315. l) setact list ;;
  316. r) setact reorder ;;
  317. d) setact delete ;;
  318. D)
  319. setact delete
  320. fulladdr="$OPTARG"
  321. ;;
  322. y)
  323. setact sync
  324. fulladdr="$OPTARG"
  325. ;;
  326. Y) setact sync ;;
  327. a)
  328. setact add
  329. fulladdr="$OPTARG"
  330. ;;
  331. i)
  332. setact add
  333. imap="$OPTARG"
  334. ;;
  335. I)
  336. setact add
  337. iport="$OPTARG"
  338. ;;
  339. s)
  340. setact add
  341. smtp="$OPTARG"
  342. ;;
  343. S)
  344. setact add
  345. sport="$OPTARG"
  346. ;;
  347. u)
  348. setact add
  349. login="$OPTARG"
  350. ;;
  351. n)
  352. setact add
  353. realname="$OPTARG"
  354. ;;
  355. P)
  356. setact add
  357. passprefix="$OPTARG"
  358. ;;
  359. m)
  360. setact add
  361. maxmes="$OPTARG"
  362. ;;
  363. o)
  364. setact add
  365. type="online"
  366. ;;
  367. p)
  368. setact add
  369. type="pop"
  370. protocol="pop3s"
  371. iport="${iport:-995}"
  372. ;;
  373. f)
  374. setact add
  375. force=True
  376. ;;
  377. x)
  378. setact add
  379. password="$OPTARG"
  380. ;;
  381. O)
  382. setact add
  383. oauthtokenfile="$OPTARG"
  384. ;;
  385. X)
  386. setact delete
  387. purge=True
  388. ;;
  389. t)
  390. setact toggle
  391. cronmin="$OPTARG"
  392. ;;
  393. T) setact toggle ;;
  394. h) setact info ;;
  395. \?)
  396. echo "See \`$(basename $0) -h\` for possible options and help."
  397. exit 1
  398. ;;
  399. esac done
  400. [ -z "$action" ] && action="info"
  401. case "$action" in
  402. list) list ;;
  403. add) checkbasics && askinfo && getboxes && getprofiles && finalize ;;
  404. delete) delete ;;
  405. sync)
  406. echo "\`mw -y\` and \`mw -Y\` are now deprecated and will be removed in a future update. Please switch to using \`mailsync\`."
  407. mailsync $fulladdr
  408. ;;
  409. toggle) togglecron ;;
  410. reorder) reorder ;;
  411. info)
  412. mwinfo
  413. exit 1
  414. ;;
  415. esac