Thanks to both Daniel Wagner and Brandon Allbery for their
suggestions.
I've ended up using Daniel's one because it seemed more similar to what I
was thinking. I only changed the order of "yes\nno\n", to make the default
be "no" since I very, very rarely exit xmonad (on purpose).
Daniel's solution has the potential for problems, because xmonad's main loop isn't processing events while it's running. (This is a common problem with most of the contrib functions that process input; they really need to do their input handling in the handleEventHook instead of opening a new connection to the X server. For those who keep track of such things, the possible failure mode is the same as with the infamous putSelection.)