6

Q: how do I prevent mu4e from automatically checking mail when the internet's down?

Problem

I use mu4e and offlineimap to handle my email. When on a laptop, I have to deal with the fact that I sometimes have spotty wifi connections. offlineimap freaks out when it gets called without an internet connection.

Problem as it relates to mu4e

I have mu4e invoke offlineimap automatically every 5 minutes or so:

(setq mu4e-get-mail-command "offlineimap -o"
      mu4e-update-interval  300)

Unfortunately, that means it doesn't check if I've actually got an internet connection.

So...

How do I tell mu4e not to invoke offlineimap if there's no internet connection?

AnFi
  • 173
  • 1
  • 11
Dan
  • 32,584
  • 6
  • 98
  • 168

2 Answers2

6

Use wrapper shell script.

Below please find trivial sample script checking connection status as reported by Unix/Linux NetworkManager.

#!/bin/sh
# exit if there is no internet connection
/usr/bin/nm-online || exit 

/usr/bin/offlineimap -o

Emacs fix (use wrapper instead of directly using offlineimap):

(setq mu4e-get-mail-command "/home/me/bin/offlineimap-wrapper.sh"
      mu4e-update-interval  300)
AnFi
  • 173
  • 1
  • 11
  • Thanks! I'm on Linux so this works for me, but I'm going to leave the post open for a little longer to see if there are any OS-agnostic ways to do it. – Dan Oct 03 '18 at 01:28
5

(Posting this Q&A combo since it took me a while to figure out, and it might save someone else the trouble.)

Based on this answer to a question on how to test for an internet connection within elisp, we can call a shell command:

(defun internet-up-p (&optional host)
  (= 0 (call-process "ping" nil nil nil "-c" "1" "-W" "1" 
                     (if host host "www.google.com"))))

We can then add advice to the relevant mu4e function to tell it to do its thing only if there's a connection:

(advice-add #'mu4e-update-mail-and-index
            :before-while
            (lambda (&rest args)
              (internet-up-p)))
Dan
  • 32,584
  • 6
  • 98
  • 168