annotate sat/plugins/plugin_xep_0163.py @ 3742:bf0505d41c09

comp AP: helper methods to get AP objects: rel 364
author Goffi <goffi@goffi.org>
date Tue, 22 Mar 2022 17:00:42 +0100
parents 33d75cd3c371
children 5d72dc52ee4a
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
771
bfabeedbf32e core: i18n refactoring:
Goffi <goffi@goffi.org>
parents: 704
diff changeset
20 from sat.core.i18n import _
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 916
diff changeset
21 from sat.core import exceptions
916
1a759096ccbd core: use of Const for profile_key + replaced '@DEFAULT@' default profile_key by '@NONE@'
Goffi <goffi@goffi.org>
parents: 811
diff changeset
22 from sat.core.constants import Const as C
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 916
diff changeset
23 from sat.core.log import getLogger
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
24
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 916
diff changeset
25 log = getLogger(__name__)
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
2807
0b7ce5daee9b plugin XEP-0277: blog items data are now entirely serialised before going to bridge:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
30 from sat.tools.common import data_format
283
68cd30d982a5 core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff changeset
31
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
32 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
33
68cd30d982a5 core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff changeset
34 PLUGIN_INFO = {
2145
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
35 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
36 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
37 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
38 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
39 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
40 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
41 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
42 C.PI_HANDLER: "no",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
43 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
44 }
68cd30d982a5 core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff changeset
45
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
46
588
beaf6bec2fcd Remove every old-style class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
47 class XEP_0163(object):
283
68cd30d982a5 core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff changeset
48 def __init__(self, host):
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 916
diff changeset
49 log.info(_("PEP plugin initialization"))
283
68cd30d982a5 core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff changeset
50 self.host = host
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
51 self.pep_events = set()
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
52 self.pep_out_cb = {}
283
68cd30d982a5 core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff changeset
53 host.trigger.add("PubSub Disco Info", self.disoInfoTrigger)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
54 host.bridge.addMethod(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
55 "PEPSend",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
56 ".plugin",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
57 in_sign="sa{ss}s",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
58 out_sign="",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
59 method=self.PEPSend,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2807
diff changeset
60 async_=True,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
61 ) # args: type(MOOD, TUNE, etc), data, profile_key;
290
59a82af700e2 plugin xep 163: added generic sendPEPEvent method
Goffi <goffi@goffi.org>
parents: 286
diff changeset
62 self.addPEPEvent("MOOD", NS_USER_MOOD, self.userMoodCB, self.sendMood)
283
68cd30d982a5 core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff changeset
63
68cd30d982a5 core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff changeset
64 def disoInfoTrigger(self, disco_info, profile):
68cd30d982a5 core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff changeset
65 """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
66
283
68cd30d982a5 core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff changeset
67 @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
68 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
69 @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
70 """
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2807
diff changeset
71 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
72 return True
68cd30d982a5 core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff changeset
73
1459
4c4f88d7b156 plugins xep-0060, xep-0163, xep-0277, groupblog: bloging improvments (huge patch, sorry):
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
74 def addPEPEvent(self, event_type, node, in_callback, out_callback=None, notify=True):
283
68cd30d982a5 core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff changeset
75 """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
76
2643
189e38fb11ff core: style improvments (90 chars limit)
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
77 @param event_type(unicode): type of the event (always uppercase),
189e38fb11ff core: style improvments (90 chars limit)
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
78 can be MOOD, TUNE, etc
189e38fb11ff core: style improvments (90 chars limit)
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
79 @param node(unicode): namespace of the node (e.g. http://jabber.org/protocol/mood
189e38fb11ff core: style improvments (90 chars limit)
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
80 for 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
81 @param in_callback(callable): method to call when this event occur
4c4f88d7b156 plugins xep-0060, xep-0163, xep-0277, groupblog: bloging improvments (huge patch, sorry):
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
82 the callable will be called with (itemsEvent, profile) as arguments
2643
189e38fb11ff core: style improvments (90 chars limit)
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
83 @param out_callback(callable,None): method to call when we want to publish this
189e38fb11ff core: style improvments (90 chars limit)
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
84 event (must return a deferred)
1459
4c4f88d7b156 plugins xep-0060, xep-0163, xep-0277, groupblog: bloging improvments (huge patch, sorry):
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
85 the callable will be called when sendPEPEvent is called
4c4f88d7b156 plugins xep-0060, xep-0163, xep-0277, groupblog: bloging improvments (huge patch, sorry):
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
86 @param notify(bool): add autosubscribe (+notify) if True
4c4f88d7b156 plugins xep-0060, xep-0163, xep-0277, groupblog: bloging improvments (huge patch, sorry):
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
87 """
311
0aa6ca6cdbdd plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents: 290
diff changeset
88 if out_callback:
0aa6ca6cdbdd plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents: 290
diff changeset
89 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
90 self.pep_events.add(node)
290
59a82af700e2 plugin xep 163: added generic sendPEPEvent method
Goffi <goffi@goffi.org>
parents: 286
diff changeset
91 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
92 self.pep_events.add(node + "+notify")
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
93
2305
972e33507609 plugin XEP-0060: addManagedNode callbacks now use client syntax instead of profile
Goffi <goffi@goffi.org>
parents: 2272
diff changeset
94 def filterPEPEvent(client, itemsEvent):
2660
2901e22d1766 plugin XEP-0163: fixed bad PEP filtering
Goffi <goffi@goffi.org>
parents: 2643
diff changeset
95 """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
96
1459
4c4f88d7b156 plugins xep-0060, xep-0163, xep-0277, groupblog: bloging improvments (huge patch, sorry):
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
97 @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
98 """
2660
2901e22d1766 plugin XEP-0163: fixed bad PEP filtering
Goffi <goffi@goffi.org>
parents: 2643
diff changeset
99 if not itemsEvent.sender.user or itemsEvent.sender.resource:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
100 log.debug(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
101 "ignoring non PEP event from {} (profile={})".format(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
102 itemsEvent.sender.full(), client.profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
103 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
104 )
1459
4c4f88d7b156 plugins xep-0060, xep-0163, xep-0277, groupblog: bloging improvments (huge patch, sorry):
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
105 return
2305
972e33507609 plugin XEP-0060: addManagedNode callbacks now use client syntax instead of profile
Goffi <goffi@goffi.org>
parents: 2272
diff changeset
106 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
107
4c4f88d7b156 plugins xep-0060, xep-0163, xep-0277, groupblog: bloging improvments (huge patch, sorry):
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
108 self.host.plugins["XEP-0060"].addManagedNode(node, items_cb=filterPEPEvent)
4c4f88d7b156 plugins xep-0060, xep-0163, xep-0277, groupblog: bloging improvments (huge patch, sorry):
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
109
4c4f88d7b156 plugins xep-0060, xep-0163, xep-0277, groupblog: bloging improvments (huge patch, sorry):
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
110 def sendPEPEvent(self, node, data, profile):
290
59a82af700e2 plugin xep 163: added generic sendPEPEvent method
Goffi <goffi@goffi.org>
parents: 286
diff changeset
111 """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
112
4c4f88d7b156 plugins xep-0060, xep-0163, xep-0277, groupblog: bloging improvments (huge patch, sorry):
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
113 @param node(unicode): node namespace
290
59a82af700e2 plugin xep 163: added generic sendPEPEvent method
Goffi <goffi@goffi.org>
parents: 286
diff changeset
114 @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
115 @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
116 """
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
117 client = self.host.getClient(profile)
290
59a82af700e2 plugin xep 163: added generic sendPEPEvent method
Goffi <goffi@goffi.org>
parents: 286
diff changeset
118 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
119 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
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 def PEPSend(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
122 """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
123
2643
189e38fb11ff core: style improvments (90 chars limit)
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
124 @param event_type: type of event (eg: MOOD, TUNE),
189e38fb11ff core: style improvments (90 chars limit)
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
125 must be in self.pep_out_cb.keys()
286
3b382fa0ac28 plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents: 283
diff changeset
126 @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
127 @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
128 """
286
3b382fa0ac28 plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents: 283
diff changeset
129 profile = self.host.memory.getProfileName(profile_key)
3b382fa0ac28 plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents: 283
diff changeset
130 if not profile:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
131 log.error(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2807
diff changeset
132 _("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
133 % profile_key
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
134 )
704
3c304929af74 plugin XEP-0277, group blogs: proper asynchronous methods for sending blogs.
Goffi <goffi@goffi.org>
parents: 660
diff changeset
135 raise exceptions.ProfileUnknownError
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2807
diff changeset
136 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
137 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
138 raise exceptions.DataError("Type unknown")
290
59a82af700e2 plugin xep 163: added generic sendPEPEvent method
Goffi <goffi@goffi.org>
parents: 286
diff changeset
139 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
140
283
68cd30d982a5 core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff changeset
141 def userMoodCB(self, itemsEvent, profile):
410
ef9a4453959b plugin XEP-0163: user event mood with no item fix
Goffi <goffi@goffi.org>
parents: 372
diff changeset
142 if not itemsEvent.items:
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 916
diff changeset
143 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
144 return
283
68cd30d982a5 core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff changeset
145 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
146 mood_elt = [
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
147 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
148 ][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
149 except IndexError:
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 916
diff changeset
150 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
151 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
152 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
153 if not mood:
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 916
diff changeset
154 log.debug(_("No mood found"))
283
68cd30d982a5 core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff changeset
155 return
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
156 self.host.bridge.psEvent(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
157 C.PS_PEP,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
158 itemsEvent.sender.full(),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
159 itemsEvent.nodeIdentifier,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
160 "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
161 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
162 profile,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
163 )
286
3b382fa0ac28 plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents: 283
diff changeset
164
3b382fa0ac28 plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents: 283
diff changeset
165 def sendMood(self, data, profile):
3b382fa0ac28 plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents: 283
diff changeset
166 """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
167
286
3b382fa0ac28 plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents: 283
diff changeset
168 @param data: must include mood and text
3b382fa0ac28 plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents: 283
diff changeset
169 @param profile: profile which send the mood"""
3b382fa0ac28 plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents: 283
diff changeset
170 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
171 value = data["mood"].lower()
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
172 text = data["text"] if "text" in data else ""
286
3b382fa0ac28 plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents: 283
diff changeset
173 except KeyError:
704
3c304929af74 plugin XEP-0277, group blogs: proper asynchronous methods for sending blogs.
Goffi <goffi@goffi.org>
parents: 660
diff changeset
174 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
175 mood = UserMood(value, text)
704
3c304929af74 plugin XEP-0277, group blogs: proper asynchronous methods for sending blogs.
Goffi <goffi@goffi.org>
parents: 660
diff changeset
176 return self.sendPEPEvent(NS_USER_MOOD, mood, profile)
286
3b382fa0ac28 plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents: 283
diff changeset
177
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
178
286
3b382fa0ac28 plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents: 283
diff changeset
179 class UserMood(Mood, domish.Element):
3b382fa0ac28 plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents: 283
diff changeset
180 """Improved wokkel Mood which is also a domish.Element"""
3b382fa0ac28 plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents: 283
diff changeset
181
3b382fa0ac28 plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents: 283
diff changeset
182 def __init__(self, value, text=None):
3b382fa0ac28 plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents: 283
diff changeset
183 Mood.__init__(self, value, text)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
184 domish.Element.__init__(self, (NS_USER_MOOD, "mood"))
286
3b382fa0ac28 plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents: 283
diff changeset
185 self.addElement(value)
3b382fa0ac28 plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents: 283
diff changeset
186 if text:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
187 self.addElement("text", content=text)