From 83029cfee09ce758b3c775d8ba4fb002f0f83aa4 Mon Sep 17 00:00:00 2001 From: Luke Smith Date: Thu, 11 Apr 2019 13:39:47 -0400 Subject: [PATCH] begin switch to mbsync --- README.md | 20 ++++++------- bin/mailsync | 8 +++--- mw | 79 +++++++++++++++++++++------------------------------- 3 files changed, 46 insertions(+), 61 deletions(-) diff --git a/README.md b/README.md index 3e8e77d..cff0241 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ The mutt-wizard is a simple interface gives you: - a fully-functioning neomutt-based terminal email workflow with vim bindings, vibrant display. -- email backed up offline with offlineIMAP, allowing mobile and offline access of all your mail. +- email backed up offline with isync/mbsync, allowing mobile and offline access of all your mail. - automatically generated shortcuts for jumping between different accounts or mailboxes within an account. - sensible binds that make managing, moving and reading mail quicker and easier than ever. - your passwords safely encrypted on your machine so you (and only you) can access your account easily (uses GPG). @@ -12,12 +12,12 @@ I maintain mutt-wizard for GNU/Linux, but individual contributors have also made ## Use mutt-wizard -Clone the repo to `~/.config/mutt`. Either move to backup or delete your old `~/.config/mutt` directory if you have one. Remove/backup your old offlineimap and msmtp configs if you have them as well. +Clone the repo to `~/.config/mutt`. Either move to backup or delete your old `~/.config/mutt` directory if you have one. Remove/backup your old isync/mbsync and msmtp configs if you have them as well. Be sure to install the following programs: - `neomutt` - the email client. If you delete a lot of the neomutt-specific settings in the muttrc, you can run vanilla mutt if you want. -- `offlineimap` - downloads the email. Required for installation. +- `isync` - for the `mbsync` command. Downloads the email. Required for installation. - `msmtp` - sends your email. **You must have a GPG public/private key pair as well.** This is what will safely encrypt your passwords. Run `gpg --full-gen-key` (or `gpg2 --full-gen-key`) to get one. @@ -26,9 +26,9 @@ Be sure to install the following programs: Once the repository is in place in `~/.config/mutt/`: -- Select to add an account. This will autogenerate your config files for mutt, offlineimap and msmtp. -- Run `offlineimap` once (at least a partial sync). This will create all the directories you need to finalize installation and will start downloading your mail. -- Select the option to autodetect mailboxes. This will use the directories offlineimap creates to detect your mailboxes and make your mail readable by mutt. +- Select to add an account. This will autogenerate your config files for mutt, mbsync and msmtp. +- Run `mbsync -a` once (at least a partial sync). This will create all the directories you need to finalize installation and will start downloading your mail. +- Select the option to autodetect mailboxes. This will use the directories mbsync creates to detect your mailboxes and make your mail readable by mutt. ### Optional dependencies @@ -46,16 +46,16 @@ mutt-wizard is configured by default for you to use other useful tools, but thes - More elegant attachment handling. Image/video/pdf attachments without relying on the neomutt instance. - abook integration by default. - The messy template files have been removed and are now a part of the script itself. -- Optimal XDG standards compliance, moving offlineimap and msmtp configs to `~/.config/` and moving mail to `~/.local/share/mail/`. offlineimap still uses home for its cache though. +- Optimal XDG standards compliance, moving msmtp configs to `~/.config/`, moving mail to `~/.local/share/mail/` and moving mutt-wizard files to `~/.local/share/muttwizard/`. isync/mbsync still uses home for default though as XDG compliance is not built into them. - `accounts/` hold account data and `bin/` holds script run by or for mutt. All other directories have been disintegrated. - Better handling of different gpg versions. - Script is POSIX sh compliance. ## Known issues (not my fault!) -- If you're using a Gmail account, check the pinned issue on the Github. Gmail accounts haven't been working properly with offlineimap recently, but there's an easy fix. Remember also to enable third-party ("""less secure""") applications. -- Check the ProtonMail issue as well. ProtonMail recently allows IMAP usage with their Bridge program for paid users. I don't have this, so I can't bugtest on it, but many users have gotten it working. Either way, it requires a little more work than just using the wizard. -- Don't expect mutt-wizard to work out the box on a university email. Universities often have special IMAP policies and server settings. You might be lucky, but you might have to changes some settings in the offlineimap config file to get it to work properly with a university email. +- For Gmail accounts, remember also to enable third-party ("""less secure""") applications before attempting installation. +- Check the ProtonMail issue on Github if you are a Protonmail user. ProtonMail recently allows IMAP usage with their Bridge program for paid users. I don't have this, so I can't bugtest on it, but many users have gotten it working. Either way, it requires a little more work than just using the wizard. +- Don't expect mutt-wizard to work out the box on a university email. Universities often have special IMAP policies and server settings. You might be lucky, but you might have to changes some settings in the mbsyncrc config file to get it to work properly with a university email. - If you use an email server whose mailboxes are not in English, mutt-wizard might not be able to guess which is which, so you may have to manually set your Inbox, Sent, Trash, Drafts, etc. in your mutt config file. ## Help! diff --git a/bin/mailsync b/bin/mailsync index 24b5e10..8d3052a 100755 --- a/bin/mailsync +++ b/bin/mailsync @@ -1,5 +1,5 @@ #!/usr/bin/env sh -# This script will run offlineimap and check +# This script will run mbsync and check # for new email if there is an internet connection. # # If it detects new mail, it uses mpv to play a @@ -22,8 +22,8 @@ fi echo " 🔃" > /tmp/imapsyncicon pkill -RTMIN+12 i3blocks -# Run offlineimap. You can feed this script different settings. -offlineimap -o "$@" +# Run mbsync. You can feed this script different settings. +mbsync -a "$@" rm -f /tmp/imapsyncicon pkill -RTMIN+12 i3blocks @@ -34,7 +34,7 @@ do newcount=$(find "$HOME/.local/share/mail/$acc/INBOX/new/" -type f -newer "$HOME/.config/mutt/bin/.mailsynclastrun" 2> /dev/null | wc -l) [ "$newcount" -gt "0" ] && notify "$acc" "$newcount" & done -notmuch new +notmuch new 2>/dev/null #Create a touch file that indicates the time of the last run of mailsync touch "$HOME/.config/mutt/bin/.mailsynclastrun" diff --git a/mw b/mw index 713701b..8b07184 100755 --- a/mw +++ b/mw @@ -1,5 +1,4 @@ #!/bin/sh - muttdir="$HOME/.config/mutt" # Main mutt config location accdir="$muttdir/accounts" # Directory for account settings maildir="$HOME/.local/share/mail" # Location of mail storage @@ -7,6 +6,7 @@ creddir="$HOME/.local/share/muttwizard" # Location of encrypted credentials bindir="$HOME/.config/mutt/bin" # Location of scripts run by mutt or the wizard namere="^[a-z_][a-z0-9_-]*$" # Regex to ensure viable username emailre=".\+@.\+\\..\+" # Regex to confirm valid email address +mbsyncrc="$HOME/.mbsyncrc" [ ! -f "$muttdir/domains.csv" ] || [ ! -d "$bindir" ] && printf "Read the README. Be sure to put the repo in the right place before running.\\n" && exit 1 gpgemail="$(grep "$emailre" "$creddir/gpgemail")" # Get previously set gpg email address tmpdir="$(mktemp -d)" @@ -19,7 +19,7 @@ case "$(uname)" in esac getprofiles() { \ - unset msmtp_header msmtp_profile mutt_profile offlineimap_header offlineimap_profile + unset msmtp_header msmtp_profile mutt_profile mbsync_profile printf "Creating profiles for \`%s\`..." "$title" msmtp_header="defaults auth on @@ -63,32 +63,29 @@ bind index gg first-entry unmailboxes * set imap_check_subscribed " -offlineimap_header="[general] -accounts = -starttls = yes -ssl = true -pythonfile = $bindir/imappwd.py +mbsync_profile="IMAPAccount $title +Host $imap +Port $iport +PassCmd \"gpg -q --for-your-eyes-only --no-tty -d ~/.local/share/muttwizard/$title.gpg\" +SSLType IMAPS +CertificateFile /etc/ssl/certs/ca-certificates.crt -" -offlineimap_profile=" -[Account $title] -localrepository = $title-local -remoterepository = $title-remote +IMAPStore $title-remote +Account $title -[Repository $title-remote] -auth_mechanisms = LOGIN -type = $type -remoteuser = $login -remotepasseval = mailpasswd(\"$title\") -remoteport = $iport -sslcacertfile = $sslcert -$ifgoogleline +MaildirStore $title-local +Subfolder Verbatim +Path ~/.local/share/mail/$title/ +Inbox ~/.local/share/mail/$title/INBOX -[Repository $title-local] -type = Maildir -localfolders = $maildir/$title -" +Channel $title +Master $title-remote +Slave $title-local +Patterns * !\"[Gmail]/All Mail\" +Create Both +SyncState * +# End profile" else mutt_profile="# vim: filetype=neomuttrc # muttrc file for account $title @@ -120,7 +117,7 @@ fi } addaccount() { \ - printf "Insert the \033[31memail address\033[0m that you want to autoconfigure for mutt/offlineIMAP\\n\\nEmail: " + printf "Insert the \033[31memail address\033[0m that you want to autoconfigure for mutt/mbsync\\n\\nEmail: " printf "\033[36m\t" read -r fulladdr printf "\033[0m" @@ -190,15 +187,9 @@ ssl_version = tls1_2" [ ! -f "$HOME/.config/msmtp/config" ] && echo "$msmtp_header" > "$HOME/.config/msmtp/config" echo "$msmtp_profile" >> "$HOME/.config/msmtp/config" echo "$mutt_profile" > "$accdir/$title.muttrc" - if [ "$accounttype" = "offline" ]; then - mkdir -p "$HOME/.config/offlineimap/" - [ ! -f "$HOME/.config/offlineimap/config" ] && echo "$offlineimap_header" > "$HOME/.config/offlineimap/config" - echo "$offlineimap_profile" >> "$HOME/.config/offlineimap/config" - sed -i "s/^accounts =.*[a-zA-Z]$/&, $title/g;s/^accounts =\\s*$/accounts = $title/g" "$HOME/.config/offlineimap/config" - fi echo "macro index,pager i$idnum 'source $accdir/$title.muttrc!;'" >> "$muttdir/personal.muttrc" ! grep "^source.*.muttrc" "$muttdir/personal.muttrc" >/dev/null && echo "source $accdir/$title.muttrc" >> "$muttdir/personal.muttrc" - trysync && finalize + [ ! "$accounttype" = "offline" ] && trysync && finalize } getpass() { \ @@ -244,22 +235,22 @@ formatShortcut() { \ done ;} trysync() { \ - ! ping -q -c 1 1.1.1.1 > /dev/null && printf "No internet connection detected.\\nTry rerunning offlineimap manually when connection is established, then select the option to detect mailboxes and finalize installation.\\n" && return 1 - printf "\033[32mYou must have an internet connection to continue.\033[0m\\nmutt-wizard will run offlineimap briefly to (1) ensure that login details are functional and (2) allow offlineimap to tell us what mailboxes your email account has.\\nAfter around 15 seconds, mutt-wizard will kill the process and continue.\\nYou can run offlineimap manually to finish the mail sync later.\\n" + ! ping -q -c 1 1.1.1.1 > /dev/null && printf "No internet connection detected.\\nTry rerunning \`mbsync $title\` manually when connection is established, then select the option to detect mailboxes and finalize installation.\\n" && return 1 + printf "\033[32mYou must have an internet connection to continue.\033[0m\\nmutt-wizard will run mbsync briefly to (1) ensure that login details are functional and (2) allow mbsync to tell us what mailboxes your email account has.\\nAfter around 15 seconds, mutt-wizard will kill the process and continue.\\nYou can run mbsync manually to finish the mail sync later.\\n" cont - (sleep 15; killall offlineimap; killall offlineimap; killall offlineimap)>/dev/null 2>&1 & + (sleep 15; killall mbsync)>/dev/null 2>&1 & mkdir -p "$maildir" - offlineimap -qoa "$title" + mbsync "$title" if ls -d "$maildir/$title/"* >/dev/null 2>&1; then printf "\033[32mSync successful.\033[0m\\n"; return else - printf "\033[31m\033[31mSync not successful.\033[0m Try running offlineimap manually after double-checking your password and server settings.\\nThen select to finalize the account.\\n"; return 1 + printf "\033[31m\033[31mSync not successful.\033[0m Try running \`mbsync $title\` manually after double-checking your password and server settings.\\nThen select to finalize the account.\\n"; return 1 fi } finalize() { \ boxes="$(du -a "$maildir/$title/"* -d 0 | sed "s/^.*\//=/")" - [ -z "$boxes" ] && printf "\033[31mNo local mailboxes have been detected for %s.\033[0m\\nThis means that offlineimap has not been successfully run.\\nRun offlineimap, and if it has an error, be sure to check your password and server settings manually if needbe.\\n" "$title" && return + [ -z "$boxes" ] && printf "\033[31mNo local mailboxes have been detected for %s.\033[0m\\nThis means that mbsync has not been successfully run.\\nRun mbsync, and if it has an error, be sure to check your password and server settings manually if needbe.\\n" "$title" && return echo "$boxes" > "$tmpdir/title_boxes" printf "Setting up the mutt sidebar...\\n" sidebar_width="$(sed -n -e '/^set sidebar_width/p' "$muttdir/muttrc" | awk -F'=' '{print $2}')" @@ -289,7 +280,7 @@ finalize() { \ wipe () { \ printf "Are you \033[31;1mreally\033[0m sure you want to delete all email accounts?\\n" && read -r input && ! echo "$input" | grep -i "y\(es\)*" >/dev/null && printf "That doesn't seem like a yes to me.\\n\\n" && return 1 printf "Are you really, really sure?" && read -r input && ! echo "$input" | grep -i "y\(es\)*" >/dev/null && printf "That doesn't seem like a yes to me.\\n\\n" && return 1 - #rm -rf "$HOME/.config/offlineimap/config" "$accdir" "$creddir" "$muttdir/personal.muttrc" + #rm -rf "$mbsyncrc" "$accdir" "$creddir" "$muttdir/personal.muttrc" echo deleted } @@ -307,13 +298,7 @@ pick() { \ [ -n "$2" ] && printf "Are you sure you want to %s the \`%s\` account?\\n" "$1" "$title" && read -r input && echo "$input" | grep -i "y\(es\)*" >/dev/null } -delete() { sed -i " - /Account $title]/,/Account/{//!d} - /Account $title]/d - s/ $title\(,\|$\)//g - s/=$title\(,\|$\)/=/g - s/,$//g - " "$HOME/.config/offlineimap/config" +delete() { sed -i "/IMAPAccount $title/,/# End profile/d" "$mbsyncrc" rm -rf "${accdir:?}/${title:?}" "$creddir/$title.gpg" "$accdir/$title.muttrc" sed -i "/$title.muttrc/d" "$muttdir/personal.muttrc" # Delete from the line matching the account name, until the next account or empty line @@ -351,7 +336,7 @@ cont() { [ -n "$1" ] && printf "%s\\n" "$1" addtype() { \ while : ; do - echo "Do you want to keep your mail for this account offline with offlineimap? [yes/no]" + echo "Do you want to keep your mail for this account offline with mbsync? [yes/no]" read -r offnot case "$offnot" in [Yy]es) accounttype="offline" && break ;;