Mercurial > libervia-backend
annotate libervia/backend/plugins/plugin_xep_0048.py @ 4332:71c939e34ca6
XEP-0373 (OX): Adjust to gpgme updates: generate with explicit algorithm and subkeys
author | Syndace <me@syndace.dev> |
---|---|
date | Sat, 13 Jul 2024 18:28:28 +0200 |
parents | 554a87ae17a6 |
children | 111dce64dcb5 |
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 |
4327
554a87ae17a6
plugin XEP-0048, XEP-0402; CLI (bookmarks): implement XEP-0402 (PEP Native Bookmarks):
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
4 # Libervia plugin for Bookmarks (xep-0048) |
554a87ae17a6
plugin XEP-0048, XEP-0402; CLI (bookmarks): implement XEP-0402 (PEP Native Bookmarks):
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
5 # Copyright (C) 2009-2024 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 |
4212 | 20 from typing import cast |
4071
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
21 from libervia.backend.core.i18n import _, D_ |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
22 from libervia.backend.core import exceptions |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
23 from libervia.backend.core.constants import Const as C |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
24 from libervia.backend.memory.persistent import PersistentBinaryDict |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
25 from libervia.backend.tools import xml_tools |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
26 from libervia.backend.core.log import getLogger |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
27 |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
989
diff
changeset
|
28 log = getLogger(__name__) |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 from twisted.words.xish import domish |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 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
|
31 from twisted.words.protocols.jabber.error import StanzaError |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 from twisted.internet import defer |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
35 NS_BOOKMARKS = "storage:bookmarks" |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 PLUGIN_INFO = { |
2145
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
38 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
|
39 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
|
40 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
|
41 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
|
42 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
|
43 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
|
44 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
|
45 C.PI_HANDLER: "no", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
46 C.PI_DESCRIPTION: _("""Implementation of bookmarks"""), |
982
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 |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 class XEP_0048(object): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
51 MUC_TYPE = "muc" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
52 URL_TYPE = "url" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
53 MUC_KEY = "jid" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
54 URL_KEY = "url" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
55 MUC_ATTRS = ("autojoin", "name") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
56 URL_ATTRS = ("name",) |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 def __init__(self, host): |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
989
diff
changeset
|
59 log.info(_("Bookmarks plugin initialization")) |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 self.host = host |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4001
diff
changeset
|
61 # self.__menu_id = host.register_callback(self._bookmarks_menu, with_data=True) |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4264
diff
changeset
|
62 self.__bm_save_id = host.register_callback( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4264
diff
changeset
|
63 self._bookmarks_save_cb, with_data=True |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4264
diff
changeset
|
64 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4001
diff
changeset
|
65 host.import_menu( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
66 (D_("Groups"), D_("Bookmarks")), |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4001
diff
changeset
|
67 self._bookmarks_menu, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
68 security_limit=0, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
69 help_string=D_("Use and manage bookmarks"), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
70 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4001
diff
changeset
|
71 self.__selected_id = host.register_callback( |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4001
diff
changeset
|
72 self._bookmark_selected_cb, with_data=True |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
73 ) |
4327
554a87ae17a6
plugin XEP-0048, XEP-0402; CLI (bookmarks): implement XEP-0402 (PEP Native Bookmarks):
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
74 # XXX: We're transitionning to XEP-0402, so we mark bridge method as "legacy" |
554a87ae17a6
plugin XEP-0048, XEP-0402; CLI (bookmarks): implement XEP-0402 (PEP Native Bookmarks):
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
75 # here. |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4001
diff
changeset
|
76 host.bridge.add_method( |
4327
554a87ae17a6
plugin XEP-0048, XEP-0402; CLI (bookmarks): implement XEP-0402 (PEP Native Bookmarks):
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
77 "bookmarks_legacy_list", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
78 ".plugin", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
79 in_sign="sss", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
80 out_sign="a{sa{sa{ss}}}", |
4327
554a87ae17a6
plugin XEP-0048, XEP-0402; CLI (bookmarks): implement XEP-0402 (PEP Native Bookmarks):
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
81 method=self.bookmarks_list, |
3028 | 82 async_=True, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
83 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4001
diff
changeset
|
84 host.bridge.add_method( |
4327
554a87ae17a6
plugin XEP-0048, XEP-0402; CLI (bookmarks): implement XEP-0402 (PEP Native Bookmarks):
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
85 "bookmarks_legacy_remove", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
86 ".plugin", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
87 in_sign="ssss", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
88 out_sign="", |
4327
554a87ae17a6
plugin XEP-0048, XEP-0402; CLI (bookmarks): implement XEP-0402 (PEP Native Bookmarks):
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
89 method=self.bookmarks_remove, |
3028 | 90 async_=True, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
91 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4001
diff
changeset
|
92 host.bridge.add_method( |
4327
554a87ae17a6
plugin XEP-0048, XEP-0402; CLI (bookmarks): implement XEP-0402 (PEP Native Bookmarks):
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
93 "bookmarks_legacy_add", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
94 ".plugin", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
95 in_sign="ssa{ss}ss", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
96 out_sign="", |
4327
554a87ae17a6
plugin XEP-0048, XEP-0402; CLI (bookmarks): implement XEP-0402 (PEP Native Bookmarks):
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
97 method=self.bookmarks_add, |
3028 | 98 async_=True, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
99 ) |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
100 try: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
101 self.private_plg = self.host.plugins["XEP-0049"] |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
102 except KeyError: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
103 self.private_plg = None |
983
c34e0b2bbf08
plugin XEP-0048: added /bookmark text command
Goffi <goffi@goffi.org>
parents:
982
diff
changeset
|
104 try: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4001
diff
changeset
|
105 self.host.plugins[C.TEXT_CMDS].register_text_commands(self) |
983
c34e0b2bbf08
plugin XEP-0048: added /bookmark text command
Goffi <goffi@goffi.org>
parents:
982
diff
changeset
|
106 except KeyError: |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
989
diff
changeset
|
107 log.info(_("Text commands not available")) |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
108 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4001
diff
changeset
|
109 async def profile_connected(self, client): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
110 local = client.bookmarks_local = PersistentBinaryDict( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
111 NS_BOOKMARKS, client.profile |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
112 ) |
4001
32d714a8ea51
plugin XEP-0045: dot not wait for MAM retrieval to be completed:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
113 await local.load() |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4264
diff
changeset
|
114 local = cast(dict[str, dict | None] | None, local) |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
115 if not local: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4264
diff
changeset
|
116 local = {XEP_0048.MUC_TYPE: {}, XEP_0048.URL_TYPE: {}} |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4001
diff
changeset
|
117 private = await self._get_server_bookmarks("private", client.profile) |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
118 pubsub = client.bookmarks_pubsub = None |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
119 |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
120 for bookmarks in (local, private, pubsub): |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
121 if bookmarks is not None: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4264
diff
changeset
|
122 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
|
123 if data.get("autojoin", "false") == "true": |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
124 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
|
125 defer.ensureDeferred( |
32d714a8ea51
plugin XEP-0045: dot not wait for MAM retrieval to be completed:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
126 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
|
127 ) |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
128 |
2987
8990ed9aad31
quick_frontend (contact list): fixed `nick` use for groupchat:
Goffi <goffi@goffi.org>
parents:
2879
diff
changeset
|
129 # 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
|
130 # 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
|
131 |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
132 @defer.inlineCallbacks |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4001
diff
changeset
|
133 def _get_server_bookmarks(self, storage_type, profile): |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
134 """Get distants bookmarks |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
135 |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
136 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
|
137 @param storage_type: storage type, can be: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
138 - 'private': XEP-0049 storage |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
139 - 'pubsub': XEP-0223 storage |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
140 @param profile: %(doc_profile)s |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
141 @return: data dictionary, or None if feature is not available |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
142 """ |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4001
diff
changeset
|
143 client = self.host.get_client(profile) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
144 if storage_type == "private": |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
145 try: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4001
diff
changeset
|
146 bookmarks_private_xml = yield self.private_plg.private_xml_get( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
147 "storage", NS_BOOKMARKS, profile |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
148 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4001
diff
changeset
|
149 data = client.bookmarks_private = self._bookmark_elt_2_dict( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
150 bookmarks_private_xml |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
151 ) |
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
|
152 except (StanzaError, AttributeError): |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
989
diff
changeset
|
153 log.info(_("Private XML storage not available")) |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
154 data = client.bookmarks_private = None |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
155 elif storage_type == "pubsub": |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
156 raise NotImplementedError |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
157 else: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
158 raise ValueError("storage_type must be 'private' or 'pubsub'") |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
159 defer.returnValue(data) |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
160 |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
161 @defer.inlineCallbacks |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4001
diff
changeset
|
162 def _set_server_bookmarks(self, storage_type, bookmarks_elt, profile): |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
163 """Save bookmarks on server |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
164 |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
165 @param storage_type: storage type, can be: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
166 - 'private': XEP-0049 storage |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
167 - 'pubsub': XEP-0223 storage |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
168 @param bookmarks_elt (domish.Element): bookmarks XML |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
169 @param profile: %(doc_profile)s |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
170 """ |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
171 if storage_type == "private": |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4001
diff
changeset
|
172 yield self.private_plg.private_xml_store(bookmarks_elt, profile) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
173 elif storage_type == "pubsub": |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
174 raise NotImplementedError |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
175 else: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
176 raise ValueError("storage_type must be 'private' or 'pubsub'") |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
177 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4001
diff
changeset
|
178 def _bookmark_elt_2_dict(self, storage_elt): |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
179 """Parse bookmarks to get dictionary |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
180 @param storage_elt (domish.Element): bookmarks storage |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
181 @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
|
182 - XEP_0048.MUC_TYPE |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
183 - XEP_0048.URL_TYPE |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4001
diff
changeset
|
184 - value (dict): data as for add_bookmark |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
185 """ |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
186 conf_data = {} |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
187 url_data = {} |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
188 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
189 conference_elts = storage_elt.elements(NS_BOOKMARKS, "conference") |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
190 for conference_elt in conference_elts: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
191 try: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
192 room_jid = jid.JID(conference_elt[XEP_0048.MUC_KEY]) |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
193 except KeyError: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
194 log.warning( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
195 "invalid bookmark found, igoring it:\n%s" % conference_elt.toXml() |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
196 ) |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
197 continue |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
198 |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
199 data = conf_data[room_jid] = {} |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
200 |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
201 for attr in XEP_0048.MUC_ATTRS: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
202 if conference_elt.hasAttribute(attr): |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
203 data[attr] = conference_elt[attr] |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
204 try: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4264
diff
changeset
|
205 data["nick"] = str(next(conference_elt.elements(NS_BOOKMARKS, "nick"))) |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
206 except StopIteration: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
207 pass |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
208 # 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
|
209 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
210 url_elts = storage_elt.elements(NS_BOOKMARKS, "url") |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
211 for url_elt in url_elts: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
212 try: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
213 url = url_elt[XEP_0048.URL_KEY] |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
214 except KeyError: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
215 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
|
216 continue |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
217 data = url_data[url] = {} |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
218 for attr in XEP_0048.URL_ATTRS: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
219 if url_elt.hasAttribute(attr): |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
220 data[attr] = url_elt[attr] |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
221 |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
222 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
|
223 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4001
diff
changeset
|
224 def _dict_2_bookmark_elt(self, type_, data): |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
225 """Construct a bookmark element from a data dict |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
226 @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
|
227 - XEP_0048.MUC_TYPE |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
228 - XEP_0048.URL_TYPE |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4001
diff
changeset
|
229 - value (dict): data as for add_bookmark |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
230 @return (domish.Element): bookmark element |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
231 """ |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
232 rooms_data = data.get(XEP_0048.MUC_TYPE, {}) |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
233 urls_data = data.get(XEP_0048.URL_TYPE, {}) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
234 storage_elt = domish.Element((NS_BOOKMARKS, "storage")) |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
235 for room_jid in rooms_data: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
236 conference_elt = storage_elt.addElement("conference") |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
237 conference_elt[XEP_0048.MUC_KEY] = room_jid.full() |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
238 for attr in XEP_0048.MUC_ATTRS: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
239 try: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
240 conference_elt[attr] = rooms_data[room_jid][attr] |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
241 except KeyError: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
242 pass |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
243 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
244 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
|
245 except KeyError: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
246 pass |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
247 |
3040 | 248 for url, url_data in urls_data.items(): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
249 url_elt = storage_elt.addElement("url") |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
250 url_elt[XEP_0048.URL_KEY] = url |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
251 for attr in XEP_0048.URL_ATTRS: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
252 try: |
3040 | 253 url_elt[attr] = url_data[attr] |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
254 except KeyError: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
255 pass |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
256 |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
257 return storage_elt |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
258 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4001
diff
changeset
|
259 def _bookmark_selected_cb(self, data, profile): |
984
df8e1b557125
plugin XEP-0048: added list of bookmarks in XMLUI, selecting one join the room
Goffi <goffi@goffi.org>
parents:
983
diff
changeset
|
260 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
261 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
|
262 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
|
263 except (KeyError, RuntimeError): |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
989
diff
changeset
|
264 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
|
265 return {} |
df8e1b557125
plugin XEP-0048: added list of bookmarks in XMLUI, selecting one join the room
Goffi <goffi@goffi.org>
parents:
983
diff
changeset
|
266 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4001
diff
changeset
|
267 client = self.host.get_client(profile) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
268 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
|
269 |
984
df8e1b557125
plugin XEP-0048: added list of bookmarks in XMLUI, selecting one join the room
Goffi <goffi@goffi.org>
parents:
983
diff
changeset
|
270 def join_eb(failure): |
3028 | 271 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
|
272 # 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
|
273 return {} |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
274 |
2765
378188abe941
misc: replaced all "dummy" by the more conventional and readable "__" ("_" being used for gettext)
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
275 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
|
276 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
|
277 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4001
diff
changeset
|
278 def _bookmarks_menu(self, data, profile): |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4264
diff
changeset
|
279 """XMLUI activated by menu: return Gateways UI |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
280 @param profile: %(doc_profile)s |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
281 |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
282 """ |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4001
diff
changeset
|
283 client = self.host.get_client(profile) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
284 xmlui = xml_tools.XMLUI(title=_("Bookmarks manager")) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4001
diff
changeset
|
285 adv_list = xmlui.change_container( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
286 "advanced_list", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
287 columns=3, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
288 selectable="single", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
289 callback_id=self.__selected_id, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
290 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
291 for bookmarks in ( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
292 client.bookmarks_local, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
293 client.bookmarks_private, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
294 client.bookmarks_pubsub, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
295 ): |
984
df8e1b557125
plugin XEP-0048: added list of bookmarks in XMLUI, selecting one join the room
Goffi <goffi@goffi.org>
parents:
983
diff
changeset
|
296 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
|
297 continue |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4264
diff
changeset
|
298 for room_jid, data in sorted( |
3028 | 299 list(bookmarks[XEP_0048.MUC_TYPE].items()), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
300 key=lambda item: item[1].get("name", item[0].user), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
301 ): |
984
df8e1b557125
plugin XEP-0048: added list of bookmarks in XMLUI, selecting one join the room
Goffi <goffi@goffi.org>
parents:
983
diff
changeset
|
302 room_jid_s = room_jid.full() |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4001
diff
changeset
|
303 adv_list.set_row_index( |
3028 | 304 "%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
|
305 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
306 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
|
307 xmlui.addJid(room_jid) |
2879 | 308 if C.bool(data.get("autojoin", C.BOOL_FALSE)): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
309 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
|
310 else: |
df8e1b557125
plugin XEP-0048: added list of bookmarks in XMLUI, selecting one join the room
Goffi <goffi@goffi.org>
parents:
983
diff
changeset
|
311 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
|
312 adv_list.end() |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
313 xmlui.addDivider("dash") |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
314 xmlui.addText(_("add a bookmark")) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4001
diff
changeset
|
315 xmlui.change_container("pairs") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
316 xmlui.addLabel(_("Name")) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
317 xmlui.addString("name") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
318 xmlui.addLabel(_("jid")) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
319 xmlui.addString("jid") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
320 xmlui.addLabel(_("Nickname")) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
321 xmlui.addString("nick", client.jid.user) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
322 xmlui.addLabel(_("Autojoin")) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
323 xmlui.addBool("autojoin") |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4001
diff
changeset
|
324 xmlui.change_container("vertical") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
325 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
|
326 return {"xmlui": xmlui.toXml()} |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
327 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4001
diff
changeset
|
328 def _bookmarks_save_cb(self, data, profile): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4001
diff
changeset
|
329 bm_data = xml_tools.xmlui_result_2_data_form_result(data) |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
330 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
331 location = jid.JID(bm_data.pop("jid")) |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
332 except KeyError: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
333 raise exceptions.InternalError("Can't find mandatory key") |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4001
diff
changeset
|
334 d = self.add_bookmark(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
|
335 d.addCallback(lambda __: {}) |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
336 return d |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
337 |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
338 @defer.inlineCallbacks |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4001
diff
changeset
|
339 def add_bookmark( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
340 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
|
341 ): |
983
c34e0b2bbf08
plugin XEP-0048: added /bookmark text command
Goffi <goffi@goffi.org>
parents:
982
diff
changeset
|
342 """Store a new bookmark |
c34e0b2bbf08
plugin XEP-0048: added /bookmark text command
Goffi <goffi@goffi.org>
parents:
982
diff
changeset
|
343 |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
344 @param type_: bookmark type, one of: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
345 - XEP_0048.MUC_TYPE: Multi-User chat room |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
346 - XEP_0048.URL_TYPE: web page URL |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
347 @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
|
348 @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
|
349 - name: human readable name of the bookmark |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
350 - 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
|
351 - autojoin: "true" if room must be automatically joined on connection |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
352 - password: unused yet TODO |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
353 @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
|
354 - "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
|
355 - "pubsub": PubSub private storage (XEP-0223) |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
356 - "private": Private XML storage (XEP-0049) |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
357 - "local": Store in SàT database |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
358 @param profile_key: %(doc_profile_key)s |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
359 """ |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
360 assert storage_type in ("auto", "pubsub", "private", "local") |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4264
diff
changeset
|
361 if type_ == XEP_0048.URL_TYPE and {"autojoin", "nick"}.intersection( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4264
diff
changeset
|
362 list(data.keys()) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4264
diff
changeset
|
363 ): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
364 raise ValueError("autojoin or nick can't be used with URLs") |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4001
diff
changeset
|
365 client = self.host.get_client(profile_key) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
366 if storage_type == "auto": |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
367 if client.bookmarks_pubsub is not None: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
368 storage_type = "pubsub" |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
369 elif client.bookmarks_private is not None: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
370 storage_type = "private" |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
371 else: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
372 storage_type = "local" |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
989
diff
changeset
|
373 log.warning(_("Bookmarks will be local only")) |
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
989
diff
changeset
|
374 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
|
375 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
376 if storage_type == "local": |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
377 client.bookmarks_local[type_][location] = data |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
378 yield client.bookmarks_local.force(type_) |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
379 else: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4001
diff
changeset
|
380 bookmarks = yield self._get_server_bookmarks(storage_type, client.profile) |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
381 bookmarks[type_][location] = data |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4001
diff
changeset
|
382 bookmark_elt = self._dict_2_bookmark_elt(type_, bookmarks) |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4001
diff
changeset
|
383 yield self._set_server_bookmarks(storage_type, bookmark_elt, client.profile) |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
384 |
985
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
385 @defer.inlineCallbacks |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4001
diff
changeset
|
386 def remove_bookmark( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
387 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
|
388 ): |
985
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
389 """Remove a stored bookmark |
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
390 |
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
391 @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
|
392 - 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
|
393 - 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
|
394 @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
|
395 @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
|
396 - "all": remove from everywhere |
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
397 - "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
|
398 - "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
|
399 - "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
|
400 @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
|
401 """ |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
402 assert storage_type in ("all", "pubsub", "private", "local") |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4001
diff
changeset
|
403 client = self.host.get_client(profile_key) |
985
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
404 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
405 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
|
406 try: |
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
407 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
|
408 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
|
409 except KeyError: |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
989
diff
changeset
|
410 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
|
411 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
412 if storage_type in ("all", "private"): |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4001
diff
changeset
|
413 bookmarks = yield self._get_server_bookmarks("private", client.profile) |
985
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
414 try: |
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
415 del bookmarks[type_][location] |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4001
diff
changeset
|
416 bookmark_elt = self._dict_2_bookmark_elt(type_, bookmarks) |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4001
diff
changeset
|
417 yield self._set_server_bookmarks("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
|
418 except KeyError: |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
989
diff
changeset
|
419 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
|
420 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
421 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
|
422 raise NotImplementedError |
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
423 |
4327
554a87ae17a6
plugin XEP-0048, XEP-0402; CLI (bookmarks): implement XEP-0402 (PEP Native Bookmarks):
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
424 def bookmarks_list( |
554a87ae17a6
plugin XEP-0048, XEP-0402; CLI (bookmarks): implement XEP-0402 (PEP Native Bookmarks):
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
425 self, |
554a87ae17a6
plugin XEP-0048, XEP-0402; CLI (bookmarks): implement XEP-0402 (PEP Native Bookmarks):
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
426 type_: str, |
554a87ae17a6
plugin XEP-0048, XEP-0402; CLI (bookmarks): implement XEP-0402 (PEP Native Bookmarks):
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
427 storage_location: str, |
554a87ae17a6
plugin XEP-0048, XEP-0402; CLI (bookmarks): implement XEP-0402 (PEP Native Bookmarks):
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
428 profile_key: str = C.PROF_KEY_NONE |
554a87ae17a6
plugin XEP-0048, XEP-0402; CLI (bookmarks): implement XEP-0402 (PEP Native Bookmarks):
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
429 ) -> defer.Deferred[dict]: |
986 | 430 """Return stored bookmarks |
431 | |
432 @param type_: bookmark type, one of: | |
433 - XEP_0048.MUC_TYPE: Multi-User chat room | |
434 - XEP_0048.URL_TYPE: web page URL | |
435 @param storage_location: can be: | |
436 - 'all' | |
437 - 'local' | |
438 - 'private' | |
439 - 'pubsub' | |
440 @param profile_key: %(doc_profile_key)s | |
441 @param return (dict): (key: storage_location, value dict) with: | |
442 - value (dict): (key: bookmark_location, value: bookmark data) | |
443 """ | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4001
diff
changeset
|
444 client = self.host.get_client(profile_key) |
986 | 445 ret = {} |
446 ret_d = defer.succeed(ret) | |
447 | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4001
diff
changeset
|
448 def fill_bookmarks(__, _storage_location): |
986 | 449 bookmarks_ori = getattr(client, "bookmarks_" + _storage_location) |
450 if bookmarks_ori is None: | |
451 return ret | |
4264
3fbd1a1285c1
plugin XEP-0048: don't fail if a type is missing in bookmark data.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
452 try: |
3fbd1a1285c1
plugin XEP-0048: don't fail if a type is missing in bookmark data.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
453 data = bookmarks_ori[type_] |
3fbd1a1285c1
plugin XEP-0048: don't fail if a type is missing in bookmark data.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
454 except KeyError: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4264
diff
changeset
|
455 log.warning(f"{type_!r} missing in {storage_location} storage.") |
4264
3fbd1a1285c1
plugin XEP-0048: don't fail if a type is missing in bookmark data.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
456 data = bookmarks_ori[type_] = {} |
986 | 457 for bookmark in data: |
3040 | 458 if type_ == XEP_0048.MUC_TYPE: |
459 ret[_storage_location][bookmark.full()] = data[bookmark].copy() | |
460 else: | |
461 ret[_storage_location][bookmark] = data[bookmark].copy() | |
986 | 462 return ret |
463 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
464 for _storage_location in ("local", "private", "pubsub"): |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
465 if storage_location in ("all", _storage_location): |
986 | 466 ret[_storage_location] = {} |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
467 if _storage_location in ("private",): |
4327
554a87ae17a6
plugin XEP-0048, XEP-0402; CLI (bookmarks): implement XEP-0402 (PEP Native Bookmarks):
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
468 # we update distant bookmarks, just in case an other client added |
554a87ae17a6
plugin XEP-0048, XEP-0402; CLI (bookmarks): implement XEP-0402 (PEP Native Bookmarks):
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
469 # something |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4001
diff
changeset
|
470 d = self._get_server_bookmarks(_storage_location, client.profile) |
986 | 471 else: |
472 d = defer.succeed(None) | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4001
diff
changeset
|
473 d.addCallback(fill_bookmarks, _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
|
474 ret_d.addCallback(lambda __: d) |
986 | 475 |
476 return ret_d | |
477 | |
4327
554a87ae17a6
plugin XEP-0048, XEP-0402; CLI (bookmarks): implement XEP-0402 (PEP Native Bookmarks):
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
478 def bookmarks_remove( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
479 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
|
480 ): |
986 | 481 """Return stored bookmarks |
482 | |
483 @param type_: bookmark type, one of: | |
484 - XEP_0048.MUC_TYPE: Multi-User chat room | |
485 - XEP_0048.URL_TYPE: web page URL | |
486 @param location: dependeding on type_, can be a MUC room jid or an url | |
487 @param storage_location: can be: | |
488 - "all": remove from everywhere | |
489 - "pubsub": PubSub private storage (XEP-0223) | |
490 - "private": Private XML storage (XEP-0049) | |
491 - "local": Store in SàT database | |
492 @param profile_key: %(doc_profile_key)s | |
493 """ | |
494 if type_ == XEP_0048.MUC_TYPE: | |
495 location = jid.JID(location) | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4001
diff
changeset
|
496 return self.remove_bookmark(type_, location, storage_location, profile_key) |
986 | 497 |
4327
554a87ae17a6
plugin XEP-0048, XEP-0402; CLI (bookmarks): implement XEP-0402 (PEP Native Bookmarks):
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
498 def bookmarks_add( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
499 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
|
500 ): |
986 | 501 if type_ == XEP_0048.MUC_TYPE: |
502 location = jid.JID(location) | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4001
diff
changeset
|
503 return self.add_bookmark(type_, location, data, storage_type, profile_key) |
986 | 504 |
1963
a2bc5089c2eb
backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
505 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
|
506 """(Un)bookmark a MUC room |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
507 |
985
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
508 @command (group): [autojoin | remove] |
983
c34e0b2bbf08
plugin XEP-0048: added /bookmark text command
Goffi <goffi@goffi.org>
parents:
982
diff
changeset
|
509 - autojoin: join room automatically on connection |
1373
6d0e01809893
plugin text commands: minor docstrings improvments
Goffi <goffi@goffi.org>
parents:
1371
diff
changeset
|
510 - remove: remove bookmark(s) for this room |
983
c34e0b2bbf08
plugin XEP-0048: added /bookmark text command
Goffi <goffi@goffi.org>
parents:
982
diff
changeset
|
511 """ |
c34e0b2bbf08
plugin XEP-0048: added /bookmark text command
Goffi <goffi@goffi.org>
parents:
982
diff
changeset
|
512 txt_cmd = self.host.plugins[C.TEXT_CMDS] |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
513 |
983
c34e0b2bbf08
plugin XEP-0048: added /bookmark text command
Goffi <goffi@goffi.org>
parents:
982
diff
changeset
|
514 options = mess_data["unparsed"].strip().split() |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
515 if options and options[0] not in ("autojoin", "remove"): |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4001
diff
changeset
|
516 txt_cmd.feed_back(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
|
517 return False |
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
518 |
983
c34e0b2bbf08
plugin XEP-0048: added /bookmark text command
Goffi <goffi@goffi.org>
parents:
982
diff
changeset
|
519 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
|
520 |
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
521 if "remove" in options: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4001
diff
changeset
|
522 self.remove_bookmark(XEP_0048.MUC_TYPE, room_jid, profile_key=client.profile) |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4001
diff
changeset
|
523 txt_cmd.feed_back( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
524 client, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
525 _("All [%s] bookmarks are being removed") % room_jid.full(), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
526 mess_data, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
527 ) |
985
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
528 return False |
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
529 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
530 data = { |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
531 "name": room_jid.user, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
532 "nick": client.jid.user, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
533 "autojoin": "true" if "autojoin" in options else "false", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
534 } |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4001
diff
changeset
|
535 self.add_bookmark(XEP_0048.MUC_TYPE, room_jid, data, profile_key=client.profile) |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4001
diff
changeset
|
536 txt_cmd.feed_back(client, _("Bookmark added"), mess_data) |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
537 |
983
c34e0b2bbf08
plugin XEP-0048: added /bookmark text command
Goffi <goffi@goffi.org>
parents:
982
diff
changeset
|
538 return False |