[f3af614] | 1 | #!/usr/bin/python |
---|
| 2 | |
---|
| 3 | import sys |
---|
| 4 | import bjsonrpc |
---|
| 5 | from bjsonrpc.handlers import BaseHandler |
---|
| 6 | |
---|
| 7 | import random |
---|
| 8 | import re |
---|
| 9 | import socket |
---|
| 10 | import time |
---|
| 11 | |
---|
| 12 | # List of functions an IM plugin can export. This library will indicate to |
---|
| 13 | # BitlBee which functions are actually implemented so omitted features |
---|
| 14 | # will be disabled, but note that some/many functions are simply mandatory. |
---|
| 15 | SUPPORTED_FUNCTIONS = [ |
---|
| 16 | 'login', 'keepalive', 'logout', 'buddy_msg', 'set_away', |
---|
| 17 | 'send_typing', 'add_buddy', 'remove_buddy', 'add_permit', |
---|
| 18 | 'add_deny', 'rem_permit', 'rem_deny', 'get_info', 'chat_invite', |
---|
| 19 | 'chat_kick', 'chat_leave', 'chat_msg', 'chat_with', 'chat_join', |
---|
| 20 | 'chat_topic' |
---|
| 21 | ] |
---|
| 22 | |
---|
| 23 | class RpcForwarder(object): |
---|
| 24 | """Tiny object that forwards RPCs from local Python code to BitlBee |
---|
| 25 | with a marginally nicer syntax. This layer could eventually be |
---|
| 26 | used to add basic parameter checking though I don't think that should |
---|
| 27 | be done here.""" |
---|
| 28 | |
---|
| 29 | def __init__(self, methods, target): |
---|
| 30 | for m in methods: |
---|
| 31 | # imc(b)_ prefix is not useful here, chop it. |
---|
| 32 | # (Maybe do this in BitlBee already as well.) |
---|
| 33 | newname = re.sub("^imcb?_", "", m) |
---|
| 34 | self.__setattr__(newname, target.__getattr__(m)) |
---|
| 35 | |
---|
| 36 | class BitlBeeIMPlugin(BaseHandler): |
---|
[c5a7b8d] | 37 | # Protocol name to be used in the BitlBee CLI, etc. |
---|
[f3af614] | 38 | NAME = "rpc-test" |
---|
[c5a7b8d] | 39 | |
---|
| 40 | # See account.h (TODO: Add constants.) |
---|
| 41 | ACCOUNT_FLAGS = 3 |
---|
| 42 | |
---|
| 43 | # Supported away states. If your protocol supports a specific set of |
---|
| 44 | # away states, put them in a list in this variable. |
---|
| 45 | AWAY_STATES = ["Away", "Busy"] #None |
---|
[f3af614] | 46 | |
---|
| 47 | # Filled in during initialisation: |
---|
| 48 | # Version code in hex. So if you need to do comparisions, for example |
---|
| 49 | # check "self.bitlbee_version >= 0x030202" for versions 3.2.2+ |
---|
| 50 | bitlbee_version = None |
---|
| 51 | # Full version string |
---|
| 52 | bitlbee_version_str = None |
---|
[c5a7b8d] | 53 | # Will become an RpcForwarder object to call into BitlBee |
---|
| 54 | bee = None |
---|
[f3af614] | 55 | |
---|
| 56 | @classmethod |
---|
| 57 | def _factory(cls, *args, **kwargs): |
---|
| 58 | def handler_factory(connection): |
---|
| 59 | handler = cls(connection, *args, **kwargs) |
---|
| 60 | return handler |
---|
| 61 | return handler_factory |
---|
| 62 | |
---|
| 63 | #def __init__(self, connection, *args, **kwargs): |
---|
| 64 | # BaseHandler.__init__(self,connection) |
---|
| 65 | |
---|
| 66 | def init(self, bee): |
---|
| 67 | self.bee = RpcForwarder(bee["method_list"], self._conn.call) |
---|
| 68 | self.bitlbee_version = bee["version"] |
---|
| 69 | self.bitlbee_version_str = bee["version_str"] |
---|
| 70 | # TODO: See how to call into the module here. |
---|
| 71 | return { |
---|
| 72 | "name": self.NAME, |
---|
| 73 | "method_list": list(set(dir(self)) & set(SUPPORTED_FUNCTIONS)), |
---|
[c5a7b8d] | 74 | "account_flags": self.ACCOUNT_FLAGS, |
---|
| 75 | "away_state_list": self.AWAY_STATES, |
---|
[f3af614] | 76 | "settings": { |
---|
| 77 | "oauth": { |
---|
| 78 | "default": "off", |
---|
[f15553d] | 79 | "type": "bool", |
---|
[f3af614] | 80 | }, |
---|
| 81 | "test": { |
---|
| 82 | "default": "123", |
---|
[f15553d] | 83 | "type": "int", |
---|
[f3af614] | 84 | }, |
---|
[f15553d] | 85 | "stringetje": { |
---|
| 86 | "default": "testje", |
---|
| 87 | "flags": 0x04, |
---|
| 88 | } |
---|
[f3af614] | 89 | }, |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | def login(self, account): |
---|
| 93 | print "Logging in with username %s and password %s" % (account['user'], account['pass']) |
---|
| 94 | self.bee.log("Blaataap %r" % account) |
---|
| 95 | self.bee.error("HALP!") |
---|
| 96 | self.bee.connected() |
---|
| 97 | return [{1:2,3:4}, {"a":"A", "b":"B"}, 1, 2, True] |
---|
| 98 | |
---|
| 99 | def logout(self): |
---|
| 100 | self.bee.error("Ok bye!") |
---|
| 101 | |
---|
| 102 | def add_buddy(self, handle, group): |
---|
| 103 | print "%s is my new best friend in %s \o/" % (handle, group) |
---|
| 104 | self.bee.add_buddy(handle, group) |
---|
| 105 | self.bee.buddy_status(handle, 5, "Friend", "Best friend!") |
---|
| 106 | print self.bee.bee_user_by_handle(handle) |
---|
| 107 | print self.bee.set_setstr("test", handle) |
---|
| 108 | print self.bee.set_reset("test") |
---|
[c5a7b8d] | 109 | |
---|
| 110 | def set_away(self, state, message): |
---|
| 111 | print "You're a slacker: %s (%r)" % (state, message) |
---|
[f15553d] | 112 | |
---|
| 113 | def set_set(self, setting, value): |
---|
| 114 | print "Setting %s changed to %r" % (setting, value) |
---|
[f3af614] | 115 | |
---|
| 116 | def RunPlugin(plugin, debug=True): |
---|
| 117 | sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
---|
| 118 | sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
---|
| 119 | sock.bind("/tmp/rpcplugins/test2") |
---|
| 120 | sock.listen(3) |
---|
| 121 | |
---|
| 122 | srv = bjsonrpc.server.Server(sock, plugin._factory()) |
---|
| 123 | |
---|
| 124 | srv.debug_socket(debug) |
---|
| 125 | srv.serve() |
---|
| 126 | |
---|
| 127 | RunPlugin(BitlBeeIMPlugin) |
---|