From 9ec2149726f327766f255c755e1425fb3d94d72e Mon Sep 17 00:00:00 2001 From: Drew Fisher Date: Tue, 18 Mar 2014 20:52:12 -0700 Subject: [PATCH] WIP on making a bitlbee gateway Summary: I'm just testing phabricator Test Plan: run some things some time Reviewers: jl Reviewed By: jl Differential Revision: https://phabricator.brohaus.com/D2 --- imoo/bitlbee.py | 75 +++++++++++++++++++++++++++++++++++++++++++++++++ imoo/gateway.py | 11 ++++++++ 2 files changed, 86 insertions(+) create mode 100644 imoo/bitlbee.py create mode 100644 imoo/gateway.py diff --git a/imoo/bitlbee.py b/imoo/bitlbee.py new file mode 100644 index 0000000..0b165e3 --- /dev/null +++ b/imoo/bitlbee.py @@ -0,0 +1,75 @@ +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() diff --git a/imoo/gateway.py b/imoo/gateway.py new file mode 100644 index 0000000..878af5d --- /dev/null +++ b/imoo/gateway.py @@ -0,0 +1,11 @@ +class Gateway(object): + """ + An abstract class which provides the API to a particular chat gateway. + + No real work has been done yet. + """ + def __init__(self): + pass + + def connect_to_backend(self, userid, credential): + raise NotImplementedError("Gateway methods must be implemented by a child class") -- 2.39.5