General refactoring and initial work on handling custom URLs on OSX

It turns out that the OSX architecture for notifying programs of a
custom URL click is somehow fundamentally incompatible with our program.
I don't understand why, but the events just don't come as soon as we
start the client.

Even so, the initial work is done here. I've also done a bunch of
refactoring in main.cpp to clean up the actual main function.