Mercurial > libervia-backend
comparison src/plugins/plugin_xep_0048.py @ 983:c34e0b2bbf08
plugin XEP-0048: added /bookmark text command
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 07 Apr 2014 16:22:35 +0200 |
parents | 0e80ee1fe9af |
children | df8e1b557125 |
comparison
equal
deleted
inserted
replaced
982:0e80ee1fe9af | 983:c34e0b2bbf08 |
---|---|
17 # You should have received a copy of the GNU Affero General Public License | 17 # You should have received a copy of the GNU Affero General Public License |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | 18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 | 19 |
20 from sat.core.i18n import _, D_ | 20 from sat.core.i18n import _, D_ |
21 from sat.core import exceptions | 21 from sat.core import exceptions |
22 from sat.core.constants import Const as C | |
22 from sat.memory.persistent import PersistentBinaryDict | 23 from sat.memory.persistent import PersistentBinaryDict |
23 from sat.tools import xml_tools | 24 from sat.tools import xml_tools |
24 from logging import debug, info, warning, error | 25 from logging import debug, info, warning, error |
25 from twisted.words.xish import domish | 26 from twisted.words.xish import domish |
26 from twisted.words.protocols.jabber import jid | 27 from twisted.words.protocols.jabber import jid |
58 host.importMenu((D_("Communication"), D_("bookmarks")), self._bookmarksMenu, security_limit=0, help_string=D_("Use and manage bookmarks")) | 59 host.importMenu((D_("Communication"), D_("bookmarks")), self._bookmarksMenu, security_limit=0, help_string=D_("Use and manage bookmarks")) |
59 try: | 60 try: |
60 self.private_plg = self.host.plugins["XEP-0049"] | 61 self.private_plg = self.host.plugins["XEP-0049"] |
61 except KeyError: | 62 except KeyError: |
62 self.private_plg = None | 63 self.private_plg = None |
64 try: | |
65 self.host.plugins[C.TEXT_CMDS].registerTextCommands(self) | |
66 except KeyError: | |
67 info(_("Text commands not available")) | |
63 | 68 |
64 @defer.inlineCallbacks | 69 @defer.inlineCallbacks |
65 def profileConnected(self, profile): | 70 def profileConnected(self, profile): |
66 client = self.host.getClient(profile) | 71 client = self.host.getClient(profile) |
67 local = client.bookmarks_local = PersistentBinaryDict(NS_BOOKMARKS, profile) | 72 local = client.bookmarks_local = PersistentBinaryDict(NS_BOOKMARKS, profile) |
232 d = self.addBookmark(XEP_0048.MUC_TYPE, location, bm_data, profile_key=profile) | 237 d = self.addBookmark(XEP_0048.MUC_TYPE, location, bm_data, profile_key=profile) |
233 d.addCallback(lambda dummy: {}) | 238 d.addCallback(lambda dummy: {}) |
234 return d | 239 return d |
235 | 240 |
236 @defer.inlineCallbacks | 241 @defer.inlineCallbacks |
237 def addBookmark(self, type_, location, data, storage_type="auto", profile_key="@NONE@"): | 242 def addBookmark(self, type_, location, data, storage_type="auto", profile_key=C.PROF_KEY_NONE): |
238 """ Store a new bookmark | 243 """Store a new bookmark |
244 | |
239 @param type_: bookmark type, one of: | 245 @param type_: bookmark type, one of: |
240 - XEP_0048.MUC_TYPE: Multi-User chat room | 246 - XEP_0048.MUC_TYPE: Multi-User chat room |
241 - XEP_0048.URL_TYPE: web page URL | 247 - XEP_0048.URL_TYPE: web page URL |
242 @param location: dependeding on type_, can be a MUC room jid or an url | 248 @param location: dependeding on type_, can be a MUC room jid or an url |
243 @param data (dict): depending on type_, can contains the following keys: | 249 @param data (dict): depending on type_, can contains the following keys: |
249 - "auto": find best available option: pubsub, private, local in that order | 255 - "auto": find best available option: pubsub, private, local in that order |
250 - "pubsub": PubSub private storage (XEP-0223) | 256 - "pubsub": PubSub private storage (XEP-0223) |
251 - "private": Private XML storage (XEP-0049) | 257 - "private": Private XML storage (XEP-0049) |
252 - "local": Store in SàT database | 258 - "local": Store in SàT database |
253 @param profile_key: %(doc_profile_key)s | 259 @param profile_key: %(doc_profile_key)s |
254 | |
255 """ | 260 """ |
256 assert storage_type in ('auto', 'pubsub', 'private', 'local') | 261 assert storage_type in ('auto', 'pubsub', 'private', 'local') |
257 client = self.host.getClient(profile_key) | 262 client = self.host.getClient(profile_key) |
258 if storage_type == 'auto': | 263 if storage_type == 'auto': |
259 if client.bookmarks_pubsub is not None: | 264 if client.bookmarks_pubsub is not None: |
272 bookmarks = yield self._getServerBookmarks(storage_type, client.profile) | 277 bookmarks = yield self._getServerBookmarks(storage_type, client.profile) |
273 bookmarks[type_][location] = data | 278 bookmarks[type_][location] = data |
274 bookmark_elt = self._dict2BookmarkElt(type_, bookmarks) | 279 bookmark_elt = self._dict2BookmarkElt(type_, bookmarks) |
275 yield self._setServerBookmarks(storage_type, bookmark_elt, client.profile) | 280 yield self._setServerBookmarks(storage_type, bookmark_elt, client.profile) |
276 | 281 |
277 | 282 def cmd_bookmark(self, mess_data, profile): |
278 | 283 """Bookmark a MUC room |
279 | 284 |
280 | 285 @command (group): [autojoin] |
286 - autojoin: join room automatically on connection | |
287 """ | |
288 debug("Catched bookmark command") | |
289 client = self.host.getClient(profile) | |
290 txt_cmd = self.host.plugins[C.TEXT_CMDS] | |
291 | |
292 if mess_data['type'] != "groupchat": | |
293 #/bookmark command does nothing if we are not on a group chat | |
294 info("Ignoring /bookmark command on a non groupchat message") | |
295 return True | |
296 | |
297 options = mess_data["unparsed"].strip().split() | |
298 room_jid = mess_data["to"].userhostJID() | |
299 data = { "name": room_jid.user, | |
300 "nick": client.jid.user, | |
301 "autojoin": "true" if "autojoin" in options else "false", | |
302 } | |
303 self.addBookmark(XEP_0048.MUC_TYPE, room_jid, data, profile_key=profile) | |
304 txt_cmd.feedBack(_("Bookmark added"), mess_data, profile) | |
305 | |
306 return False |