summaryrefslogtreecommitdiff
path: root/sleekxmpp/plugins/xep_0258/stanza.py
blob: d844e44c1ba3e74b205d9819bff87461c34caf85 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
"""
    SleekXMPP: The Sleek XMPP Library
    Copyright (C) 2012 Nathanael C. Fritz, Lance J.T. Stout
    This file is part of SleekXMPP.

    See the file LICENSE for copying permission.
"""

from base64 import b64encode, b64decode

from sleekxmpp.thirdparty.suelta.util import bytes

from sleekxmpp.xmlstream import ElementBase, ET, register_stanza_plugin


class SecurityLabel(ElementBase):
    name = 'securitylabel'
    namespace = 'urn:xmpp:sec-label:0'
    plugin_attrib = 'security_label'

    def add_equivalent(self, label):
        equiv = EquivalentLabel(parent=self)
        equiv.append(label)
        return equiv


class Label(ElementBase):
    name = 'label'
    namespace = 'urn:xmpp:sec-label:0'
    plugin_attrib = 'label'


class DisplayMarking(ElementBase):
    name = 'displaymarking'
    namespace = 'urn:xmpp:sec-label:0'
    plugin_attrib = 'display_marking'
    interfaces = set(['fgcolor', 'bgcolor', 'value'])

    def get_fgcolor(self):
        return self._get_attr('fgcolor', 'black')

    def get_bgcolor(self):
        return self._get_attr('fgcolor', 'white')

    def get_value(self):
        return self.xml.text

    def set_value(self, value):
        self.xml.text = value

    def del_value(self):
        self.xml.text = ''


class EquivalentLabel(ElementBase):
    name = 'equivalentlabel'
    namespace = 'urn:xmpp:sec-label:0'
    plugin_attrib = 'equivalent_label'
    plugin_multi_attrib = 'equivalent_labels'


class Catalog(ElementBase):
    name = 'catalog'
    namespace = 'urn:xmpp:sec-label:catalog:2'
    plugin_attrib = 'security_label_catalog'
    interfaces = set(['to', 'from', 'name', 'desc', 'id', 'size', 'restrict'])

    def get_to(self):
        return JID(self._get_attr('to'))
        pass

    def set_to(self, value):
        return self._set_attr('to', str(value))

    def get_from(self):
        return JID(self._get_attr('from'))

    def set_from(self, value):
        return self._set_attr('from', str(value))

    def get_restrict(self):
        value = self._get_attr('restrict', '')
        if value and value.lower() in ('true', '1'):
            return True
        return False

    def set_restrict(self, value):
        self._del_attr('restrict')
        if value:
            self._set_attr('restrict', 'true')
        elif value == False:
            self._set_attr('restrict', 'false')


class CatalogItem(ElementBase):
    name = 'catalog'
    namespace = 'urn:xmpp:sec-label:catalog:2'
    plugin_attrib = 'item'
    plugin_multi_attrib = 'items'
    interfaces = set(['selector', 'default'])

    def get_default(self):
        value = self._get_attr('default', '')
        if value.lower() in ('true', '1'):
            return True
        return False

    def set_default(self, value):
        self._del_attr('default')
        if value:
            self._set_attr('default', 'true')
        elif value == False:
            self._set_attr('default', 'false')


class ESSLabel(ElementBase):
    name = 'esssecuritylabel'
    namespace = 'urn:xmpp:sec-label:ess:0'
    plugin_attrib = 'ess'
    interfaces = set(['value'])

    def get_value(self):
        if self.xml.text:
            return b64decode(bytes(self.xml.text))
        return ''

    def set_value(self, value):
        self.xml.text = ''
        if value:
            self.xml.text = b64encode(bytes(value))

    def del_value(self):
        self.xml.text = ''


register_stanza_plugin(Catalog, CatalogItem, iterable=True)
register_stanza_plugin(CatalogItem, SecurityLabel)
register_stanza_plugin(EquivalentLabel, ESSLabel)
register_stanza_plugin(Label, ESSLabel)
register_stanza_plugin(SecurityLabel, DisplayMarking)
register_stanza_plugin(SecurityLabel, EquivalentLabel, iterable=True)
register_stanza_plugin(SecurityLabel, Label)