summaryrefslogtreecommitdiff
path: root/sleekxmpp/plugins/xep_0313/stanza.py
blob: 6552dc5a454be576a1c1e5833c73ef4e63d55d77 (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
"""
    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 permissio
"""

import datetime as dt

from sleekxmpp.jid import JID
from sleekxmpp.xmlstream import ElementBase, ET
from sleekxmpp.plugins import xep_0082


class MAM(ElementBase):
    name = 'query'
    namespace = 'urn:xmpp:mam:tmp'
    plugin_attrib = 'mam'
    interfaces = set(['queryid', 'start', 'end', 'with', 'results'])
    sub_interfaces = set(['start', 'end', 'with'])

    def setup(self, xml=None):
        ElementBase.setup(self, xml)

        self._results = []

    def get_start(self):
        timestamp = self._get_attr('start')
        return xep_0082.parse(timestamp)

    def set_start(self, value):
        if isinstance(value, dt.datetime):
            value = xep_0082.format_datetime(value)
        self._set_attr('start', value)

    def get_end(self):
        timestamp = self._get_sub_text('end')
        return xep_0082.parse(timestamp)

    def set_end(self, value):
        if isinstance(value, dt.datetime):
            value = xep_0082.format_datetime(value)
        self._set_sub_text('end', value)

    def get_with(self):
        return JID(self._get_sub_text('with'))

    def set_with(self, value):
        self._set_sub_text('with', str(value))


    # The results interface is meant only as an easy
    # way to access the set of collected message responses
    # from the query.

    def get_results(self):
        return self._results

    def set_results(self, values):
        self._results = values

    def del_results(self):
        self._results = []


class Preferences(ElementBase):
    name = 'prefs'
    namespace = 'urn:xmpp:mam:tmp'
    plugin_attrib = 'mam_prefs'
    interfaces = set(['default', 'always', 'never'])
    sub_interfaces = set(['always', 'never'])

    def get_always(self):
        results = set()

        jids = self.xml.findall('{%s}always/{%s}jid' % (
            self.namespace, self.namespace))

        for jid in jids:
            results.add(JID(jid.text))

        return results

    def set_always(self, value):
        self._set_sub_text('always', '', keep=True)
        always = self.xml.find('{%s}always' % self.namespace)
        always.clear()

        if not isinstance(value, (list, set)):
            value = [value]

        for jid in value:
            jid_xml = ET.Element('{%s}jid' % self.namespace)
            jid_xml.text = str(jid)
            always.append(jid_xml)

    def get_never(self):
        results = set()

        jids = self.xml.findall('{%s}never/{%s}jid' % (
            self.namespace, self.namespace))

        for jid in jids:
            results.add(JID(jid.text))

        return results

    def set_never(self, value):
        self._set_sub_text('never', '', keep=True)
        never = self.xml.find('{%s}never' % self.namespace)
        never.clear()

        if not isinstance(value, (list, set)):
            value = [value]

        for jid in value:
            jid_xml = ET.Element('{%s}jid' % self.namespace)
            jid_xml.text = str(jid)
            never.append(jid_xml)


class Result(ElementBase):
    name = 'result'
    namespace = 'urn:xmpp:mam:tmp'
    plugin_attrib = 'mam_result'
    interfaces = set(['forwarded', 'queryid', 'id'])

    def get_forwarded(self):
        return self.parent()['forwarded']

    def del_forwarded(self):
        del self.parent()['forwarded']