summaryrefslogtreecommitdiff
path: root/slixmpp/plugins/xep_0045/muc.py
blob: 4507be5910e22761739e062007e2ae07c43bbdf8 (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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
# 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 __future__ import with_statement

import asyncio
import logging
from datetime import datetime
from typing import (
    Any,
    Dict,
    List,
    Tuple,
    Optional,
)

from slixmpp import (
    Presence,
    Message,
    Iq,
    JID,
)
from slixmpp.plugins import BasePlugin
from slixmpp.xmlstream import register_stanza_plugin, ET
from slixmpp.xmlstream.handler.callback import Callback
from slixmpp.xmlstream.matcher.xpath import MatchXPath
from slixmpp.xmlstream.matcher.stanzapath import StanzaPath
from slixmpp.xmlstream.matcher.xmlmask import MatchXMLMask
from slixmpp.exceptions import IqError, IqTimeout, PresenceError

from slixmpp.plugins.xep_0004 import Form
from slixmpp.plugins.xep_0045 import stanza
from slixmpp.plugins.xep_0045.stanza import (
    MUCInvite,
    MUCDecline,
    MUCPresence,
    MUCJoin,
    MUCMessage,
    MUCAdminQuery,
    MUCAdminItem,
    MUCHistory,
    MUCOwnerQuery,
    MUCOwnerDestroy,
    MUCStatus,
    MUCActor,
    MUCUserItem,
)
from slixmpp.types import (
    MucRole,
    MucAffiliation,
    MucRoomItem,
    MucRoomItemKeys,
    PresenceArgs,
)


log = logging.getLogger(__name__)

AFFILIATIONS = ('outcast', 'member', 'admin', 'owner', 'none')
ROLES = ('moderator', 'participant', 'visitor', 'none')


class XEP_0045(BasePlugin):

    """
    XEP-0045 Multi-User Chat
    """

    name = 'xep_0045'
    description = 'XEP-0045: Multi-User Chat'
    dependencies = {'xep_0030', 'xep_0004'}
    stanza = stanza

    rooms: Dict[JID, Dict[str, MucRoomItem]]
    our_nicks: Dict[JID, str]

    def plugin_init(self):
        self.rooms = {}
        self.our_nicks = {}
        # load MUC support in presence stanzas
        register_stanza_plugin(MUCMessage, MUCUserItem)
        register_stanza_plugin(MUCPresence, MUCUserItem)
        register_stanza_plugin(MUCUserItem, MUCActor)
        register_stanza_plugin(MUCMessage, MUCInvite)
        register_stanza_plugin(MUCMessage, MUCDecline)
        register_stanza_plugin(MUCMessage, MUCStatus)
        register_stanza_plugin(MUCPresence, MUCStatus)
        register_stanza_plugin(Presence, MUCPresence)
        register_stanza_plugin(Presence, MUCJoin)
        register_stanza_plugin(MUCJoin, MUCHistory)
        register_stanza_plugin(Message, MUCMessage)
        register_stanza_plugin(Iq, MUCAdminQuery)
        register_stanza_plugin(Iq, MUCOwnerQuery)
        register_stanza_plugin(MUCOwnerQuery, MUCOwnerDestroy)
        register_stanza_plugin(MUCOwnerQuery, Form)
        register_stanza_plugin(MUCAdminQuery, MUCAdminItem, iterable=True)

        # Register handlers
        self.xmpp.register_handler(
            Callback(
                'MUCPresence',
                StanzaPath("presence/muc"),
                self._handle_groupchat_presence,
        ))
        # <x xmlns="http://jabber.org/protocol/muc"/> is only used in
        # presence when joining on the client side, and for errors on
        # the server side.
        if self.xmpp.is_component:
            self.xmpp.register_handler(
                Callback(
                    'MUCPresenceJoin',
                    StanzaPath("presence/muc_join"),
                    self._handle_groupchat_join,
            ))
        self.xmpp.register_handler(
            Callback(
                "MUCPresenceError",
                StanzaPath("presence@type=error/muc_join"),
                self._handle_presence_error,
            )
        )

        self.xmpp.register_handler(
            Callback(
                'MUCError',
                MatchXMLMask("<message xmlns='%s' type='error'><error/></message>" % self.xmpp.default_ns),
                self._handle_groupchat_error_message
        ))
        self.xmpp.register_handler(
            Callback(
                'MUCMessage',
                MatchXMLMask("<message xmlns='%s' type='groupchat'><body/></message>" % self.xmpp.default_ns),
                self._handle_groupchat_message
        ))
        self.xmpp.register_handler(
            Callback(
                'MUCSubject',
                MatchXMLMask("<message xmlns='%s' type='groupchat'><subject/></message>" % self.xmpp.default_ns),
                self._handle_groupchat_subject
        ))
        self.xmpp.register_handler(
            Callback(
                'MUCConfig',
                StanzaPath('message/muc/status'),
                self._handle_config_change
        ))
        self.xmpp.register_handler(
            Callback(
                'MUCInvite',
                StanzaPath('message/muc/invite'),
                self._handle_groupchat_invite
        ))
        self.xmpp.register_handler(
            Callback(
                'MUCDecline',
                StanzaPath('message/muc/decline'),
                self._handle_groupchat_decline
        ))

    def plugin_end(self):
        self.xmpp.plugin['xep_0030'].del_feature(feature=stanza.NS)

    def session_bind(self, jid):
        self.xmpp.plugin['xep_0030'].add_feature(stanza.NS)

    def _handle_groupchat_invite(self, inv: Message):
        """ Handle an invite into a muc. """
        if self.xmpp.is_component:
            self.xmpp.event('groupchat_invite', inv)
        else:
            if inv['from'] not in self.rooms.keys():
                self.xmpp.event("groupchat_invite", inv)

    def _handle_groupchat_decline(self, decl: Message):
        """Handle an invitation decline."""
        if self.xmpp.is_component:
            self.xmpp.event('groupchat_invite', decl)
        else:
            if decl['from'] in self.room.keys():
                self.xmpp.event('groupchat_decline', decl)

    def _handle_config_change(self, msg: Message):
        """Handle a MUC configuration change (with status code)."""
        self.xmpp.event('groupchat_config_status', msg)
        self.xmpp.event('muc::%s::config_status' % msg['from'].bare , msg)

    def _client_handle_presence(self, pr: Presence):
        """As a client, handle a presence stanza"""
        got_offline = False
        got_online = False
        if pr['muc']['room'] not in self.rooms.keys():
            return
        self.xmpp.roster[pr['from']].ignore_updates = True
        entry = pr['muc'].get_stanza_values()
        entry['show'] = pr['show'] if pr['show'] in pr.showtypes else None
        entry['status'] = pr['status']
        entry['alt_nick'] = pr['nick']
        if pr['type'] == 'unavailable':
            if entry['nick'] in self.rooms[entry['room']]:
                del self.rooms[entry['room']][entry['nick']]
            got_offline = True
        else:
            if entry['nick'] not in self.rooms[entry['room']]:
                got_online = True
            self.rooms[entry['room']][entry['nick']] = entry
        log.debug("MUC presence from %s/%s : %s", entry['room'],entry['nick'], entry)
        self.xmpp.event("groupchat_presence", pr)
        if 110 in pr['muc']['status_codes']:
            self.xmpp.event("muc::%s::self-presence" % entry['room'], pr)
        self.xmpp.event("muc::%s::presence" % entry['room'], pr)
        if got_offline:
            self.xmpp.event("muc::%s::got_offline" % entry['room'], pr)
        if got_online:
            self.xmpp.event("muc::%s::got_online" % entry['room'], pr)

    def _handle_presence_error(self, pr: Presence):
        """Generate MUC presence error events"""
        self.xmpp.event("muc::%s::presence-error" % pr['from'].bare, pr)

    def _handle_groupchat_presence(self, pr: Presence):
        """ Handle a presence in a muc."""
        if self.xmpp.is_component:
            self.xmpp.event('groupchat_presence', pr)
        else:
            self._client_handle_presence(pr)

    def _handle_groupchat_join(self, pr: Presence):
        """Received a join presence (as a component)"""
        self.xmpp.event('groupchat_join', pr)

    def _handle_groupchat_message(self, msg: Message):
        """ Handle a message event in a muc.
        """
        self.xmpp.event('groupchat_message', msg)
        self.xmpp.event("muc::%s::message" % msg['from'].bare, msg)

    def _handle_groupchat_error_message(self, msg: Message):
        """ Handle a message error event in a muc.
        """
        self.xmpp.event('groupchat_message_error', msg)
        self.xmpp.event("muc::%s::message_error" % msg['from'].bare, msg)


    def _handle_groupchat_subject(self, msg: Message):
        """ Handle a message coming from a muc indicating
        a change of subject (or announcing it when joining the room)
        """
        # See poezio#3452. A message containing subject _and_ (body or thread)
        # is not a subject change.
        if msg['body'] or msg['thread']:
            return
        self.xmpp.event('groupchat_subject', msg)

    async def join_muc_wait(self, room: JID, nick: str, *,
                            password: Optional[str] = None,
                            maxchars: Optional[int] = None,
                            maxstanzas: Optional[int] = None,
                            seconds: Optional[int] = None,
                            since: Optional[datetime] = None,
                            presence_options: Optional[PresenceArgs] = None,
                            timeout: Optional[int] = None) -> Presence:
        """
        Try to join a MUC and block until we are joined or get an error.

        Only one of {maxchars, maxstanzas, seconds, since} will be used, in
        that order.

        .. versionadded:: 1.8.0

        :param password: The optional room password.
        :param maxchars: Max number of characters to return from history.
        :param maxstanzas: Max number of stanzas to return from history.
        :param seconds: Fetch history until that many seconds in the past.
        :param since: Fetch history since that timestamp.
        :param timeout: Timeout after which a TimeoutError is raised.
                        None means no timeout.
        :raises: A slixmpp.exceptions.PresenceError if the MUC returns a
                 presence error.
        :raises: An asyncio.TimeoutError if there is neither success nor
                presence error when the timeout is reached.
        :return: Our own presence
        """
        if presence_options is None:
            presence_options = {}
        stanza = self.xmpp.make_presence(
            pto="%s/%s" % (room, nick),
            **presence_options
        )
        stanza.enable('muc_join')
        if password is not None:
            stanza['muc_join']['password'] = password
        if maxchars is not None:
            stanza['muc_join']['history']['maxchars'] = str(maxchars)
        elif maxstanzas is not None:
            stanza['muc_join']['history']['maxstanzas'] = str(maxstanzas)
        elif seconds is not None:
            stanza['muc_join']['history']['seconds'] = str(seconds)
        elif since is not None:
            fmt = self.xmpp.plugin['xep_0082'].format_datetime(since)
            stanza['muc_join']['history']['since'] = fmt
        self.rooms[room] = {}
        self.our_nicks[room] = nick
        stanza.send()

        future: asyncio.Future = asyncio.Future()
        context1 = self.xmpp.event_handler("muc::%s::self-presence" % room, future.set_result)
        context2 = self.xmpp.event_handler("muc::%s::presence-error" % room, future.set_result)
        with context1, context2:
            done, pending = await asyncio.wait(
                [future],
                timeout=timeout,
            )
        if pending:
            raise asyncio.TimeoutError()
        pres = await future
        if pres['type'] == 'error':
            raise PresenceError(pres)
        # update known nick in case it has changed
        self.our_nicks[room] = pres['from'].resource
        return pres

    def join_muc(self, room: JID, nick: str, maxhistory="0", password='',
                 pstatus='', pshow='', pfrom='') -> asyncio.Future:
        """ Join the specified room, requesting 'maxhistory' lines of history.

        .. deprecated:: 1.8.0

            :meth:`join_muc_wait` will replace this old API starting from version
            1.9.0.

        """
        presence_options = PresenceArgs(
            pshow=pshow,
            pstatus=pstatus,
            pfrom=pfrom,
        )
        maxchars, maxstanzas = None, None
        if maxhistory:
            if maxhistory == "0":
                maxchars = 9
            else:
                maxstanzas = int(maxhistory)
        return asyncio.ensure_future(
            self.join_muc_wait(
                room=room,
                nick=nick,
                password=password,
                presence_options=presence_options,
                maxchars=maxchars,
                maxstanzas=maxstanzas,
            ),
            loop=self.xmpp.loop,
        )

    def leave_muc(self, room: JID, nick: str, msg: str = '', pfrom: Optional[JID] = None):
        """ Leave the specified room.

        :param room: Room to leave.
        :param nick: Your nickname.
        :param msg: Presence status to use.
        """
        if msg:
            self.xmpp.send_presence(
                pshow='unavailable',
                pto="%s/%s" % (room, nick),
                pstatus=msg,
                pfrom=pfrom
            )
        else:
            self.xmpp.send_presence(
                pshow='unavailable',
                pto="%s/%s" % (room, nick),
                pfrom=pfrom
            )
        del self.rooms[room]

    def set_subject(self, room: JID, subject: str, *, mfrom: Optional[JID] = None):
        """Set a room’s subject.

        :param room: JID of the room.
        :param subject: Room subject to set.
        """
        msg = self.xmpp.make_message(room, mfrom=mfrom)
        msg['type'] = 'groupchat'
        msg['subject'] = subject
        msg.send()

    async def get_room_config(self, room: JID, ifrom: Optional[JID] = None,
                              **iqkwargs) -> Form:
        """Get the room config form in 0004 plugin format.

        :param room: Room to get the config form from.
        :raises ValueError: When the form is not found.
        :returns: A form object.
        """
        iq = self.xmpp.make_iq_get(stanza.NS_OWNER, ito=room, ifrom=ifrom)
        result = await iq.send(**iqkwargs)
        form = result['mucowner_query'].get_plugin('form', check=True)
        if form is None:
            raise ValueError("Configuration form not found")
        return form

    async def set_room_config(self, room: JID, config: Form, *,
                              ifrom: Optional[JID] = None, **iqkwargs):
        """Send a room config form.

        :param room: Room to send the form to.
        :param config: A filled room form.
        """
        query = MUCOwnerQuery()
        config['type'] = 'submit'
        query.append(config)
        iq = self.xmpp.make_iq_set(query, ito=room, ifrom=ifrom)
        await iq.send(**iqkwargs)

    async def cancel_config(self, room: JID, *,
                            ifrom: Optional[JID] = None, **iqkwargs):
        """Cancel a requested config form.

        :param room: Room to cancel the form for.
        """
        query = MUCOwnerQuery()
        query['form']['type'] = 'cancel'
        iq = self.xmpp.make_iq_set(query, ito=room, ifrom=ifrom)
        await iq.send(**iqkwargs)

    async def destroy(self, room: JID, reason: str = '', altroom: Optional[JID] = None, *,
                      ifrom: Optional[JID] = None, **iqkwargs):
        """Destroy a room.

        :param room: Room JID to destroy.
        :param reason: Reason for destroying the room.
        :param altroom: An alternate room that users should join.
        """
        iq = self.xmpp.make_iq_set(ifrom=ifrom, ito=room)
        iq.enable('mucowner_query')
        iq['mucowner_query'].enable('destroy')
        if altroom:
            iq['mucowner_query']['destroy']['jid'] = altroom
        if reason:
            iq['mucowner_query']['destroy']['reason'] = reason
        await iq.send(**iqkwargs)

    async def set_affiliation(self, room: JID, affiliation: MucAffiliation, *,
                              jid: Optional[JID] = None,
                              nick: Optional[str] = None, reason: str = '',
                              ifrom: Optional[JID] = None, **iqkwargs):
        """ Change room affiliation for a JID or nickname.

        :param room: Room to modify.
        :param affiliation: Affiliation to set.
        :param jid: User JID to use in the set operation.
        :param reason: Reason for the affiliation change.
        """
        if affiliation not in AFFILIATIONS:
            raise ValueError('%s is not a valid affiliation' % affiliation)
        if not any((jid, nick)):
            raise ValueError('One of jid or nick must be set')
        iq = self.xmpp.make_iq_set(ito=room, ifrom=ifrom)
        iq['mucadmin_query']['item']['affiliation'] = affiliation
        if nick:
            iq['mucadmin_query']['item']['nick'] = nick
        if jid:
            iq['mucadmin_query']['item']['jid'] = jid
        if reason:
            iq['mucadmin_query']['item']['reason'] = reason
        await iq.send(**iqkwargs)

    async def get_affiliation_list(self, room: JID, affiliation: MucAffiliation, *,
                                   ifrom: Optional[JID] = None, **iqkwargs) -> List[JID]:
        """Get a list of JIDs with the specified affiliation

        :param room: Room to get affiliations from.
        :param affiliation: The affiliation to list.
        """
        iq = self.xmpp.make_iq_get(stanza.NS_ADMIN, ito=room, ifrom=ifrom)
        iq['mucadmin_query']['item']['affiliation'] = affiliation
        result = await iq.send(**iqkwargs)
        return [item['jid'] for item in result['mucadmin_query']]

    async def send_affiliation_list(self, room: JID,
                                    affiliations: List[Tuple[JID, MucAffiliation]], *,
                                    ifrom: Optional[JID] = None, **iqkwargs):
        """Send an affiliation delta list.

        :param room: Room to send the affiliations to.
        :param affiliations: List of couples (jid, affiliation) to set.
        """
        iq = self.xmpp.make_iq_set(ito=room, ifrom=ifrom)
        for jid, affiliation in affiliations:
            item = MUCAdminItem()
            item['jid'] = jid
            item['affiliation'] = affiliation
            iq['mucadmin_query'].append(item)
        await iq.send(**iqkwargs)

    async def set_role(self, room: JID, nick: str, role: MucRole, *,
                       reason: str = '', ifrom: Optional[JID] = None, **iqkwargs):
        """ Change role property of a nick in a room.
            Typically, roles are temporary (they last only as long as you are in the
            room), whereas affiliations are permanent (they last across groupchat
            sessions).

        :param room: Room to modify.
        :param nick: User nickname to use in the set operation.
        :param role: Role to set.
        :param reason: Reason for the role change.
        """
        if role not in ROLES:
            raise ValueError("Role %s does not exist" % role)
        iq = self.xmpp.make_iq_set(ito=room, ifrom=ifrom)
        iq['mucadmin_query']['item']['role'] = role
        iq['mucadmin_query']['item']['nick'] = nick
        if reason:
            iq['mucadmin_query']['item']['reason'] = reason
        await iq.send(**iqkwargs)

    async def get_roles_list(self, room: JID, role: MucRole, *,
                             ifrom: Optional[JID] = None, **iqkwargs) -> List[str]:
        """"Get a list of JIDs with the specified role

        :param room: Room to get roles from.
        :param role: The role to list.
        """
        iq = self.xmpp.make_iq_get(stanza.NS_ADMIN, ito=room, ifrom=ifrom)
        iq['mucadmin_query']['item']['role'] = role
        result = await iq.send(**iqkwargs)
        return [item['nick'] for item in result['mucadmin_query']]

    async def send_role_list(self, room: JID, roles: List[Tuple[str, MucRole]], *,
                             ifrom: Optional[JID] = None, **iqkwargs):
        """Send a role delta list.

        :param room: Room to send the roles to.
        :param roles: List of couples (nick, role) to set.
        """
        iq = self.xmpp.make_iq_set(ito=room, ifrom=ifrom)
        for nick, affiliation in roles:
            item = MUCAdminItem()
            item['nick'] = nick
            item['affiliation'] = affiliation
            iq['mucadmin_query'].append(item)
        await iq.send(**iqkwargs)

    def invite(self, room: JID, jid: JID, reason: str = '', *,
               mfrom: Optional[JID] = None):
        """ Invite a jid to a room (mediated invitation).

        :param room: Room to invite the user in.
        :param jid: JID of the user to invite.
        :param reason: Reason for inviting the user.
        """
        msg = self.xmpp.make_message(room, mfrom=mfrom)
        msg['muc']['invite']['to'] = jid
        if reason:
            msg['muc']['invite']['reason'] = reason
        self.xmpp.send(msg)

    def invite_server(self, room: JID, jid: JID,
                         invite_from: JID, reason: str = ''):
        """Send a mediated invite to a user, as a MUC service.

        .. versionadded:: 1.8.0

        :param room: Room to invite the user in.
        :param jid: JID of the user to invite.
        :param invite_from: JID of the user to send the invitation from.
        :param reason: Reason for inviting the user.
        """
        if not self.xmpp.is_component:
            raise ValueError("Cannot use this method as a client.")
        msg = self.xmpp.make_message(jid, mfrom=room)
        msg['muc']['invite']['from'] = invite_from
        if reason:
            msg['muc']['invite']['reason'] = reason
        msg.send()

    def decline(self, room: JID, jid: JID, reason: str = '', *,
                mfrom: Optional[JID] = None):
        """Decline a mediated invitation.

        :param room: Room the invitation came from.
        :param jid: JID of the user who sent the invitation.
        :param reason: Reason for declining.
        """
        msg = self.xmpp.make_message(room, mfrom=mfrom)
        msg['muc']['decline']['to'] = jid
        if reason:
            msg['muc']['decline']['reason'] = reason
        self.xmpp.send(msg)

    def jid_in_room(self, room: JID, jid: JID) -> bool:
        """Check if a JID is present in a room.

        :param room: Room to check.
        :param jid: JID to check.
        """
        for nick in self.rooms[room]:
            entry = self.rooms[room][nick]
            if not entry.get('jid'):
                continue
            if entry is not None and entry['jid'].full == jid:
                return True
        return False

    def get_nick(self, room: JID, jid: JID) -> Optional[str]:
        """Get the nickname of a specific JID in a room.

        :param room: Room to inspect.
        :param jid: JID whose nick to return.
        """
        for nick in self.rooms[room]:
            entry = self.rooms[room][nick]
            if not entry.get('jid'):
                continue
            if entry is not None and entry['jid'].full == jid:
                return nick
        return None

    def get_joined_rooms(self) -> List[JID]:
        """Get the list of rooms we sent a join presence to
        and did not explicitly leave.
        """
        return list(self.rooms.keys())

    def get_our_jid_in_room(self, room_jid: JID) -> str:
        """ Return the jid we're using in a room.
        """
        return "%s/%s" % (room_jid, self.our_nicks[room_jid])

    def get_jid_property(self, room: JID, nick: str,
                         jid_property: MucRoomItemKeys) -> Any:
        """ Get the property of a nick in a room, such as its 'jid' or 'affiliation'
            If not found, return None.

        :param room: Get the property for this room.
        :param nick: Which nickname information to get.
        :param jid_property: Property to fetch.
        """
        if room in self.rooms and nick in self.rooms[room] and jid_property in self.rooms[room][nick]:
            return self.rooms[room][nick][jid_property]
        else:
            return None

    def get_roster(self, room: JID) -> List[str]:
        """ Get the list of nicks in a room.

        :param room: Room to list nicks from.
        """
        if room not in self.rooms.keys():
            raise ValueError("Room %s is not joined" % room)
        return list(self.rooms[room].keys())

    def get_users_by_affiliation(self, room: JID, affiliation='member', *, ifrom: Optional[JID] = None):
        # Preserve old API
        if affiliation not in AFFILIATIONS:
            raise ValueError("Affiliation %s does not exist" % affiliation)
        return self.get_affiliation_list(room, affiliation, ifrom=ifrom)