| 1 |
#!/bin/sh |
| 2 |
|
| •3 |
prefix="/usr" |
| 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_CONFIG_HOME:-$HOME/.config}/msmtp/msmtp.log" |
| 11 |
mbsyncrc="${MBSYNCRC:-$HOME/.mbsyncrc}" |
| 12 |
mpoprc="${XDG_CONFIG_HOME:-$HOME/.config}/mpop/config" |
| 13 |
alias mbsync='mbsync -c "$mbsyncrc"' |
| 14 |
|
| 15 |
# On Ubuntu/Debian, a link is needed since they use an older version. |
| 16 |
if command -V apt-get >/dev/null 2>&1; then |
| 17 |
ln -s "$msmtprc" "$HOME/.msmtprc" 2>/dev/null |
| 18 |
master="Master" |
| 19 |
slave="Slave" |
| 20 |
fi |
| 21 |
|
| 22 |
for x in "/etc/ssl/certs/ca-certificates.crt" \ |
| 23 |
"/etc/pki/tls/certs/ca-bundle.crt" "/etc/ssl/cert.pem" \ |
| 24 |
"/etc/ssl/ca-bundle.pem" "/etc/pki/tls/cacert.pem" \ |
| 25 |
"/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem" \ |
| 26 |
"/usr/local/share/ca-certificates/"; do |
| 27 |
[ -f "$x" ] && sslcert="$x" && break |
| 28 |
done || { echo "CA Certificate not found. Please install one or link it to /etc/ssl/certs/ca-certificates.crt" && exit 1 ;} |
| 29 |
|
| 30 |
checkbasics() { command -V gpg >/dev/null 2>&1 && GPG="gpg" || GPG="gpg2" |
| 31 |
PASSWORD_STORE_DIR="${PASSWORD_STORE_DIR:-$HOME/.password-store}" |
| 32 |
[ -r "$PASSWORD_STORE_DIR/.gpg-id" ] || { |
| 33 |
echo "First run \`pass init <yourgpgemail>\` to set up a password archive." |
| 34 |
echo "(If you don't already have a GPG key pair, first run \`$GPG --full-generate-key\`.)" |
| 35 |
exit 1 ;} ;} |
| 36 |
|
| 37 |
getaccounts() { accounts="$(find -L "$accdir" -type f 2>/dev/null | grep -o "\S*.muttrc" | sed "s|.*/\([0-9]-\)*||;s/\.muttrc$//" | nl)" ;} |
| 38 |
|
| 39 |
list() { getaccounts && [ -n "$accounts" ] && echo "$accounts" || exit 1 ;} |
| 40 |
|
| 41 |
prepmsmtp() { echo "account $fulladdr |
| 42 |
host $smtp |
| 43 |
port ${sport:-465} |
| 44 |
from $fulladdr |
| 45 |
user $login |
| 46 |
passwordeval \"pass $fulladdr\" |
| 47 |
auth ${auth:-on} |
| 48 |
tls on |
| 49 |
tls_trust_file $sslcert |
| 50 |
logfile $msmtplog |
| 51 |
${tlsline:-tls_starttls off} |
| 52 |
" >> "$msmtprc" |
| 53 |
} |
| 54 |
|
| 55 |
prepmbsync() { mkdir -p "${mbsyncrc%/*}" |
| 56 |
echo " |
| 57 |
IMAPStore $fulladdr-remote |
| 58 |
Host $imap |
| 59 |
Port ${iport:-993} |
| 60 |
User $login |
| 61 |
PassCmd \"pass $fulladdr\" |
| 62 |
AuthMechs LOGIN |
| 63 |
SSLType ${imapssl:-IMAPS} |
| 64 |
CertificateFile $sslcert |
| 65 |
|
| 66 |
MaildirStore $fulladdr-local |
| 67 |
Subfolders Verbatim |
| 68 |
Path ${XDG_DATA_HOME:-$HOME/.local/share}/mail/$fulladdr/ |
| 69 |
Inbox ${XDG_DATA_HOME:-$HOME/.local/share}/mail/$fulladdr/${inbox:-INBOX} |
| 70 |
|
| 71 |
Channel $fulladdr |
| 72 |
Expunge Both |
| 73 |
${master:-Far} :$fulladdr-remote: |
| 74 |
${slave:-Near} :$fulladdr-local: |
| 75 |
Patterns * !\"[Gmail]/All Mail\" |
| 76 |
Create Both |
| 77 |
SyncState * |
| 78 |
MaxMessages ${maxmes:-0} |
| 79 |
ExpireUnread no |
| 80 |
# End profile |
| 81 |
" >> "$mbsyncrc" ;} |
| 82 |
|
| 83 |
prepmpop() { mkdir -p "${mpoprc%/*}" |
| 84 |
echo " |
| 85 |
account $fulladdr |
| 86 |
tls on |
| 87 |
user $login |
| 88 |
host $imap |
| 89 |
delivery maildir ${XDG_DATA_HOME:-$HOME/.local/share}/mail/$fulladdr/${inbox:-INBOX} |
| 90 |
passwordeval pass $fulladdr |
| 91 |
" >> "$mpoprc" ;} |
| 92 |
|
| 93 |
prepmutt() { echo "# vim: filetype=neomuttrc |
| 94 |
# muttrc file for account $fulladdr |
| 95 |
set realname = \"$realname\" |
| 96 |
set from = \"$fulladdr\" |
| 97 |
set sendmail = \"msmtp -a $fulladdr\" |
| 98 |
alias me $realname <$fulladdr> |
| 99 |
set folder = \"$folder\" |
| 100 |
set header_cache = $cachedir/$fulladdr/headers |
| 101 |
set message_cachedir = $cachedir/$fulladdr/bodies |
| 102 |
set mbox_type = Maildir |
| 103 |
set hostname = \"$hostname\" |
| •104 |
'source' $muttshare/switch.muttrc |
| 105 |
set spoolfile = "+INBOX" |
| 106 |
set postponed = "+Drafts" |
| 107 |
set trash = "+Trash" |
| 108 |
set record = "+Sent" |
| 109 |
$extra |
| 110 |
$synccmd |
| 111 |
macro index \eg \"<enter-command>unset wait_key<enter><shell-escape>gpg --list-secret-keys; printf 'Enter email ID of user to publish: '; read eID; printf 'Enter fingerprint of GPG key to publish: '; read eFGPT; $prefix/libexec/gpg-wks-client --create \\\$eFGPT \\\$eID | msmtp --read-envelope-from --read-recipients -a $fulladdr<enter>\" \"publish GPG key to WKS provider\" |
| 112 |
macro index \eh \"<pipe-message>$prefix/libexec/gpg-wks-client --receive | msmtp --read-envelope-from --read-recipients -a $fulladdr<enter>\" \"confirm GPG publication\" |
| 113 |
" > "$accdir/$fulladdr.muttrc" |
| 114 |
|
| 115 |
[ ! -f "$muttrc" ] && echo "# vim: filetype=neomuttrc" > "$muttrc" |
| 116 |
! grep -q "^source.*mutt-wizard.muttrc" "$muttrc" && echo "source $muttshare/mutt-wizard.muttrc" >> "$muttrc" |
| 117 |
! grep "^source.*.muttrc" "$muttrc" | grep -qv "$muttshare/mutt-wizard.muttrc" && echo "source $accdir/$fulladdr.muttrc" >> "$muttrc" |
| 118 |
echo "macro index,pager i$idnum '<sync-mailbox><enter-command>source $accdir/$fulladdr.muttrc<enter><change-folder>!<enter>;<check-stats>' \"switch to $fulladdr\"" >> "$muttrc" |
| 119 |
} |
| 120 |
|
| 121 |
getprofiles() { \ |
| 122 |
mkdir -p "${muttrc%/*}" "$accdir" "${XDG_CONFIG_HOME:-$HOME/.config}/msmtp" |
| 123 |
unset msmtp_header msmtp_profile mutt_profile mbsync_profile |
| 124 |
case "$iport" in |
| 125 |
1143) imapssl=None ;; |
| 126 |
143) imapssl=STARTTLS ;; |
| 127 |
esac |
| 128 |
case "$type" in |
| 129 |
online) folder="imaps://$login@$imap:${iport:-993}" |
| 130 |
extra="set imap_user = \"$login\" |
| 131 |
set imap_pass = \"\`pass $fulladdr\`\" |
| 132 |
set ssl_starttls = yes |
| 133 |
set ssl_force_tls = yes" |
| 134 |
;; |
| 135 |
pop) synccmd="macro index o \"<shell-escape>mpop $fulladdr<enter>\" \"run mpop to get $fulladdr's mail\"" |
| 136 |
folder="$maildir/$fulladdr" |
| 137 |
prepmpop ;; |
| 138 |
*) synccmd="macro index o \"<shell-escape>mw -y $fulladdr<enter>\" \"run mbsync to sync $fulladdr\"" |
| 139 |
folder="$maildir/$fulladdr" |
| 140 |
prepmbsync ;; |
| 141 |
esac |
| 142 |
prepmsmtp |
| 143 |
prepmutt |
| 144 |
prepnotmuch # Create a notmuch config file if not present already. |
| 145 |
} |
| 146 |
|
| 147 |
parsedomains(){ serverinfo="$(grep "^${fulladdr#*@}" "$muttshare/domains.csv" 2>/dev/null)" |
| 148 |
|
| 149 |
[ -z "$serverinfo" ] && serverinfo="$(grep "$(echo "${fulladdr#*@}" | sed "s/\.[^\.]*$/\.\\\*/")" "$muttshare/domains.csv" 2>/dev/null)" |
| 150 |
|
| 151 |
IFS=, read -r service imapsugg iportsugg smtpsugg sportsugg <<EOF |
| 152 |
$serverinfo |
| 153 |
EOF |
| 154 |
imap="${imap:-$imapsugg}" |
| 155 |
smtp="${smtp:-$smtpsugg}" |
| 156 |
sport="${sport:-$sportsugg}" |
| 157 |
iport="${iport:-$iportsugg}" |
| 158 |
} |
| 159 |
|
| 160 |
delete() { if [ -z "${fulladdr+x}" ]; then |
| 161 |
echo "Select the account you would like to delete (by number):" |
| 162 |
list || exit 1 |
| 163 |
read -r input |
| 164 |
match="^\s*$input\s\+" |
| 165 |
else |
| 166 |
match="\s\+$fulladdr$" |
| 167 |
getaccounts |
| 168 |
fi |
| 169 |
|
| 170 |
fulladdr="$(echo "$accounts" | grep "$match" | grep -o "\S*@\S*")" |
| 171 |
|
| 172 |
[ -z "$fulladdr" ] && echo "$fulladdr is not a valid account name." && return 1 |
| 173 |
|
| 174 |
sed -ibu "/IMAPStore $fulladdr-remote$/,/# End profile/d" "$mbsyncrc" 2>/dev/null ; rm -f "$mbsyncrc"bu |
| 175 |
rm -rf "${cachedir:?}/${fulladdr:?}" "$accdir/$fulladdr.muttrc" "$accdir/"[0-9]-"$fulladdr.muttrc" |
| 176 |
sed -ibu "/\([0-9]-\)\?$fulladdr.muttrc/d" "$muttrc" 2>/dev/null; rm -f "$muttrc"bu |
| 177 |
sed -ibu "/account $fulladdr$/,/^\(\s*$\|account\)/d" "$msmtprc" 2>/dev/null; rm -f "$msmtprc"bu |
| 178 |
sed -ibu "/account $fulladdr$/,/^\(\s*$\|account\)/d" "$mpoprc" 2>/dev/null; rm -f "$mpoprc"bu |
| 179 |
pass rm -f "$fulladdr" >/dev/null 2>&1 |
| 180 |
[ -n "${purge+x}" ] && rm -rf "${maildir:?}/${fulladdr:?}" |
| 181 |
|
| 182 |
for file in "$msmtprc" "$mbsyncrc" "$mpoprc"; do |
| 183 |
sed -ibu 'N;/^\n$/D;P;D;' "$file" 2>/dev/null; rm -f "$file"bu |
| 184 |
done |
| 185 |
} |
| 186 |
|
| 187 |
askinfo() { \ |
| 188 |
[ -z "$fulladdr" ] && echo "Give the full email address to add:" && |
| 189 |
read -r fulladdr |
| 190 |
while ! echo "$fulladdr" | grep -qE "^.+@.+\.[A-Za-z]+$"; do |
| 191 |
echo "$fulladdr is not a valid email address. Please retype the address:" |
| 192 |
read -r fulladdr |
| 193 |
done |
| 194 |
getaccounts; echo "$accounts" | grep -q "\s$fulladdr$" 2>/dev/null && |
| 195 |
{ echo "$fulladdr has already been added" && exit 1 ;} |
| 196 |
{ [ -z "$imap" ] || [ -z "$smtp" ] ;} && parsedomains |
| 197 |
[ -z "$imap" ] && echo "Give your email server's IMAP address (excluding the port number):" && |
| 198 |
read -r imap |
| 199 |
[ -z "$smtp" ] && echo "Give your email server's SMTP address (excluding the port number):" && |
| 200 |
read -r smtp |
| 201 |
[ "$sport" = 587 ] && tlsline="# tls_starttls" |
| 202 |
[ -z "$realname" ] && realname="${fulladdr%%@*}" |
| 203 |
hostname="${fulladdr#*@}" |
| 204 |
login="${login:-$fulladdr}" |
| 205 |
if [ -n "${password+x}" ]; then |
| 206 |
createpass |
| 207 |
else |
| 208 |
getpass |
| 209 |
fi |
| 210 |
} |
| 211 |
|
| 212 |
createpass() { echo "$password" > "$PASSWORD_STORE_DIR/$fulladdr" |
| 213 |
"$GPG" -qe $(printf -- " -r %s" $(cat "$PASSWORD_STORE_DIR/.gpg-id")) "$PASSWORD_STORE_DIR/$fulladdr" |
| 214 |
rm -f "$PASSWORD_STORE_DIR/$fulladdr" ;} |
| 215 |
|
| 216 |
getpass() { while : ; do pass rm -f "$fulladdr" >/dev/null 2>&1 |
| 217 |
pass insert -f "$fulladdr" && break; done ;} |
| 218 |
|
| 219 |
getboxes() { if [ -n "${force+x}" ] ; then |
| 220 |
mailboxes="$(printf "INBOX\\nDrafts\\nJunk\\nTrash\\nSent\\nArchive")" |
| 221 |
else |
| 222 |
info="$(curl --location-trusted -s -m 5 --user "$login:$(pass "$fulladdr")" --url "${protocol:-imaps}://$imap:${iport:-993}")" |
| 223 |
[ -z "$info" ] && echo "Log-on not successful." && return 1 |
| 224 |
mailboxes="$(echo "$info" | grep -v HasChildren | sed "s/.*\" //;s/\"//g" | tr -d '\r')" |
| 225 |
fi |
| 226 |
[ "$type" = "pop" ] && mailboxes="INBOX" |
| 227 |
for x in $(sed -n "/^macro.* i[0-9] / s/\(^macro.* i\| .*\)//gp " "$muttrc" | sort -u; echo 0); do |
| 228 |
idnum=$((idnum + 1)) |
| 229 |
[ "$idnum" -eq "$x" ] || break |
| 230 |
done |
| 231 |
toappend="mailboxes $(echo "$mailboxes" | sed "s/^/\"=/;s/$/\"/" | paste -sd ' ' - )" |
| 232 |
} |
| 233 |
|
| 234 |
finalize() { echo "$toappend" >> "$accdir/$fulladdr.muttrc" |
| 235 |
[ "$type" != "online" ] && echo "$mailboxes" | xargs -I {} mkdir -p "$maildir/$fulladdr/{}/cur" "$maildir/$fulladdr/{}/tmp" "$maildir/$fulladdr/{}/new" |
| 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 |
echo "[database] |
| 244 |
path=$maildir |
| 245 |
[user] |
| 246 |
name=$realname |
| 247 |
primary_email=$fulladdr |
| 248 |
[new] |
| 249 |
tags=unread;inbox; |
| 250 |
ignore=.mbsyncstate;.uidvalidity |
| 251 |
[search] |
| 252 |
exclude_tags=deleted;spam; |
| 253 |
[maildir] |
| 254 |
synchronize_flags=true |
| 255 |
[crypto] |
| 256 |
gpg_path=$GPG" > "$NOTMUCH_CONFIG" ;} |
| 257 |
|
| 258 |
togglecron() { cron="$(mktemp)" |
| 259 |
crontab -l > "$cron" |
| •260 |
if grep -q mw-mailsync "$cron"; then |
| 261 |
echo "Removing automatic mw-mailsync..." |
| 262 |
sed -ibu /mw-mailsync/d "$cron"; rm -f "$cron"bu |
| 263 |
else |
| •264 |
echo "Adding automatic mw-mailsync every ${cronmin:-10} minutes..." |
| 265 |
echo "*/${cronmin-10} * * * * $prefix/bin/mw-mailsync" >> "$cron" |
| 266 |
fi && |
| 267 |
crontab "$cron"; rm -f "$cron" ;} |
| 268 |
|
| 269 |
setact() { if [ -n "${action+x}" ] && [ "$action" != "$1" ]; then |
| 270 |
echo "Running $1 with $action..." |
| 271 |
echo "Incompatible options given. Only one action may be specified per run." |
| 272 |
return 1 |
| 273 |
else |
| 274 |
action="$1" |
| 275 |
fi; } |
| 276 |
|
| 277 |
mwinfo() { cat << EOF |
| 278 |
mw: mutt-wizard, auto-configure email accounts for mutt |
| 279 |
including downloadable mail with \`isync\`. |
| 280 |
|
| 281 |
Main actions: |
| 282 |
-a [email protected] Add an email address |
| 283 |
-l List email addresses configured |
| 284 |
-d Remove an already added address |
| 285 |
-D [email protected] Force remove account without confirmation |
| 286 |
-y [email protected] Sync mail for account by name |
| 287 |
-Y Sync mail for all accounts |
| •288 |
-t number Toggle automatic mw-mailsync every <number> minutes |
| 289 |
-T Toggle automatic mw-mailsync |
| 290 |
-r Reorder account numbers |
| 291 |
|
| 292 |
Options allowed with -a: |
| 293 |
-u Account login name if not full address |
| 294 |
-n "Real name" to be on the email account |
| 295 |
-i IMAP/POP server address |
| 296 |
-I IMAP/POP server port |
| 297 |
-s SMTP server address |
| 298 |
-S SMTP server port |
| 299 |
-x Password for account (recommended to be in double quotes) |
| 300 |
-p Install for a Protonmail account. |
| 301 |
-P Add for a POP server instead of IMAP. |
| 302 |
-X Delete an account's local email too when deleting. |
| 303 |
-o Configure address, but keep mail online. |
| 304 |
-f Assume typical English mailboxes without attempting log-on. |
| 305 |
|
| 306 |
NOTE: Once at least one account is added, you can run |
| 307 |
\`mbsync -a\` to begin downloading mail. |
| 308 |
|
| 309 |
To change an account's password, run \`pass edit [email protected]\`. |
| 310 |
EOF |
| 311 |
} |
| 312 |
|
| 313 |
reorder() { |
| 314 |
tempfile="$(mktemp -u)" |
| 315 |
trap 'rm -f $tempfile' HUP INT QUIT TERM PWR EXIT |
| 316 |
echo "# Carefully reorder these accounts with the desired numbers in the first column. |
| 317 |
# DO NOT reorder rows or rename the accounts in the second column." > "$tempfile" |
| 318 |
sed -n " |
| 319 |
/ i[0-9] / s?\(.* i\|'<sync.*/\|\.muttrc.*\)??g p |
| 320 |
" "$muttrc" >> "$tempfile" |
| 321 |
${EDITOR:-vim} "$tempfile" || exit 1 |
| 322 |
sed -i -e 's/#.*//' -e '/^$/d' "$tempfile" |
| 323 |
default="$(sort -n "$tempfile" | head -n 1)" |
| 324 |
default="${default#* }" |
| 325 |
sed -ibu " |
| 326 |
/.* i[0-9] .*.muttrc/d |
| 327 |
/^source.*accounts.*.muttrc/d |
| 328 |
" "$muttrc" 2>/dev/null; rm -f "$muttrc"bu |
| 329 |
awk -v a="$accdir" -v d="$default" ' BEGIN { print "source "a"/"d".muttrc" } |
| 330 |
{ |
| 331 |
print "macro index,pager i"$1" '\''<sync-mailbox><enter-command>source "a"/"$2".muttrc<enter><change-folder>!<enter>;<check-stats>'\'' \"switch to "$2"\"" |
| 332 |
} |
| 333 |
' "$tempfile" >> "$muttrc" |
| 334 |
} |
| 335 |
|
| 336 |
while getopts "rfpPXlhodTYD:y:i:I:s:S:u:a:n:x:m:t:" o; do case "${o}" in |
| 337 |
l) setact list || exit 1 ;; |
| 338 |
r) setact reorder || exit 1 ;; |
| 339 |
d) setact delete || exit 1 ;; |
| 340 |
D) setact delete || exit 1 ; fulladdr="$OPTARG" ;; |
| 341 |
y) setact sync || exit 1 ; fulladdr="$OPTARG" ;; |
| 342 |
Y) setact sync || exit 1 ;; |
| 343 |
a) setact add || exit 1 ; fulladdr="$OPTARG" ;; |
| 344 |
i) setact add || exit 1 ; imap="$OPTARG" ;; |
| 345 |
I) setact add || exit 1 ; iport="$OPTARG" ;; |
| 346 |
s) setact add || exit 1 ; smtp="$OPTARG" ;; |
| 347 |
S) setact add || exit 1 ; sport="$OPTARG" ;; |
| 348 |
u) setact add || exit 1 ; login="$OPTARG" ;; |
| 349 |
n) setact add || exit 1 ; realname="$OPTARG" ;; |
| 350 |
m) setact add || exit 1 ; maxmes="$OPTARG" ;; |
| 351 |
o) setact add || exit 1 ; type="online" ;; |
| 352 |
P) setact add || exit 1 ; type="pop"; protocol="pop3s" ; iport="${iport:-995}" ;; |
| 353 |
f) setact add || exit 1 ; force=True ;; |
| 354 |
x) setact add || exit 1 ; password="$OPTARG" ;; |
| 355 |
X) setact delete || exit 1 ; purge=True ;; |
| 356 |
t) setact toggle || exit 1 ; cronmin="$OPTARG" ;; |
| 357 |
T) setact toggle || exit 1 ;; |
| 358 |
p) echo "NOTE: Protonmail users must install and configure Protonmail Bridge first for the first sync to work." |
| 359 |
protocol="imap" |
| 360 |
imap="127.0.0.1" |
| 361 |
iport="1143" |
| 362 |
smtp="127.0.0.1" |
| 363 |
sport="1025" |
| 364 |
auth="login" |
| 365 |
tlsline="tls_fingerprint $(msmtp --serverinfo --host=$smtp --port=$sport --tls --tls-certcheck=off | sed -n '/SHA256:/ s/^\s*SHA256:\s*// p')" |
| 366 |
setact add || exit 1 |
| 367 |
;; |
| 368 |
*) mwinfo; exit 1 ;; |
| 369 |
esac done |
| 370 |
|
| 371 |
case "$action" in |
| 372 |
list) list ;; |
| 373 |
add) checkbasics && askinfo && getboxes && getprofiles && finalize ;; |
| 374 |
delete) delete ;; |
| •375 |
sync) mw-mailsync $fulladdr ;; |
| 376 |
toggle) togglecron ;; |
| 377 |
reorder) reorder ;; |
| 378 |
*) mwinfo; exit 1 ;; |
| 379 |
esac |