Mercurial > libervia-backend
annotate sat/plugins/plugin_xep_0048.py @ 4001:32d714a8ea51
plugin XEP-0045: dot not wait for MAM retrieval to be completed:
in `_join_MAM`, `room.fully_joined` is called before retrieving the MAM archive, as the
process can be very long, and is not necessary to have the room working (message can be
received after being in the room, and added out of order). This avoid blocking the `join`
workflow for an extended time.
Some renaming and coroutine integrations.
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 10 Mar 2023 17:22:41 +0100 |
parents | be6d91572633 |
children | 524856bd7b19 |
rev | line source |
---|---|
3028 | 1 #!/usr/bin/env python3 |
3137 | 2 |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 # SAT plugin for Bookmarks (xep-0048) |
3479 | 5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org) |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 # (at your option) any later version. |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 # GNU Affero General Public License for more details. |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 from sat.core.i18n import _, D_ |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 from sat.core import exceptions |
983
c34e0b2bbf08
plugin XEP-0048: added /bookmark text command
Goffi <goffi@goffi.org>
parents:
982
diff
changeset
|
22 from sat.core.constants import Const as C |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 from sat.memory.persistent import PersistentBinaryDict |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 from sat.tools import xml_tools |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
989
diff
changeset
|
25 from sat.core.log import getLogger |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
26 |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
989
diff
changeset
|
27 log = getLogger(__name__) |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 from twisted.words.xish import domish |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 from twisted.words.protocols.jabber import jid |
989
93359853e4bc
plugins XEP-0048, XEP-0049: feature is not checked anymore before using private XML storage, as feature announcement is not mandatory in XEP-0049
Goffi <goffi@goffi.org>
parents:
986
diff
changeset
|
30 from twisted.words.protocols.jabber.error import StanzaError |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 from twisted.internet import defer |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
34 NS_BOOKMARKS = "storage:bookmarks" |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 PLUGIN_INFO = { |
2145
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
37 C.PI_NAME: "Bookmarks", |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
38 C.PI_IMPORT_NAME: "XEP-0048", |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
39 C.PI_TYPE: "XEP", |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
40 C.PI_PROTOCOLS: ["XEP-0048"], |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
41 C.PI_DEPENDENCIES: ["XEP-0045"], |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
42 C.PI_RECOMMENDATIONS: ["XEP-0049"], |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
43 C.PI_MAIN: "XEP_0048", |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
44 C.PI_HANDLER: "no", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
45 C.PI_DESCRIPTION: _("""Implementation of bookmarks"""), |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 } |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 class XEP_0048(object): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
50 MUC_TYPE = "muc" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
51 URL_TYPE = "url" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
52 MUC_KEY = "jid" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
53 URL_KEY = "url" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
54 MUC_ATTRS = ("autojoin", "name") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
55 URL_ATTRS = ("name",) |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 def __init__(self, host): |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
989
diff
changeset
|
58 log.info(_("Bookmarks plugin initialization")) |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 self.host = host |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 # self.__menu_id = host.registerCallback(self._bookmarksMenu, with_data=True) |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 self.__bm_save_id = host.registerCallback(self._bookmarksSaveCb, with_data=True) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
62 host.importMenu( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
63 (D_("Groups"), D_("Bookmarks")), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
64 self._bookmarksMenu, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
65 security_limit=0, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
66 help_string=D_("Use and manage bookmarks"), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
67 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
68 self.__selected_id = host.registerCallback( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
69 self._bookmarkSelectedCb, with_data=True |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
70 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
71 host.bridge.addMethod( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
72 "bookmarksList", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
73 ".plugin", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
74 in_sign="sss", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
75 out_sign="a{sa{sa{ss}}}", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
76 method=self._bookmarksList, |
3028 | 77 async_=True, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
78 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
79 host.bridge.addMethod( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
80 "bookmarksRemove", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
81 ".plugin", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
82 in_sign="ssss", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
83 out_sign="", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
84 method=self._bookmarksRemove, |
3028 | 85 async_=True, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
86 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
87 host.bridge.addMethod( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
88 "bookmarksAdd", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
89 ".plugin", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
90 in_sign="ssa{ss}ss", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
91 out_sign="", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
92 method=self._bookmarksAdd, |
3028 | 93 async_=True, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
94 ) |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
95 try: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
96 self.private_plg = self.host.plugins["XEP-0049"] |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
97 except KeyError: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
98 self.private_plg = None |
983
c34e0b2bbf08
plugin XEP-0048: added /bookmark text command
Goffi <goffi@goffi.org>
parents:
982
diff
changeset
|
99 try: |
c34e0b2bbf08
plugin XEP-0048: added /bookmark text command
Goffi <goffi@goffi.org>
parents:
982
diff
changeset
|
100 self.host.plugins[C.TEXT_CMDS].registerTextCommands(self) |
c34e0b2bbf08
plugin XEP-0048: added /bookmark text command
Goffi <goffi@goffi.org>
parents:
982
diff
changeset
|
101 except KeyError: |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
989
diff
changeset
|
102 log.info(_("Text commands not available")) |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
103 |
4001
32d714a8ea51
plugin XEP-0045: dot not wait for MAM retrieval to be completed:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
104 async def profileConnected(self, client): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
105 local = client.bookmarks_local = PersistentBinaryDict( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
106 NS_BOOKMARKS, client.profile |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
107 ) |
4001
32d714a8ea51
plugin XEP-0045: dot not wait for MAM retrieval to be completed:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
108 await local.load() |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
109 if not local: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
110 local[XEP_0048.MUC_TYPE] = dict() |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
111 local[XEP_0048.URL_TYPE] = dict() |
4001
32d714a8ea51
plugin XEP-0045: dot not wait for MAM retrieval to be completed:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
112 private = await self._getServerBookmarks("private", client.profile) |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
113 pubsub = client.bookmarks_pubsub = None |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
114 |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
115 for bookmarks in (local, private, pubsub): |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
116 if bookmarks is not None: |
3028 | 117 for (room_jid, data) in list(bookmarks[XEP_0048.MUC_TYPE].items()): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
118 if data.get("autojoin", "false") == "true": |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
119 nick = data.get("nick", client.jid.user) |
4001
32d714a8ea51
plugin XEP-0045: dot not wait for MAM retrieval to be completed:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
120 defer.ensureDeferred( |
32d714a8ea51
plugin XEP-0045: dot not wait for MAM retrieval to be completed:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
121 self.host.plugins["XEP-0045"].join(client, room_jid, nick, {}) |
32d714a8ea51
plugin XEP-0045: dot not wait for MAM retrieval to be completed:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
122 ) |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
123 |
2987
8990ed9aad31
quick_frontend (contact list): fixed `nick` use for groupchat:
Goffi <goffi@goffi.org>
parents:
2879
diff
changeset
|
124 # we don't use a DeferredList to gather result here, as waiting for all room would |
8990ed9aad31
quick_frontend (contact list): fixed `nick` use for groupchat:
Goffi <goffi@goffi.org>
parents:
2879
diff
changeset
|
125 # slow down a lot the connection process, and result in a bad user experience. |
8990ed9aad31
quick_frontend (contact list): fixed `nick` use for groupchat:
Goffi <goffi@goffi.org>
parents:
2879
diff
changeset
|
126 |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
127 @defer.inlineCallbacks |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
128 def _getServerBookmarks(self, storage_type, profile): |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
129 """Get distants bookmarks |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
130 |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
131 update also the client.bookmarks_[type] key, with None if service is not available |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
132 @param storage_type: storage type, can be: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
133 - 'private': XEP-0049 storage |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
134 - 'pubsub': XEP-0223 storage |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
135 @param profile: %(doc_profile)s |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
136 @return: data dictionary, or None if feature is not available |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
137 """ |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
138 client = self.host.getClient(profile) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
139 if storage_type == "private": |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
140 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
141 bookmarks_private_xml = yield self.private_plg.privateXMLGet( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
142 "storage", NS_BOOKMARKS, profile |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
143 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
144 data = client.bookmarks_private = self._bookmarkElt2Dict( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
145 bookmarks_private_xml |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
146 ) |
989
93359853e4bc
plugins XEP-0048, XEP-0049: feature is not checked anymore before using private XML storage, as feature announcement is not mandatory in XEP-0049
Goffi <goffi@goffi.org>
parents:
986
diff
changeset
|
147 except (StanzaError, AttributeError): |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
989
diff
changeset
|
148 log.info(_("Private XML storage not available")) |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
149 data = client.bookmarks_private = None |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
150 elif storage_type == "pubsub": |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
151 raise NotImplementedError |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
152 else: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
153 raise ValueError("storage_type must be 'private' or 'pubsub'") |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
154 defer.returnValue(data) |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
155 |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
156 @defer.inlineCallbacks |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
157 def _setServerBookmarks(self, storage_type, bookmarks_elt, profile): |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
158 """Save bookmarks on server |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
159 |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
160 @param storage_type: storage type, can be: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
161 - 'private': XEP-0049 storage |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
162 - 'pubsub': XEP-0223 storage |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
163 @param bookmarks_elt (domish.Element): bookmarks XML |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
164 @param profile: %(doc_profile)s |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
165 """ |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
166 if storage_type == "private": |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
167 yield self.private_plg.privateXMLStore(bookmarks_elt, profile) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
168 elif storage_type == "pubsub": |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
169 raise NotImplementedError |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
170 else: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
171 raise ValueError("storage_type must be 'private' or 'pubsub'") |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
172 |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
173 def _bookmarkElt2Dict(self, storage_elt): |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
174 """Parse bookmarks to get dictionary |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
175 @param storage_elt (domish.Element): bookmarks storage |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
176 @return (dict): bookmark data (key: bookmark type, value: list) where key can be: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
177 - XEP_0048.MUC_TYPE |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
178 - XEP_0048.URL_TYPE |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
179 - value (dict): data as for addBookmark |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
180 """ |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
181 conf_data = {} |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
182 url_data = {} |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
183 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
184 conference_elts = storage_elt.elements(NS_BOOKMARKS, "conference") |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
185 for conference_elt in conference_elts: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
186 try: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
187 room_jid = jid.JID(conference_elt[XEP_0048.MUC_KEY]) |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
188 except KeyError: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
189 log.warning( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
190 "invalid bookmark found, igoring it:\n%s" % conference_elt.toXml() |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
191 ) |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
192 continue |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
193 |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
194 data = conf_data[room_jid] = {} |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
195 |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
196 for attr in XEP_0048.MUC_ATTRS: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
197 if conference_elt.hasAttribute(attr): |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
198 data[attr] = conference_elt[attr] |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
199 try: |
3028 | 200 data["nick"] = str( |
201 next(conference_elt.elements(NS_BOOKMARKS, "nick")) | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
202 ) |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
203 except StopIteration: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
204 pass |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
205 # TODO: manage password (need to be secured, see XEP-0049 §4) |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
206 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
207 url_elts = storage_elt.elements(NS_BOOKMARKS, "url") |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
208 for url_elt in url_elts: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
209 try: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
210 url = url_elt[XEP_0048.URL_KEY] |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
211 except KeyError: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
212 log.warning("invalid bookmark found, igoring it:\n%s" % url_elt.toXml()) |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
213 continue |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
214 data = url_data[url] = {} |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
215 for attr in XEP_0048.URL_ATTRS: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
216 if url_elt.hasAttribute(attr): |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
217 data[attr] = url_elt[attr] |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
218 |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
219 return {XEP_0048.MUC_TYPE: conf_data, XEP_0048.URL_TYPE: url_data} |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
220 |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
221 def _dict2BookmarkElt(self, type_, data): |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
222 """Construct a bookmark element from a data dict |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
223 @param data (dict): bookmark data (key: bookmark type, value: list) where key can be: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
224 - XEP_0048.MUC_TYPE |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
225 - XEP_0048.URL_TYPE |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
226 - value (dict): data as for addBookmark |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
227 @return (domish.Element): bookmark element |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
228 """ |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
229 rooms_data = data.get(XEP_0048.MUC_TYPE, {}) |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
230 urls_data = data.get(XEP_0048.URL_TYPE, {}) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
231 storage_elt = domish.Element((NS_BOOKMARKS, "storage")) |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
232 for room_jid in rooms_data: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
233 conference_elt = storage_elt.addElement("conference") |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
234 conference_elt[XEP_0048.MUC_KEY] = room_jid.full() |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
235 for attr in XEP_0048.MUC_ATTRS: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
236 try: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
237 conference_elt[attr] = rooms_data[room_jid][attr] |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
238 except KeyError: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
239 pass |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
240 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
241 conference_elt.addElement("nick", content=rooms_data[room_jid]["nick"]) |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
242 except KeyError: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
243 pass |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
244 |
3040 | 245 for url, url_data in urls_data.items(): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
246 url_elt = storage_elt.addElement("url") |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
247 url_elt[XEP_0048.URL_KEY] = url |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
248 for attr in XEP_0048.URL_ATTRS: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
249 try: |
3040 | 250 url_elt[attr] = url_data[attr] |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
251 except KeyError: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
252 pass |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
253 |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
254 return storage_elt |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
255 |
984
df8e1b557125
plugin XEP-0048: added list of bookmarks in XMLUI, selecting one join the room
Goffi <goffi@goffi.org>
parents:
983
diff
changeset
|
256 def _bookmarkSelectedCb(self, data, profile): |
df8e1b557125
plugin XEP-0048: added list of bookmarks in XMLUI, selecting one join the room
Goffi <goffi@goffi.org>
parents:
983
diff
changeset
|
257 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
258 room_jid_s, nick = data["index"].split(" ", 1) |
984
df8e1b557125
plugin XEP-0048: added list of bookmarks in XMLUI, selecting one join the room
Goffi <goffi@goffi.org>
parents:
983
diff
changeset
|
259 room_jid = jid.JID(room_jid_s) |
df8e1b557125
plugin XEP-0048: added list of bookmarks in XMLUI, selecting one join the room
Goffi <goffi@goffi.org>
parents:
983
diff
changeset
|
260 except (KeyError, RuntimeError): |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
989
diff
changeset
|
261 log.warning(_("No room jid selected")) |
984
df8e1b557125
plugin XEP-0048: added list of bookmarks in XMLUI, selecting one join the room
Goffi <goffi@goffi.org>
parents:
983
diff
changeset
|
262 return {} |
df8e1b557125
plugin XEP-0048: added list of bookmarks in XMLUI, selecting one join the room
Goffi <goffi@goffi.org>
parents:
983
diff
changeset
|
263 |
1963
a2bc5089c2eb
backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
264 client = self.host.getClient(profile) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
265 d = self.host.plugins["XEP-0045"].join(client, room_jid, nick, {}) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
266 |
984
df8e1b557125
plugin XEP-0048: added list of bookmarks in XMLUI, selecting one join the room
Goffi <goffi@goffi.org>
parents:
983
diff
changeset
|
267 def join_eb(failure): |
3028 | 268 log.warning("Error while trying to join room: {}".format(failure)) |
984
df8e1b557125
plugin XEP-0048: added list of bookmarks in XMLUI, selecting one join the room
Goffi <goffi@goffi.org>
parents:
983
diff
changeset
|
269 # FIXME: failure are badly managed in plugin XEP-0045. Plugin XEP-0045 need to be fixed before managing errors correctly here |
df8e1b557125
plugin XEP-0048: added list of bookmarks in XMLUI, selecting one join the room
Goffi <goffi@goffi.org>
parents:
983
diff
changeset
|
270 return {} |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
271 |
2765
378188abe941
misc: replaced all "dummy" by the more conventional and readable "__" ("_" being used for gettext)
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
272 d.addCallbacks(lambda __: {}, join_eb) |
984
df8e1b557125
plugin XEP-0048: added list of bookmarks in XMLUI, selecting one join the room
Goffi <goffi@goffi.org>
parents:
983
diff
changeset
|
273 return d |
df8e1b557125
plugin XEP-0048: added list of bookmarks in XMLUI, selecting one join the room
Goffi <goffi@goffi.org>
parents:
983
diff
changeset
|
274 |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
275 def _bookmarksMenu(self, data, profile): |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
276 """ XMLUI activated by menu: return Gateways UI |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
277 @param profile: %(doc_profile)s |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
278 |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
279 """ |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
280 client = self.host.getClient(profile) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
281 xmlui = xml_tools.XMLUI(title=_("Bookmarks manager")) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
282 adv_list = xmlui.changeContainer( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
283 "advanced_list", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
284 columns=3, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
285 selectable="single", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
286 callback_id=self.__selected_id, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
287 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
288 for bookmarks in ( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
289 client.bookmarks_local, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
290 client.bookmarks_private, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
291 client.bookmarks_pubsub, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
292 ): |
984
df8e1b557125
plugin XEP-0048: added list of bookmarks in XMLUI, selecting one join the room
Goffi <goffi@goffi.org>
parents:
983
diff
changeset
|
293 if bookmarks is None: |
df8e1b557125
plugin XEP-0048: added list of bookmarks in XMLUI, selecting one join the room
Goffi <goffi@goffi.org>
parents:
983
diff
changeset
|
294 continue |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
295 for (room_jid, data) in sorted( |
3028 | 296 list(bookmarks[XEP_0048.MUC_TYPE].items()), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
297 key=lambda item: item[1].get("name", item[0].user), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
298 ): |
984
df8e1b557125
plugin XEP-0048: added list of bookmarks in XMLUI, selecting one join the room
Goffi <goffi@goffi.org>
parents:
983
diff
changeset
|
299 room_jid_s = room_jid.full() |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
300 adv_list.setRowIndex( |
3028 | 301 "%s %s" % (room_jid_s, data.get("nick") or client.jid.user) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
302 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
303 xmlui.addText(data.get("name", "")) |
984
df8e1b557125
plugin XEP-0048: added list of bookmarks in XMLUI, selecting one join the room
Goffi <goffi@goffi.org>
parents:
983
diff
changeset
|
304 xmlui.addJid(room_jid) |
2879 | 305 if C.bool(data.get("autojoin", C.BOOL_FALSE)): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
306 xmlui.addText("autojoin") |
984
df8e1b557125
plugin XEP-0048: added list of bookmarks in XMLUI, selecting one join the room
Goffi <goffi@goffi.org>
parents:
983
diff
changeset
|
307 else: |
df8e1b557125
plugin XEP-0048: added list of bookmarks in XMLUI, selecting one join the room
Goffi <goffi@goffi.org>
parents:
983
diff
changeset
|
308 xmlui.addEmpty() |
df8e1b557125
plugin XEP-0048: added list of bookmarks in XMLUI, selecting one join the room
Goffi <goffi@goffi.org>
parents:
983
diff
changeset
|
309 adv_list.end() |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
310 xmlui.addDivider("dash") |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
311 xmlui.addText(_("add a bookmark")) |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
312 xmlui.changeContainer("pairs") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
313 xmlui.addLabel(_("Name")) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
314 xmlui.addString("name") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
315 xmlui.addLabel(_("jid")) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
316 xmlui.addString("jid") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
317 xmlui.addLabel(_("Nickname")) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
318 xmlui.addString("nick", client.jid.user) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
319 xmlui.addLabel(_("Autojoin")) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
320 xmlui.addBool("autojoin") |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
321 xmlui.changeContainer("vertical") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
322 xmlui.addButton(self.__bm_save_id, _("Save"), ("name", "jid", "nick", "autojoin")) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
323 return {"xmlui": xmlui.toXml()} |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
324 |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
325 def _bookmarksSaveCb(self, data, profile): |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
326 bm_data = xml_tools.XMLUIResult2DataFormResult(data) |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
327 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
328 location = jid.JID(bm_data.pop("jid")) |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
329 except KeyError: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
330 raise exceptions.InternalError("Can't find mandatory key") |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
331 d = self.addBookmark(XEP_0048.MUC_TYPE, location, bm_data, profile_key=profile) |
2765
378188abe941
misc: replaced all "dummy" by the more conventional and readable "__" ("_" being used for gettext)
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
332 d.addCallback(lambda __: {}) |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
333 return d |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
334 |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
335 @defer.inlineCallbacks |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
336 def addBookmark( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
337 self, type_, location, data, storage_type="auto", profile_key=C.PROF_KEY_NONE |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
338 ): |
983
c34e0b2bbf08
plugin XEP-0048: added /bookmark text command
Goffi <goffi@goffi.org>
parents:
982
diff
changeset
|
339 """Store a new bookmark |
c34e0b2bbf08
plugin XEP-0048: added /bookmark text command
Goffi <goffi@goffi.org>
parents:
982
diff
changeset
|
340 |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
341 @param type_: bookmark type, one of: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
342 - XEP_0048.MUC_TYPE: Multi-User chat room |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
343 - XEP_0048.URL_TYPE: web page URL |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
344 @param location: dependeding on type_, can be a MUC room jid or an url |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
345 @param data (dict): depending on type_, can contains the following keys: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
346 - name: human readable name of the bookmark |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
347 - nick: user preferred room nick (default to user part of profile's jid) |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
348 - autojoin: "true" if room must be automatically joined on connection |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
349 - password: unused yet TODO |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
350 @param storage_type: where the bookmark will be stored, can be: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
351 - "auto": find best available option: pubsub, private, local in that order |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
352 - "pubsub": PubSub private storage (XEP-0223) |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
353 - "private": Private XML storage (XEP-0049) |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
354 - "local": Store in SàT database |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
355 @param profile_key: %(doc_profile_key)s |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
356 """ |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
357 assert storage_type in ("auto", "pubsub", "private", "local") |
3028 | 358 if type_ == XEP_0048.URL_TYPE and {"autojoin", "nick"}.intersection(list(data.keys())): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
359 raise ValueError("autojoin or nick can't be used with URLs") |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
360 client = self.host.getClient(profile_key) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
361 if storage_type == "auto": |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
362 if client.bookmarks_pubsub is not None: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
363 storage_type = "pubsub" |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
364 elif client.bookmarks_private is not None: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
365 storage_type = "private" |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
366 else: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
367 storage_type = "local" |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
989
diff
changeset
|
368 log.warning(_("Bookmarks will be local only")) |
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
989
diff
changeset
|
369 log.info(_('Type selected for "auto" storage: %s') % storage_type) |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
370 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
371 if storage_type == "local": |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
372 client.bookmarks_local[type_][location] = data |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
373 yield client.bookmarks_local.force(type_) |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
374 else: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
375 bookmarks = yield self._getServerBookmarks(storage_type, client.profile) |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
376 bookmarks[type_][location] = data |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
377 bookmark_elt = self._dict2BookmarkElt(type_, bookmarks) |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
378 yield self._setServerBookmarks(storage_type, bookmark_elt, client.profile) |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
379 |
985
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
380 @defer.inlineCallbacks |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
381 def removeBookmark( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
382 self, type_, location, storage_type="all", profile_key=C.PROF_KEY_NONE |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
383 ): |
985
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
384 """Remove a stored bookmark |
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
385 |
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
386 @param type_: bookmark type, one of: |
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
387 - XEP_0048.MUC_TYPE: Multi-User chat room |
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
388 - XEP_0048.URL_TYPE: web page URL |
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
389 @param location: dependeding on type_, can be a MUC room jid or an url |
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
390 @param storage_type: where the bookmark is stored, can be: |
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
391 - "all": remove from everywhere |
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
392 - "pubsub": PubSub private storage (XEP-0223) |
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
393 - "private": Private XML storage (XEP-0049) |
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
394 - "local": Store in SàT database |
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
395 @param profile_key: %(doc_profile_key)s |
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
396 """ |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
397 assert storage_type in ("all", "pubsub", "private", "local") |
985
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
398 client = self.host.getClient(profile_key) |
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
399 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
400 if storage_type in ("all", "local"): |
985
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
401 try: |
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
402 del client.bookmarks_local[type_][location] |
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
403 yield client.bookmarks_local.force(type_) |
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
404 except KeyError: |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
989
diff
changeset
|
405 log.debug("Bookmark is not present in local storage") |
985
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
406 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
407 if storage_type in ("all", "private"): |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
408 bookmarks = yield self._getServerBookmarks("private", client.profile) |
985
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
409 try: |
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
410 del bookmarks[type_][location] |
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
411 bookmark_elt = self._dict2BookmarkElt(type_, bookmarks) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
412 yield self._setServerBookmarks("private", bookmark_elt, client.profile) |
985
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
413 except KeyError: |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
989
diff
changeset
|
414 log.debug("Bookmark is not present in private storage") |
985
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
415 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
416 if storage_type == "pubsub": |
985
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
417 raise NotImplementedError |
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
418 |
986 | 419 def _bookmarksList(self, type_, storage_location, profile_key=C.PROF_KEY_NONE): |
420 """Return stored bookmarks | |
421 | |
422 @param type_: bookmark type, one of: | |
423 - XEP_0048.MUC_TYPE: Multi-User chat room | |
424 - XEP_0048.URL_TYPE: web page URL | |
425 @param storage_location: can be: | |
426 - 'all' | |
427 - 'local' | |
428 - 'private' | |
429 - 'pubsub' | |
430 @param profile_key: %(doc_profile_key)s | |
431 @param return (dict): (key: storage_location, value dict) with: | |
432 - value (dict): (key: bookmark_location, value: bookmark data) | |
433 """ | |
434 client = self.host.getClient(profile_key) | |
435 ret = {} | |
436 ret_d = defer.succeed(ret) | |
437 | |
2765
378188abe941
misc: replaced all "dummy" by the more conventional and readable "__" ("_" being used for gettext)
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
438 def fillBookmarks(__, _storage_location): |
986 | 439 bookmarks_ori = getattr(client, "bookmarks_" + _storage_location) |
440 if bookmarks_ori is None: | |
441 return ret | |
442 data = bookmarks_ori[type_] | |
443 for bookmark in data: | |
3040 | 444 if type_ == XEP_0048.MUC_TYPE: |
445 ret[_storage_location][bookmark.full()] = data[bookmark].copy() | |
446 else: | |
447 ret[_storage_location][bookmark] = data[bookmark].copy() | |
986 | 448 return ret |
449 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
450 for _storage_location in ("local", "private", "pubsub"): |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
451 if storage_location in ("all", _storage_location): |
986 | 452 ret[_storage_location] = {} |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
453 if _storage_location in ("private",): |
986 | 454 # we update distant bookmarks, just in case an other client added something |
455 d = self._getServerBookmarks(_storage_location, client.profile) | |
456 else: | |
457 d = defer.succeed(None) | |
458 d.addCallback(fillBookmarks, _storage_location) | |
2765
378188abe941
misc: replaced all "dummy" by the more conventional and readable "__" ("_" being used for gettext)
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
459 ret_d.addCallback(lambda __: d) |
986 | 460 |
461 return ret_d | |
462 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
463 def _bookmarksRemove( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
464 self, type_, location, storage_location, profile_key=C.PROF_KEY_NONE |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
465 ): |
986 | 466 """Return stored bookmarks |
467 | |
468 @param type_: bookmark type, one of: | |
469 - XEP_0048.MUC_TYPE: Multi-User chat room | |
470 - XEP_0048.URL_TYPE: web page URL | |
471 @param location: dependeding on type_, can be a MUC room jid or an url | |
472 @param storage_location: can be: | |
473 - "all": remove from everywhere | |
474 - "pubsub": PubSub private storage (XEP-0223) | |
475 - "private": Private XML storage (XEP-0049) | |
476 - "local": Store in SàT database | |
477 @param profile_key: %(doc_profile_key)s | |
478 """ | |
479 if type_ == XEP_0048.MUC_TYPE: | |
480 location = jid.JID(location) | |
481 return self.removeBookmark(type_, location, storage_location, profile_key) | |
482 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
483 def _bookmarksAdd( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
484 self, type_, location, data, storage_type="auto", profile_key=C.PROF_KEY_NONE |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
485 ): |
986 | 486 if type_ == XEP_0048.MUC_TYPE: |
487 location = jid.JID(location) | |
488 return self.addBookmark(type_, location, data, storage_type, profile_key) | |
489 | |
1963
a2bc5089c2eb
backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
490 def cmd_bookmark(self, client, mess_data): |
985
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
491 """(Un)bookmark a MUC room |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
492 |
985
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
493 @command (group): [autojoin | remove] |
983
c34e0b2bbf08
plugin XEP-0048: added /bookmark text command
Goffi <goffi@goffi.org>
parents:
982
diff
changeset
|
494 - autojoin: join room automatically on connection |
1373
6d0e01809893
plugin text commands: minor docstrings improvments
Goffi <goffi@goffi.org>
parents:
1371
diff
changeset
|
495 - remove: remove bookmark(s) for this room |
983
c34e0b2bbf08
plugin XEP-0048: added /bookmark text command
Goffi <goffi@goffi.org>
parents:
982
diff
changeset
|
496 """ |
c34e0b2bbf08
plugin XEP-0048: added /bookmark text command
Goffi <goffi@goffi.org>
parents:
982
diff
changeset
|
497 txt_cmd = self.host.plugins[C.TEXT_CMDS] |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
498 |
983
c34e0b2bbf08
plugin XEP-0048: added /bookmark text command
Goffi <goffi@goffi.org>
parents:
982
diff
changeset
|
499 options = mess_data["unparsed"].strip().split() |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
500 if options and options[0] not in ("autojoin", "remove"): |
1963
a2bc5089c2eb
backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
501 txt_cmd.feedBack(client, _("Bad arguments"), mess_data) |
985
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
502 return False |
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
503 |
983
c34e0b2bbf08
plugin XEP-0048: added /bookmark text command
Goffi <goffi@goffi.org>
parents:
982
diff
changeset
|
504 room_jid = mess_data["to"].userhostJID() |
985
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
505 |
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
506 if "remove" in options: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
507 self.removeBookmark(XEP_0048.MUC_TYPE, room_jid, profile_key=client.profile) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
508 txt_cmd.feedBack( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
509 client, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
510 _("All [%s] bookmarks are being removed") % room_jid.full(), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
511 mess_data, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
512 ) |
985
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
513 return False |
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
514 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
515 data = { |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
516 "name": room_jid.user, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
517 "nick": client.jid.user, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
518 "autojoin": "true" if "autojoin" in options else "false", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
519 } |
1963
a2bc5089c2eb
backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
520 self.addBookmark(XEP_0048.MUC_TYPE, room_jid, data, profile_key=client.profile) |
a2bc5089c2eb
backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
521 txt_cmd.feedBack(client, _("Bookmark added"), mess_data) |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
522 |
983
c34e0b2bbf08
plugin XEP-0048: added /bookmark text command
Goffi <goffi@goffi.org>
parents:
982
diff
changeset
|
523 return False |