diff options
author | Luke Smith <luke@lukesmith.xyz> | 2022-10-07 12:01:13 -0400 |
---|---|---|
committer | Luke Smith <luke@lukesmith.xyz> | 2022-10-07 12:01:13 -0400 |
commit | 3fe9922ec3112c5a1dafdb6af355c08cb8353e36 (patch) | |
tree | 764f032c843f9cc57858edb7e2563e690610f89f /bin | |
parent | 3e9b64379a8d1f3103efb438e5c9bec53f033cd5 (diff) | |
parent | b2bf5535675f7c3e9d9b5eeb084d0546b8b1ae32 (diff) |
Merge branch 'master' of github.com:LukeSmithxyz/mutt-wizard
Diffstat (limited to 'bin')
-rwxr-xr-x | bin/mailsync | 38 | ||||
-rwxr-xr-x | bin/mw | 60 |
2 files changed, 48 insertions, 50 deletions
diff --git a/bin/mailsync b/bin/mailsync index 97cd224..1dc3d47 100755 --- a/bin/mailsync +++ b/bin/mailsync @@ -11,8 +11,6 @@ # issues. It also should at least be compatible with Linux (and maybe BSD) with # Xorg and MacOS as well. -# Run only if user logged in (prevent cron errors) -pgrep -u "${USER:=$LOGNAME}" >/dev/null || { echo "$USER not logged in; sync will not run."; exit ;} # Run only if not already running in other instance pgrep mbsync >/dev/null && { echo "mbsync is already running."; exit ;} @@ -21,7 +19,7 @@ pgrep mbsync >/dev/null && { echo "mbsync is already running."; exit ;} # files for variable assignments. This is ugly, but there are few options that # will work on the maximum number of machines. eval "$(grep -h -- \ - "^\s*\(export \)\?\(MBSYNCRC\|PASSWORD_STORE_DIR\|NOTMUCH_CONFIG\|GNUPGHOME\)=" \ + "^\s*\(export \)\?\(MBSYNCRC\|MPOPRC\|PASSWORD_STORE_DIR\|NOTMUCH_CONFIG\|GNUPGHOME\)=" \ "$HOME/.profile" "$HOME/.bash_profile" "$HOME/.zprofile" "$HOME/.config/zsh/.zprofile" "$HOME/.zshenv" \ "$HOME/.config/zsh/.zshenv" "$HOME/.bashrc" "$HOME/.zshrc" "$HOME/.config/zsh/.zshrc" \ "$HOME/.pam_environment" 2>/dev/null)" @@ -29,6 +27,7 @@ eval "$(grep -h -- \ export GPG_TTY="$(tty)" [ -n "$MBSYNCRC" ] && alias mbsync="mbsync -c $MBSYNCRC" || MBSYNCRC="$HOME/.mbsyncrc" +[ -n "$MPOPRC" ] || MPOPRC="$HOME/.config/mpop/config" lastrun="${XDG_CONFIG_HOME:-$HOME/.config}/mutt/.mailsynclastrun" @@ -42,7 +41,7 @@ case "$(uname)" in *systemd*|*openrc*) export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus ;; esac # remember if a display server is running since `ps` doesn't always contain a display - pgrepoutput="$(pgrep -a X\(org\|wayland\))" + pgrepoutput="$(pgrep -ax X\(\|org\|wayland\))" displays="$(echo "$pgrepoutput" | grep -wo "[0-9]*:[0-9]\+" | sort -u)" notify() { [ -n "$pgrepoutput" ] && for x in ${displays:-0:}; do export DISPLAY=$x @@ -54,14 +53,16 @@ esac # Check account for new mail. Notify if there is new content. syncandnotify() { acc="$(echo "$account" | sed "s/.*\///")" - if [ -z "$opts" ]; then mbsync "$acc"; else mbsync "$opts" "$acc"; fi + if [ "$1" = "pop" ]; then + # Handle POP + mpop "$acc" + else + # Handle IMAP + if [ -z "$opts" ]; then mbsync "$acc"; else mbsync "$opts" "$acc"; fi + fi new=$(find\ - "$HOME/.local/share/mail/$acc/INBOX/new/"\ - "$HOME/.local/share/mail/$acc/Inbox/new/"\ - "$HOME/.local/share/mail/$acc/inbox/new/"\ - "$HOME/.local/share/mail/$acc/INBOX/cur/"\ - "$HOME/.local/share/mail/$acc/Inbox/cur/"\ - "$HOME/.local/share/mail/$acc/inbox/cur/"\ + "$HOME/.local/share/mail/$acc/"[Ii][Nn][Bb][Oo][Xx]/new/\ + "$HOME/.local/share/mail/$acc/"[Ii][Nn][Bb][Oo][Xx]/cur/\ -type f -newer "$lastrun" 2> /dev/null) newcount=$(echo "$new" | sed '/^\s*$/d' | wc -l) case 1 in @@ -70,23 +71,28 @@ syncandnotify() { } # Sync accounts passed as argument or all. -if [ "$#" -eq "0" ]; then - accounts="$(awk '/^Channel/ {print $2}' "$MBSYNCRC")" -else +if [ "$#" -gt "0" ]; then for arg in "$@"; do [ "${arg%${arg#?}}" = '-' ] && opts="${opts:+${opts} }${arg}" && shift 1 done accounts=$* fi +# TODO handle account passed from argument +[ -z "$accounts" ] && accounts="$(awk '/^Channel/ {print $2}' "$MBSYNCRC")" +[ -z "$pop_accounts" ] && pop_accounts="$(awk '/^account/ {print $2}' "$MPOPRC")" # Parallelize multiple accounts for account in $accounts; do - syncandnotify & + syncandnotify "imap" & +done + +for account in $pop_accounts; do + syncandnotify "pop" & done wait -notmuch new 2>/dev/null +notmuch new --quiet #Create a touch file that indicates the time of the last run of mailsync touch "$lastrun" @@ -7,14 +7,13 @@ cachedir="${XDG_CACHE_HOME:-$HOME/.cache}/mutt-wizard" muttrc="${XDG_CONFIG_HOME:-$HOME/.config}/mutt/muttrc" accdir="${XDG_CONFIG_HOME:-$HOME/.config}/mutt/accounts" msmtprc="${XDG_CONFIG_HOME:-$HOME/.config}/msmtp/config" -msmtplog="${XDG_CONFIG_HOME:-$HOME/.config}/msmtp/msmtp.log" +msmtplog="${XDG_CACHE_HOME:-$HOME/.cache}/msmtp/msmtp.log" mbsyncrc="${MBSYNCRC:-$HOME/.mbsyncrc}" mpoprc="${XDG_CONFIG_HOME:-$HOME/.config}/mpop/config" alias mbsync='mbsync -c "$mbsyncrc"' # On Ubuntu/Debian, a link is needed since they use an older version. if command -V apt-get >/dev/null 2>&1; then - ln -s "$msmtprc" "$HOME/.msmtprc" 2>/dev/null master="Master" slave="Slave" fi @@ -38,12 +37,14 @@ getaccounts() { accounts="$(find -L "$accdir" -type f 2>/dev/null | grep -o "\S* list() { getaccounts && [ -n "$accounts" ] && echo "$accounts" || exit 1 ;} -prepmsmtp() { echo "account $fulladdr +prepmsmtp() { mkdir -p "${msmtprc%/*}" "${msmtplog%/*}" + ln -s "$msmtprc" "$HOME/.msmtprc" 2>/dev/null + echo "account $fulladdr host $smtp port ${sport:-465} from $fulladdr user $login -passwordeval \"pass $fulladdr\" +passwordeval \"pass $passprefix$fulladdr\" auth ${auth:-on} tls on tls_trust_file $sslcert @@ -58,7 +59,7 @@ IMAPStore $fulladdr-remote Host $imap Port ${iport:-993} User $login -PassCmd \"pass $fulladdr\" +PassCmd \"pass $passprefix$fulladdr\" AuthMechs LOGIN SSLType ${imapssl:-IMAPS} CertificateFile $sslcert @@ -87,10 +88,11 @@ tls on user $login host $imap delivery maildir ${XDG_DATA_HOME:-$HOME/.local/share}/mail/$fulladdr/${inbox:-INBOX} -passwordeval pass $fulladdr +passwordeval pass $passprefix$fulladdr " >> "$mpoprc" ;} -prepmutt() { echo "# vim: filetype=neomuttrc +prepmutt() { mkdir -p "${muttrc%/*}" "$accdir" + echo "# vim: filetype=neomuttrc # muttrc file for account $fulladdr set realname = \"$realname\" set from = \"$fulladdr\" @@ -119,8 +121,6 @@ macro index \eh \"<pipe-message>$prefix/libexec/gpg-wks-client --receive | msmtp } getprofiles() { \ - mkdir -p "${muttrc%/*}" "$accdir" "${XDG_CONFIG_HOME:-$HOME/.config}/msmtp" - unset msmtp_header msmtp_profile mutt_profile mbsync_profile case "$iport" in 1143) imapssl=None ;; 143) imapssl=STARTTLS ;; @@ -128,7 +128,7 @@ getprofiles() { \ case "$type" in online) folder="imaps://$login@$imap:${iport:-993}" extra="set imap_user = \"$login\" -set imap_pass = \"\`pass $fulladdr\`\" +set imap_pass = \"\`pass $passprefix$fulladdr\`\" set ssl_starttls = yes set ssl_force_tls = yes" ;; @@ -176,7 +176,7 @@ delete() { if [ -z "${fulladdr+x}" ]; then sed -ibu "/\([0-9]-\)\?$fulladdr.muttrc/d" "$muttrc" 2>/dev/null; rm -f "$muttrc"bu 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 - pass rm -f "$fulladdr" >/dev/null 2>&1 + pass rm -f "$passprefix$fulladdr" >/dev/null 2>&1 [ -n "${purge+x}" ] && rm -rf "${maildir:?}/${fulladdr:?}" for file in "$msmtprc" "$mbsyncrc" "$mpoprc"; do @@ -200,6 +200,7 @@ askinfo() { \ read -r smtp [ "$sport" = 587 ] && tlsline="# tls_starttls" [ -z "$realname" ] && realname="${fulladdr%%@*}" + [ -z "$passprefix" ] && passprefix="" hostname="${fulladdr#*@}" login="${login:-$fulladdr}" if [ -n "${password+x}" ]; then @@ -209,22 +210,22 @@ askinfo() { \ fi } -createpass() { echo "$password" > "$PASSWORD_STORE_DIR/$fulladdr" - "$GPG" -qe $(printf -- " -r %s" $(cat "$PASSWORD_STORE_DIR/.gpg-id")) "$PASSWORD_STORE_DIR/$fulladdr" - rm -f "$PASSWORD_STORE_DIR/$fulladdr" ;} +createpass() { echo "$password" > "$PASSWORD_STORE_DIR/$passprefix$fulladdr" + "$GPG" -qe $(printf -- " -r %s" $(cat "$PASSWORD_STORE_DIR/.gpg-id")) "$PASSWORD_STORE_DIR/$passprefix$fulladdr" + rm -f "$PASSWORD_STORE_DIR/$passprefix$fulladdr" ;} -getpass() { while : ; do pass rm -f "$fulladdr" >/dev/null 2>&1 - pass insert -f "$fulladdr" && break; done ;} +getpass() { while : ; do pass rm -f "$passprefix$fulladdr" >/dev/null 2>&1 + pass 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:$(pass "$fulladdr")" --url "${protocol:-imaps}://$imap:${iport:-993}")" + info="$(curl --location-trusted -s -m 5 --user "$login:$(pass "$passprefix$fulladdr")" --url "${protocol:-imaps}://$imap:${iport:-993}")" [ -z "$info" ] && echo "Log-on not successful." && return 1 mailboxes="$(echo "$info" | grep -v HasChildren | sed "s/.*\" //;s/\"//g" | tr -d '\r')" fi [ "$type" = "pop" ] && mailboxes="INBOX" - for x in $(sed -n "/^macro.* i[0-9] / s/\(^macro.* i\| .*\)//gp " "$muttrc" | sort -u; echo 0); do + for x in $(sed -n "/^macro.* i[0-9] / s/\(^macro.* i\| .*\)//gp " "$muttrc" 2>/dev/null | sort -u; echo 0); do idnum=$((idnum + 1)) [ "$idnum" -eq "$x" ] || break done @@ -262,7 +263,7 @@ togglecron() { cron="$(mktemp)" 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" + echo "*/${cronmin:-10} * * * * $prefix/bin/mailsync -q" >> "$cron" fi && crontab "$cron"; rm -f "$cron" ;} @@ -297,8 +298,8 @@ Options allowed with -a: -s SMTP server address -S SMTP server port -x Password for account (recommended to be in double quotes) - -p Install for a Protonmail account. - -P Add for a POP server instead of IMAP. + -P Pass Prefix (prefix of the file where password is stored) + -p Add for a POP server instead of IMAP. -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. @@ -306,7 +307,7 @@ Options allowed with -a: NOTE: Once at least one account is added, you can run \`mbsync -a\` to begin downloading mail. -To change an account's password, run \`pass edit your@email.com\`. +To change an account's password, run \`pass edit '$passprefix'your@email.com\`. EOF } @@ -333,7 +334,7 @@ reorder() { ' "$tempfile" >> "$muttrc" } -while getopts "rfpPXlhodTYD:y:i:I:s:S:u:a:n:x:m:t:" o; do case "${o}" in +while getopts "rfpXlhodTYD:y:i:I:s:S:u:a:n:P:x:m:t:" o; do case "${o}" in l) setact list || exit 1 ;; r) setact reorder || exit 1 ;; d) setact delete || exit 1 ;; @@ -347,24 +348,15 @@ while getopts "rfpPXlhodTYD:y:i:I:s:S:u:a:n:x:m:t:" o; do case "${o}" in S) setact add || exit 1 ; sport="$OPTARG" ;; u) setact add || exit 1 ; login="$OPTARG" ;; n) setact add || exit 1 ; realname="$OPTARG" ;; + P) setact add || exit 1 ; passprefix="$OPTARG" ;; m) setact add || exit 1 ; maxmes="$OPTARG" ;; o) setact add || exit 1 ; type="online" ;; - P) setact add || exit 1 ; type="pop"; protocol="pop3s" ; iport="${iport:-995}" ;; + p) setact add || exit 1 ; type="pop"; protocol="pop3s" ; iport="${iport:-995}" ;; f) setact add || exit 1 ; force=True ;; x) setact add || exit 1 ; password="$OPTARG" ;; X) setact delete || exit 1 ; purge=True ;; t) setact toggle || exit 1 ; cronmin="$OPTARG" ;; T) setact toggle || exit 1 ;; - p) echo "NOTE: Protonmail users must install and configure Protonmail Bridge first for the first sync to work." - protocol="imap" - imap="127.0.0.1" - iport="1143" - smtp="127.0.0.1" - sport="1025" - auth="login" - tlsline="tls_fingerprint $(msmtp --serverinfo --host=$smtp --port=$sport --tls --tls-certcheck=off | sed -n '/SHA256:/ s/^\s*SHA256:\s*// p')" - setact add || exit 1 - ;; *) mwinfo; exit 1 ;; esac done |