annotate src/plugins/plugin_xep_0048.py @ 1163:60ebe98c9d04

primitivus: forgot to rename _historyCb method
author Goffi <goffi@goffi.org>
date Thu, 04 Sep 2014 19:40:43 +0200
parents 291eb8216f6e
children 876c9fbd0b3d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
982
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
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)
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Jérôme Poisson (goffi@goffi.org)
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
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 989
diff changeset
26 log = getLogger(__name__)
982
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from twisted.words.xish import domish
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
28 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
29 from twisted.words.protocols.jabber.error import StanzaError
982
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
30
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
31 from twisted.internet import defer
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
32
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
33 NS_BOOKMARKS = 'storage:bookmarks'
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
34
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
35 PLUGIN_INFO = {
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
36 "name": "Bookmarks",
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
37 "import_name": "XEP-0048",
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
38 "type": "XEP",
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
39 "protocols": ["XEP-0048"],
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
40 "dependencies": ["XEP-0045"],
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
41 "recommendations": ["XEP-0049"],
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
42 "main": "XEP_0048",
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
43 "handler": "no",
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
44 "description": _("""Implementation of bookmarks""")
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
45 }
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 class XEP_0048(object):
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
49 MUC_TYPE = 'muc'
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
50 URL_TYPE = 'url'
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
51 MUC_KEY = 'jid'
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
52 URL_KEY = 'url'
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
53 MUC_ATTRS = ('autojoin', 'name')
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
54 URL_ATTRS = ('name',)
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
55
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
56 def __init__(self, host):
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 989
diff changeset
57 log.info(_("Bookmarks plugin initialization"))
982
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
58 self.host = host
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
59 # self.__menu_id = host.registerCallback(self._bookmarksMenu, with_data=True)
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
60 self.__bm_save_id = host.registerCallback(self._bookmarksSaveCb, with_data=True)
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
61 host.importMenu((D_("Communication"), D_("bookmarks")), self._bookmarksMenu, security_limit=0, help_string=D_("Use and manage bookmarks"))
984
df8e1b557125 plugin XEP-0048: added list of bookmarks in XMLUI, selecting one join the room
Goffi <goffi@goffi.org>
parents: 983
diff changeset
62 self.__selected_id = host.registerCallback(self._bookmarkSelectedCb, with_data=True)
986
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
63 host.bridge.addMethod("bookmarksList", ".plugin", in_sign='sss', out_sign='a{sa{sa{ss}}}', method=self._bookmarksList, async=True)
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
64 host.bridge.addMethod("bookmarksRemove", ".plugin", in_sign='ssss', out_sign='', method=self._bookmarksRemove, async=True)
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
65 host.bridge.addMethod("bookmarksAdd", ".plugin", in_sign='ssa{ss}ss', out_sign='', method=self._bookmarksAdd, async=True)
982
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
66 try:
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
67 self.private_plg = self.host.plugins["XEP-0049"]
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
68 except KeyError:
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
69 self.private_plg = None
983
c34e0b2bbf08 plugin XEP-0048: added /bookmark text command
Goffi <goffi@goffi.org>
parents: 982
diff changeset
70 try:
c34e0b2bbf08 plugin XEP-0048: added /bookmark text command
Goffi <goffi@goffi.org>
parents: 982
diff changeset
71 self.host.plugins[C.TEXT_CMDS].registerTextCommands(self)
c34e0b2bbf08 plugin XEP-0048: added /bookmark text command
Goffi <goffi@goffi.org>
parents: 982
diff changeset
72 except KeyError:
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 989
diff changeset
73 log.info(_("Text commands not available"))
982
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
74
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
75 @defer.inlineCallbacks
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
76 def profileConnected(self, profile):
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
77 client = self.host.getClient(profile)
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
78 local = client.bookmarks_local = PersistentBinaryDict(NS_BOOKMARKS, profile)
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
79 yield local.load()
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
80 if not local:
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
81 local[XEP_0048.MUC_TYPE] = dict()
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
82 local[XEP_0048.URL_TYPE] = dict()
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
83 private = yield self._getServerBookmarks('private', profile)
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
84 pubsub = client.bookmarks_pubsub = None
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
85
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
86 for bookmarks in (local, private, pubsub):
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
87 if bookmarks is not None:
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
88 for (room_jid, data) in bookmarks[XEP_0048.MUC_TYPE].items():
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
89 if data.get('autojoin', 'false') == 'true':
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
90 nick = data.get('nick', client.jid.user)
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
91 self.host.plugins['XEP-0045'].join(room_jid, nick, {}, profile_key=client.profile)
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
92
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
93 @defer.inlineCallbacks
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
94 def _getServerBookmarks(self, storage_type, profile):
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
95 """Get distants bookmarks
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
96
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
97 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
98 @param storage_type: storage type, can be:
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
99 - 'private': XEP-0049 storage
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
100 - 'pubsub': XEP-0223 storage
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
101 @param profile: %(doc_profile)s
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
102 @return: data dictionary, or None if feature is not available
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 client = self.host.getClient(profile)
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
105 if storage_type == 'private':
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
106 try:
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
107 bookmarks_private_xml = yield self.private_plg.privateXMLGet('storage', NS_BOOKMARKS, profile)
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
108 data = client.bookmarks_private = self._bookmarkElt2Dict(bookmarks_private_xml)
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
109 except (StanzaError, AttributeError):
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 989
diff changeset
110 log.info(_("Private XML storage not available"))
982
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
111 data = client.bookmarks_private = None
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
112 elif storage_type == 'pubsub':
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
113 raise NotImplementedError
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
114 else:
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
115 raise ValueError("storage_type must be 'private' or 'pubsub'")
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
116 defer.returnValue(data)
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
117
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
118 @defer.inlineCallbacks
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
119 def _setServerBookmarks(self, storage_type, bookmarks_elt, profile):
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
120 """Save bookmarks on server
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
121
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
122 @param storage_type: storage type, can be:
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
123 - 'private': XEP-0049 storage
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
124 - 'pubsub': XEP-0223 storage
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
125 @param bookmarks_elt (domish.Element): bookmarks XML
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
126 @param profile: %(doc_profile)s
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
127 """
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
128 if storage_type == 'private':
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
129 yield self.private_plg.privateXMLStore(bookmarks_elt, profile)
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
130 elif storage_type == 'pubsub':
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
131 raise NotImplementedError
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
132 else:
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
133 raise ValueError("storage_type must be 'private' or 'pubsub'")
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
134
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
135 def _bookmarkElt2Dict(self, storage_elt):
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
136 """Parse bookmarks to get dictionary
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
137 @param storage_elt (domish.Element): bookmarks storage
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
138 @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
139 - XEP_0048.MUC_TYPE
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
140 - XEP_0048.URL_TYPE
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
141 - value (dict): data as for addBookmark
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
142 """
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
143 conf_data = {}
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
144 url_data = {}
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
145
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
146 conference_elts = storage_elt.elements(NS_BOOKMARKS, 'conference')
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
147 for conference_elt in conference_elts:
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
148 try:
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
149 room_jid = jid.JID(conference_elt[XEP_0048.MUC_KEY])
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
150 except KeyError:
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 989
diff changeset
151 log.warning ("invalid bookmark found, igoring it:\n%s" % conference_elt.toXml())
982
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
152 continue
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
153
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
154 data = conf_data[room_jid] = {}
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 for attr in XEP_0048.MUC_ATTRS:
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
157 if conference_elt.hasAttribute(attr):
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
158 data[attr] = conference_elt[attr]
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
159 try:
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
160 data['nick'] = unicode(conference_elt.elements(NS_BOOKMARKS, 'nick').next())
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
161 except StopIteration:
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
162 pass
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
163 # 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
164
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
165 url_elts = storage_elt.elements(NS_BOOKMARKS, 'url')
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
166 for url_elt in url_elts:
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
167 try:
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
168 url = url_elt[XEP_0048.URL_KEY]
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
169 except KeyError:
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 989
diff changeset
170 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
171 continue
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
172 data = url_data[url] = {}
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
173 for attr in XEP_0048.URL_ATTRS:
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
174 if url_elt.hasAttribute(attr):
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
175 data[attr] = url_elt[attr]
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 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
178
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
179 def _dict2BookmarkElt(self, type_, data):
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
180 """Construct a bookmark element from a data dict
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
181 @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
182 - XEP_0048.MUC_TYPE
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
183 - XEP_0048.URL_TYPE
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
184 - value (dict): data as for addBookmark
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
185 @return (domish.Element): bookmark element
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
186 """
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
187 rooms_data = data.get(XEP_0048.MUC_TYPE, {})
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
188 urls_data = data.get(XEP_0048.URL_TYPE, {})
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
189 storage_elt = domish.Element((NS_BOOKMARKS, 'storage'))
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
190 for room_jid in rooms_data:
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
191 conference_elt = storage_elt.addElement('conference')
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
192 conference_elt[XEP_0048.MUC_KEY] = room_jid.full()
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
193 for attr in XEP_0048.MUC_ATTRS:
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
194 try:
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
195 conference_elt[attr] = rooms_data[room_jid][attr]
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
196 except KeyError:
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
197 pass
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
198 try:
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
199 conference_elt.addElement('nick', content=rooms_data[room_jid]['nick'])
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
200 except KeyError:
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
201 pass
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
202
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
203 for url in urls_data:
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
204 url_elt = storage_elt.addElement('url')
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
205 url_elt[XEP_0048.URL_KEY] = url
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
206 for attr in XEP_0048.URL_ATTRS:
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
207 try:
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
208 url_elt[attr] = url[attr]
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
209 except KeyError:
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
210 pass
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
211
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
212 return storage_elt
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
213
984
df8e1b557125 plugin XEP-0048: added list of bookmarks in XMLUI, selecting one join the room
Goffi <goffi@goffi.org>
parents: 983
diff changeset
214 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
215 try:
df8e1b557125 plugin XEP-0048: added list of bookmarks in XMLUI, selecting one join the room
Goffi <goffi@goffi.org>
parents: 983
diff changeset
216 room_jid_s, nick = data['index'].split(' ', 1)
df8e1b557125 plugin XEP-0048: added list of bookmarks in XMLUI, selecting one join the room
Goffi <goffi@goffi.org>
parents: 983
diff changeset
217 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
218 except (KeyError, RuntimeError):
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 989
diff changeset
219 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
220 return {}
df8e1b557125 plugin XEP-0048: added list of bookmarks in XMLUI, selecting one join the room
Goffi <goffi@goffi.org>
parents: 983
diff changeset
221
df8e1b557125 plugin XEP-0048: added list of bookmarks in XMLUI, selecting one join the room
Goffi <goffi@goffi.org>
parents: 983
diff changeset
222 d = self.host.plugins['XEP-0045'].join(room_jid, nick, {}, profile_key=profile)
df8e1b557125 plugin XEP-0048: added list of bookmarks in XMLUI, selecting one join the room
Goffi <goffi@goffi.org>
parents: 983
diff changeset
223 def join_eb(failure):
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 989
diff changeset
224 log.warning("Error while trying to join room: %s" % 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
225 # 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
226 return {}
df8e1b557125 plugin XEP-0048: added list of bookmarks in XMLUI, selecting one join the room
Goffi <goffi@goffi.org>
parents: 983
diff changeset
227 d.addCallbacks(lambda dummy: {}, join_eb)
df8e1b557125 plugin XEP-0048: added list of bookmarks in XMLUI, selecting one join the room
Goffi <goffi@goffi.org>
parents: 983
diff changeset
228 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
229
982
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
230 def _bookmarksMenu(self, data, profile):
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
231 """ XMLUI activated by menu: return Gateways UI
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
232 @param profile: %(doc_profile)s
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
233
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
234 """
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
235 client = self.host.getClient(profile)
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
236 xmlui = xml_tools.XMLUI(title=_('Bookmarks manager'))
984
df8e1b557125 plugin XEP-0048: added list of bookmarks in XMLUI, selecting one join the room
Goffi <goffi@goffi.org>
parents: 983
diff changeset
237 adv_list = xmlui.changeContainer('advanced_list', columns=3, selectable='single', callback_id=self.__selected_id)
df8e1b557125 plugin XEP-0048: added list of bookmarks in XMLUI, selecting one join the room
Goffi <goffi@goffi.org>
parents: 983
diff changeset
238 for bookmarks in (client.bookmarks_local, client.bookmarks_private, client.bookmarks_pubsub):
df8e1b557125 plugin XEP-0048: added list of bookmarks in XMLUI, selecting one join the room
Goffi <goffi@goffi.org>
parents: 983
diff changeset
239 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
240 continue
df8e1b557125 plugin XEP-0048: added list of bookmarks in XMLUI, selecting one join the room
Goffi <goffi@goffi.org>
parents: 983
diff changeset
241 for (room_jid, data) in sorted(bookmarks[XEP_0048.MUC_TYPE].items(), key=lambda item: item[1].get('name',item[0].user)):
df8e1b557125 plugin XEP-0048: added list of bookmarks in XMLUI, selecting one join the room
Goffi <goffi@goffi.org>
parents: 983
diff changeset
242 room_jid_s = room_jid.full()
df8e1b557125 plugin XEP-0048: added list of bookmarks in XMLUI, selecting one join the room
Goffi <goffi@goffi.org>
parents: 983
diff changeset
243 adv_list.setRowIndex(u'%s %s' % (room_jid_s, data.get('nick') or client.jid.user))
df8e1b557125 plugin XEP-0048: added list of bookmarks in XMLUI, selecting one join the room
Goffi <goffi@goffi.org>
parents: 983
diff changeset
244 xmlui.addText(data.get('name',''))
df8e1b557125 plugin XEP-0048: added list of bookmarks in XMLUI, selecting one join the room
Goffi <goffi@goffi.org>
parents: 983
diff changeset
245 xmlui.addJid(room_jid)
df8e1b557125 plugin XEP-0048: added list of bookmarks in XMLUI, selecting one join the room
Goffi <goffi@goffi.org>
parents: 983
diff changeset
246 if data.get('autojoin', 'false') == 'true':
df8e1b557125 plugin XEP-0048: added list of bookmarks in XMLUI, selecting one join the room
Goffi <goffi@goffi.org>
parents: 983
diff changeset
247 xmlui.addText('autojoin')
df8e1b557125 plugin XEP-0048: added list of bookmarks in XMLUI, selecting one join the room
Goffi <goffi@goffi.org>
parents: 983
diff changeset
248 else:
df8e1b557125 plugin XEP-0048: added list of bookmarks in XMLUI, selecting one join the room
Goffi <goffi@goffi.org>
parents: 983
diff changeset
249 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
250 adv_list.end()
df8e1b557125 plugin XEP-0048: added list of bookmarks in XMLUI, selecting one join the room
Goffi <goffi@goffi.org>
parents: 983
diff changeset
251 xmlui.addDivider('dash')
982
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
252 xmlui.addText(_("add a bookmark"))
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
253 xmlui.changeContainer("pairs")
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
254 xmlui.addLabel(_('Name'))
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
255 xmlui.addString('name')
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
256 xmlui.addLabel(_('jid'))
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
257 xmlui.addString('jid')
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
258 xmlui.addLabel(_('Nickname'))
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
259 xmlui.addString('nick', client.jid.user)
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
260 xmlui.addLabel(_('Autojoin'))
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
261 xmlui.addBool('autojoin')
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
262 xmlui.changeContainer("vertical")
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
263 xmlui.addButton(self.__bm_save_id, _("Save"), ('name', 'jid', 'nick', 'autojoin'))
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
264 return {'xmlui': xmlui.toXml()}
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
265
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
266 def _bookmarksSaveCb(self, data, profile):
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
267 bm_data = xml_tools.XMLUIResult2DataFormResult(data)
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
268 try:
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
269 location = jid.JID(bm_data.pop('jid'))
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
270 except KeyError:
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
271 raise exceptions.InternalError("Can't find mandatory key")
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
272 d = self.addBookmark(XEP_0048.MUC_TYPE, location, bm_data, profile_key=profile)
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
273 d.addCallback(lambda dummy: {})
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
274 return d
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 @defer.inlineCallbacks
983
c34e0b2bbf08 plugin XEP-0048: added /bookmark text command
Goffi <goffi@goffi.org>
parents: 982
diff changeset
277 def addBookmark(self, type_, location, data, storage_type="auto", profile_key=C.PROF_KEY_NONE):
c34e0b2bbf08 plugin XEP-0048: added /bookmark text command
Goffi <goffi@goffi.org>
parents: 982
diff changeset
278 """Store a new bookmark
c34e0b2bbf08 plugin XEP-0048: added /bookmark text command
Goffi <goffi@goffi.org>
parents: 982
diff changeset
279
982
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
280 @param type_: bookmark type, one of:
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
281 - XEP_0048.MUC_TYPE: Multi-User chat room
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
282 - XEP_0048.URL_TYPE: web page URL
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
283 @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
284 @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
285 - name: human readable name of the bookmark
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
286 - 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
287 - autojoin: "true" if room must be automatically joined on connection
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
288 - password: unused yet TODO
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
289 @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
290 - "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
291 - "pubsub": PubSub private storage (XEP-0223)
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
292 - "private": Private XML storage (XEP-0049)
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
293 - "local": Store in SàT database
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
294 @param profile_key: %(doc_profile_key)s
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
295 """
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
296 assert storage_type in ('auto', 'pubsub', 'private', 'local')
986
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
297 if type_ == XEP_0048.URL_TYPE and {'autojoin', 'nick'}.intersection(data.keys()):
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
298 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
299 client = self.host.getClient(profile_key)
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
300 if storage_type == 'auto':
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
301 if client.bookmarks_pubsub is not None:
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
302 storage_type = 'pubsub'
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
303 elif client.bookmarks_private is not None:
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
304 storage_type = 'private'
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
305 else:
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
306 storage_type = 'local'
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 989
diff changeset
307 log.warning(_("Bookmarks will be local only"))
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 989
diff changeset
308 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
309
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
310 if storage_type == 'local':
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
311 client.bookmarks_local[type_][location] = data
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
312 yield client.bookmarks_local.force(type_)
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
313 else:
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
314 bookmarks = yield self._getServerBookmarks(storage_type, client.profile)
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
315 bookmarks[type_][location] = data
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
316 bookmark_elt = self._dict2BookmarkElt(type_, bookmarks)
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
317 yield self._setServerBookmarks(storage_type, bookmark_elt, client.profile)
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
318
985
9ebdba4ab907 plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents: 984
diff changeset
319 @defer.inlineCallbacks
9ebdba4ab907 plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents: 984
diff changeset
320 def removeBookmark(self, type_, location, storage_type="all", profile_key=C.PROF_KEY_NONE):
9ebdba4ab907 plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents: 984
diff changeset
321 """Remove a stored bookmark
9ebdba4ab907 plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents: 984
diff changeset
322
9ebdba4ab907 plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents: 984
diff changeset
323 @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
324 - 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
325 - 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
326 @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
327 @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
328 - "all": remove from everywhere
9ebdba4ab907 plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents: 984
diff changeset
329 - "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
330 - "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
331 - "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
332 @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
333 """
9ebdba4ab907 plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents: 984
diff changeset
334 assert storage_type in ('all', 'pubsub', 'private', 'local')
9ebdba4ab907 plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents: 984
diff changeset
335 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
336
9ebdba4ab907 plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents: 984
diff changeset
337 if storage_type in ('all', 'local'):
9ebdba4ab907 plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents: 984
diff changeset
338 try:
9ebdba4ab907 plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents: 984
diff changeset
339 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
340 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
341 except KeyError:
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 989
diff changeset
342 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
343
9ebdba4ab907 plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents: 984
diff changeset
344 if storage_type in ('all', 'private'):
9ebdba4ab907 plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents: 984
diff changeset
345 bookmarks = yield self._getServerBookmarks('private', client.profile)
9ebdba4ab907 plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents: 984
diff changeset
346 try:
9ebdba4ab907 plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents: 984
diff changeset
347 del bookmarks[type_][location]
9ebdba4ab907 plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents: 984
diff changeset
348 bookmark_elt = self._dict2BookmarkElt(type_, bookmarks)
9ebdba4ab907 plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents: 984
diff changeset
349 yield self._setServerBookmarks('private', bookmark_elt, client.profile)
9ebdba4ab907 plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents: 984
diff changeset
350 except KeyError:
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 989
diff changeset
351 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
352
9ebdba4ab907 plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents: 984
diff changeset
353 if storage_type == 'pubsub':
9ebdba4ab907 plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents: 984
diff changeset
354 raise NotImplementedError
9ebdba4ab907 plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents: 984
diff changeset
355
986
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
356 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
357 """Return stored bookmarks
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
358
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
359 @param type_: bookmark type, one of:
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
360 - XEP_0048.MUC_TYPE: Multi-User chat room
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
361 - XEP_0048.URL_TYPE: web page URL
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
362 @param storage_location: can be:
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
363 - 'all'
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
364 - 'local'
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
365 - 'private'
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
366 - 'pubsub'
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
367 @param profile_key: %(doc_profile_key)s
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
368 @param return (dict): (key: storage_location, value dict) with:
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
369 - value (dict): (key: bookmark_location, value: bookmark data)
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
370 """
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
371 client = self.host.getClient(profile_key)
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
372 ret = {}
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
373 ret_d = defer.succeed(ret)
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
374
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
375 def fillBookmarks(dummy, _storage_location):
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
376 bookmarks_ori = getattr(client, "bookmarks_" + _storage_location)
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
377 if bookmarks_ori is None:
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
378 return ret
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
379 data = bookmarks_ori[type_]
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
380 for bookmark in data:
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
381 ret[_storage_location][bookmark.full()] = data[bookmark].copy()
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
382 return ret
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
383
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
384 for _storage_location in ('local', 'private', 'pubsub'):
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
385 if storage_location in ('all', _storage_location):
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
386 ret[_storage_location] = {}
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
387 if _storage_location in ('private',):
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
388 # 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
389 d = self._getServerBookmarks(_storage_location, client.profile)
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
390 else:
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
391 d = defer.succeed(None)
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
392 d.addCallback(fillBookmarks, _storage_location)
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
393 ret_d.addCallback(lambda dummy: d)
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
394
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
395 return ret_d
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
396
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
397 def _bookmarksRemove(self, type_, location, storage_location, profile_key=C.PROF_KEY_NONE):
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
398 """Return stored bookmarks
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
399
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
400 @param type_: bookmark type, one of:
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
401 - XEP_0048.MUC_TYPE: Multi-User chat room
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
402 - XEP_0048.URL_TYPE: web page URL
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
403 @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
404 @param storage_location: can be:
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
405 - "all": remove from everywhere
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
406 - "pubsub": PubSub private storage (XEP-0223)
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
407 - "private": Private XML storage (XEP-0049)
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
408 - "local": Store in SàT database
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
409 @param profile_key: %(doc_profile_key)s
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
410 """
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
411 if type_ == XEP_0048.MUC_TYPE:
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
412 location = jid.JID(location)
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
413 return self.removeBookmark(type_, location, storage_location, profile_key)
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
414
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
415 def _bookmarksAdd(self, type_, location, data, storage_type="auto", profile_key=C.PROF_KEY_NONE):
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
416 if type_ == XEP_0048.MUC_TYPE:
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
417 location = jid.JID(location)
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
418 return self.addBookmark(type_, location, data, storage_type, profile_key)
224cafc67324 jp: added bookmarks subcommands
Goffi <goffi@goffi.org>
parents: 985
diff changeset
419
983
c34e0b2bbf08 plugin XEP-0048: added /bookmark text command
Goffi <goffi@goffi.org>
parents: 982
diff changeset
420 def cmd_bookmark(self, mess_data, profile):
985
9ebdba4ab907 plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents: 984
diff changeset
421 """(Un)bookmark a MUC room
982
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
422
985
9ebdba4ab907 plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents: 984
diff changeset
423 @command (group): [autojoin | remove]
983
c34e0b2bbf08 plugin XEP-0048: added /bookmark text command
Goffi <goffi@goffi.org>
parents: 982
diff changeset
424 - autojoin: join room automatically on connection
c34e0b2bbf08 plugin XEP-0048: added /bookmark text command
Goffi <goffi@goffi.org>
parents: 982
diff changeset
425 """
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 989
diff changeset
426 log.debug("Catched bookmark command")
983
c34e0b2bbf08 plugin XEP-0048: added /bookmark text command
Goffi <goffi@goffi.org>
parents: 982
diff changeset
427 client = self.host.getClient(profile)
c34e0b2bbf08 plugin XEP-0048: added /bookmark text command
Goffi <goffi@goffi.org>
parents: 982
diff changeset
428 txt_cmd = self.host.plugins[C.TEXT_CMDS]
982
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
429
983
c34e0b2bbf08 plugin XEP-0048: added /bookmark text command
Goffi <goffi@goffi.org>
parents: 982
diff changeset
430 if mess_data['type'] != "groupchat":
1002
291eb8216f6e plugins TEXT-COMMANDS, XEP-0045, XEP-0048, XEP-0249:
souliane <souliane@mailoo.org>
parents: 993
diff changeset
431 self.host.plugins[C.TEXT_CMDS].feedBackWrongContext('bookmark', 'groupchat', mess_data, profile)
291eb8216f6e plugins TEXT-COMMANDS, XEP-0045, XEP-0048, XEP-0249:
souliane <souliane@mailoo.org>
parents: 993
diff changeset
432 return False
982
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
433
983
c34e0b2bbf08 plugin XEP-0048: added /bookmark text command
Goffi <goffi@goffi.org>
parents: 982
diff changeset
434 options = mess_data["unparsed"].strip().split()
985
9ebdba4ab907 plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents: 984
diff changeset
435 if options and options[0] not in ('autojoin', 'remove'):
9ebdba4ab907 plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents: 984
diff changeset
436 txt_cmd.feedBack(_("Bad arguments"), mess_data, profile)
9ebdba4ab907 plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents: 984
diff changeset
437 return False
9ebdba4ab907 plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents: 984
diff changeset
438
983
c34e0b2bbf08 plugin XEP-0048: added /bookmark text command
Goffi <goffi@goffi.org>
parents: 982
diff changeset
439 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
440
9ebdba4ab907 plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents: 984
diff changeset
441 if "remove" in options:
9ebdba4ab907 plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents: 984
diff changeset
442 self.removeBookmark(XEP_0048.MUC_TYPE, room_jid, profile_key = profile)
9ebdba4ab907 plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents: 984
diff changeset
443 txt_cmd.feedBack(_("All [%s] bookmarks are being removed") % room_jid.full(), mess_data, profile)
9ebdba4ab907 plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents: 984
diff changeset
444 return False
9ebdba4ab907 plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
Goffi <goffi@goffi.org>
parents: 984
diff changeset
445
983
c34e0b2bbf08 plugin XEP-0048: added /bookmark text command
Goffi <goffi@goffi.org>
parents: 982
diff changeset
446 data = { "name": room_jid.user,
c34e0b2bbf08 plugin XEP-0048: added /bookmark text command
Goffi <goffi@goffi.org>
parents: 982
diff changeset
447 "nick": client.jid.user,
c34e0b2bbf08 plugin XEP-0048: added /bookmark text command
Goffi <goffi@goffi.org>
parents: 982
diff changeset
448 "autojoin": "true" if "autojoin" in options else "false",
c34e0b2bbf08 plugin XEP-0048: added /bookmark text command
Goffi <goffi@goffi.org>
parents: 982
diff changeset
449 }
c34e0b2bbf08 plugin XEP-0048: added /bookmark text command
Goffi <goffi@goffi.org>
parents: 982
diff changeset
450 self.addBookmark(XEP_0048.MUC_TYPE, room_jid, data, profile_key=profile)
c34e0b2bbf08 plugin XEP-0048: added /bookmark text command
Goffi <goffi@goffi.org>
parents: 982
diff changeset
451 txt_cmd.feedBack(_("Bookmark added"), mess_data, profile)
982
0e80ee1fe9af plugin XEP-0048: bookmarks (first draft)
Goffi <goffi@goffi.org>
parents:
diff changeset
452
983
c34e0b2bbf08 plugin XEP-0048: added /bookmark text command
Goffi <goffi@goffi.org>
parents: 982
diff changeset
453 return False