changeset 986:224cafc67324

jp: added bookmarks subcommands
author Goffi <goffi@goffi.org>
date Mon, 07 Apr 2014 16:24:29 +0200
parents 9ebdba4ab907
children 3a96920c07b7
files frontends/src/jp/cmd_bookmarks.py src/plugins/plugin_xep_0048.py
diffstat 2 files changed, 188 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/frontends/src/jp/cmd_bookmarks.py	Mon Apr 07 16:24:29 2014 +0200
@@ -0,0 +1,119 @@
+#! /usr/bin/python
+# -*- coding: utf-8 -*-
+
+# jp: a SAT command line tool
+# Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Jérôme Poisson (goffi@goffi.org)
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+from logging import debug, info, error, warning
+
+import base
+from sat.core.i18n import _
+
+__commands__ = ["Bookmarks"]
+
+STORAGE_LOCATIONS = ('local', 'private', 'pubsub')
+TYPES = ('muc', 'url')
+
+class BookmarksCommon(base.CommandBase):
+    """Class used to group common options of bookmarks subcommands"""
+
+    def add_parser_options(self, location_default='all'):
+        self.parser.add_argument('-l', '--location', type=str, choices=(location_default,) + STORAGE_LOCATIONS, default=location_default, help=_("storage location (default: %(default)s)"))
+        self.parser.add_argument('-t', '--type', type=str, choices=TYPES, default=TYPES[0], help=_("bookmarks type (default: %(default)s)"))
+
+    def _errback(self, failure):
+        print (("Something went wrong: [%s]") % failure)
+        self.host.quit(1)
+
+class BookmarksList(BookmarksCommon):
+
+    def __init__(self, host):
+        super(BookmarksList, self).__init__(host, 'list', help=_('list bookmarks'))
+
+    def connected(self):
+        super(BookmarksList, self).connected()
+        data = self.host.bridge.bookmarksList(self.args.type, self.args.location, self.host.profile)
+        mess = []
+        for location in STORAGE_LOCATIONS:
+            if not data[location]:
+                continue
+            loc_mess = []
+            loc_mess.append(u"%s:" % location)
+            book_mess = []
+            for book_link, book_data in data[location].items():
+                name = book_data.get('name')
+                autojoin = book_data.get('autojoin', 'false') == 'true'
+                nick = book_data.get('nick')
+                book_mess.append(u"\t%s[%s%s]%s" % ((name+' ') if name else '',
+                                                 book_link,
+                                                 u' (%s)' % nick if nick else '',
+                                                 u' (*)' if autojoin else ''))
+            loc_mess.append(u'\n'.join(book_mess))
+            mess.append(u'\n'.join(loc_mess))
+
+        print u'\n\n'.join(mess)
+
+
+class BookmarksRemove(BookmarksCommon):
+    need_loop = True
+
+    def __init__(self, host):
+        super(BookmarksRemove, self).__init__(host, 'remove', help=_('remove a bookmark'))
+
+    def add_parser_options(self):
+        super(BookmarksRemove, self).add_parser_options()
+        self.parser.add_argument('bookmark', type=base.unicode_decoder, help=_('jid (for muc bookmark) or url of to remove'))
+
+    def connected(self):
+        super(BookmarksRemove, self).connected()
+        self.host.bridge.bookmarksRemove(self.args.type, self.args.bookmark, self.args.location, self.host.profile, callback = lambda: self.host.quit(), errback=self._errback)
+
+
+class BookmarksAdd(BookmarksCommon):
+
+    def __init__(self, host):
+        super(BookmarksAdd, self).__init__(host, 'add', help=_('add a bookmark'))
+
+    def add_parser_options(self):
+        super(BookmarksAdd, self).add_parser_options(location_default='auto')
+        self.parser.add_argument('bookmark', type=base.unicode_decoder, help=_('jid (for muc bookmark) or url of to remove'))
+        self.parser.add_argument('-n', '--name', type=base.unicode_decoder, help=_("bookmark name"))
+        muc_group = self.parser.add_argument_group(_('MUC specific options'))
+        muc_group.add_argument('-N', '--nick', type=base.unicode_decoder, help=_('nickname'))
+        muc_group.add_argument('-a', '--autojoin', action='store_true', help=_('join room on profile connection'))
+
+    def connected(self):
+        self.need_loop = True
+        super(BookmarksAdd, self).connected()
+        if self.args.type == 'url' and (self.args.autojoin or self.args.nick is not None):
+            # XXX: Argparse doesn't seem to manage this case, any better way ?
+            print _(u"You can't use --autojoin or --nick with --type url")
+            self.host.quit(1)
+        data = {}
+        if self.args.autojoin:
+            data['autojoin'] = 'true'
+        if self.args.nick is not None:
+            data['nick'] = self.args.nick
+        if self.args.name is not None:
+            data['name'] = self.args.name
+        self.host.bridge.bookmarksAdd(self.args.type, self.args.bookmark, data, self.args.location, self.host.profile, callback = lambda: self.host.quit(), errback=self._errback)
+
+
+class Bookmarks(base.CommandBase):
+    subcommands = (BookmarksList, BookmarksRemove, BookmarksAdd)
+
+    def __init__(self, host):
+        super(Bookmarks, self).__init__(host, 'bookmarks', use_profile=False, help=_('manage bookmarks'))
--- a/src/plugins/plugin_xep_0048.py	Mon Apr 07 16:24:29 2014 +0200
+++ b/src/plugins/plugin_xep_0048.py	Mon Apr 07 16:24:29 2014 +0200
@@ -58,6 +58,9 @@
         self.__bm_save_id = host.registerCallback(self._bookmarksSaveCb, with_data=True)
         host.importMenu((D_("Communication"), D_("bookmarks")), self._bookmarksMenu, security_limit=0, help_string=D_("Use and manage bookmarks"))
         self.__selected_id = host.registerCallback(self._bookmarkSelectedCb, with_data=True)
+        host.bridge.addMethod("bookmarksList", ".plugin", in_sign='sss', out_sign='a{sa{sa{ss}}}', method=self._bookmarksList, async=True)
+        host.bridge.addMethod("bookmarksRemove", ".plugin", in_sign='ssss', out_sign='', method=self._bookmarksRemove, async=True)
+        host.bridge.addMethod("bookmarksAdd", ".plugin", in_sign='ssa{ss}ss', out_sign='', method=self._bookmarksAdd, async=True)
         try:
             self.private_plg = self.host.plugins["XEP-0049"]
         except KeyError:
@@ -291,6 +294,8 @@
         @param profile_key: %(doc_profile_key)s
         """
         assert storage_type in ('auto', 'pubsub', 'private', 'local')
+        if type_ == XEP_0048.URL_TYPE and {'autojoin', 'nick'}.intersection(data.keys()):
+                raise ValueError("autojoin or nick can't be used with URLs")
         client = self.host.getClient(profile_key)
         if storage_type == 'auto':
             if client.bookmarks_pubsub is not None:
@@ -348,6 +353,70 @@
         if storage_type == 'pubsub':
             raise NotImplementedError
 
+    def _bookmarksList(self, type_, storage_location, profile_key=C.PROF_KEY_NONE):
+        """Return stored bookmarks
+
+        @param type_: bookmark type, one of:
+            - XEP_0048.MUC_TYPE: Multi-User chat room
+            - XEP_0048.URL_TYPE: web page URL
+        @param storage_location: can be:
+            - 'all'
+            - 'local'
+            - 'private'
+            - 'pubsub'
+        @param profile_key: %(doc_profile_key)s
+        @param return (dict): (key: storage_location, value dict) with:
+            - value (dict): (key: bookmark_location, value: bookmark data)
+        """
+        client = self.host.getClient(profile_key)
+        ret = {}
+        ret_d = defer.succeed(ret)
+
+        def fillBookmarks(dummy, _storage_location):
+            bookmarks_ori = getattr(client, "bookmarks_" + _storage_location)
+            if bookmarks_ori is None:
+                return ret
+            data = bookmarks_ori[type_]
+            for bookmark in data:
+                ret[_storage_location][bookmark.full()] = data[bookmark].copy()
+            return ret
+
+        for _storage_location in ('local', 'private', 'pubsub'):
+            if storage_location in ('all', _storage_location):
+                ret[_storage_location] = {}
+                if _storage_location in ('private',):
+                    # we update distant bookmarks, just in case an other client added something
+                    d = self._getServerBookmarks(_storage_location, client.profile)
+                else:
+                    d = defer.succeed(None)
+                d.addCallback(fillBookmarks, _storage_location)
+                ret_d.addCallback(lambda dummy: d)
+
+        return ret_d
+
+    def _bookmarksRemove(self, type_, location, storage_location, profile_key=C.PROF_KEY_NONE):
+        """Return stored bookmarks
+
+        @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_location: 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
+        """
+        if type_ == XEP_0048.MUC_TYPE:
+            location = jid.JID(location)
+        return self.removeBookmark(type_, location, storage_location, profile_key)
+
+    def _bookmarksAdd(self, type_, location, data, storage_type="auto", profile_key=C.PROF_KEY_NONE):
+        if type_ == XEP_0048.MUC_TYPE:
+            location = jid.JID(location)
+        return self.addBookmark(type_, location, data, storage_type, profile_key)
+
     def cmd_bookmark(self, mess_data, profile):
         """(Un)bookmark a MUC room