diff frontends/src/jp/cmd_roster.py @ 1793:f39ca2832774

jp: add command "roster purge" to remove the contacts with no from/to subscription
author souliane <souliane@mailoo.org>
date Wed, 13 Jan 2016 18:15:20 +0100
parents
children 314d2eb7fbaa
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/frontends/src/jp/cmd_roster.py	Wed Jan 13 18:15:20 2016 +0100
@@ -0,0 +1,83 @@
+#! /usr/bin/python
+# -*- coding: utf-8 -*-
+
+# jp: a SAT command line tool
+# Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org)
+# Copyright (C) 2003-2016 Adrien Cossa (souliane@mailoo.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/>.
+
+import base
+from sat_frontends.jp.constants import Const as C
+from sat.core.i18n import _
+
+__commands__ = ["Roster"]
+
+
+
+class Purge(base.CommandBase):
+
+    def __init__(self, host):
+        super(Purge, self).__init__(host, 'purge', help=_('Purge the roster from its contacts with no subscription'))
+
+    def add_parser_options(self):
+        pass
+
+    def connected(self):
+        self.need_loop = True
+        super(Purge, self).connected()
+        self.host.bridge.getContacts(profile_key=self.host.profile, callback=self.gotContacts, errback=self.error)
+
+    def error(self, failure):
+        print (_("Error while retrieving the contacts [%s]") % failure)
+        self.host.quit(1)
+
+    def ask_confirmation(self, to_remove):
+        """Ask the confirmation before removing contacts without subscription.
+
+        @param to_remove (list[unicode]): list of contacts
+        @return bool
+        """
+        print "There's no subscription between profile [%s] and the following contacts:" % self.host.profile
+        print "    " + "\n    ".join(to_remove)
+        message = "REMOVE them from profile [%s]'s roster" % self.host.profile
+        while True:
+            res = raw_input("%s (y/N)? " % message)
+            if not res or res.lower() == 'n':
+                return False
+            if res.lower() == 'y':
+                return True
+
+    def gotContacts(self, contacts):
+        """Process the list of contacts.
+
+        @param contacts(list[tuple]): list of contacts with their attributes and groups
+        """
+        to_remove = []
+        for contact, attrs, groups in contacts:
+            if not C.bool(attrs["from"]) and not C.bool(attrs["to"]):
+                to_remove.append(contact)
+        if not to_remove:
+            print "Nothing to do - there's a from and/or to subscription(s) between profile [%s] and each of its contacts" % self.host.profile
+        elif self.ask_confirmation(to_remove):
+            for contact in to_remove:
+                self.host.bridge.delContact(contact, profile_key=self.host.profile, callback=lambda dummy: None, errback=lambda failure: None)
+        self.host.quit()
+
+
+class Roster(base.CommandBase):
+    subcommands = (Purge,)
+
+    def __init__(self, host):
+        super(Roster, self).__init__(host, 'roster', use_profile=True, help=_("Manage an entity's roster"))