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.

mw 14 KiB

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