annotate frontends/src/jp/cmd_roster.py @ 2023:224c8b0886bc

plugin XEP-0184: Implementation of XEP-0184 (Message Delivery Receipts)
author Geoffrey POUZET <chteufleur@kingpenguin.tk>
date Thu, 28 Jul 2016 19:11:31 +0200
parents 3e168cde7a7d
children 8b37a62336c3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1960
3e168cde7a7d jp: fixed shebang python call
Goffi <goffi@goffi.org>
parents: 1932
diff changeset
1 #!/usr/bin/env python2
1793
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
3
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
4 # jp: a SAT command line tool
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
5 # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org)
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
6 # Copyright (C) 2003-2016 Adrien Cossa (souliane@mailoo.org)
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
7
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
8 # This program is free software: you can redistribute it and/or modify
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
9 # it under the terms of the GNU Affero General Public License as published by
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
10 # the Free Software Foundation, either version 3 of the License, or
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
11 # (at your option) any later version.
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
12
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
13 # This program is distributed in the hope that it will be useful,
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
16 # GNU Affero General Public License for more details.
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
17
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
18 # You should have received a copy of the GNU Affero General Public License
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
20
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
21 import base
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
22 from sat_frontends.jp.constants import Const as C
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
23 from sat.core.i18n import _
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
24
1796
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
25 from twisted.words.protocols.jabber import jid
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
26 from collections import OrderedDict
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
27
1793
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
28 __commands__ = ["Roster"]
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
29
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
30
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
31
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
32 class Purge(base.CommandBase):
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
33
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
34 def __init__(self, host):
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
35 super(Purge, self).__init__(host, 'purge', help=_('Purge the roster from its contacts with no subscription'))
1864
96ba685162f6 jp: all commands now use the new start method and set need_loop in __init__ when needed
Goffi <goffi@goffi.org>
parents: 1800
diff changeset
36 self.need_loop = True
1793
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
37
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
38 def add_parser_options(self):
1797
40cda0c08727 jp (roster): add parameters "--no_from" and "--no_to" to command "roster purge" in order to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents: 1796
diff changeset
39 self.parser.add_argument("--no_from", action="store_true", help=_("Also purge contacts with no 'from' subscription"))
40cda0c08727 jp (roster): add parameters "--no_from" and "--no_to" to command "roster purge" in order to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents: 1796
diff changeset
40 self.parser.add_argument("--no_to", action="store_true", help=_("Also purge contacts with no 'to' subscription"))
1793
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
41
1864
96ba685162f6 jp: all commands now use the new start method and set need_loop in __init__ when needed
Goffi <goffi@goffi.org>
parents: 1800
diff changeset
42 def start(self):
1793
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
43 self.host.bridge.getContacts(profile_key=self.host.profile, callback=self.gotContacts, errback=self.error)
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
44
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
45 def error(self, failure):
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
46 print (_("Error while retrieving the contacts [%s]") % failure)
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
47 self.host.quit(1)
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
48
1798
c5d58387d031 jp (roster stats): add some extra information
souliane <souliane@mailoo.org>
parents: 1797
diff changeset
49 def ask_confirmation(self, no_sub, no_from, no_to):
1797
40cda0c08727 jp (roster): add parameters "--no_from" and "--no_to" to command "roster purge" in order to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents: 1796
diff changeset
50 """Ask the confirmation before removing contacts.
1793
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
51
1798
c5d58387d031 jp (roster stats): add some extra information
souliane <souliane@mailoo.org>
parents: 1797
diff changeset
52 @param no_sub (list[unicode]): list of contacts with no subscription
1797
40cda0c08727 jp (roster): add parameters "--no_from" and "--no_to" to command "roster purge" in order to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents: 1796
diff changeset
53 @param no_from (list[unicode]): list of contacts with no 'from' subscription
40cda0c08727 jp (roster): add parameters "--no_from" and "--no_to" to command "roster purge" in order to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents: 1796
diff changeset
54 @param no_to (list[unicode]): list of contacts with no 'to' subscription
1793
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
55 @return bool
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
56 """
1798
c5d58387d031 jp (roster stats): add some extra information
souliane <souliane@mailoo.org>
parents: 1797
diff changeset
57 if no_sub:
1797
40cda0c08727 jp (roster): add parameters "--no_from" and "--no_to" to command "roster purge" in order to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents: 1796
diff changeset
58 print "There's no subscription between profile [%s] and the following contacts:" % self.host.profile
1798
c5d58387d031 jp (roster stats): add some extra information
souliane <souliane@mailoo.org>
parents: 1797
diff changeset
59 print " " + "\n ".join(no_sub)
1797
40cda0c08727 jp (roster): add parameters "--no_from" and "--no_to" to command "roster purge" in order to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents: 1796
diff changeset
60 if no_from:
40cda0c08727 jp (roster): add parameters "--no_from" and "--no_to" to command "roster purge" in order to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents: 1796
diff changeset
61 print "There's no 'from' subscription between profile [%s] and the following contacts:" % self.host.profile
40cda0c08727 jp (roster): add parameters "--no_from" and "--no_to" to command "roster purge" in order to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents: 1796
diff changeset
62 print " " + "\n ".join(no_from)
40cda0c08727 jp (roster): add parameters "--no_from" and "--no_to" to command "roster purge" in order to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents: 1796
diff changeset
63 if no_to:
40cda0c08727 jp (roster): add parameters "--no_from" and "--no_to" to command "roster purge" in order to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents: 1796
diff changeset
64 print "There's no 'to' subscription between profile [%s] and the following contacts:" % self.host.profile
40cda0c08727 jp (roster): add parameters "--no_from" and "--no_to" to command "roster purge" in order to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents: 1796
diff changeset
65 print " " + "\n ".join(no_to)
1793
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
66 message = "REMOVE them from profile [%s]'s roster" % self.host.profile
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
67 while True:
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
68 res = raw_input("%s (y/N)? " % message)
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
69 if not res or res.lower() == 'n':
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
70 return False
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
71 if res.lower() == 'y':
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
72 return True
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
73
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
74 def gotContacts(self, contacts):
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
75 """Process the list of contacts.
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
76
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
77 @param contacts(list[tuple]): list of contacts with their attributes and groups
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
78 """
1798
c5d58387d031 jp (roster stats): add some extra information
souliane <souliane@mailoo.org>
parents: 1797
diff changeset
79 no_sub, no_from, no_to = [], [], []
1793
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
80 for contact, attrs, groups in contacts:
1797
40cda0c08727 jp (roster): add parameters "--no_from" and "--no_to" to command "roster purge" in order to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents: 1796
diff changeset
81 from_, to = C.bool(attrs["from"]), C.bool(attrs["to"])
40cda0c08727 jp (roster): add parameters "--no_from" and "--no_to" to command "roster purge" in order to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents: 1796
diff changeset
82 if not from_:
40cda0c08727 jp (roster): add parameters "--no_from" and "--no_to" to command "roster purge" in order to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents: 1796
diff changeset
83 if not to:
1798
c5d58387d031 jp (roster stats): add some extra information
souliane <souliane@mailoo.org>
parents: 1797
diff changeset
84 no_sub.append(contact)
1797
40cda0c08727 jp (roster): add parameters "--no_from" and "--no_to" to command "roster purge" in order to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents: 1796
diff changeset
85 elif self.args.no_from:
40cda0c08727 jp (roster): add parameters "--no_from" and "--no_to" to command "roster purge" in order to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents: 1796
diff changeset
86 no_from.append(contact)
40cda0c08727 jp (roster): add parameters "--no_from" and "--no_to" to command "roster purge" in order to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents: 1796
diff changeset
87 elif not to and self.args.no_to:
40cda0c08727 jp (roster): add parameters "--no_from" and "--no_to" to command "roster purge" in order to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents: 1796
diff changeset
88 no_to.append(contact)
1798
c5d58387d031 jp (roster stats): add some extra information
souliane <souliane@mailoo.org>
parents: 1797
diff changeset
89 if not no_sub and not no_from and not no_to:
1793
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
90 print "Nothing to do - there's a from and/or to subscription(s) between profile [%s] and each of its contacts" % self.host.profile
1798
c5d58387d031 jp (roster stats): add some extra information
souliane <souliane@mailoo.org>
parents: 1797
diff changeset
91 elif self.ask_confirmation(no_sub, no_from, no_to):
c5d58387d031 jp (roster stats): add some extra information
souliane <souliane@mailoo.org>
parents: 1797
diff changeset
92 for contact in no_sub + no_from + no_to:
1793
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
93 self.host.bridge.delContact(contact, profile_key=self.host.profile, callback=lambda dummy: None, errback=lambda failure: None)
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
94 self.host.quit()
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
95
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
96
1796
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
97 class Stats(base.CommandBase):
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
98
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
99 def __init__(self, host):
1798
c5d58387d031 jp (roster stats): add some extra information
souliane <souliane@mailoo.org>
parents: 1797
diff changeset
100 super(Stats, self).__init__(host, 'stats', help=_('Show statistics about a roster'))
1864
96ba685162f6 jp: all commands now use the new start method and set need_loop in __init__ when needed
Goffi <goffi@goffi.org>
parents: 1800
diff changeset
101 self.need_loop = True
1796
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
102
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
103 def add_parser_options(self):
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
104 pass
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
105
1864
96ba685162f6 jp: all commands now use the new start method and set need_loop in __init__ when needed
Goffi <goffi@goffi.org>
parents: 1800
diff changeset
106 def start(self):
1796
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
107 self.host.bridge.getContacts(profile_key=self.host.profile, callback=self.gotContacts, errback=self.error)
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
108
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
109 def error(self, failure):
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
110 print (_("Error while retrieving the contacts [%s]") % failure)
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
111 self.host.quit(1)
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
112
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
113 def gotContacts(self, contacts):
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
114 """Process the list of contacts.
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
115
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
116 @param contacts(list[tuple]): list of contacts with their attributes and groups
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
117 """
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
118 hosts = {}
1798
c5d58387d031 jp (roster stats): add some extra information
souliane <souliane@mailoo.org>
parents: 1797
diff changeset
119 unique_groups = set()
c5d58387d031 jp (roster stats): add some extra information
souliane <souliane@mailoo.org>
parents: 1797
diff changeset
120 no_sub, no_from, no_to, no_group, total_group_subscription = 0, 0, 0, 0, 0
1796
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
121 for contact, attrs, groups in contacts:
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
122 from_, to = C.bool(attrs["from"]), C.bool(attrs["to"])
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
123 if not from_:
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
124 if not to:
1798
c5d58387d031 jp (roster stats): add some extra information
souliane <souliane@mailoo.org>
parents: 1797
diff changeset
125 no_sub += 1
1796
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
126 else:
1798
c5d58387d031 jp (roster stats): add some extra information
souliane <souliane@mailoo.org>
parents: 1797
diff changeset
127 no_from += 1
1796
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
128 elif not to:
1798
c5d58387d031 jp (roster stats): add some extra information
souliane <souliane@mailoo.org>
parents: 1797
diff changeset
129 no_to += 1
1796
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
130 host = jid.JID(contact).host
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
131 hosts.setdefault(host, 0)
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
132 hosts[host] += 1
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
133 if groups:
1798
c5d58387d031 jp (roster stats): add some extra information
souliane <souliane@mailoo.org>
parents: 1797
diff changeset
134 unique_groups.update(groups)
1796
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
135 total_group_subscription += len(groups)
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
136 if not groups:
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
137 no_group += 1
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
138 hosts = OrderedDict(sorted(hosts.items(), key=lambda item:-item[1]))
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
139
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
140 print
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
141 print "Total number of contacts: %d" % len(contacts)
1798
c5d58387d031 jp (roster stats): add some extra information
souliane <souliane@mailoo.org>
parents: 1797
diff changeset
142 print "Number of different hosts: %d" % len(hosts)
1796
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
143 print
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
144 for host, count in hosts.iteritems():
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
145 print "Contacts on {host}: {count} ({rate:.1f}%)".format(host=host, count=count, rate=100 * float(count) / len(contacts))
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
146 print
1798
c5d58387d031 jp (roster stats): add some extra information
souliane <souliane@mailoo.org>
parents: 1797
diff changeset
147 print "Contacts with no 'from' subscription: %d" % no_from
c5d58387d031 jp (roster stats): add some extra information
souliane <souliane@mailoo.org>
parents: 1797
diff changeset
148 print "Contacts with no 'to' subscription: %d" % no_to
c5d58387d031 jp (roster stats): add some extra information
souliane <souliane@mailoo.org>
parents: 1797
diff changeset
149 print "Contacts with no subscription at all: %d" % no_sub
1796
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
150 print
1798
c5d58387d031 jp (roster stats): add some extra information
souliane <souliane@mailoo.org>
parents: 1797
diff changeset
151 print "Total number of groups: %d" % len(unique_groups)
1932
44342730df66 jp (cmd/roster): fixes division by zero exceptions
souliane <souliane@mailoo.org>
parents: 1864
diff changeset
152 try:
44342730df66 jp (cmd/roster): fixes division by zero exceptions
souliane <souliane@mailoo.org>
parents: 1864
diff changeset
153 contacts_per_group = float(total_group_subscription) / len(unique_groups)
44342730df66 jp (cmd/roster): fixes division by zero exceptions
souliane <souliane@mailoo.org>
parents: 1864
diff changeset
154 except ZeroDivisionError:
44342730df66 jp (cmd/roster): fixes division by zero exceptions
souliane <souliane@mailoo.org>
parents: 1864
diff changeset
155 contacts_per_group = 0
44342730df66 jp (cmd/roster): fixes division by zero exceptions
souliane <souliane@mailoo.org>
parents: 1864
diff changeset
156 print "Average contacts per group: {:.1f}".format(contacts_per_group)
44342730df66 jp (cmd/roster): fixes division by zero exceptions
souliane <souliane@mailoo.org>
parents: 1864
diff changeset
157 try:
44342730df66 jp (cmd/roster): fixes division by zero exceptions
souliane <souliane@mailoo.org>
parents: 1864
diff changeset
158 groups_per_contact = float(total_group_subscription) / len(contacts)
44342730df66 jp (cmd/roster): fixes division by zero exceptions
souliane <souliane@mailoo.org>
parents: 1864
diff changeset
159 except ZeroDivisionError:
44342730df66 jp (cmd/roster): fixes division by zero exceptions
souliane <souliane@mailoo.org>
parents: 1864
diff changeset
160 groups_per_contact = 0
44342730df66 jp (cmd/roster): fixes division by zero exceptions
souliane <souliane@mailoo.org>
parents: 1864
diff changeset
161 print "Average groups' subscriptions per contact: {:.1f}".format(groups_per_contact)
1796
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
162 print "Contacts not assigned to any group: %d" % no_group
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
163 self.host.quit()
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
164
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
165
1799
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
166 class Get(base.CommandBase):
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
167
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
168 def __init__(self, host):
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
169 super(Get, self).__init__(host, 'get', help=_('Retrieve the roster contacts'))
1864
96ba685162f6 jp: all commands now use the new start method and set need_loop in __init__ when needed
Goffi <goffi@goffi.org>
parents: 1800
diff changeset
170 self.need_loop = True
1799
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
171
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
172 def add_parser_options(self):
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
173 self.parser.add_argument("--subscriptions", action="store_true", help=_("Show the contacts' subscriptions"))
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
174 self.parser.add_argument("--groups", action="store_true", help=_("Show the contacts' groups"))
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
175 self.parser.add_argument("--name", action="store_true", help=_("Show the contacts' names"))
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
176
1864
96ba685162f6 jp: all commands now use the new start method and set need_loop in __init__ when needed
Goffi <goffi@goffi.org>
parents: 1800
diff changeset
177 def start(self):
1799
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
178 self.host.bridge.getContacts(profile_key=self.host.profile, callback=self.gotContacts, errback=self.error)
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
179
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
180 def error(self, failure):
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
181 print (_("Error while retrieving the contacts [%s]") % failure)
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
182 self.host.quit(1)
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
183
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
184 def gotContacts(self, contacts):
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
185 """Process the list of contacts.
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
186
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
187 @param contacts(list[tuple]): list of contacts with their attributes and groups
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
188 """
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
189 field_count = 1 # only display the contact by default
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
190 if self.args.subscriptions:
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
191 field_count += 3 # ask, from, to
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
192 if self.args.name:
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
193 field_count += 1
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
194 if self.args.groups:
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
195 field_count += 1
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
196 for contact, attrs, groups in contacts:
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
197 args = [contact]
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
198 if self.args.subscriptions:
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
199 args.append("ask" if C.bool(attrs["ask"]) else "")
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
200 args.append("from" if C.bool(attrs["from"]) else "")
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
201 args.append("to" if C.bool(attrs["to"]) else "")
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
202 if self.args.name:
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
203 args.append(unicode(attrs.get("name", "")))
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
204 if self.args.groups:
1800
5f4d688d8b6e jp (roster): change groups delimiter for "roster get" output, from "|" to "\t"
souliane <souliane@mailoo.org>
parents: 1799
diff changeset
205 args.append(u"\t".join(groups) if groups else "")
1799
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
206 print u";".join(["{}"] * field_count).format(*args).encode("utf-8")
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
207 self.host.quit()
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
208
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
209
1793
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
210 class Roster(base.CommandBase):
1799
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
211 subcommands = (Get, Stats, Purge)
1793
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
212
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
213 def __init__(self, host):
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
214 super(Roster, self).__init__(host, 'roster', use_profile=True, help=_("Manage an entity's roster"))