First, the code: http://joshodom.net/misc/omegle_hal.pl
I am running it on perl v5.8.8.
It's about as kludgy as any of my code, as of late. If you notice any obvious errors or inefficiencies, they were probably accidental on my part, since I'm not all that proficient with perl or any of the modules I was using.
The way it works is as follows: there's a main thread and an event-polling thread. I have a separate polling thread because requesting the events page blocks until it has an event. The main thread opens a new chat, and waits for the events thread to get a message (which gets placed in the shared scalar $pending_messages). Once it receives a message, the main thread waits a couple seconds (as if it were reading), generates a response, toggles "typing", "types" for several seconds, and sends it. If the person waited too long to respond initially, then hal disconnects. If the person waited too long to respond after sending a message, hal gives them another chance (by sending another message), and if it times out again, hal disconnects.
Perhaps I should port all of my omegle-specific code to an omegle module. That would make omegle hacking a lot easier...
As for a man-in-the-middle attack, I was actually thinking about doing that, again, for the purpose of logging chats. That's my next project, I think.
Finally, megahal is sort of a confused parrot -- he will respond back with things he had already heard (or mutations thereof). The fact that he turned from an insane encyclopedia into a rude, obscene, horny sexbot says a lot about the internet (or, at least about omegle). The fact that a lot of people seem to consider him human also says a lot about the internet... (or, at least about the turing test).