Skip to content

Commit

Permalink
Merge branch 'master' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeSmithxyz authored Jan 7, 2021
2 parents 44c70ed + 8b9f8c1 commit 4742f9d
Show file tree
Hide file tree
Showing 7 changed files with 235 additions and 91 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ install:

uninstall:
for script in bin/*; do \
rm -f $(DESTDIR)$(PREFIX)/bin/$$script; \
rm -f $(DESTDIR)$(PREFIX)/$$script; \
done
rm -rf $(DESTDIR)$(PREFIX)/share/mutt-wizard
rm -f $(DESTDIR)$(MANPREFIX)/man1/mw.1
Expand Down
27 changes: 21 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Get this great stuff without effort:
* view and write emails while you're away from
the internet
* make backups
- Provides a `mailsync` script that can be scheduled to run as often as you
like, which downloads/syncs mail and notifies you when new mail has arrived.

Specifically, this wizard:

Expand All @@ -27,9 +29,10 @@ Specifically, this wizard:
#### Dependencies

- `neomutt` - the email client.
- `isync` - downloads and syncs the mail. (required at install)
- `curl` - tests connections (required at install).
- `isync` - downloads and syncs the mail (required if storing IMAP mail locally).
- `msmtp` - sends the email.
- `pass` - safely encrypts passwords (required at install)
- `pass` - safely encrypts passwords (required at install).

**Note**: There's a chance of errors if you use a slow-release distro like Ubuntu, Debian,
or Mint. If you get errors in `neomutt`, install the most recent version
Expand All @@ -45,6 +48,16 @@ sudo make install
A user of Arch-based distros can also install mutt-wizard from the AUR as
[mutt-wizard-git](https://aur.archlinux.org/packages/mutt-wizard-git/).

### Optional Dependencies

- `pam-gnupg` - Automatically logs you into your GPG key on login so you will never need to input your password once logged on to your system. Check the repo and directions out [here](https://github.com/cruegge/pam-gnupg).
- `lynx` - view HTML email in neomutt.
- `notmuch` - index and search mail. Install it and run `notmuch setup`, tell it that your mail is in `~/.local/share/mail/` (although `mw` will do this automatically if you haven't set notmuch up before). You can run it in mutt with `ctrl-f`. Run `notmuch new` to process new mail.
- `abook` - a terminal-based address book. Pressing tab while typing an address to send mail to will suggest contacts that are in your abook.
- `urlview` - outputs urls in mail to browser.
- `cronie` - (or any other major cronjob manager) to set up automatic mail syncing.


## Usage

The mutt-wizard runs via the command `mw`. Once setup is complete, you'll use
Expand All @@ -56,6 +69,8 @@ The mutt-wizard runs via the command `mw`. Once setup is complete, you'll use
- `mw -Y` -- sync all configured email accounts
- `mw -d` -- choose an account to delete
- `mw -D [email protected]` -- delete account settings without confirmation
- `mw -t 30` -- toggle automatic mailsync to every 30 minutes
- `mw -T` -- toggle mailsync without specifying minutes (default is 10)
- `pass edit [email protected]` -- revise an account's password

### Options usable when adding an account
Expand All @@ -64,7 +79,7 @@ The mutt-wizard runs via the command `mw`. Once setup is complete, you'll use

- `-u` -- Give an account username if different from the email address.
- `-n` -- A real name to be used by the account. Put in quotations if multiple
words
words.
- `-i` -- IMAP server address
- `-I` -- IMAP server port (otherwise assumed to be 993)
- `-s` -- SMTP server address
Expand Down Expand Up @@ -126,8 +141,7 @@ To give you an example of the interface, here's an idea:
steps to install the system.
- `mw` is now an installed program instead of just a script needed to be kept in
your mutt folder.
- `dialog` is no longer used (i.e. bloat) and the interface is simply text
commands.
- `dialog` is no longer used and the interface is simply text commands.
- More autogenerated shortcuts that allow quickly moving and copying mail
between boxes.
- More elegant attachment handling. Image/video/pdf attachments without relying
Expand All @@ -140,11 +154,12 @@ To give you an example of the interface, here's an idea:
- `pass` is used as a password manager instead of separately saving passwords.
- Script is POSIX sh compliant.
- Error handling for the many people who don't read or follow directions. Fewer
errors, generally.
errors generally.
- Addition of a manual `man mw`

## Help the Project!


- Try mutt-wizard out on weird machines and weird email addresses and report any
errors.
- Open a PR to add new server information into `domains.csv` so their users can
Expand Down
94 changes: 94 additions & 0 deletions bin/mailsync
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#!/bin/sh

# - Syncs mail for all accounts, or a single account given as an argument.
# - Displays a notification showing the number of new mails.
# - Displays a notification for each new mail with its subject displayed.
# - Runs notmuch to index new mail.
# - This script can be set up as a cron job for automated mail syncing.

# There are many arbitrary and ugly features in this script because it is
# inherently difficult to pass environmental variables to cronjobs and other
# 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 -x mbsync >/dev/null && { echo "mbsync is already running." ; exit ;}

# First, we have to get the right variables for the mbsync file, the pass
# archive, notmuch and the GPG home. This is done by searching common profile
# 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\)=" \
"$HOME/.profile" "$HOME/.bash_profile" "$HOME/.zprofile" "$HOME/.bashrc" "$HOME/.zshrc" "$HOME/.pam_environment" 2>/dev/null)"
# One alternative to this kind of command would be marking the script for
# /bin/sh -l. That might cause other problems on other particular setups that
# do more complicated things on login, or those people who assign environmental
# variables in shell rc files.

case "$(readlink -f /sbin/init)" in
*systemd*) export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus ;;
esac
export GPG_TTY=$TTY

[ -n "$MBSYNCRC" ] && alias mbsync="mbsync -c $MBSYNCRC" || MBSYNCRC="$HOME/.mbsyncrc"

# Settings are different for MacOS (Darwin) systems.
case "$(uname)" in
Darwin)
notify() { osascript -e "display notification \"$2 in $1\" with title \"You've got Mail\" subtitle \"Account: $account\"" && sleep 2 ;}
messageinfo() { osascript -e "display notification with title \"📧 $from\" subtitle \"$subject\"" ;}
;;
*)
displays="$(ps axo user,cmd | grep "^$USER\s\+.*Xorg" | grep -wo "[0-9]*:[0-9]\+")"
notify() { for x in $displays; do
export DISPLAY=$x
notify-send --app-name="mutt-wizard" "mutt-wizard" "📬 $2 new mail(s) in \`$1\` account."
done ;}
messageinfo() { for x in $displays; do
export DISPLAY=$x
notify-send --app-name="mutt-wizard" "📧$from:" "$subject"
done ;}
;;
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
new=$(find "$HOME/.local/share/mail/$acc/INBOX/new/" "$HOME/.local/share/mail/$acc/Inbox/new/" "$HOME/.local/share/mail/$acc/inbox/new/" -type f -newer "$HOME/.config/mutt/.mailsynclastrun" 2> /dev/null)
newcount=$(echo "$new" | sed '/^\s*$/d' | wc -l)
if [ "$newcount" -gt "0" ]; then
notify "$acc" "$newcount" &
for file in $new; do
# Extract subject and sender from mail.
from=$(awk '/^From: / && ++n ==1,/^\<.*\>:/' "$file" | perl -CS -MEncode -ne 'print decode("MIME-Header", $_)' | awk '{ $1=""; if (NF>=3)$NF=""; print $0 }' | sed 's/^[[:blank:]]*[\"'\''\<]*//;s/[\"'\''\>]*[[:blank:]]*$//')
subject=$(awk '/^Subject: / && ++n == 1,/^\<.*\>: / && ++i == 2' "$file" | head -n 1 | perl -CS -MEncode -ne 'print decode("MIME-Header", $_)' | sed 's/^Subject: //' | sed 's/^{[[:blank:]]*[\"'\''\<]*//;s/[\"'\''\>]*[[:blank:]]*$//' | tr -d '\n')
messageinfo &
done
fi
}

# Sync accounts passed as argument or all.
if [ "$#" -eq "0" ]; then
accounts="$(awk '/^Channel/ {print $2}' "$MBSYNCRC")"
else
for arg in "$@"; do
[ "${arg%${arg#?}}" = '-' ] && opts="${opts:+${opts} }${arg}" && shift 1
done
accounts=$*
fi

# Parallelize multiple accounts
for account in $accounts; do
syncandnotify &
done

wait

notmuch new 2>/dev/null

#Create a touch file that indicates the time of the last run of mailsync
touch "$HOME/.config/mutt/.mailsynclastrun"
Loading

0 comments on commit 4742f9d

Please sign in to comment.