From 7a2ab7c468b370879bb34af2992310845096f34f Mon Sep 17 00:00:00 2001 From: Goffi Date: Tue, 14 Apr 2020 21:01:04 +0200 Subject: [PATCH] jp (roster): new roster/set command --- doc/jp/roster.rst | 23 +++++++++++++++++ sat_frontends/jp/cmd_roster.py | 47 +++++++++++++++++++++++++++++++--- 2 files changed, 67 insertions(+), 3 deletions(-) diff --git a/doc/jp/roster.rst b/doc/jp/roster.rst index 57493313..58b1540b 100644 --- a/doc/jp/roster.rst +++ b/doc/jp/roster.rst @@ -49,6 +49,29 @@ Get roster or default profile and show the result in JSON:: $ jp roster get -O json +set +=== + +Set metadata for a roster entity. Only ``name`` and ``groups`` can be set, ``name`` being +the user chosed name to use with a contact. + +By default, values are appended, i.e. if ``name`` is not set it won't delete existing one, +and ``groups`` are appended to existing one. However, if you use the ``-R, --replace`` +option, former values will be entirely replaced by given ones (i.e. if you don't use ``-n +NAME, --name NAME`` option, the former one will be deleted, and any former group no added +using ``-g GROUP, --group GROUP`` will be removed). + +example +------- + +Set a name used to privately identify your contact Louise:: + + $ jp roster set -n Enjolras louise@example.net + +Replace all groups of Pierre, to add him only to ``friends`` and ``housemates``:: + + $ jp roster set -g friends -g housemates pierre@example.net + stats ===== diff --git a/sat_frontends/jp/cmd_roster.py b/sat_frontends/jp/cmd_roster.py index eac47dc9..eb0651c7 100644 --- a/sat_frontends/jp/cmd_roster.py +++ b/sat_frontends/jp/cmd_roster.py @@ -1,7 +1,6 @@ #!/usr/bin/env python3 - -# jp: a SAT command line tool +# jp: a SàT command line tool # Copyright (C) 2009-2020 Jérôme Poisson (goffi@goffi.org) # Copyright (C) 2003-2016 Adrien Cossa (souliane@mailoo.org) @@ -84,6 +83,48 @@ class Get(base.CommandBase): self.host.quit() +class Set(base.CommandBase): + + def __init__(self, host): + super().__init__(host, 'set', help=_('set metadata for a roster entity')) + + def add_parser_options(self): + self.parser.add_argument( + "-n", "--name", default="", help=_('name to use for this entity')) + self.parser.add_argument( + "-g", "--group", dest='groups', action='append', metavar='GROUP', default=[], + help=_('groups for this entity')) + self.parser.add_argument( + "-R", "--replace", action="store_true", + help=_("replace all metadata instead of adding them")) + self.parser.add_argument( + "jid", help=_("jid of the roster entity")) + + async def start(self): + + if self.args.replace: + name = self.args.name + groups = self.args.groups + else: + try: + entity_data = await self.host.bridge.contactGet( + self.args.jid, self.host.profile) + except Exception as e: + self.disp(f"error while retrieving the contact: {e}", error=True) + self.host.quit(C.EXIT_BRIDGE_ERRBACK) + name = self.args.name or entity_data[0].get('name') or '' + groups = set(entity_data[1]) + groups = list(groups.union(self.args.groups)) + + try: + await self.host.bridge.updateContact( + self.args.jid, name, groups, self.host.profile) + except Exception as e: + self.disp(f"error while updating the contact: {e}", error=True) + self.host.quit(C.EXIT_BRIDGE_ERRBACK) + self.host.quit() + + class Stats(base.CommandBase): def __init__(self, host): @@ -252,7 +293,7 @@ class Resync(base.CommandBase): class Roster(base.CommandBase): - subcommands = (Get, Stats, Purge, Resync) + subcommands = (Get, Set, Stats, Purge, Resync) def __init__(self, host): super(Roster, self).__init__(