comparison sat_frontends/jp/cmd_roster.py @ 3255:012e89fb2dd1

jp (roster): new roster/set command
author Goffi <goffi@goffi.org>
date Tue, 14 Apr 2020 21:01:04 +0200
parents 559a625a236b
children f30b238d9c45
comparison
equal deleted inserted replaced
3254:6cf4bd6972c2 3255:012e89fb2dd1
1 #!/usr/bin/env python3 1 #!/usr/bin/env python3
2 2
3 3 # jp: a SàT command line tool
4 # jp: a SAT command line tool
5 # Copyright (C) 2009-2020 Jérôme Poisson (goffi@goffi.org) 4 # Copyright (C) 2009-2020 Jérôme Poisson (goffi@goffi.org)
6 # Copyright (C) 2003-2016 Adrien Cossa (souliane@mailoo.org) 5 # Copyright (C) 2003-2016 Adrien Cossa (souliane@mailoo.org)
7 6
8 # This program is free software: you can redistribute it and/or modify 7 # 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 8 # it under the terms of the GNU Affero General Public License as published by
79 data[key] = C.bool(data[key]) 78 data[key] = C.bool(data[key])
80 data['groups'] = list(groups) 79 data['groups'] = list(groups)
81 contacts_dict[jid.JID(contact_jid_s)] = data 80 contacts_dict[jid.JID(contact_jid_s)] = data
82 81
83 await self.output(contacts_dict) 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.contactGet(
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.updateContact(
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)
84 self.host.quit() 125 self.host.quit()
85 126
86 127
87 class Stats(base.CommandBase): 128 class Stats(base.CommandBase):
88 129
250 self.disp(_("Roster resynchronized")) 291 self.disp(_("Roster resynchronized"))
251 self.host.quit(C.EXIT_OK) 292 self.host.quit(C.EXIT_OK)
252 293
253 294
254 class Roster(base.CommandBase): 295 class Roster(base.CommandBase):
255 subcommands = (Get, Stats, Purge, Resync) 296 subcommands = (Get, Set, Stats, Purge, Resync)
256 297
257 def __init__(self, host): 298 def __init__(self, host):
258 super(Roster, self).__init__( 299 super(Roster, self).__init__(
259 host, 'roster', use_profile=True, help=_("Manage an entity's roster")) 300 host, 'roster', use_profile=True, help=_("Manage an entity's roster"))