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.
 
 
 
 

437 line
13 KiB

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