changeset 985:9ebdba4ab907

plugin XEP-0048: bookmarks deletion + added "delete" option for /bookmark command
author Goffi <goffi@goffi.org>
date Mon, 07 Apr 2014 16:24:29 +0200
parents df8e1b557125
children 224cafc67324
files src/plugins/plugin_xep_0048.py
diffstat 1 files changed, 49 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/plugins/plugin_xep_0048.py	Mon Apr 07 16:24:28 2014 +0200
+++ b/src/plugins/plugin_xep_0048.py	Mon Apr 07 16:24:29 2014 +0200
@@ -311,10 +311,47 @@
             bookmark_elt = self._dict2BookmarkElt(type_, bookmarks)
             yield self._setServerBookmarks(storage_type, bookmark_elt, client.profile)
 
+    @defer.inlineCallbacks
+    def removeBookmark(self, type_, location, storage_type="all", profile_key=C.PROF_KEY_NONE):
+        """Remove a stored bookmark
+
+        @param type_: bookmark type, one of:
+            - XEP_0048.MUC_TYPE: Multi-User chat room
+            - XEP_0048.URL_TYPE: web page URL
+        @param location: dependeding on type_, can be a MUC room jid or an url
+        @param storage_type: where the bookmark is stored, can be:
+            - "all": remove from everywhere
+            - "pubsub": PubSub private storage (XEP-0223)
+            - "private": Private XML storage (XEP-0049)
+            - "local": Store in SàT database
+        @param profile_key: %(doc_profile_key)s
+        """
+        assert storage_type in ('all', 'pubsub', 'private', 'local')
+        client = self.host.getClient(profile_key)
+
+        if storage_type in ('all', 'local'):
+            try:
+                del client.bookmarks_local[type_][location]
+                yield client.bookmarks_local.force(type_)
+            except KeyError:
+                debug("Bookmark is not present in local storage")
+
+        if storage_type in ('all', 'private'):
+            bookmarks = yield self._getServerBookmarks('private', client.profile)
+            try:
+                del bookmarks[type_][location]
+                bookmark_elt = self._dict2BookmarkElt(type_, bookmarks)
+                yield self._setServerBookmarks('private', bookmark_elt, client.profile)
+            except KeyError:
+                debug("Bookmark is not present in private storage")
+
+        if storage_type == 'pubsub':
+            raise NotImplementedError
+
     def cmd_bookmark(self, mess_data, profile):
-        """Bookmark a MUC room
+        """(Un)bookmark a MUC room
 
-        @command (group): [autojoin]
+        @command (group): [autojoin | remove]
             - autojoin: join room automatically on connection
         """
         debug("Catched bookmark command")
@@ -327,7 +364,17 @@
             return True
 
         options = mess_data["unparsed"].strip().split()
+        if options and options[0] not in ('autojoin', 'remove'):
+            txt_cmd.feedBack(_("Bad arguments"), mess_data, profile)
+            return False
+
         room_jid = mess_data["to"].userhostJID()
+
+        if "remove" in options:
+            self.removeBookmark(XEP_0048.MUC_TYPE, room_jid, profile_key = profile)
+            txt_cmd.feedBack(_("All [%s] bookmarks are being removed") % room_jid.full(), mess_data, profile)
+            return False
+
         data = { "name": room_jid.user,
                  "nick": client.jid.user,
                  "autojoin": "true" if "autojoin" in options else "false",