source: python/implugin.py @ f15553d

Last change on this file since f15553d was f15553d, checked in by Wilmer van der Gaast <wilmer@…>, at 2015-04-12T15:04:55Z

Common set evaluator that sends updates to the RPC server if online.

  • Property mode set to 100755
File size: 3.8 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                                        "type": "bool",
80                                },
81                                "test": {
82                                        "default": "123",
83                                        "type": "int",
84                                },
85                                "stringetje": {
86                                        "default": "testje",
87                                        "flags": 0x04,
88                                }
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")
109       
110        def set_away(self, state, message):
111                print "You're a slacker: %s (%r)" % (state, message)
112       
113        def set_set(self, setting, value):
114                print "Setting %s changed to %r" % (setting, value)
115
116def 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
127RunPlugin(BitlBeeIMPlugin)
Note: See TracBrowser for help on using the repository browser.