source: python/implugin.py @ c5a7b8d

Last change on this file since c5a7b8d was c5a7b8d, checked in by Wilmer van der Gaast <wilmer@…>, at 2015-04-12T13:46:45Z

Away states/messages, account flags, error reporting, some comments.

  • Property mode set to 100755
File size: 3.6 KB
Line 
1#!/usr/bin/python
2
3import sys
4import bjsonrpc
5from bjsonrpc.handlers import BaseHandler
6
7import random
8import re
9import socket
10import 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.
15SUPPORTED_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
23class 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
36class BitlBeeIMPlugin(BaseHandler):
37        # Protocol name to be used in the BitlBee CLI, etc.
38        NAME = "rpc-test"
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
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
53        # Will become an RpcForwarder object to call into BitlBee
54        bee = None
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)),
74                        "account_flags": self.ACCOUNT_FLAGS,
75                        "away_state_list": self.AWAY_STATES,
76                        "settings": {
77                                "oauth": {
78                                        "default": "off",
79                                },
80                                "test": {
81                                        "default": "123",
82                                },
83                        },
84                }
85       
86        def login(self, account):
87                print "Logging in with username %s and password %s" % (account['user'], account['pass'])
88                self.bee.log("Blaataap %r" % account)
89                self.bee.error("HALP!")
90                self.bee.connected()
91                return [{1:2,3:4}, {"a":"A", "b":"B"}, 1, 2, True]
92
93        def logout(self):
94                self.bee.error("Ok bye!")
95
96        def add_buddy(self, handle, group):
97                print "%s is my new best friend in %s \o/" % (handle, group)
98                self.bee.add_buddy(handle, group)
99                self.bee.buddy_status(handle, 5, "Friend", "Best friend!")
100                print self.bee.bee_user_by_handle(handle)
101                print self.bee.set_setstr("test", handle)
102                print self.bee.set_reset("test")
103       
104        def set_away(self, state, message):
105                print "You're a slacker: %s (%r)" % (state, message)
106
107def RunPlugin(plugin, debug=True):
108        sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
109        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
110        sock.bind("/tmp/rpcplugins/test2")
111        sock.listen(3)
112       
113        srv = bjsonrpc.server.Server(sock, plugin._factory())
114       
115        srv.debug_socket(debug)
116        srv.serve()
117
118RunPlugin(BitlBeeIMPlugin)
Note: See TracBrowser for help on using the repository browser.