summaryrefslogtreecommitdiff
path: root/slixmpp/plugins/xep_0405/mix_pam.py
blob: 1158a6f98efe34b4c7996bd14e0be0507020b0ba (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
"""
    Slixmpp: The Slick XMPP Library
    Copyright (C) 2020 Mathieu Pasquet <mathieui@mathieui.net>
    This file is part of Slixmpp.

    See the file LICENSE for copying permission.
"""
from typing import (
    List,
    Optional,
    Set,
    Tuple,
)

from slixmpp import JID, Iq
from slixmpp.exceptions import IqError, IqTimeout
from slixmpp.plugins import BasePlugin
from slixmpp.stanza.roster import RosterItem
from slixmpp.plugins.xep_0405 import stanza
from slixmpp.plugins.xep_0369 import stanza as mix_stanza


BASE_NODES = [
    'urn:xmpp:mix:nodes:messages',
    'urn:xmpp:mix:nodes:participants',
    'urn:xmpp:mix:nodes:info',
]


class XEP_0405(BasePlugin):
    '''XEP-0405: MIX-PAM'''

    name = 'xep_0405'
    description = 'MIX-PAM'
    dependencies = {'xep_0369'}
    stanza = stanza
    namespace = stanza.NS

    def plugin_init(self) -> None:
        stanza.register_plugins()

    async def check_server_capability(self) -> bool:
        """Check if the server is MIX-PAM capable"""
        result = await self.xmpp.plugin['xep_0030'].get_info(jid=self.xmpp.boundjid.bare)
        features = result['disco_info']['features']
        return stanza.NS in features

    async def join_channel(self, room: JID, nick: str, subscribe: Optional[Set[str]] = None, *,
                           ito: Optional[JID] = None,
                           ifrom: Optional[JID] = None,
                           **iqkwargs) -> Set[str]:
        """
        Join a MIX channel.

        :param JID room: JID of the MIX channel
        :param str nick: Desired nickname on that channel
        :param Set[str] subscribe: Set of nodes to subscribe to when joining.
            If empty, all nodes will be subscribed by default.

        :rtype: Set[str]
        :return: The nodes that failed to subscribe, if any
        """
        if subscribe is None:
            subscribe = set(BASE_NODES)
        if ito is None:
            ito = self.xmpp.boundjid.bare
        iq = self.xmpp.make_iq_set(ito=ito, ifrom=ifrom)
        iq['client_join']['channel'] = room
        iq['client_join']['mix_join']['nick'] = nick
        for node in subscribe:
            sub = mix_stanza.Subscribe()
            sub['node'] = node
            iq['client_join']['mix_join'].append(sub)
        result = await iq.send(**iqkwargs)
        result_nodes = {sub['node'] for sub in result['client_join']['mix_join']}
        return result_nodes.difference(subscribe)

    async def leave_channel(self, room: JID, *,
                            ito: Optional[JID] = None,
                            ifrom: Optional[JID] = None,
                            **iqkwargs) -> Iq:
        """"
        Leave a MIX channel
        :param JID room: JID of the channel to leave
        """
        if ito is None:
            ito = self.xmpp.boundjid.bare
        iq = self.xmpp.make_iq_set(ito=ito, ifrom=ifrom)
        iq['client_leave']['channel'] = room
        iq['client_leave'].enable('mix_leave')
        return await iq.send(**iqkwargs)

    async def get_mix_roster(self, *,
                            ito: Optional[JID] = None,
                            ifrom: Optional[JID] = None,
                            **iqkwargs) -> Tuple[List[RosterItem], List[RosterItem]]:
        """
        Get the annotated roster, with MIX channels.

        :return: A tuple of (contacts, mix channels) as RosterItem elements
        """
        iq = self.xmpp.make_iq_get(ito=ito, ifrom=ifrom)
        iq['roster'].enable('annotate')
        result = await iq.send(**iqkwargs)
        self.xmpp.event("roster_update", result)
        contacts = []
        mix = []
        for item in result['roster']:
            channel = item._get_plugin('channel', check=True)
            if channel:
                mix.append(item)
            else:
                contacts.append(item)
        return (contacts, mix)