annotate sat/plugins/plugin_xep_0048.py @ 3375:b41ed8d9d40b

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