File: vcard_temp.py

package info (click to toggle)
python-nbxmpp 6.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,340 kB
  • sloc: python: 19,639; makefile: 4
file content (140 lines) | stat: -rw-r--r-- 4,157 bytes parent folder | download | duplicates (2)
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
# Copyright (C) 2020 Philipp Hörist <philipp AT hoerist.com>
#
# This file is part of nbxmpp.
#
# SPDX-License-Identifier: GPL-3.0-or-later

from __future__ import annotations

from typing import TYPE_CHECKING

import hashlib
from dataclasses import dataclass
from dataclasses import field

from nbxmpp.errors import MalformedStanzaError
from nbxmpp.errors import StanzaError
from nbxmpp.modules.base import BaseModule
from nbxmpp.modules.util import process_response
from nbxmpp.namespaces import Namespace
from nbxmpp.protocol import Iq
from nbxmpp.protocol import JID
from nbxmpp.simplexml import Node
from nbxmpp.task import iq_request_task
from nbxmpp.util import b64decode
from nbxmpp.util import b64encode

if TYPE_CHECKING:
    from nbxmpp.client import Client


class VCardTemp(BaseModule):
    def __init__(self, client: Client) -> None:
        BaseModule.__init__(self, client)

        self._client = client
        self.handlers = []

    @iq_request_task
    def request_vcard(self, jid: JID | None = None):
        _task = yield

        response = yield _make_vcard_request(jid)

        if response.isError():
            raise StanzaError(response)

        vcard_node = _get_vcard_node(response)
        yield VCard.from_node(vcard_node)

    @iq_request_task
    def set_vcard(self, vcard: VCard, jid: JID | None = None):
        _task = yield

        response = yield _make_vcard_publish(jid, vcard)
        yield process_response(response)


def _make_vcard_request(jid: JID) -> Iq:
    iq = Iq(typ="get", to=jid)
    iq.addChild("vCard", namespace=Namespace.VCARD)
    return iq


def _get_vcard_node(response: Node) -> Node:
    vcard_node = response.getTag("vCard", namespace=Namespace.VCARD)
    if vcard_node is None:
        raise MalformedStanzaError("vCard node missing", response)
    return vcard_node


def _make_vcard_publish(jid: JID, vcard: VCard) -> Iq:
    iq = Iq(typ="set", to=jid)
    iq.addChild(node=vcard.to_node())
    return iq


@dataclass
class VCard:
    data: dict = field(default_factory=dict)

    @classmethod
    def from_node(cls, node: Node) -> VCard:
        dict_ = {}
        for info in node.getChildren():
            name = info.getName()
            if name in ("ADR", "TEL", "EMAIL"):
                dict_.setdefault(name, [])
                entry = {}
                for child in info.getChildren():
                    entry[child.getName()] = child.getData()
                dict_[name].append(entry)
            elif info.getChildren() == []:
                dict_[name] = info.getData()
            else:
                dict_[name] = {}
                for child in info.getChildren():
                    dict_[name][child.getName()] = child.getData()

        return cls(data=dict_)

    def to_node(self) -> Node:
        vcard = Node(tag="vCard", attrs={"xmlns": Namespace.VCARD})
        for i in self.data:
            if i == "jid":
                continue
            if isinstance(self.data[i], dict):
                child = vcard.addChild(i)
                for j in self.data[i]:
                    child.addChild(j).setData(self.data[i][j])
            elif isinstance(self.data[i], list):
                for j in self.data[i]:
                    child = vcard.addChild(i)
                    for k in j:
                        child.addChild(k).setData(j[k])
            else:
                vcard.addChild(i).setData(self.data[i])
        return vcard

    def set_avatar(self, avatar: bytes, type_: str | None = None) -> None:
        encoded_avatar = b64encode(avatar)
        if "PHOTO" not in self.data:
            self.data["PHOTO"] = {}

        self.data["PHOTO"]["BINVAL"] = encoded_avatar

        if type_ is not None:
            self.data["PHOTO"]["TYPE"] = type_

    def get_avatar(self) -> tuple[bytes | None, str | None]:
        try:
            avatar = self.data["PHOTO"]["BINVAL"]
        except Exception:
            return None, None

        if not avatar:
            return None, None

        encoded_avatar = b64decode(avatar)
        avatar_sha = hashlib.sha1(encoded_avatar).hexdigest()
        return encoded_avatar, avatar_sha