--- /dev/null
+import gateway, ircclient
+
+class BitlbeeIRCClient(ircclient.IRCClient):
+ """
+ An IRC client that makes some deep assumptions about the things that the server will send back.
+
+ Implemented as a state machine for now.
+ """
+ def __init__(self, host="127.0.0.1", port=6667, tracing=False):
+ super(BitlbeeIRCClient, self).__init__(host, port, tracing)
+ self.state = "DISCONNECTED"
+
+ def transition_state_to(self, new_state):
+ # Add hooks here if you want things to happen on all state transitions arriving at a state
+
+ # Actually transition states.
+ self.state = new_state
+
+ def handle_command(self, prefix, command, params):
+ # Answer pings.
+ if command == b"PING":
+ self.send_command(b"PONG", *params)
+
+ # state machine to wait until we're in the READY state to send messages
+ handler = getattr(self, "handler_{}".format(self.state), None)
+ if handler is not None:
+ handler(prefix, command, params)
+ else:
+ raise NotImplementedError("HEY! you don't seem to have a handler for the {} state".format(self.state))
+
+ def handler_DISCONNECTED(self, prefix, command, params):
+ if command == '001':
+ self.trace("I was welcomed")
+ self.transition_state_to("JOINING")
+
+ def handler_JOINING(self, prefix, command, params):
+ if command == b'JOIN' and "&bitlbee" in params:
+ self.trace("I was joined")
+ self.transition_state_to("DRAINING")
+
+ def handler_DRAINING(self, prefix, command, params):
+ if command == b'PRIVMSG' and "If you already have an account on this server, just use the \x02identify\x02 command to identify yourself." in params[-1]:
+ self.trace("Ready!")
+ self.send_command(b'PRIVMSG', "&bitlbee", "help account")
+ self.transition_state_to("READY")
+
+ def handler_READY(self, prefix, command, params):
+ pass
+
+ def trace(self, message):
+ if self.tracing:
+ print message
+
+class BitlbeeGateway(gateway.Gateway):
+ """
+ A gateway class that will use a connection to bitlbee to connect to the
+ backend chat network.
+
+ Note that this must still be specialized per chat network that bitlbee supports.
+ """
+ def __init__(self, host="127.0.0.1", port="6667"):
+ self._ircclient = IRCClient(host, port)
+ self._ircclient.connect()
+
+ def _send_root_message(self, text):
+ self._ircclient.send_command("PRIVMSG", "&bitlbee", text)
+
+ def _send_chat_message(self, buddy_id, text):
+ pass
+
+if __name__ == "__main__":
+ c = BitlbeeIRCClient(tracing=True)
+ c.connect()
+ import tornado.ioloop
+ tornado.ioloop.IOLoop.instance().start()