summaryrefslogtreecommitdiff
path: root/sleekxmpp/plugins/xep_0128/static.py
blob: 427011c0454356b25147cea81fc01c1644ee1836 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""
    SleekXMPP: The Sleek XMPP Library
    Copyright (C) 2010 Nathanael C. Fritz, Lance J.T. Stout
    This file is part of SleekXMPP.

    See the file LICENSE for copying permission.
"""

import logging

import sleekxmpp
from sleekxmpp.plugins.xep_0030 import StaticDisco


log = logging.getLogger(__name__)


class StaticExtendedDisco(object):

    """
    Extend the default StaticDisco implementation to provide
    support for extended identity information.
    """

    def __init__(self, static):
        """
        Augment the default XEP-0030 static handler object.

        Arguments:
            static -- The default static XEP-0030 handler object.
        """
        self.static = static

    def set_extended_info(self, jid, node, ifrom, data):
        """
        Replace the extended identity data for a JID/node combination.

        The data parameter may provide:
            data -- Either a single data form, or a list of data forms.
        """
        with self.static.lock:
            self.del_extended_info(jid, node, ifrom, data)
            self.add_extended_info(jid, node, ifrom, data)

    def add_extended_info(self, jid, node, ifrom, data):
        """
        Add additional extended identity data for a JID/node combination.

        The data parameter may provide:
            data -- Either a single data form, or a list of data forms.
        """
        with self.static.lock:
            self.static.add_node(jid, node)

            forms = data.get('data', [])
            if not isinstance(forms, list):
                forms = [forms]

            info = self.static.get_node(jid, node)['info']
            for form in forms:
                info.append(form)

    def del_extended_info(self, jid, node, ifrom, data):
        """
        Replace the extended identity data for a JID/node combination.

        The data parameter is not used.
        """
        with self.static.lock:
            if self.static.node_exists(jid, node):
                info = self.static.get_node(jid, node)['info']
                for form in info['substanza']:
                    info.xml.remove(form.xml)