annotate libervia/backend/plugins/plugin_xep_0048.py @ 4259:49019947cc76

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