Mercurial > libervia-backend
annotate libervia/backend/plugins/plugin_xep_0131.py @ 4314:6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
- SHIM is now supported and put in `msg_data["extra"]["headers"]`.
- `Keywords` are converted from and to list of string in `msg_data["extra"]["keywords"]`
field (if present in headers on message sending, values are merged).
- Python minimal version upgraded to 3.11 due to use of `StrEnum`.
rel 451
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 28 Sep 2024 15:56:04 +0200 |
parents | |
children |
rev | line source |
---|---|
4314
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/env python3 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 # Libervia plugin Stanza Headers and Internet Metadata (XEP-0131) |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 # Copyright (C) 2009-2024 Jérôme Poisson (goffi@goffi.org) |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 # This program is free software: you can redistribute it and/or modify |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # it under the terms of the GNU Affero General Public License as published by |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # the Free Software Foundation, either version 3 of the License, or |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 # (at your option) any later version. |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 # This program is distributed in the hope that it will be useful, |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # GNU Affero General Public License for more details. |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 # You should have received a copy of the GNU Affero General Public License |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 from email.utils import quote, unquote |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 from enum import StrEnum, auto |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 import re |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 from typing import Iterator, List, Literal, Optional, Self |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 from pydantic import BaseModel, ConfigDict, Field, RootModel |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 from twisted.internet import defer |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 from twisted.words.protocols.jabber.xmlstream import XMPPHandler |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 from twisted.words.xish import domish |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 from wokkel import disco |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 from zope.interface import implementer |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 from libervia.backend.core import exceptions |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 from libervia.backend.core.constants import Const as C |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 from libervia.backend.core.core_types import SatXMPPEntity |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 from libervia.backend.core.i18n import _ |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 from libervia.backend.core.log import getLogger |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 from libervia.backend.models.core import MessageData |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 log = getLogger(__name__) |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 PLUGIN_INFO = { |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 C.PI_NAME: "Stanza Headers and Internet Metadata Plugin", |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 C.PI_IMPORT_NAME: "XEP-0131", |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 C.PI_TYPE: "XEP", |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 C.PI_MODES: C.PLUG_MODE_BOTH, |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 C.PI_PROTOCOLS: ["XEP-0131"], |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 C.PI_DEPENDENCIES: [], |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 C.PI_MAIN: "XEP_0131", |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 C.PI_HANDLER: "yes", |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 C.PI_DESCRIPTION: _( |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 "Enables the inclusion of non-addressing header information in XMPP stanzas." |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 ), |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 } |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 NS_SHIM = "http://jabber.org/protocol/shim" |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 # Regex to match quoted and non-quoted values. |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 RE_QUOTED_VALUES = re.compile( |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 r""" |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 # Match quoted phrases |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 " |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 (?: |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 # Match any escaped character |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 \\. |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 # Match any character that is not a double quote or a backslash |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 |[^"\\] |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 )* |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 " |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 | |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 # Match non-quoted phrases |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 (?:[^,]+) |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
71 """, |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
72 re.VERBOSE, |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
73 ) |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
76 class Urgency(StrEnum): |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
77 low = auto() |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
78 medium = auto() |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
79 high = auto() |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
80 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 class Priority(StrEnum): |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
83 non_urgent = "non-urgent" |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
84 normal = auto() |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
85 urgent = auto() |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
86 emergency = auto() |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
87 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
88 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
89 class HeadersData(BaseModel): |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
90 model_config = ConfigDict(extra="allow") |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
91 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
92 __pydantic_extra__: dict[str, str] = Field(init=False) # type: ignore |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
93 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
94 keywords: str | None = None |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
95 urgency: Urgency | None = None |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
96 priority: Priority | None = None |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
97 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
98 def items(self): |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
99 return self.__pydantic_extra__.items() |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
100 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
101 def to_element(self) -> domish.Element: |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
102 """Build the <headers> element from this instance's data.""" |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
103 headers_elt = domish.Element((NS_SHIM, "headers")) |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
104 header_names = list(self.model_fields.keys()) + list( |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
105 self.__pydantic_extra__.keys() |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
106 ) |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
107 for name in header_names: |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
108 value = getattr(self, name) |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
109 if value is None: |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
110 continue |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
111 header_elt = headers_elt.addElement("header") |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
112 header_elt["name"] = name |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
113 header_elt.addContent(value) |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
114 return headers_elt |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
115 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
116 @classmethod |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
117 def from_element(cls, headers_elt: domish.Element) -> Self: |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
118 """Create a HeadersData instance from a <headers> element.""" |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
119 if headers_elt.uri != NS_SHIM or headers_elt.name != "headers": |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
120 child_headers_elt = next(headers_elt.elements(NS_SHIM, "headers"), None) |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
121 if child_headers_elt is None: |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
122 raise exceptions.NotFound("<headers> element not found") |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
123 else: |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
124 headers_elt = child_headers_elt |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
125 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
126 headers = {} |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
127 for header_elt in headers_elt.elements(NS_SHIM, "header"): |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
128 name = header_elt.getAttribute("name") |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
129 value = str(header_elt) |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
130 headers[name] = value |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
131 return cls(**headers) |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
132 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
133 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
134 class Keywords(RootModel): |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
135 root: list[str] |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
136 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
137 def __iter__(self) -> Iterator[str]: # type: ignore |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
138 return iter(self.root) |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
139 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
140 def __getitem__(self, item) -> str: |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
141 return self.root[item] |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
142 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
143 def __len__(self) -> int: |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
144 return len(self.root) |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
145 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
146 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
147 class XEP_0131: |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
148 """Implementation for XEP-0131""" |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
149 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
150 def __init__(self, host): |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
151 log.info(_("Stanza Headers and Internet Metadata plugin initialization")) |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
152 self.host = host |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
153 host.register_namespace("shim", NS_SHIM) |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
154 host.trigger.add("sendMessage", self.send_message_trigger) |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
155 host.trigger.add("sendMessageComponent", self.send_message_trigger) |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
156 host.trigger.add("message_received", self.message_received_trigger) |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
157 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
158 def quote_value(self, value: str) -> str: |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
159 """Quote a value if it contain special characters |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
160 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
161 @param value: Value to quote if necessary. |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
162 @return: Quoted value. |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
163 """ |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
164 if any(c in value for c in r" ,\""): |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
165 value = f'"{quote(value)}"' |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
166 return value |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
167 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
168 def unquote_values(self, raw_header: str) -> list[str]: |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
169 """Unquote raw list of values header. |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
170 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
171 This is raw header for potentially quoted values separated by commas, like in the |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
172 "keywords" header. |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
173 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
174 @param raw_keywords_header: Raw Keywords header. |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
175 @return: A list of unquoted strings. |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
176 """ |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
177 unquoted_values = [] |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
178 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
179 for match in RE_QUOTED_VALUES.finditer(raw_header): |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
180 value = match.group(0).strip() |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
181 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
182 # Unquote the keyword if needed. |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
183 if value.startswith('"') and value.endswith('"'): |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
184 value = unquote(value) |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
185 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
186 value = value.strip() |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
187 if value: |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
188 unquoted_values.append(value) |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
189 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
190 return unquoted_values |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
191 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
192 def move_keywords_to_headers(self, extra: dict) -> None: |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
193 """Check if keywords are present in extra, and move them to headers. |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
194 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
195 The list of keywords will be converted to a header value and set in the right |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
196 location. |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
197 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
198 @param extra: MessageData's ``extra`` field. Will be modified in place by |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
199 creating/updating the ``headers`` field. |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
200 """ |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
201 # Keywords can be in a list of strings in extra's "keywords" field. |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
202 if "keywords" in extra: |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
203 keywords = Keywords(extra["keywords"]) |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
204 if keywords: |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
205 headers = extra.setdefault("headers", {}) |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
206 quoted_kw = ",".join(self.quote_value(kw) for kw in keywords) |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
207 existing_kw = headers.get("keywords") |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
208 if existing_kw: |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
209 # We have also keywords in headers, we merge both. |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
210 quoted_kw = f"{existing_kw},{quoted_kw}" |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
211 headers["keywords"] = quoted_kw |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
212 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
213 def send_message_trigger( |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
214 self, client, mess_data, pre_xml_treatments, post_xml_treatments |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
215 ) -> Literal[True]: |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
216 """Process the XEP-0131 related data to be sent""" |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
217 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
218 def add_headers(mess_data: MessageData) -> MessageData: |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
219 extra = mess_data["extra"] |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
220 self.move_keywords_to_headers(extra) |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
221 # Now we parse headers, if any. |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
222 if "headers" in extra: |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
223 headers_data = HeadersData(**extra["headers"]) |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
224 message_elt = mess_data["xml"] |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
225 message_elt.addChild(headers_data.to_element()) |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
226 return mess_data |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
227 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
228 post_xml_treatments.addCallback(add_headers) |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
229 return True |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
230 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
231 def message_received_trigger( |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
232 self, |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
233 client: SatXMPPEntity, |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
234 message_elt: domish.Element, |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
235 post_treat: defer.Deferred, |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
236 ) -> Literal[True]: |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
237 """Parse headers information and add them to message data.""" |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
238 try: |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
239 headers = HeadersData.from_element(message_elt) |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
240 except exceptions.NotFound: |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
241 pass |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
242 else: |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
243 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
244 def post_treat_addr(mess_data: MessageData): |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
245 """Add the headers metadata to the message data""" |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
246 if headers.keywords: |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
247 # We move keywords to a list of string in extra's "keywords" field. |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
248 mess_data["extra"]["keywords"] = self.unquote_values(headers.keywords) |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
249 headers.keywords = None |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
250 mess_data["extra"]["headers"] = headers.model_dump( |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
251 mode="json", exclude_none=True |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
252 ) |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
253 return mess_data |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
254 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
255 post_treat.addCallback(post_treat_addr) |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
256 return True |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
257 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
258 def get_handler(self, client): |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
259 return XEP_0131_handler() |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
260 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
261 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
262 @implementer(disco.IDisco) |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
263 class XEP_0131_handler(XMPPHandler): |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
264 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
265 def getDiscoInfo( |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
266 self, requestor, target, nodeIdentifier: Optional[str] = "" |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
267 ) -> List[disco.DiscoFeature]: |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
268 return [disco.DiscoFeature(NS_SHIM)] |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
269 |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
270 def getDiscoItems( |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
271 self, requestor, target, nodeIdentifier: Optional[str] = "" |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
272 ) -> List[disco.DiscoItem]: |
6a70fcd93a7a
plugin XEP-0131: Stanza Headers and Internet Metadata implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
273 return [] |