aerc-wizard

aerc and isync auto-configuration
git clone https://git.awy.one/aerc-wizard
Log | Files | Refs | README | LICENSE

aew (15037B) - View raw


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
#!/bin/bash
# aew - auto-configure email accounts for aerc
# including downloadable mail with `isync`.
#
# Copyright (C) 2018-2025 Luke Smith <luke@lukesmith.xyz>
# Copyright (C) 2025 awy <awy@awy.one>
#
# This file is part of aerc-wizard.
#
# aerc-wizard is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation,
# either version 3 of the License, or (at your option) any later version.
#
# aerc-wizard is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with aerc-wizard. If not, see
# <https://www.gnu.org/licenses/>.

set -a

prefix="/usr/local"
maildir="${XDG_DATA_HOME:-$HOME/.local/share}/mail"
aercshare="$prefix/share/aerc-wizard"
cachedir="${XDG_CACHE_HOME:-$HOME/.cache}/aerc-wizard"
aercdir="${XDG_CONFIG_HOME:-$HOME/.config}/aerc"
aercconf="$aercdir/aerc.conf"
accountsconf="$aercdir/accounts.conf"
bindsconf="$aercdir/binds.conf"
msmtprc="${XDG_CONFIG_HOME:-$HOME/.config}/msmtp/config"
msmtplog="${XDG_STATE_HOME:-$HOME/.local/state}/msmtp/msmtp.log"
mbsyncrc="${MBSYNCRC:-$HOME/.mbsyncrc}"
mpoprc="${XDG_CONFIG_HOME:-$HOME/.config}/mpop/config"
imapnotify="${XDG_CONFIG_HOME:-$HOME/.config}/imapnotify"
mpoptemp="$aercshare/mpop-temp"
mbsynctemp="$aercshare/mbsync-temp"
mbsyncgmailtemp="$aercshare/mbsync-gmail-temp"
aerctemp="$aercshare/aerc-temp"
aercbase="$aercshare/aerc-base"
aerctempconf="$aercshare/aerc-conf"
aerctempbinds="$aercshare/aerc-binds"
msmtptemp="$aercshare/msmtp-temp"
onlinetemp="$aercshare/online-temp"
notmuchtemp="$aercshare/notmuch-temp"
imapnotifytemp="$aercshare/imapnotify-temp"
# With the use of templates, it's impossible to use parameter substitution.
# Therefore, some default variables that might be otherwise overwritten are set
# here.
iport="993"
sport="465"
imapssl="IMAPS"
tlsline="tls_starttls off"
maxmes="0"

alias mbsync='mbsync -c "$mbsyncrc"'

# mbsync >=1.4.0 requires "Far/Near" rather than "Master/Slave."
mbver="$(mbsync -v)"
mbver="${mbver#* }"
mbver="${mbver%.*}"
# Comparing two float numbers in bash is a pain in the butt, so we get rid of
# dots and turn them into nice integers
mbver="${mbver/\./}"
if [ "${mbver}" -gt 14 ]; then
  master="Far"
  slave="Near"
else
  master="Master"
  slave="Slave"
fi

for x in "/etc/ssl/certs/ca-certificates.crt" \
  "/etc/pki/tls/certs/ca-bundle.crt" "/etc/ssl/cert.pem" \
  "/etc/ssl/ca-bundle.pem" "/etc/pki/tls/cacert.pem" \
  "/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem" \
  "/usr/local/share/ca-certificates/"; do
  [ -f "$x" ] && sslcert="$x" && break
done || { echo "CA Certificate not found. Please install one or link it to /etc/ssl/certs/ca-certificates.crt" && exit 1; }

checkbasics() {
  command -V gpg >/dev/null 2>&1 && GPG="gpg" || GPG="gpg2"
  PASSWORD_STORE_DIR="${PASSWORD_STORE_DIR:-$HOME/.password-store}"
  [ -r "$PASSWORD_STORE_DIR/.gpg-id" ] || {
    echo "First run \`gopass init <yourgpgemail>\` to set up a password archive."
    echo "(If you don't already have a GPG key pair, first run \`$GPG --full-generate-key\`.)"
    exit 1
  }
}

getaccounts() { accounts="$(sed -n 's/^\[\(.*\)\]/\1/p' "$accountsconf" 2>/dev/null | nl)"; }

list() { getaccounts && [ -n "$accounts" ] && echo "$accounts" || exit 1; }

prepmsmtp() {
  mkdir -p "${msmtprc%/*}" "${msmtplog%/*}"
  ln -s "$msmtprc" "$HOME/.msmtprc" 2>/dev/null
  envsubst <"$msmtptemp" >>"$msmtprc"
}

prepmbsync() {
  mkdir -p "${mbsyncrc%/*}"
  [ -f "$mbsyncrc" ] && echo >>"$mbsyncrc"
  case "$fulladdr" in
  *@gmail.com)
    envsubst <"$mbsyncgmailtemp" >>"$mbsyncrc"
    ;;
  *)
    envsubst <"$mbsynctemp" >>"$mbsyncrc"
    ;;
  esac
}

prepmpop() {
  mkdir -p "${mpoprc%/*}"
  envsubst <"$mpoptemp" >>"$mpoprc"
}

prepimapnotify() {
  mkdir -p "$imapnotify"
  envsubst <"$imapnotifytemp" >>"$imapnotify/$fulladdr.conf"
}

prepaerc() {
  mkdir -p "$aercdir"
  aercuser="${fulladdr%@*}"
  aercdomain="${fulladdr#*@}"
  [ ! -f "$aercconf" ] && cat "$aerctempconf" >"$aercconf"
  [ ! -f "$bindsconf" ] && cat "$aerctempbinds" >"$bindsconf"
  [ ! -f "$accountsconf" ] && envsubst <"$aercbase" >"$accountsconf"
  ! grep -q "^multi-file-strategy" "$accountsconf" && envsubst <"$aercbase" >"$accountsconf"
  envsubst <"$aerctemp" >>"$accountsconf"
  sed -i "/# end of account switch binds/i\
    i$idnum = :change-tab $fulladdr<Enter>" "$bindsconf"
  chmod 600 "$accountsconf"
}

getprofiles() {
  safename="$(echo $fulladdr | sed 's/@/_/g')"
  case "$type" in
  online)
    folder="imaps://$login@$imap:$iport"
    extra="$(envsubst <"$onlinetemp")"
    ;;
  pop) prepmpop ;;
  *)
    case "$iport" in
    1143) imapssl=None ;;
    143) imapssl=STARTTLS ;;
    esac
    prepmbsync
    ;;
  esac
  prepmsmtp
  prepaerc
  prepnotmuch
  prepimapnotify
}

parsedomains() {
  serverinfo="$(grep "^${fulladdr#*@}" "$aercshare/domains.csv" 2>/dev/null)"

  [ -z "$serverinfo" ] && serverinfo="$(grep "$(echo "${fulladdr#*@}" | sed "s/\.[^\.]*$/\.\\\*/")" "$aercshare/domains.csv" 2>/dev/null)"

  IFS=, read -r service imapsugg iportsugg smtpsugg sportsugg <<EOF
$serverinfo
EOF
  imap="${imap:-$imapsugg}"
  smtp="${smtp:-$smtpsugg}"
  sport="${sport:-$sportsugg}"
  iport="${iport:-$iportsugg}"
}

delete() {
  if [ -z "${fulladdr+x}" ]; then
    echo "Select the account you would like to delete (by number):"
    list || exit 1
    read -r input
    match="^\s*$input\s\+"
  else
    match="\s\+$fulladdr$"
    getaccounts
  fi

  fulladdr="$(echo "$accounts" | grep "$match" | grep -o "\S*@\S*")"

  [ -z "$fulladdr" ] && echo "$fulladdr is not a valid account name." && return 1

  sed -ibu "/IMAPStore $fulladdr-remote$/,/# End profile/d" "$mbsyncrc" 2>/dev/null
  rm -f "$mbsyncrc"bu
  sed -i "/^\[$fulladdr\]/,/^\[/ {
    /^\[$fulladdr\]/d
    /^\[/b
    d
  }" "$accountsconf" 2>/dev/null
  sed -ibu "/account $fulladdr$/,/^\(\s*$\|account\)/d" "$msmtprc" 2>/dev/null
  rm -f "$msmtprc"bu
  sed -ibu "/account $fulladdr$/,/^\(\s*$\|account\)/d" "$mpoprc" 2>/dev/null
  rm -f "$mpoprc"bu
  gopass rm -f "$passprefix$fulladdr" >/dev/null 2>&1
  [ -n "${purge+x}" ] && safename="$(echo $fulladdr | sed 's/@/_/g')" && rm -rf "${cachedir:?}/${safename:?}" "${maildir:?}/${fulladdr:?}"
}

askinfo() {
  [ -z "$fulladdr" ] && echo "Give the full email address to add:" &&
    read -r fulladdr
  while ! echo "$fulladdr" | grep -qE "^.+@.+\.[A-Za-z]+$"; do
    echo "$fulladdr is not a valid email address. Please retype the address:"
    read -r fulladdr
  done
  folder="$maildir/$fulladdr"
  getaccounts
  echo "$accounts" | grep -q "\s$fulladdr$" 2>/dev/null &&
    { echo "$fulladdr has already been added" && exit 1; }
  { [ -z "$imap" ] || [ -z "$smtp" ]; } && parsedomains
  [ -z "$imap" ] && echo "Give your email server's IMAP address (excluding the port number):" &&
    read -r imap
  [ -z "$smtp" ] && echo "Give your email server's SMTP address (excluding the port number):" &&
    read -r smtp
  case $sport in
  587) tlsline="# tls_starttls" ;;
  esac
  [ -z "$realname" ] && realname="${fulladdr%%@*}"
  [ -z "$passprefix" ] && passprefix=""
  hostname="${fulladdr#*@}"
  login="${login:-$fulladdr}"
  if [ -n "${password+x}" ] && [ ! -f "$PASSWORD_STORE_DIR/$passprefix$fulladdr.gpg" ]; then
    insertpass
  elif [ ! -f "$PASSWORD_STORE_DIR/$passprefix$fulladdr.gpg" ]; then
    getpass
  fi
}

insertpass() {
  printf "%s" "$password" | gopass insert -fe "$passprefix$fulladdr"
}

errorexit() {
  echo "Log-on not successful."
  case "$imap" in
  imap.gmail.com)
    echo "This account with $service is using Google's Gmail servers, which disable all third-party applications without an application-specific password.
Please be sure you are using OAUTH with your Gmail account, or better yet, stop using Gmail."
    ;;
  imap.mail.me.com)
    echo "This account with $service is using Apple's iCloud servers, which disable all non-Apple applications by default.
Please be sure you either enable third-party applications, or create an app-specific password, or best of all, stop using Apple."
    ;;
  esac
  exit 1
}

getpass() { while :; do
  gopass rm -f "$passprefix$fulladdr" >/dev/null 2>&1
  gopass insert -f "$passprefix$fulladdr" && break
done; }

getboxes() {
  if [ -n "${force+x}" ]; then
    mailboxes="$(printf "INBOX\\nDrafts\\nJunk\\nTrash\\nSent\\nArchive")"
  else
    info="$(curl --location-trusted -s -m 5 --user "$login:$(gopass "$passprefix$fulladdr")" --url "${protocol:-imaps}://$imap:${iport:-993}")"
    [ -z "$info" ] && errorexit
    mailboxes="$(echo "$info" | grep -v HasChildren | sed "s/.*\" //;s/\"//g" | tr -d '\r')"
  fi
  [ "$type" = "pop" ] && mailboxes="INBOX"
  idnum=$(sed -n 's/^\[\(.*\)\]/\1/p' "$accountsconf" | wc -l)
  idnum=$((idnum + 1))
  case "$fulladdr" in
  *@gmail.com)
    mailboxes=$(echo "$mailboxes" | sed -e 's#^\[Gmail\]/##' -e 's/^Sent Mail$/Sent/' -e 's/^Spam$/Junk/')
    toappend="mailboxes $(echo "$mailboxes" | sed "s/^/\"=/;s/$/\"/;s/'/\\\'/g" | paste -sd ' ' -)"
    ;;
  *)
    toappend="mailboxes $(echo "$mailboxes" | sed "s/^/\"=/;s/$/\"/;s/'/\\\'/g" | paste -sd ' ' -)"
    ;;
  esac
}

finalize() {
  [ "$type" != "online" ] && echo "$mailboxes" | xargs -I {} mkdir -p "$maildir/$fulladdr/{}/cur" "$maildir/$fulladdr/{}/tmp" "$maildir/$fulladdr/{}/new"
  mkdir -p "$cachedir/$safename/bodies"
  echo "$fulladdr (account #$idnum) added successfully."
  command -V urlview >/dev/null 2>&1 && [ ! -f "$HOME/.urlview" ] && echo "COMMAND \$BROWSER" >"$HOME/.urlview"
  return 0
}

prepnotmuch() {
  [ -z "$NOTMUCH_CONFIG" ] && NOTMUCH_CONFIG="$HOME/.notmuch-config"
  [ -f "$NOTMUCH_CONFIG" ] && return 0
  envsubst <"$notmuchtemp" >"$NOTMUCH_CONFIG"
}

togglecron() {
  cron="$(mktemp)"
  crontab -l >"$cron"
  if grep -q mailsync "$cron"; then
    echo "Removing automatic mailsync..."
    sed -ibu /mailsync/d "$cron"
    rm -f "$cron"bu
  else
    echo "Adding automatic mailsync every ${cronmin:-10} minutes..."
    echo "*/${cronmin:-10} * * * * $prefix/bin/mailsync" >>"$cron"
  fi &&
    crontab "$cron"
  rm -f "$cron"
}

setact() { if [ -n "${action+x}" ] && [ "$action" != "$1" ]; then
  echo "Running $1 with $action..."
  echo "Incompatible options given. Only one action may be specified per run."
  exit 1
else
  action="$1"
fi; }

aewinfo() {
  cat <<EOF
aew: aerc-wizard, auto-configure email accounts for aerc
including downloadable mail with \`isync\`.

Main actions:
  -a your@email.com	Add an email address
  -l			List email addresses configured
  -d			Remove an already added address
  -D your@email.com	Force remove account without confirmation
  -t number		Toggle automatic mailsync every <number> minutes
  -T			Toggle automatic mailsync
  -r			Reorder account numbers

Options allowed with -a:
  -u	Account login name if not full address
  -n	"Real name" to be on the email account
  -i	IMAP/POP server address
  -I	IMAP/POP server port
  -s	SMTP server address
  -S	SMTP server port
  -x	Password for account (recommended to be in double quotes)
  -p	Add for a POP server instead of IMAP.
  -P	Pass Prefix (prefix of the file where password is stored)
  -X	Delete an account's local email too when deleting.
  -o	Configure address, but keep mail online.
  -f	Assume typical English mailboxes without attempting log-on.

NOTE: Once at least one account is added, you can run
\`mbsync -a\` to begin downloading mail.

To change an account's password, run \`gopass edit '$passprefix'your@email.com\`.
EOF
}

reorder() {
  tempfile="$(mktemp -u)"
  tmp_out="$(mktemp -u)"
  trap 'rm -f $tempfile' HUP INT QUIT TERM PWR EXIT
  echo "# Carefully reorder these accounts with the desired numbers in the first column.
# DO NOT reorder rows or rename the accounts in the second column." >"$tempfile"
  sed -n 's/^\[\(.*\)\]/\1/p' "$accountsconf" | nl -w1 -s' ' >>"$tempfile"
  ${EDITOR:-vim} "$tempfile" || exit 1
  awk '
  /^\[/ { exit }
  { print }
  ' "$accountsconf" >"$tmp_out"
  echo >>"$tmp_out"
  sed -e 's/#.*//' -e '/^[[:space:]]*$/d' "$tempfile" >"${tempfile}.clean"
  sort -n "${tempfile}.clean" >"${tempfile}.sorted"
  while read -r idx email; do
    awk -v acct="[$email]" '
    $0 == acct { inblock=1; print; next }
    inblock && /^\[/ { exit }
    inblock { print }
    ' "$accountsconf" >>"$tmp_out"
    echo >>"$tmp_out"
  done <"${tempfile}.sorted"
  # remove duplicate blank lines
  awk 'NF{blank=0} !NF{blank++} blank<2' "$tmp_out" >"$accountsconf"
  rm -f "${tempfile}.clean" "${tempfile}.sorted"

  # reorder account switching binds in binds.conf
  getaccounts
  start='# account switch binds'
  end='# end of account switch binds'

  tmp="$(mktemp)"
  trap 'rm -f "$tmp"' EXIT

  {
    echo "$start"
    i=1
    # iterate over $accounts line by line
    while IFS= read -r line; do
      # strip the nl number + whitespace
      acct="${line##*[[:space:]]}"
      [ -z "$acct" ] && continue
      printf 'i%d = :change-tab %s<Enter>\n' "$i" "$acct"
      i=$((i + 1))
    done <<<"$accounts"
    echo "$end"
  } >"$tmp"

  # replace the old block in the config
  awk -v start="$start" -v end="$end" -v repl="$tmp" '
        $0 == start { system("cat " repl); skip=1; next }
        $0 == end   { skip=0; next }
        !skip
    ' "$bindsconf" >"$bindsconf.new"

  mv "$bindsconf.new" "$bindsconf"
}

while getopts "rfpXlhodTYD:y:i:I:s:S:u:a:n:P:x:m:t:" o; do case "${o}" in
  l) setact list ;;
  r) setact reorder ;;
  d) setact delete ;;
  D)
    setact delete
    fulladdr="$OPTARG"
    ;;
  y)
    setact sync
    fulladdr="$OPTARG"
    ;;
  Y) setact sync ;;
  a)
    setact add
    fulladdr="$OPTARG"
    ;;
  i)
    setact add
    imap="$OPTARG"
    ;;
  I)
    setact add
    iport="$OPTARG"
    ;;
  s)
    setact add
    smtp="$OPTARG"
    ;;
  S)
    setact add
    sport="$OPTARG"
    ;;
  u)
    setact add
    login="$OPTARG"
    ;;
  n)
    setact add
    realname="$OPTARG"
    ;;
  P)
    setact add
    passprefix="$OPTARG"
    ;;
  m)
    setact add
    maxmes="$OPTARG"
    ;;
  o)
    setact add
    type="online"
    ;;
  p)
    setact add
    type="pop"
    protocol="pop3s"
    iport="${iport:-995}"
    ;;
  f)
    setact add
    force=True
    ;;
  x)
    setact add
    password="$OPTARG"
    ;;
  X)
    setact delete
    purge=True
    ;;
  t)
    setact toggle
    cronmin="$OPTARG"
    ;;
  T) setact toggle ;;
  h) setact info ;;
  \?)
    echo "See \`$(basename $0) -h\` for possible options and help."
    exit 1
    ;;
  esac done

[ -z "$action" ] && action="info"

case "$action" in
list) list ;;
add) checkbasics && askinfo && getboxes && getprofiles && finalize ;;
delete) delete ;;
sync)
  echo "\`aew -y\` and \`aew -Y\` are now deprecated and will be removed in a future update. Please switch to using \`mailsync\`."
  mailsync $fulladdr
  ;;
toggle) togglecron ;;
reorder) reorder ;;
info)
  aewinfo
  exit 1
  ;;
esac