[WIP] Untangle network code

Don't merge this yet - needs more testing. The basic idea is to separate the network/IMAP/POP and UI / app related concerns.