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

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