annotate libervia/backend/plugins/plugin_xep_0163.py @ 4212:5f2d496c633f

core: get rid of `pickle`: Use of `pickle` to serialise data was a technical legacy that was causing trouble to store in database, to update (if a class was serialised, a change could break update), and to security (pickle can lead to code execution). This patch remove all use of Pickle in favour in JSON, notably: - for caching data, a Pydantic model is now used instead - for SQLAlchemy model, the LegacyPickle is replaced by JSON serialisation - in XEP-0373 a class `PublicKeyMetadata` was serialised. New method `from_dict` and `to_dict` method have been implemented to do serialisation. - new methods to (de)serialise data can now be specified with Identity data types. It is notably used to (de)serialise `path` of avatars. A migration script has been created to convert data (for upgrade or downgrade), with special care for XEP-0373 case. Depending of size of database, this migration script can be long to run. rel 443
author Goffi <goffi@goffi.org>
date Fri, 23 Feb 2024 13:31:04 +0100
parents 4b842c1fb686
children 0d7bb4df2343
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
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
21 from libervia.backend.core.i18n import _
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
22 from libervia.backend.core import exceptions
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
23 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
24 from libervia.backend.core.log import getLogger
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
25
286
3b382fa0ac28 plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents: 283
diff changeset
26 from twisted.words.xish import domish
283
68cd30d982a5 core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff changeset
27
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
28 from wokkel import disco, pubsub
283
68cd30d982a5 core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff changeset
29 from wokkel.formats import Mood
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
30 from libervia.backend.tools.common import data_format
283
68cd30d982a5 core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff changeset
31
3823
5d72dc52ee4a plugin XEP-0163: allow unset `event_type` in `addPEPEvent`, check conflict + type hints
Goffi <goffi@goffi.org>
parents: 3726
diff changeset
32
5d72dc52ee4a plugin XEP-0163: allow unset `event_type` in `addPEPEvent`, check conflict + type hints
Goffi <goffi@goffi.org>
parents: 3726
diff changeset
33 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
34
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
35 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
36
68cd30d982a5 core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff changeset
37 PLUGIN_INFO = {
2145
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
38 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
39 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
40 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
41 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
42 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
43 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
44 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
45 C.PI_HANDLER: "no",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
46 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
47 }
68cd30d982a5 core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff changeset
48
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
49
588
beaf6bec2fcd Remove every old-style class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
50 class XEP_0163(object):
283
68cd30d982a5 core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff changeset
51 def __init__(self, host):
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 916
diff changeset
52 log.info(_("PEP plugin initialization"))
283
68cd30d982a5 core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff changeset
53 self.host = host
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
54 self.pep_events = set()
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
55 self.pep_out_cb = {}
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3823
diff changeset
56 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
57 host.bridge.add_method(
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3823
diff changeset
58 "pep_send",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
59 ".plugin",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
60 in_sign="sa{ss}s",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
61 out_sign="",
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3823
diff changeset
62 method=self.pep_send,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2807
diff changeset
63 async_=True,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
64 ) # 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
65 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
66
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3823
diff changeset
67 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
68 """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
69
283
68cd30d982a5 core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff changeset
70 @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
71 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
72 @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
73 """
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2807
diff changeset
74 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
75 return True
68cd30d982a5 core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff changeset
76
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3823
diff changeset
77 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
78 self,
5d72dc52ee4a plugin XEP-0163: allow unset `event_type` in `addPEPEvent`, check conflict + type hints
Goffi <goffi@goffi.org>
parents: 3726
diff changeset
79 event_type: Optional[str],
5d72dc52ee4a plugin XEP-0163: allow unset `event_type` in `addPEPEvent`, check conflict + type hints
Goffi <goffi@goffi.org>
parents: 3726
diff changeset
80 node: str,
5d72dc52ee4a plugin XEP-0163: allow unset `event_type` in `addPEPEvent`, check conflict + type hints
Goffi <goffi@goffi.org>
parents: 3726
diff changeset
81 in_callback: Callable,
5d72dc52ee4a plugin XEP-0163: allow unset `event_type` in `addPEPEvent`, check conflict + type hints
Goffi <goffi@goffi.org>
parents: 3726
diff changeset
82 out_callback: Optional[Callable] = None,
5d72dc52ee4a plugin XEP-0163: allow unset `event_type` in `addPEPEvent`, check conflict + type hints
Goffi <goffi@goffi.org>
parents: 3726
diff changeset
83 notify: bool = True
5d72dc52ee4a plugin XEP-0163: allow unset `event_type` in `addPEPEvent`, check conflict + type hints
Goffi <goffi@goffi.org>
parents: 3726
diff changeset
84 ) -> None:
283
68cd30d982a5 core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff changeset
85 """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
86
3823
5d72dc52ee4a plugin XEP-0163: allow unset `event_type` in `addPEPEvent`, check conflict + type hints
Goffi <goffi@goffi.org>
parents: 3726
diff changeset
87 @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
88 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
89 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
90 @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
91 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
92 @param in_callback: method to call when this event occur
1459
4c4f88d7b156 plugins xep-0060, xep-0163, xep-0277, groupblog: bloging improvments (huge patch, sorry):
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
93 the callable will be called with (itemsEvent, profile) as arguments
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 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
95 event (must return a deferred)
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3823
diff changeset
96 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
97 @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
98 """
3823
5d72dc52ee4a plugin XEP-0163: allow unset `event_type` in `addPEPEvent`, check conflict + type hints
Goffi <goffi@goffi.org>
parents: 3726
diff changeset
99 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
100 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
101 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
102 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
103 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
104 )
311
0aa6ca6cdbdd plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents: 290
diff changeset
105 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
106 self.pep_events.add(node)
290
59a82af700e2 plugin xep 163: added generic sendPEPEvent method
Goffi <goffi@goffi.org>
parents: 286
diff changeset
107 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
108 self.pep_events.add(node + "+notify")
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
109
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3823
diff changeset
110 def filter_pep_event(client, itemsEvent):
2660
2901e22d1766 plugin XEP-0163: fixed bad PEP filtering
Goffi <goffi@goffi.org>
parents: 2643
diff changeset
111 """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
112
1459
4c4f88d7b156 plugins xep-0060, xep-0163, xep-0277, groupblog: bloging improvments (huge patch, sorry):
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
113 @param itemsEvent(pubsub.ItemsEvent): pubsub event
4c4f88d7b156 plugins xep-0060, xep-0163, xep-0277, groupblog: bloging improvments (huge patch, sorry):
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
114 """
2660
2901e22d1766 plugin XEP-0163: fixed bad PEP filtering
Goffi <goffi@goffi.org>
parents: 2643
diff changeset
115 if not itemsEvent.sender.user or itemsEvent.sender.resource:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
116 log.debug(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
117 "ignoring non PEP event from {} (profile={})".format(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
118 itemsEvent.sender.full(), client.profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
119 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
120 )
1459
4c4f88d7b156 plugins xep-0060, xep-0163, xep-0277, groupblog: bloging improvments (huge patch, sorry):
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
121 return
2305
972e33507609 plugin XEP-0060: addManagedNode callbacks now use client syntax instead of profile
Goffi <goffi@goffi.org>
parents: 2272
diff changeset
122 in_callback(itemsEvent, client.profile)
1459
4c4f88d7b156 plugins xep-0060, xep-0163, xep-0277, groupblog: bloging improvments (huge patch, sorry):
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
123
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3823
diff changeset
124 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
125
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3823
diff changeset
126 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
127 """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
128
4c4f88d7b156 plugins xep-0060, xep-0163, xep-0277, groupblog: bloging improvments (huge patch, sorry):
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
129 @param node(unicode): node namespace
290
59a82af700e2 plugin xep 163: added generic sendPEPEvent method
Goffi <goffi@goffi.org>
parents: 286
diff changeset
130 @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
131 @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
132 """
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3823
diff changeset
133 client = self.host.get_client(profile)
290
59a82af700e2 plugin xep 163: added generic sendPEPEvent method
Goffi <goffi@goffi.org>
parents: 286
diff changeset
134 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
135 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
136
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3823
diff changeset
137 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
138 """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
139
2643
189e38fb11ff core: style improvments (90 chars limit)
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
140 @param event_type: type of event (eg: MOOD, TUNE),
189e38fb11ff core: style improvments (90 chars limit)
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
141 must be in self.pep_out_cb.keys()
286
3b382fa0ac28 plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents: 283
diff changeset
142 @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
143 @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
144 """
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3823
diff changeset
145 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
146 if not profile:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
147 log.error(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2807
diff changeset
148 _("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
149 % profile_key
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
150 )
704
3c304929af74 plugin XEP-0277, group blogs: proper asynchronous methods for sending blogs.
Goffi <goffi@goffi.org>
parents: 660
diff changeset
151 raise exceptions.ProfileUnknownError
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2807
diff changeset
152 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
153 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
154 raise exceptions.DataError("Type unknown")
290
59a82af700e2 plugin xep 163: added generic sendPEPEvent method
Goffi <goffi@goffi.org>
parents: 286
diff changeset
155 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
156
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3823
diff changeset
157 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
158 if not itemsEvent.items:
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 916
diff changeset
159 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
160 return
283
68cd30d982a5 core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff changeset
161 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
162 mood_elt = [
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
163 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
164 ][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
165 except IndexError:
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 916
diff changeset
166 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
167 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
168 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
169 if not mood:
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 916
diff changeset
170 log.debug(_("No mood found"))
283
68cd30d982a5 core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff changeset
171 return
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3823
diff changeset
172 self.host.bridge.ps_event(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
173 C.PS_PEP,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
174 itemsEvent.sender.full(),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
175 itemsEvent.nodeIdentifier,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
176 "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
177 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
178 profile,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
179 )
286
3b382fa0ac28 plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents: 283
diff changeset
180
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3823
diff changeset
181 def send_mood(self, data, profile):
286
3b382fa0ac28 plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents: 283
diff changeset
182 """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
183
286
3b382fa0ac28 plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents: 283
diff changeset
184 @param data: must include mood and text
3b382fa0ac28 plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents: 283
diff changeset
185 @param profile: profile which send the mood"""
3b382fa0ac28 plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents: 283
diff changeset
186 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
187 value = data["mood"].lower()
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
188 text = data["text"] if "text" in data else ""
286
3b382fa0ac28 plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents: 283
diff changeset
189 except KeyError:
704
3c304929af74 plugin XEP-0277, group blogs: proper asynchronous methods for sending blogs.
Goffi <goffi@goffi.org>
parents: 660
diff changeset
190 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
191 mood = UserMood(value, text)
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3823
diff changeset
192 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
193
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
194
286
3b382fa0ac28 plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents: 283
diff changeset
195 class UserMood(Mood, domish.Element):
3b382fa0ac28 plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents: 283
diff changeset
196 """Improved wokkel Mood which is also a domish.Element"""
3b382fa0ac28 plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents: 283
diff changeset
197
3b382fa0ac28 plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents: 283
diff changeset
198 def __init__(self, value, text=None):
3b382fa0ac28 plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents: 283
diff changeset
199 Mood.__init__(self, value, text)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
200 domish.Element.__init__(self, (NS_USER_MOOD, "mood"))
286
3b382fa0ac28 plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents: 283
diff changeset
201 self.addElement(value)
3b382fa0ac28 plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents: 283
diff changeset
202 if text:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
203 self.addElement("text", content=text)