annotate libervia/backend/plugins/plugin_xep_0163.py @ 4351:6a0a081485b8

plugin autocrypt: Autocrypt protocol implementation: Implementation of autocrypt: `autocrypt` header is checked, and if present and no public key is known for the peer, the key is imported. `autocrypt` header is also added to outgoing message (only if an email gateway is detected). For the moment, the JID is use as identifier, but the real email used by gateway should be used in the future. rel 456
author Goffi <goffi@goffi.org>
date Fri, 28 Feb 2025 09:23:35 +0100
parents 554a87ae17a6
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2807
diff changeset
1 #!/usr/bin/env python3
3137
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
2
283
68cd30d982a5 core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff changeset
3
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
4 # SAT plugin for Personal Eventing Protocol (xep-0163)
3479
be6d91572633 date update
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
283
68cd30d982a5 core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff changeset
6
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
7 # This program is free software: you can redistribute it and/or modify
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
10 # (at your option) any later version.
283
68cd30d982a5 core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff changeset
11
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
12 # This program is distributed in the hope that it will be useful,
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
15 # GNU Affero General Public License for more details.
283
68cd30d982a5 core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff changeset
16
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
283
68cd30d982a5 core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff changeset
19
3823
5d72dc52ee4a plugin XEP-0163: allow unset `event_type` in `addPEPEvent`, check conflict + type hints
Goffi <goffi@goffi.org>
parents: 3726
diff changeset
20 from typing import Optional, Callable
4327
554a87ae17a6 plugin XEP-0048, XEP-0402; CLI (bookmarks): implement XEP-0402 (PEP Native Bookmarks):
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
21
554a87ae17a6 plugin XEP-0048, XEP-0402; CLI (bookmarks): implement XEP-0402 (PEP Native Bookmarks):
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
22 from twisted.python.failure import Failure
554a87ae17a6 plugin XEP-0048, XEP-0402; CLI (bookmarks): implement XEP-0402 (PEP Native Bookmarks):
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
23 from libervia.backend.core.core_types import SatXMPPEntity
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
24 from libervia.backend.core.i18n import _
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
25 from libervia.backend.core import exceptions
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
26 from libervia.backend.core.constants import Const as C
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
27 from libervia.backend.core.log import getLogger
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
28
286
3b382fa0ac28 plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents: 283
diff changeset
29 from twisted.words.xish import domish
283
68cd30d982a5 core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff changeset
30
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
31 from wokkel import disco, pubsub
283
68cd30d982a5 core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff changeset
32 from wokkel.formats import Mood
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
33 from libervia.backend.tools.common import data_format
4327
554a87ae17a6 plugin XEP-0048, XEP-0402; CLI (bookmarks): implement XEP-0402 (PEP Native Bookmarks):
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
34 from libervia.backend.tools import utils
283
68cd30d982a5 core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff changeset
35
3823
5d72dc52ee4a plugin XEP-0163: allow unset `event_type` in `addPEPEvent`, check conflict + type hints
Goffi <goffi@goffi.org>
parents: 3726
diff changeset
36
5d72dc52ee4a plugin XEP-0163: allow unset `event_type` in `addPEPEvent`, check conflict + type hints
Goffi <goffi@goffi.org>
parents: 3726
diff changeset
37 log = getLogger(__name__)
5d72dc52ee4a plugin XEP-0163: allow unset `event_type` in `addPEPEvent`, check conflict + type hints
Goffi <goffi@goffi.org>
parents: 3726
diff changeset
38
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
39 NS_USER_MOOD = "http://jabber.org/protocol/mood"
283
68cd30d982a5 core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff changeset
40
68cd30d982a5 core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff changeset
41 PLUGIN_INFO = {
2145
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
42 C.PI_NAME: "Personal Eventing Protocol Plugin",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
43 C.PI_IMPORT_NAME: "XEP-0163",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
44 C.PI_TYPE: "XEP",
3726
33d75cd3c371 plugin XEP-0060, XEP-0163, XEP-0277, text syntaxes: make those plugins usable with components
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
45 C.PI_MODES: C.PLUG_MODE_BOTH,
2145
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
46 C.PI_PROTOCOLS: ["XEP-0163", "XEP-0107"],
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
47 C.PI_DEPENDENCIES: ["XEP-0060"],
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
48 C.PI_MAIN: "XEP_0163",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
49 C.PI_HANDLER: "no",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
50 C.PI_DESCRIPTION: _("""Implementation of Personal Eventing Protocol"""),
283
68cd30d982a5 core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff changeset
51 }
68cd30d982a5 core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff changeset
52
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
53
588
beaf6bec2fcd Remove every old-style class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
54 class XEP_0163(object):
283
68cd30d982a5 core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff changeset
55 def __init__(self, host):
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 916
diff changeset
56 log.info(_("PEP plugin initialization"))
283
68cd30d982a5 core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff changeset
57 self.host = host
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
58 self.pep_events = set()
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
59 self.pep_out_cb = {}
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3823
diff changeset
60 host.trigger.add("PubSub Disco Info", self.diso_info_trigger)
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3823
diff changeset
61 host.bridge.add_method(
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3823
diff changeset
62 "pep_send",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
63 ".plugin",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
64 in_sign="sa{ss}s",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
65 out_sign="",
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3823
diff changeset
66 method=self.pep_send,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2807
diff changeset
67 async_=True,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
68 ) # args: type(MOOD, TUNE, etc), data, profile_key;
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3823
diff changeset
69 self.add_pep_event("MOOD", NS_USER_MOOD, self.user_mood_cb, self.send_mood)
283
68cd30d982a5 core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff changeset
70
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3823
diff changeset
71 def diso_info_trigger(self, disco_info, profile):
283
68cd30d982a5 core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff changeset
72 """Add info from managed PEP
1459
4c4f88d7b156 plugins xep-0060, xep-0163, xep-0277, groupblog: bloging improvments (huge patch, sorry):
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
73
283
68cd30d982a5 core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff changeset
74 @param disco_info: list of disco feature as returned by PubSub,
68cd30d982a5 core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff changeset
75 will be filled with PEP features
1459
4c4f88d7b156 plugins xep-0060, xep-0163, xep-0277, groupblog: bloging improvments (huge patch, sorry):
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
76 @param profile: profile we are handling
4c4f88d7b156 plugins xep-0060, xep-0163, xep-0277, groupblog: bloging improvments (huge patch, sorry):
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
77 """
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2807
diff changeset
78 disco_info.extend(list(map(disco.DiscoFeature, self.pep_events)))
283
68cd30d982a5 core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff changeset
79 return True
68cd30d982a5 core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff changeset
80
4327
554a87ae17a6 plugin XEP-0048, XEP-0402; CLI (bookmarks): implement XEP-0402 (PEP Native Bookmarks):
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
81 def log_error(self, failure_: Failure) -> None:
554a87ae17a6 plugin XEP-0048, XEP-0402; CLI (bookmarks): implement XEP-0402 (PEP Native Bookmarks):
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
82 log.error(f"Failed to call callback: {failure_}")
554a87ae17a6 plugin XEP-0048, XEP-0402; CLI (bookmarks): implement XEP-0402 (PEP Native Bookmarks):
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
83
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3823
diff changeset
84 def add_pep_event(
3823
5d72dc52ee4a plugin XEP-0163: allow unset `event_type` in `addPEPEvent`, check conflict + type hints
Goffi <goffi@goffi.org>
parents: 3726
diff changeset
85 self,
4327
554a87ae17a6 plugin XEP-0048, XEP-0402; CLI (bookmarks): implement XEP-0402 (PEP Native Bookmarks):
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
86 event_type: str | None,
3823
5d72dc52ee4a plugin XEP-0163: allow unset `event_type` in `addPEPEvent`, check conflict + type hints
Goffi <goffi@goffi.org>
parents: 3726
diff changeset
87 node: str,
4327
554a87ae17a6 plugin XEP-0048, XEP-0402; CLI (bookmarks): implement XEP-0402 (PEP Native Bookmarks):
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
88 in_callback: Callable[[pubsub.ItemsEvent, str], None],
554a87ae17a6 plugin XEP-0048, XEP-0402; CLI (bookmarks): implement XEP-0402 (PEP Native Bookmarks):
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
89 out_callback: Callable | None = None,
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
90 notify: bool = True,
3823
5d72dc52ee4a plugin XEP-0163: allow unset `event_type` in `addPEPEvent`, check conflict + type hints
Goffi <goffi@goffi.org>
parents: 3726
diff changeset
91 ) -> None:
283
68cd30d982a5 core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff changeset
92 """Add a Personal Eventing Protocol event manager
1459
4c4f88d7b156 plugins xep-0060, xep-0163, xep-0277, groupblog: bloging improvments (huge patch, sorry):
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
93
3823
5d72dc52ee4a plugin XEP-0163: allow unset `event_type` in `addPEPEvent`, check conflict + type hints
Goffi <goffi@goffi.org>
parents: 3726
diff changeset
94 @param event_type: type of the event (stored uppercase),
5d72dc52ee4a plugin XEP-0163: allow unset `event_type` in `addPEPEvent`, check conflict + type hints
Goffi <goffi@goffi.org>
parents: 3726
diff changeset
95 only used when out_callback is set.
5d72dc52ee4a plugin XEP-0163: allow unset `event_type` in `addPEPEvent`, check conflict + type hints
Goffi <goffi@goffi.org>
parents: 3726
diff changeset
96 Can be MOOD, TUNE, etc.
5d72dc52ee4a plugin XEP-0163: allow unset `event_type` in `addPEPEvent`, check conflict + type hints
Goffi <goffi@goffi.org>
parents: 3726
diff changeset
97 @param node: namespace of the node (e.g. http://jabber.org/protocol/mood
2643
189e38fb11ff core: style improvments (90 chars limit)
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
98 for User Mood)
3823
5d72dc52ee4a plugin XEP-0163: allow unset `event_type` in `addPEPEvent`, check conflict + type hints
Goffi <goffi@goffi.org>
parents: 3726
diff changeset
99 @param in_callback: method to call when this event occur
4327
554a87ae17a6 plugin XEP-0048, XEP-0402; CLI (bookmarks): implement XEP-0402 (PEP Native Bookmarks):
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
100 the callable will be called with (itemsEvent, profile) as arguments.
554a87ae17a6 plugin XEP-0048, XEP-0402; CLI (bookmarks): implement XEP-0402 (PEP Native Bookmarks):
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
101 Can be blocking or async.
3823
5d72dc52ee4a plugin XEP-0163: allow unset `event_type` in `addPEPEvent`, check conflict + type hints
Goffi <goffi@goffi.org>
parents: 3726
diff changeset
102 @param out_callback: method to call when we want to publish this
2643
189e38fb11ff core: style improvments (90 chars limit)
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
103 event (must return a deferred)
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3823
diff changeset
104 the callable will be called when send_pep_event is called
3823
5d72dc52ee4a plugin XEP-0163: allow unset `event_type` in `addPEPEvent`, check conflict + type hints
Goffi <goffi@goffi.org>
parents: 3726
diff changeset
105 @param notify: add autosubscribe (+notify) if True
1459
4c4f88d7b156 plugins xep-0060, xep-0163, xep-0277, groupblog: bloging improvments (huge patch, sorry):
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
106 """
3823
5d72dc52ee4a plugin XEP-0163: allow unset `event_type` in `addPEPEvent`, check conflict + type hints
Goffi <goffi@goffi.org>
parents: 3726
diff changeset
107 if event_type and out_callback:
5d72dc52ee4a plugin XEP-0163: allow unset `event_type` in `addPEPEvent`, check conflict + type hints
Goffi <goffi@goffi.org>
parents: 3726
diff changeset
108 event_type = event_type.upper()
5d72dc52ee4a plugin XEP-0163: allow unset `event_type` in `addPEPEvent`, check conflict + type hints
Goffi <goffi@goffi.org>
parents: 3726
diff changeset
109 if event_type in self.pep_out_cb:
5d72dc52ee4a plugin XEP-0163: allow unset `event_type` in `addPEPEvent`, check conflict + type hints
Goffi <goffi@goffi.org>
parents: 3726
diff changeset
110 raise exceptions.ConflictError(
5d72dc52ee4a plugin XEP-0163: allow unset `event_type` in `addPEPEvent`, check conflict + type hints
Goffi <goffi@goffi.org>
parents: 3726
diff changeset
111 f"event_type {event_type!r} already exists"
5d72dc52ee4a plugin XEP-0163: allow unset `event_type` in `addPEPEvent`, check conflict + type hints
Goffi <goffi@goffi.org>
parents: 3726
diff changeset
112 )
311
0aa6ca6cdbdd plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents: 290
diff changeset
113 self.pep_out_cb[event_type] = out_callback
1459
4c4f88d7b156 plugins xep-0060, xep-0163, xep-0277, groupblog: bloging improvments (huge patch, sorry):
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
114 self.pep_events.add(node)
290
59a82af700e2 plugin xep 163: added generic sendPEPEvent method
Goffi <goffi@goffi.org>
parents: 286
diff changeset
115 if notify:
1459
4c4f88d7b156 plugins xep-0060, xep-0163, xep-0277, groupblog: bloging improvments (huge patch, sorry):
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
116 self.pep_events.add(node + "+notify")
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
117
4327
554a87ae17a6 plugin XEP-0048, XEP-0402; CLI (bookmarks): implement XEP-0402 (PEP Native Bookmarks):
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
118 def filter_pep_event(client: SatXMPPEntity, itemsEvent: pubsub.ItemsEvent):
2660
2901e22d1766 plugin XEP-0163: fixed bad PEP filtering
Goffi <goffi@goffi.org>
parents: 2643
diff changeset
119 """Ignore messages which are not coming from PEP (i.e. a bare jid)
290
59a82af700e2 plugin xep 163: added generic sendPEPEvent method
Goffi <goffi@goffi.org>
parents: 286
diff changeset
120
4327
554a87ae17a6 plugin XEP-0048, XEP-0402; CLI (bookmarks): implement XEP-0402 (PEP Native Bookmarks):
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
121 @param itemsEvent: pubsub event
1459
4c4f88d7b156 plugins xep-0060, xep-0163, xep-0277, groupblog: bloging improvments (huge patch, sorry):
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
122 """
2660
2901e22d1766 plugin XEP-0163: fixed bad PEP filtering
Goffi <goffi@goffi.org>
parents: 2643
diff changeset
123 if not itemsEvent.sender.user or itemsEvent.sender.resource:
4327
554a87ae17a6 plugin XEP-0048, XEP-0402; CLI (bookmarks): implement XEP-0402 (PEP Native Bookmarks):
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
124 log.warning(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
125 "ignoring non PEP event from {} (profile={})".format(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
126 itemsEvent.sender.full(), client.profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
127 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
128 )
1459
4c4f88d7b156 plugins xep-0060, xep-0163, xep-0277, groupblog: bloging improvments (huge patch, sorry):
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
129 return
4327
554a87ae17a6 plugin XEP-0048, XEP-0402; CLI (bookmarks): implement XEP-0402 (PEP Native Bookmarks):
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
130 d = utils.as_deferred(in_callback, itemsEvent, client.profile)
554a87ae17a6 plugin XEP-0048, XEP-0402; CLI (bookmarks): implement XEP-0402 (PEP Native Bookmarks):
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
131 d.addErrback(self.log_error)
1459
4c4f88d7b156 plugins xep-0060, xep-0163, xep-0277, groupblog: bloging improvments (huge patch, sorry):
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
132
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3823
diff changeset
133 self.host.plugins["XEP-0060"].add_managed_node(node, items_cb=filter_pep_event)
1459
4c4f88d7b156 plugins xep-0060, xep-0163, xep-0277, groupblog: bloging improvments (huge patch, sorry):
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
134
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3823
diff changeset
135 def send_pep_event(self, node, data, profile):
290
59a82af700e2 plugin xep 163: added generic sendPEPEvent method
Goffi <goffi@goffi.org>
parents: 286
diff changeset
136 """Publish the event data
1459
4c4f88d7b156 plugins xep-0060, xep-0163, xep-0277, groupblog: bloging improvments (huge patch, sorry):
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
137
4c4f88d7b156 plugins xep-0060, xep-0163, xep-0277, groupblog: bloging improvments (huge patch, sorry):
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
138 @param node(unicode): node namespace
290
59a82af700e2 plugin xep 163: added generic sendPEPEvent method
Goffi <goffi@goffi.org>
parents: 286
diff changeset
139 @param data: domish.Element to use as payload
1459
4c4f88d7b156 plugins xep-0060, xep-0163, xep-0277, groupblog: bloging improvments (huge patch, sorry):
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
140 @param profile: profile which send the data
4c4f88d7b156 plugins xep-0060, xep-0163, xep-0277, groupblog: bloging improvments (huge patch, sorry):
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
141 """
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3823
diff changeset
142 client = self.host.get_client(profile)
290
59a82af700e2 plugin xep 163: added generic sendPEPEvent method
Goffi <goffi@goffi.org>
parents: 286
diff changeset
143 item = pubsub.Item(payload=data)
2272
b5befe7722d3 plugin XEP-0060: added sendItem and psItemSend bridge method as a way to send directly raw XML for an item + use client instead of profile_key in publish + renamed psItemGet to psItemsGet
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
144 return self.host.plugins["XEP-0060"].publish(client, None, node, [item])
283
68cd30d982a5 core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff changeset
145
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3823
diff changeset
146 def pep_send(self, event_type, data, profile_key=C.PROF_KEY_NONE):
286
3b382fa0ac28 plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents: 283
diff changeset
147 """Send personal event after checking the data is alright
1459
4c4f88d7b156 plugins xep-0060, xep-0163, xep-0277, groupblog: bloging improvments (huge patch, sorry):
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
148
2643
189e38fb11ff core: style improvments (90 chars limit)
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
149 @param event_type: type of event (eg: MOOD, TUNE),
189e38fb11ff core: style improvments (90 chars limit)
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
150 must be in self.pep_out_cb.keys()
286
3b382fa0ac28 plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents: 283
diff changeset
151 @param data: dict of {string:string} of event_type dependant data
3b382fa0ac28 plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents: 283
diff changeset
152 @param profile_key: profile who send the event
704
3c304929af74 plugin XEP-0277, group blogs: proper asynchronous methods for sending blogs.
Goffi <goffi@goffi.org>
parents: 660
diff changeset
153 """
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3823
diff changeset
154 profile = self.host.memory.get_profile_name(profile_key)
286
3b382fa0ac28 plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents: 283
diff changeset
155 if not profile:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
156 log.error(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2807
diff changeset
157 _("Trying to send personal event with an unknown profile key [%s]")
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
158 % profile_key
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
159 )
704
3c304929af74 plugin XEP-0277, group blogs: proper asynchronous methods for sending blogs.
Goffi <goffi@goffi.org>
parents: 660
diff changeset
160 raise exceptions.ProfileUnknownError
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2807
diff changeset
161 if not event_type in list(self.pep_out_cb.keys()):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
162 log.error(_("Trying to send personal event for an unknown type"))
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
163 raise exceptions.DataError("Type unknown")
290
59a82af700e2 plugin xep 163: added generic sendPEPEvent method
Goffi <goffi@goffi.org>
parents: 286
diff changeset
164 return self.pep_out_cb[event_type](data, profile)
286
3b382fa0ac28 plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents: 283
diff changeset
165
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3823
diff changeset
166 def user_mood_cb(self, itemsEvent, profile):
410
ef9a4453959b plugin XEP-0163: user event mood with no item fix
Goffi <goffi@goffi.org>
parents: 372
diff changeset
167 if not itemsEvent.items:
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 916
diff changeset
168 log.debug(_("No item found"))
410
ef9a4453959b plugin XEP-0163: user event mood with no item fix
Goffi <goffi@goffi.org>
parents: 372
diff changeset
169 return
283
68cd30d982a5 core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff changeset
170 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
171 mood_elt = [
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
172 child for child in itemsEvent.items[0].elements() if child.name == "mood"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
173 ][0]
660
69a8bfd266a5 core, plugins: fixed bad use of children instead of elements() for domish.Element instances.
Goffi <goffi@goffi.org>
parents: 609
diff changeset
174 except IndexError:
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 916
diff changeset
175 log.error(_("Can't find mood element in mood event"))
283
68cd30d982a5 core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff changeset
176 return
660
69a8bfd266a5 core, plugins: fixed bad use of children instead of elements() for domish.Element instances.
Goffi <goffi@goffi.org>
parents: 609
diff changeset
177 mood = Mood.fromXml(mood_elt)
69a8bfd266a5 core, plugins: fixed bad use of children instead of elements() for domish.Element instances.
Goffi <goffi@goffi.org>
parents: 609
diff changeset
178 if not mood:
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 916
diff changeset
179 log.debug(_("No mood found"))
283
68cd30d982a5 core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff changeset
180 return
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3823
diff changeset
181 self.host.bridge.ps_event(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
182 C.PS_PEP,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
183 itemsEvent.sender.full(),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
184 itemsEvent.nodeIdentifier,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
185 "MOOD",
2807
0b7ce5daee9b plugin XEP-0277: blog items data are now entirely serialised before going to bridge:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
186 data_format.serialise({"mood": mood.value or "", "text": mood.text or ""}),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
187 profile,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
188 )
286
3b382fa0ac28 plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents: 283
diff changeset
189
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3823
diff changeset
190 def send_mood(self, data, profile):
286
3b382fa0ac28 plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents: 283
diff changeset
191 """Send XEP-0107's User Mood
1459
4c4f88d7b156 plugins xep-0060, xep-0163, xep-0277, groupblog: bloging improvments (huge patch, sorry):
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
192
286
3b382fa0ac28 plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents: 283
diff changeset
193 @param data: must include mood and text
3b382fa0ac28 plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents: 283
diff changeset
194 @param profile: profile which send the mood"""
3b382fa0ac28 plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents: 283
diff changeset
195 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
196 value = data["mood"].lower()
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
197 text = data["text"] if "text" in data else ""
286
3b382fa0ac28 plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents: 283
diff changeset
198 except KeyError:
704
3c304929af74 plugin XEP-0277, group blogs: proper asynchronous methods for sending blogs.
Goffi <goffi@goffi.org>
parents: 660
diff changeset
199 raise exceptions.DataError("Mood data must contain at least 'mood' key")
660
69a8bfd266a5 core, plugins: fixed bad use of children instead of elements() for domish.Element instances.
Goffi <goffi@goffi.org>
parents: 609
diff changeset
200 mood = UserMood(value, text)
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3823
diff changeset
201 return self.send_pep_event(NS_USER_MOOD, mood, profile)
286
3b382fa0ac28 plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents: 283
diff changeset
202
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
203
286
3b382fa0ac28 plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents: 283
diff changeset
204 class UserMood(Mood, domish.Element):
3b382fa0ac28 plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents: 283
diff changeset
205 """Improved wokkel Mood which is also a domish.Element"""
3b382fa0ac28 plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents: 283
diff changeset
206
3b382fa0ac28 plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents: 283
diff changeset
207 def __init__(self, value, text=None):
3b382fa0ac28 plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents: 283
diff changeset
208 Mood.__init__(self, value, text)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
209 domish.Element.__init__(self, (NS_USER_MOOD, "mood"))
286
3b382fa0ac28 plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents: 283
diff changeset
210 self.addElement(value)
3b382fa0ac28 plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents: 283
diff changeset
211 if text:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
212 self.addElement("text", content=text)