source: python/implugin.py @ f3af614

Last change on this file since f3af614 was f3af614, checked in by Wilmer van der Gaast <wilmer@…>, at 2015-04-11T23:27:57Z

Whole bunch of changes: Added some more settings-related stuff and some calls
from plugin into BitlBee (where we *can* actually do return values). Also,
adding what I have so far for the Python counterpart, I suppose it makes sense
to have it in the source tree as well - though the naming/layout/etc is
completely made up.

Also, writing the commit message as if Git were a normal version control
system because screw arbitrary poorly documented "rules" on how commit
messages should be written in the world's most "special" VCS.

  • Property mode set to 100755
File size: 3.1 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        NAME = "rpc-test"
38       
39        # Filled in during initialisation:
40        # Version code in hex. So if you need to do comparisions, for example
41        # check "self.bitlbee_version >= 0x030202" for versions 3.2.2+
42        bitlbee_version = None
43        # Full version string
44        bitlbee_version_str = None
45       
46        @classmethod
47        def _factory(cls, *args, **kwargs):
48                def handler_factory(connection):
49                        handler = cls(connection, *args, **kwargs)
50                        return handler
51                return handler_factory
52       
53        #def __init__(self, connection, *args, **kwargs):
54        #       BaseHandler.__init__(self,connection)
55
56        def init(self, bee):
57                self.bee = RpcForwarder(bee["method_list"], self._conn.call)
58                self.bitlbee_version = bee["version"]
59                self.bitlbee_version_str = bee["version_str"]
60                # TODO: See how to call into the module here.
61                return {
62                        "name": self.NAME,
63                        "method_list": list(set(dir(self)) & set(SUPPORTED_FUNCTIONS)),
64                        "settings": {
65                                "oauth": {
66                                        "default": "off",
67                                },
68                                "test": {
69                                        "default": "123",
70                                },
71                        },
72                }
73       
74        def login(self, account):
75                print "Logging in with username %s and password %s" % (account['user'], account['pass'])
76                self.bee.log("Blaataap %r" % account)
77                self.bee.error("HALP!")
78                self.bee.connected()
79                return [{1:2,3:4}, {"a":"A", "b":"B"}, 1, 2, True]
80
81        def logout(self):
82                self.bee.error("Ok bye!")
83
84        def add_buddy(self, handle, group):
85                print "%s is my new best friend in %s \o/" % (handle, group)
86                self.bee.add_buddy(handle, group)
87                self.bee.buddy_status(handle, 5, "Friend", "Best friend!")
88                print self.bee.bee_user_by_handle(handle)
89                print self.bee.set_setstr("test", handle)
90                print self.bee.set_reset("test")
91
92def RunPlugin(plugin, debug=True):
93        sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
94        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
95        sock.bind("/tmp/rpcplugins/test2")
96        sock.listen(3)
97       
98        srv = bjsonrpc.server.Server(sock, plugin._factory())
99       
100        srv.debug_socket(debug)
101        srv.serve()
102
103RunPlugin(BitlBeeIMPlugin)
Note: See TracBrowser for help on using the repository browser.