This should help understanding what it's trying to do.
1. this should work better with modules such as imapauth 2. it fixes a null pointer dereference when the username wasn't found This module won't work as is yet, and has some other obvious issues with this approach, but was a good starting point.