Mercurial > libervia-backend
annotate sat/plugins/plugin_xep_0048.py @ 2904:5bba8953061e
primitivus (chat): fixed crash when removing focus_marker after a :search or :history command
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 10 Apr 2019 21:07:47 +0200 |
parents | 5ba98fd6c9a4 |
children | 8990ed9aad31 |
rev | line source |
---|---|
1934
2daf7b4c6756
use of /usr/bin/env instead of /usr/bin/python in shebang
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
1 #!/usr/bin/env python2 |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
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) |
2771 | 5 # Copyright (C) 2009-2019 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, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
77 async=True, |
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, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
85 async=True, |
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, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
93 async=True, |
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: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
118 for (room_jid, data) in 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 |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
123 @defer.inlineCallbacks |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
124 def _getServerBookmarks(self, storage_type, profile): |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
125 """Get distants bookmarks |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
126 |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
127 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
|
128 @param storage_type: storage type, can be: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
129 - 'private': XEP-0049 storage |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
130 - 'pubsub': XEP-0223 storage |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
131 @param profile: %(doc_profile)s |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
132 @return: data dictionary, or None if feature is not available |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
133 """ |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
134 client = self.host.getClient(profile) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
135 if storage_type == "private": |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
136 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
137 bookmarks_private_xml = yield self.private_plg.privateXMLGet( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
138 "storage", NS_BOOKMARKS, profile |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
139 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
140 data = client.bookmarks_private = self._bookmarkElt2Dict( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
141 bookmarks_private_xml |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
142 ) |
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
|
143 except (StanzaError, AttributeError): |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
989
diff
changeset
|
144 log.info(_("Private XML storage not available")) |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
145 data = client.bookmarks_private = None |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
146 elif storage_type == "pubsub": |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
147 raise NotImplementedError |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
148 else: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
149 raise ValueError("storage_type must be 'private' or 'pubsub'") |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
150 defer.returnValue(data) |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
151 |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
152 @defer.inlineCallbacks |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
153 def _setServerBookmarks(self, storage_type, bookmarks_elt, profile): |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
154 """Save bookmarks on server |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
155 |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
156 @param storage_type: storage type, can be: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
157 - 'private': XEP-0049 storage |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
158 - 'pubsub': XEP-0223 storage |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
159 @param bookmarks_elt (domish.Element): bookmarks XML |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
160 @param profile: %(doc_profile)s |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
161 """ |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
162 if storage_type == "private": |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
163 yield self.private_plg.privateXMLStore(bookmarks_elt, profile) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
164 elif storage_type == "pubsub": |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
165 raise NotImplementedError |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
166 else: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
167 raise ValueError("storage_type must be 'private' or 'pubsub'") |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
168 |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
169 def _bookmarkElt2Dict(self, storage_elt): |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
170 """Parse bookmarks to get dictionary |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
171 @param storage_elt (domish.Element): bookmarks storage |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
172 @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
|
173 - XEP_0048.MUC_TYPE |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
174 - XEP_0048.URL_TYPE |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
175 - value (dict): data as for addBookmark |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
176 """ |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
177 conf_data = {} |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
178 url_data = {} |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
179 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
180 conference_elts = storage_elt.elements(NS_BOOKMARKS, "conference") |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
181 for conference_elt in conference_elts: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
182 try: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
183 room_jid = jid.JID(conference_elt[XEP_0048.MUC_KEY]) |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
184 except KeyError: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
185 log.warning( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
186 "invalid bookmark found, igoring it:\n%s" % conference_elt.toXml() |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
187 ) |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
188 continue |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
189 |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
190 data = conf_data[room_jid] = {} |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
191 |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
192 for attr in XEP_0048.MUC_ATTRS: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
193 if conference_elt.hasAttribute(attr): |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
194 data[attr] = conference_elt[attr] |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
195 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
196 data["nick"] = unicode( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
197 conference_elt.elements(NS_BOOKMARKS, "nick").next() |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
198 ) |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
199 except StopIteration: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
200 pass |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
201 # 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
|
202 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
203 url_elts = storage_elt.elements(NS_BOOKMARKS, "url") |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
204 for url_elt in url_elts: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
205 try: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
206 url = url_elt[XEP_0048.URL_KEY] |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
207 except KeyError: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
208 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
|
209 continue |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
210 data = url_data[url] = {} |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
211 for attr in XEP_0048.URL_ATTRS: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
212 if url_elt.hasAttribute(attr): |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
213 data[attr] = url_elt[attr] |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
214 |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
215 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
|
216 |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
217 def _dict2BookmarkElt(self, type_, data): |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
218 """Construct a bookmark element from a data dict |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
219 @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
|
220 - XEP_0048.MUC_TYPE |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
221 - XEP_0048.URL_TYPE |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
222 - value (dict): data as for addBookmark |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
223 @return (domish.Element): bookmark element |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
224 """ |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
225 rooms_data = data.get(XEP_0048.MUC_TYPE, {}) |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
226 urls_data = data.get(XEP_0048.URL_TYPE, {}) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
227 storage_elt = domish.Element((NS_BOOKMARKS, "storage")) |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
228 for room_jid in rooms_data: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
229 conference_elt = storage_elt.addElement("conference") |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
230 conference_elt[XEP_0048.MUC_KEY] = room_jid.full() |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
231 for attr in XEP_0048.MUC_ATTRS: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
232 try: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
233 conference_elt[attr] = rooms_data[room_jid][attr] |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
234 except KeyError: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
235 pass |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
236 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
237 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
|
238 except KeyError: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
239 pass |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
240 |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
241 for url in urls_data: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
242 url_elt = storage_elt.addElement("url") |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
243 url_elt[XEP_0048.URL_KEY] = url |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
244 for attr in XEP_0048.URL_ATTRS: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
245 try: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
246 url_elt[attr] = url[attr] |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
247 except KeyError: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
248 pass |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
249 |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
250 return storage_elt |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
251 |
984
df8e1b557125
plugin XEP-0048: added list of bookmarks in XMLUI, selecting one join the room
Goffi <goffi@goffi.org>
parents:
983
diff
changeset
|
252 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
|
253 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
254 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
|
255 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
|
256 except (KeyError, RuntimeError): |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
989
diff
changeset
|
257 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
|
258 return {} |
df8e1b557125
plugin XEP-0048: added list of bookmarks in XMLUI, selecting one join the room
Goffi <goffi@goffi.org>
parents:
983
diff
changeset
|
259 |
1963
a2bc5089c2eb
backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
260 client = self.host.getClient(profile) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
261 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
|
262 |
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 def join_eb(failure): |
1963
a2bc5089c2eb
backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
264 log.warning(u"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
|
265 # 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
|
266 return {} |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
267 |
2765
378188abe941
misc: replaced all "dummy" by the more conventional and readable "__" ("_" being used for gettext)
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
268 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
|
269 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
|
270 |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
271 def _bookmarksMenu(self, data, profile): |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
272 """ XMLUI activated by menu: return Gateways UI |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
273 @param profile: %(doc_profile)s |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
274 |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
275 """ |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
276 client = self.host.getClient(profile) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
277 xmlui = xml_tools.XMLUI(title=_("Bookmarks manager")) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
278 adv_list = xmlui.changeContainer( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
279 "advanced_list", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
280 columns=3, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
281 selectable="single", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
282 callback_id=self.__selected_id, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
283 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
284 for bookmarks in ( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
285 client.bookmarks_local, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
286 client.bookmarks_private, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
287 client.bookmarks_pubsub, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
288 ): |
984
df8e1b557125
plugin XEP-0048: added list of bookmarks in XMLUI, selecting one join the room
Goffi <goffi@goffi.org>
parents:
983
diff
changeset
|
289 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
|
290 continue |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
291 for (room_jid, data) in sorted( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
292 bookmarks[XEP_0048.MUC_TYPE].items(), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
293 key=lambda item: item[1].get("name", item[0].user), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
294 ): |
984
df8e1b557125
plugin XEP-0048: added list of bookmarks in XMLUI, selecting one join the room
Goffi <goffi@goffi.org>
parents:
983
diff
changeset
|
295 room_jid_s = room_jid.full() |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
296 adv_list.setRowIndex( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
297 u"%s %s" % (room_jid_s, data.get("nick") or client.jid.user) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
298 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
299 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
|
300 xmlui.addJid(room_jid) |
2879 | 301 if C.bool(data.get("autojoin", C.BOOL_FALSE)): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
302 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
|
303 else: |
df8e1b557125
plugin XEP-0048: added list of bookmarks in XMLUI, selecting one join the room
Goffi <goffi@goffi.org>
parents:
983
diff
changeset
|
304 xmlui.addEmpty() |
df8e1b557125
plugin XEP-0048: added list of bookmarks in XMLUI, selecting one join the room
Goffi <goffi@goffi.org>
parents:
983
diff
changeset
|
305 adv_list.end() |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
306 xmlui.addDivider("dash") |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
307 xmlui.addText(_("add a bookmark")) |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
308 xmlui.changeContainer("pairs") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
309 xmlui.addLabel(_("Name")) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
310 xmlui.addString("name") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
311 xmlui.addLabel(_("jid")) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
312 xmlui.addString("jid") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
313 xmlui.addLabel(_("Nickname")) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
314 xmlui.addString("nick", client.jid.user) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
315 xmlui.addLabel(_("Autojoin")) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
316 xmlui.addBool("autojoin") |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
317 xmlui.changeContainer("vertical") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
318 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
|
319 return {"xmlui": xmlui.toXml()} |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
320 |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
321 def _bookmarksSaveCb(self, data, profile): |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
322 bm_data = xml_tools.XMLUIResult2DataFormResult(data) |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
323 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
324 location = jid.JID(bm_data.pop("jid")) |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
325 except KeyError: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
326 raise exceptions.InternalError("Can't find mandatory key") |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
327 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
|
328 d.addCallback(lambda __: {}) |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
329 return d |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
330 |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
331 @defer.inlineCallbacks |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
332 def addBookmark( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
333 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
|
334 ): |
983
c34e0b2bbf08
plugin XEP-0048: added /bookmark text command
Goffi <goffi@goffi.org>
parents:
982
diff
changeset
|
335 """Store a new bookmark |
c34e0b2bbf08
plugin XEP-0048: added /bookmark text command
Goffi <goffi@goffi.org>
parents:
982
diff
changeset
|
336 |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
337 @param type_: bookmark type, one of: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
338 - XEP_0048.MUC_TYPE: Multi-User chat room |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
339 - XEP_0048.URL_TYPE: web page URL |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
340 @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
|
341 @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
|
342 - name: human readable name of the bookmark |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
343 - 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
|
344 - autojoin: "true" if room must be automatically joined on connection |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
345 - password: unused yet TODO |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
346 @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
|
347 - "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
|
348 - "pubsub": PubSub private storage (XEP-0223) |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
349 - "private": Private XML storage (XEP-0049) |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
350 - "local": Store in SàT database |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
351 @param profile_key: %(doc_profile_key)s |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
352 """ |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
353 assert storage_type in ("auto", "pubsub", "private", "local") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
354 if type_ == XEP_0048.URL_TYPE and {"autojoin", "nick"}.intersection(data.keys()): |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
355 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
|
356 client = self.host.getClient(profile_key) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
357 if storage_type == "auto": |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
358 if client.bookmarks_pubsub is not None: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
359 storage_type = "pubsub" |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
360 elif client.bookmarks_private is not None: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
361 storage_type = "private" |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
362 else: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
363 storage_type = "local" |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
989
diff
changeset
|
364 log.warning(_("Bookmarks will be local only")) |
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
989
diff
changeset
|
365 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
|
366 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
367 if storage_type == "local": |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
368 client.bookmarks_local[type_][location] = data |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
369 yield client.bookmarks_local.force(type_) |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
370 else: |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
371 bookmarks = yield self._getServerBookmarks(storage_type, client.profile) |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
372 bookmarks[type_][location] = data |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
373 bookmark_elt = self._dict2BookmarkElt(type_, bookmarks) |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
374 yield self._setServerBookmarks(storage_type, bookmark_elt, client.profile) |
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
375 |
985
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
376 @defer.inlineCallbacks |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
377 def removeBookmark( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
378 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
|
379 ): |
985
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
380 """Remove a stored bookmark |
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
381 |
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
382 @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
|
383 - 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
|
384 - 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
|
385 @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
|
386 @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
|
387 - "all": remove from everywhere |
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
388 - "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
|
389 - "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
|
390 - "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
|
391 @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
|
392 """ |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
393 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
|
394 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
|
395 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
396 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
|
397 try: |
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
398 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
|
399 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
|
400 except KeyError: |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
989
diff
changeset
|
401 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
|
402 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
403 if storage_type in ("all", "private"): |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
404 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
|
405 try: |
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
406 del bookmarks[type_][location] |
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
407 bookmark_elt = self._dict2BookmarkElt(type_, bookmarks) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
408 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
|
409 except KeyError: |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
989
diff
changeset
|
410 log.debug("Bookmark is not present in private storage") |
985
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
411 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
412 if storage_type == "pubsub": |
985
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
413 raise NotImplementedError |
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
414 |
986 | 415 def _bookmarksList(self, type_, storage_location, profile_key=C.PROF_KEY_NONE): |
416 """Return stored bookmarks | |
417 | |
418 @param type_: bookmark type, one of: | |
419 - XEP_0048.MUC_TYPE: Multi-User chat room | |
420 - XEP_0048.URL_TYPE: web page URL | |
421 @param storage_location: can be: | |
422 - 'all' | |
423 - 'local' | |
424 - 'private' | |
425 - 'pubsub' | |
426 @param profile_key: %(doc_profile_key)s | |
427 @param return (dict): (key: storage_location, value dict) with: | |
428 - value (dict): (key: bookmark_location, value: bookmark data) | |
429 """ | |
430 client = self.host.getClient(profile_key) | |
431 ret = {} | |
432 ret_d = defer.succeed(ret) | |
433 | |
2765
378188abe941
misc: replaced all "dummy" by the more conventional and readable "__" ("_" being used for gettext)
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
434 def fillBookmarks(__, _storage_location): |
986 | 435 bookmarks_ori = getattr(client, "bookmarks_" + _storage_location) |
436 if bookmarks_ori is None: | |
437 return ret | |
438 data = bookmarks_ori[type_] | |
439 for bookmark in data: | |
440 ret[_storage_location][bookmark.full()] = data[bookmark].copy() | |
441 return ret | |
442 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
443 for _storage_location in ("local", "private", "pubsub"): |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
444 if storage_location in ("all", _storage_location): |
986 | 445 ret[_storage_location] = {} |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
446 if _storage_location in ("private",): |
986 | 447 # we update distant bookmarks, just in case an other client added something |
448 d = self._getServerBookmarks(_storage_location, client.profile) | |
449 else: | |
450 d = defer.succeed(None) | |
451 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
|
452 ret_d.addCallback(lambda __: d) |
986 | 453 |
454 return ret_d | |
455 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
456 def _bookmarksRemove( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
457 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
|
458 ): |
986 | 459 """Return stored bookmarks |
460 | |
461 @param type_: bookmark type, one of: | |
462 - XEP_0048.MUC_TYPE: Multi-User chat room | |
463 - XEP_0048.URL_TYPE: web page URL | |
464 @param location: dependeding on type_, can be a MUC room jid or an url | |
465 @param storage_location: can be: | |
466 - "all": remove from everywhere | |
467 - "pubsub": PubSub private storage (XEP-0223) | |
468 - "private": Private XML storage (XEP-0049) | |
469 - "local": Store in SàT database | |
470 @param profile_key: %(doc_profile_key)s | |
471 """ | |
472 if type_ == XEP_0048.MUC_TYPE: | |
473 location = jid.JID(location) | |
474 return self.removeBookmark(type_, location, storage_location, profile_key) | |
475 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
476 def _bookmarksAdd( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
477 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
|
478 ): |
986 | 479 if type_ == XEP_0048.MUC_TYPE: |
480 location = jid.JID(location) | |
481 return self.addBookmark(type_, location, data, storage_type, profile_key) | |
482 | |
1963
a2bc5089c2eb
backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
483 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
|
484 """(Un)bookmark a MUC room |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
485 |
985
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
486 @command (group): [autojoin | remove] |
983
c34e0b2bbf08
plugin XEP-0048: added /bookmark text command
Goffi <goffi@goffi.org>
parents:
982
diff
changeset
|
487 - autojoin: join room automatically on connection |
1373
6d0e01809893
plugin text commands: minor docstrings improvments
Goffi <goffi@goffi.org>
parents:
1371
diff
changeset
|
488 - remove: remove bookmark(s) for this room |
983
c34e0b2bbf08
plugin XEP-0048: added /bookmark text command
Goffi <goffi@goffi.org>
parents:
982
diff
changeset
|
489 """ |
c34e0b2bbf08
plugin XEP-0048: added /bookmark text command
Goffi <goffi@goffi.org>
parents:
982
diff
changeset
|
490 txt_cmd = self.host.plugins[C.TEXT_CMDS] |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
491 |
983
c34e0b2bbf08
plugin XEP-0048: added /bookmark text command
Goffi <goffi@goffi.org>
parents:
982
diff
changeset
|
492 options = mess_data["unparsed"].strip().split() |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
493 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
|
494 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
|
495 return False |
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
496 |
983
c34e0b2bbf08
plugin XEP-0048: added /bookmark text command
Goffi <goffi@goffi.org>
parents:
982
diff
changeset
|
497 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
|
498 |
9ebdba4ab907
plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents:
984
diff
changeset
|
499 if "remove" in options: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
500 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
|
501 txt_cmd.feedBack( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
502 client, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
503 _("All [%s] bookmarks are being removed") % room_jid.full(), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
504 mess_data, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
505 ) |
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 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
508 data = { |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
509 "name": room_jid.user, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
510 "nick": client.jid.user, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
511 "autojoin": "true" if "autojoin" in options else "false", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
512 } |
1963
a2bc5089c2eb
backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
513 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
|
514 txt_cmd.feedBack(client, _("Bookmark added"), mess_data) |
982
0e80ee1fe9af
plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
515 |
983
c34e0b2bbf08
plugin XEP-0048: added /bookmark text command
Goffi <goffi@goffi.org>
parents:
982
diff
changeset
|
516 return False |