summaryrefslogtreecommitdiff
path: root/slixmpp/plugins/xep_0045/stanza.py
blob: 428dbce77bc4efaa454441732a37e65a6acb17d9 (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290

# Slixmpp: The Slick XMPP Library
# Copyright (C) 2010 Nathanael C. Fritz
# Copyright (C) 2020 "Maxime “pep” Buquet <pep@bouah.net>"
# This file is part of Slixmpp.
# See the file LICENSE for copying permission.
from typing import (
    Iterable,
    Set,
    Optional,
    Union,
)
import logging
from slixmpp.xmlstream import ElementBase, ET, JID


log = logging.getLogger(__name__)

NS = 'http://jabber.org/protocol/muc'
NS_USER = 'http://jabber.org/protocol/muc#user'
NS_ADMIN = 'http://jabber.org/protocol/muc#admin'
NS_OWNER = 'http://jabber.org/protocol/muc#owner'


class MUCBase(ElementBase):
    name = 'x'
    namespace = NS_USER
    plugin_attrib = 'muc'
    interfaces = {'affiliation', 'role', 'jid', 'nick', 'room', 'status_codes'}

    def get_status_codes(self) -> Set[str]:
        status = self.xml.findall(f'{{{NS_USER}}}status')
        return {int(status.attrib['code']) for status in status}

    def set_status_codes(self, codes: Iterable[int]):
        self.del_status_codes()
        for code in set(codes):
            self._add_status_code(code)

    def del_status_codes(self):
        status = self.xml.findall(f'{{{NS_USER}}}status')
        for elem in status:
            self.xml.remove(elem)

    def _add_status_code(self, code: int):
        status = MUCStatus()
        status['code'] = code
        self.append(status)

    def get_item_attr(self, attr: str, default):
        item = self.xml.find(f'{{{NS_USER}}}item')
        if item is None:
            return default
        return self['item'][attr]

    def set_item_attr(self, attr: str, value: str):
        item = self['item']
        item[attr] = value
        return item

    def del_item_attr(self, attr):
        item = self.xml.find(f'{{{NS_USER}}}item')
        if item is not None:
            del self['item'][attr]

    def get_affiliation(self):
        return self.get_item_attr('affiliation', '')

    def set_affiliation(self, value):
        self.set_item_attr('affiliation', value)

    def del_affiliation(self):
        self.del_item_attr('affiliation')

    def get_jid(self) -> JID:
        return JID(self.get_item_attr('jid', ''))

    def set_jid(self, value: Union[JID, str]):
        if not isinstance(value, str):
            value = str(value)
        self.set_item_attr('jid', value)

    def del_jid(self):
        self.del_item_attr('jid')

    def get_role(self) -> str:
        return self.get_item_attr('role', '')

    def set_role(self, value: str):
        # TODO: check for valid role
        self.set_item_attr('role', value)

    def del_role(self):
        # TODO: set default role
        self.del_item_attr('role')

    def get_nick(self) -> str:
        return self.parent()['from'].resource

    def get_room(self) -> str:
        return self.parent()['from'].bare

    def set_nick(self, value):
        log.warning(
            "Cannot set nick through the %s plugin.",
            self.__class__.__name__,
        )
        return self

    def set_room(self, value):
        log.warning(
            "Cannot set room through the %s plugin.",
            self.__class__.__name__,
        )
        return self

    def del_nick(self):
        log.warning(
            "Cannot delete nick through the %s plugin.",
            self.__class__.__name__,
        )
        return self

    def del_room(self):
        log.warning(
            "Cannot delete room through the %s plugin.",
            self.__class__.__name__,
        )
        return self


class MUCPresence(MUCBase):
    '''
    A MUC Presence

    ::

        <presence from='foo@muc/user1' type='unavailable'>
            <x xmlns='http://jabber.org/protocol/muc#user'>
                <item affiliation='none'
                      role='none'
                      nick='newnick2'
                      jid='some@jid'/>
                <status code='303'/>
            </x>
        </presence>
    '''


class MUCMessage(MUCBase):
    '''
    A MUC Message

    ::

        <message from='foo@muc/user1' type='groupchat' id='someid'>
            <body>Foo</body>
            <x xmlns='http://jabber.org/protocol/muc#user'>
                <item affiliation='none'
                      role='none'
                      nick='newnick2'
                      jid='some@jid'/>
            </x>
        </message>
    '''

class MUCJoin(ElementBase):
    name = 'x'
    namespace = NS
    plugin_attrib = 'muc_join'
    interfaces = {'password'}
    sub_interfaces = {'password'}


class MUCInvite(ElementBase):
    name = 'invite'
    plugin_attrib = 'invite'
    namespace = NS_USER
    interfaces = {'to', 'from', 'reason'}
    sub_interfaces = {'reason'}

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

    def set_to(self, value: Union[JID, str]):
        if not isinstance(value, JID):
            value = JID(value)
        self._set_attr('to', value)

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

    def set_from(self, value: Union[JID, str]):
        if not isinstance(value, JID):
            value = JID(value)
        self._set_attr('from', value)

class MUCDecline(ElementBase):
    name = 'decline'
    plugin_attrib = 'decline'
    namespace = NS_USER
    interfaces = {'to', 'from', 'reason'}
    sub_interfaces = {'reason'}

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

    def set_to(self, value: Union[JID, str]):
        if not isinstance(value, JID):
            value = JID(value)
        self._set_attr('to', value)

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

    def set_from(self, value: Union[JID, str]):
        if not isinstance(value, JID):
            value = JID(value)
        self._set_attr('from', value)


class MUCHistory(ElementBase):
    name = 'history'
    plugin_attrib = 'history'
    namespace = NS
    interfaces = {'maxchars', 'maxstanzas', 'since', 'seconds'}


class MUCOwnerQuery(ElementBase):
    name = 'query'
    plugin_attrib = 'mucowner_query'
    namespace = NS_OWNER


class MUCOwnerDestroy(ElementBase):
    name = 'destroy'
    plugin_attrib = 'destroy'
    interfaces = {'reason', 'jid'}
    sub_interfaces = {'reason'}


class MUCAdminQuery(ElementBase):
    name = 'query'
    plugin_attrib = 'mucadmin_query'
    namespace = NS_ADMIN


class MUCAdminItem(ElementBase):
    namespace = NS_ADMIN
    name = 'item'
    plugin_attrib = 'item'
    interfaces = {'role', 'affiliation', 'nick', 'jid', 'reason'}
    sub_interfaces = {'reason'}


class MUCStatus(ElementBase):
    namespace = NS_USER
    name = 'status'
    plugin_attrib = 'status'
    interfaces = {'code'}

    def set_code(self, code: int):
        self.xml.attrib['code'] = str(code)


class MUCUserItem(ElementBase):
    namespace = NS_USER
    name = 'item'
    plugin_attrib = 'item'
    interfaces = {'role', 'affiliation', 'jid', 'reason', 'nick'}
    sub_interfaces = {'reason'}

    def get_jid(self) -> Optional[JID]:
        jid = self.xml.attrib.get('jid', None)
        if jid:
            return JID(jid)
        return jid


class MUCActor(ElementBase):
    namespace = NS_USER
    name = 'actor'
    plugin_attrib = 'actor'
    interfaces = {'jid', 'nick'}

    def get_jid(self) -> Optional[JID]:
        jid = self.xml.attrib.get('jid', None)
        if jid:
            return JID(jid)
        return jid