comparison libervia/frontends/jp/cmd_roster.py @ 4074:26b7ed2817da

refactoring: rename `sat_frontends` to `libervia.frontends`
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 14:12:38 +0200
parents sat_frontends/jp/cmd_roster.py@4b842c1fb686
children
comparison
equal deleted inserted replaced
4073:7c5654c54fed 4074:26b7ed2817da
1 #!/usr/bin/env python3
2
3 # jp: a SàT command line tool
4 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
5 # Copyright (C) 2003-2016 Adrien Cossa (souliane@mailoo.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 from . import base
21 from collections import OrderedDict
22 from libervia.backend.core.i18n import _
23 from libervia.frontends.jp.constants import Const as C
24 from libervia.frontends.tools import jid
25 from libervia.backend.tools.common.ansi import ANSI as A
26
27 __commands__ = ["Roster"]
28
29
30 class Get(base.CommandBase):
31
32 def __init__(self, host):
33 super().__init__(
34 host, 'get', use_output=C.OUTPUT_DICT, use_verbose=True,
35 extra_outputs = {"default": self.default_output},
36 help=_('retrieve the roster entities'))
37
38 def add_parser_options(self):
39 pass
40
41 def default_output(self, data):
42 for contact_jid, contact_data in data.items():
43 all_keys = list(contact_data.keys())
44 keys_to_show = []
45 name = contact_data.get('name', contact_jid.node)
46
47 if self.verbosity >= 1:
48 keys_to_show.append('groups')
49 all_keys.remove('groups')
50 if self.verbosity >= 2:
51 keys_to_show.extend(all_keys)
52
53 if name is None:
54 self.disp(A.color(C.A_HEADER, contact_jid))
55 else:
56 self.disp(A.color(C.A_HEADER, name, A.RESET, f" ({contact_jid})"))
57 for k in keys_to_show:
58 value = contact_data[k]
59 if value:
60 if isinstance(value, list):
61 value = ', '.join(value)
62 self.disp(A.color(
63 " ", C.A_SUBHEADER, f"{k}: ", A.RESET, str(value)))
64
65 async def start(self):
66 try:
67 contacts = await self.host.bridge.contacts_get(profile_key=self.host.profile)
68 except Exception as e:
69 self.disp(f"error while retrieving the contacts: {e}", error=True)
70 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
71
72 contacts_dict = {}
73 for contact_jid_s, data, groups in contacts:
74 # FIXME: we have to convert string to bool here for historical reason
75 # contacts_get format should be changed and serialised properly
76 for key in ('from', 'to', 'ask'):
77 if key in data:
78 data[key] = C.bool(data[key])
79 data['groups'] = list(groups)
80 contacts_dict[jid.JID(contact_jid_s)] = data
81
82 await self.output(contacts_dict)
83 self.host.quit()
84
85
86 class Set(base.CommandBase):
87
88 def __init__(self, host):
89 super().__init__(host, 'set', help=_('set metadata for a roster entity'))
90
91 def add_parser_options(self):
92 self.parser.add_argument(
93 "-n", "--name", default="", help=_('name to use for this entity'))
94 self.parser.add_argument(
95 "-g", "--group", dest='groups', action='append', metavar='GROUP', default=[],
96 help=_('groups for this entity'))
97 self.parser.add_argument(
98 "-R", "--replace", action="store_true",
99 help=_("replace all metadata instead of adding them"))
100 self.parser.add_argument(
101 "jid", help=_("jid of the roster entity"))
102
103 async def start(self):
104
105 if self.args.replace:
106 name = self.args.name
107 groups = self.args.groups
108 else:
109 try:
110 entity_data = await self.host.bridge.contact_get(
111 self.args.jid, self.host.profile)
112 except Exception as e:
113 self.disp(f"error while retrieving the contact: {e}", error=True)
114 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
115 name = self.args.name or entity_data[0].get('name') or ''
116 groups = set(entity_data[1])
117 groups = list(groups.union(self.args.groups))
118
119 try:
120 await self.host.bridge.contact_update(
121 self.args.jid, name, groups, self.host.profile)
122 except Exception as e:
123 self.disp(f"error while updating the contact: {e}", error=True)
124 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
125 self.host.quit()
126
127
128 class Delete(base.CommandBase):
129
130 def __init__(self, host):
131 super().__init__(host, 'delete', help=_('remove an entity from roster'))
132
133 def add_parser_options(self):
134 self.parser.add_argument(
135 "-f", "--force", action="store_true", help=_("delete without confirmation")
136 )
137 self.parser.add_argument(
138 "jid", help=_("jid of the roster entity"))
139
140 async def start(self):
141 if not self.args.force:
142 message = _("Are you sure to delete {entity} from your roster?").format(
143 entity=self.args.jid
144 )
145 await self.host.confirm_or_quit(message, _("entity deletion cancelled"))
146 try:
147 await self.host.bridge.contact_del(
148 self.args.jid, self.host.profile)
149 except Exception as e:
150 self.disp(f"error while deleting the entity: {e}", error=True)
151 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
152 self.host.quit()
153
154
155 class Stats(base.CommandBase):
156
157 def __init__(self, host):
158 super(Stats, self).__init__(host, 'stats', help=_('Show statistics about a roster'))
159
160 def add_parser_options(self):
161 pass
162
163 async def start(self):
164 try:
165 contacts = await self.host.bridge.contacts_get(profile_key=self.host.profile)
166 except Exception as e:
167 self.disp(f"error while retrieving the contacts: {e}", error=True)
168 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
169
170 hosts = {}
171 unique_groups = set()
172 no_sub, no_from, no_to, no_group, total_group_subscription = 0, 0, 0, 0, 0
173 for contact, attrs, groups in contacts:
174 from_, to = C.bool(attrs["from"]), C.bool(attrs["to"])
175 if not from_:
176 if not to:
177 no_sub += 1
178 else:
179 no_from += 1
180 elif not to:
181 no_to += 1
182
183 host = jid.JID(contact).domain
184
185 hosts.setdefault(host, 0)
186 hosts[host] += 1
187 if groups:
188 unique_groups.update(groups)
189 total_group_subscription += len(groups)
190 if not groups:
191 no_group += 1
192 hosts = OrderedDict(sorted(list(hosts.items()), key=lambda item:-item[1]))
193
194 print()
195 print("Total number of contacts: %d" % len(contacts))
196 print("Number of different hosts: %d" % len(hosts))
197 print()
198 for host, count in hosts.items():
199 print("Contacts on {host}: {count} ({rate:.1f}%)".format(
200 host=host, count=count, rate=100 * float(count) / len(contacts)))
201 print()
202 print("Contacts with no 'from' subscription: %d" % no_from)
203 print("Contacts with no 'to' subscription: %d" % no_to)
204 print("Contacts with no subscription at all: %d" % no_sub)
205 print()
206 print("Total number of groups: %d" % len(unique_groups))
207 try:
208 contacts_per_group = float(total_group_subscription) / len(unique_groups)
209 except ZeroDivisionError:
210 contacts_per_group = 0
211 print("Average contacts per group: {:.1f}".format(contacts_per_group))
212 try:
213 groups_per_contact = float(total_group_subscription) / len(contacts)
214 except ZeroDivisionError:
215 groups_per_contact = 0
216 print(f"Average groups' subscriptions per contact: {groups_per_contact:.1f}")
217 print("Contacts not assigned to any group: %d" % no_group)
218 self.host.quit()
219
220
221 class Purge(base.CommandBase):
222
223 def __init__(self, host):
224 super(Purge, self).__init__(
225 host, 'purge',
226 help=_('purge the roster from its contacts with no subscription'))
227
228 def add_parser_options(self):
229 self.parser.add_argument(
230 "--no-from", action="store_true",
231 help=_("also purge contacts with no 'from' subscription"))
232 self.parser.add_argument(
233 "--no-to", action="store_true",
234 help=_("also purge contacts with no 'to' subscription"))
235
236 async def start(self):
237 try:
238 contacts = await self.host.bridge.contacts_get(self.host.profile)
239 except Exception as e:
240 self.disp(f"error while retrieving the contacts: {e}", error=True)
241 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
242
243 no_sub, no_from, no_to = [], [], []
244 for contact, attrs, groups in contacts:
245 from_, to = C.bool(attrs["from"]), C.bool(attrs["to"])
246 if not from_:
247 if not to:
248 no_sub.append(contact)
249 elif self.args.no_from:
250 no_from.append(contact)
251 elif not to and self.args.no_to:
252 no_to.append(contact)
253 if not no_sub and not no_from and not no_to:
254 self.disp(
255 f"Nothing to do - there's a from and/or to subscription(s) between "
256 f"profile {self.host.profile!r} and each of its contacts"
257 )
258 elif await self.ask_confirmation(no_sub, no_from, no_to):
259 for contact in no_sub + no_from + no_to:
260 try:
261 await self.host.bridge.contact_del(
262 contact, profile_key=self.host.profile)
263 except Exception as e:
264 self.disp(f"can't delete contact {contact!r}: {e}", error=True)
265 else:
266 self.disp(f"contact {contact!r} has been removed")
267
268 self.host.quit()
269
270 async def ask_confirmation(self, no_sub, no_from, no_to):
271 """Ask the confirmation before removing contacts.
272
273 @param no_sub (list[unicode]): list of contacts with no subscription
274 @param no_from (list[unicode]): list of contacts with no 'from' subscription
275 @param no_to (list[unicode]): list of contacts with no 'to' subscription
276 @return bool
277 """
278 if no_sub:
279 self.disp(
280 f"There's no subscription between profile {self.host.profile!r} and the "
281 f"following contacts:")
282 self.disp(" " + "\n ".join(no_sub))
283 if no_from:
284 self.disp(
285 f"There's no 'from' subscription between profile {self.host.profile!r} "
286 f"and the following contacts:")
287 self.disp(" " + "\n ".join(no_from))
288 if no_to:
289 self.disp(
290 f"There's no 'to' subscription between profile {self.host.profile!r} and "
291 f"the following contacts:")
292 self.disp(" " + "\n ".join(no_to))
293 message = f"REMOVE them from profile {self.host.profile}'s roster"
294 while True:
295 res = await self.host.ainput(f"{message} (y/N)? ")
296 if not res or res.lower() == 'n':
297 return False
298 if res.lower() == 'y':
299 return True
300
301
302 class Resync(base.CommandBase):
303
304 def __init__(self, host):
305 super(Resync, self).__init__(
306 host, 'resync', help=_('do a full resynchronisation of roster with server'))
307
308 def add_parser_options(self):
309 pass
310
311 async def start(self):
312 try:
313 await self.host.bridge.roster_resync(profile_key=self.host.profile)
314 except Exception as e:
315 self.disp(f"can't resynchronise roster: {e}", error=True)
316 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
317 else:
318 self.disp(_("Roster resynchronized"))
319 self.host.quit(C.EXIT_OK)
320
321
322 class Roster(base.CommandBase):
323 subcommands = (Get, Set, Delete, Stats, Purge, Resync)
324
325 def __init__(self, host):
326 super(Roster, self).__init__(
327 host, 'roster', use_profile=True, help=_("Manage an entity's roster"))