comparison 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
comparison
equal deleted inserted replaced
1792:17c0364607be 1793:f39ca2832774
1 #! /usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # jp: a SAT command line tool
5 # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org)
6 # Copyright (C) 2003-2016 Adrien Cossa (souliane@mailoo.org)
7
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU Affero General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU Affero General Public License for more details.
17
18 # You should have received a copy of the GNU Affero General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21 import base
22 from sat_frontends.jp.constants import Const as C
23 from sat.core.i18n import _
24
25 __commands__ = ["Roster"]
26
27
28
29 class Purge(base.CommandBase):
30
31 def __init__(self, host):
32 super(Purge, self).__init__(host, 'purge', help=_('Purge the roster from its contacts with no subscription'))
33
34 def add_parser_options(self):
35 pass
36
37 def connected(self):
38 self.need_loop = True
39 super(Purge, self).connected()
40 self.host.bridge.getContacts(profile_key=self.host.profile, callback=self.gotContacts, errback=self.error)
41
42 def error(self, failure):
43 print (_("Error while retrieving the contacts [%s]") % failure)
44 self.host.quit(1)
45
46 def ask_confirmation(self, to_remove):
47 """Ask the confirmation before removing contacts without subscription.
48
49 @param to_remove (list[unicode]): list of contacts
50 @return bool
51 """
52 print "There's no subscription between profile [%s] and the following contacts:" % self.host.profile
53 print " " + "\n ".join(to_remove)
54 message = "REMOVE them from profile [%s]'s roster" % self.host.profile
55 while True:
56 res = raw_input("%s (y/N)? " % message)
57 if not res or res.lower() == 'n':
58 return False
59 if res.lower() == 'y':
60 return True
61
62 def gotContacts(self, contacts):
63 """Process the list of contacts.
64
65 @param contacts(list[tuple]): list of contacts with their attributes and groups
66 """
67 to_remove = []
68 for contact, attrs, groups in contacts:
69 if not C.bool(attrs["from"]) and not C.bool(attrs["to"]):
70 to_remove.append(contact)
71 if not to_remove:
72 print "Nothing to do - there's a from and/or to subscription(s) between profile [%s] and each of its contacts" % self.host.profile
73 elif self.ask_confirmation(to_remove):
74 for contact in to_remove:
75 self.host.bridge.delContact(contact, profile_key=self.host.profile, callback=lambda dummy: None, errback=lambda failure: None)
76 self.host.quit()
77
78
79 class Roster(base.CommandBase):
80 subcommands = (Purge,)
81
82 def __init__(self, host):
83 super(Roster, self).__init__(host, 'roster', use_profile=True, help=_("Manage an entity's roster"))