annotate sat_frontends/jp/cmd_blocking.py @ 3914:4cb38c8312a1

plugin XEP-0384, xml_tools: avoid `getItems` timeout + fix empty node crash + parsing: - use `max_items` in `getItems` calls for bundles, as otherwise some pubsub service may return full nodes, which may be huge is `max_items=1` is not set on the node, possibly resulting in timeouts. - the plugin was crashing when TWOMEMO devices list node has no items at all. This is not the case anymore. - a naive parsing method has been implemented in `xml_tools` to replace the serialisation/deserialisation method. This should be more efficient and will avoid annoying `ns0:` prefixes in XML logs.
author Goffi <goffi@goffi.org>
date Sat, 24 Sep 2022 16:37:46 +0200
parents 12317ba98d99
children 524856bd7b19
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3788
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python3
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
2
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
3
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # jp: a SàT command line tool
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
6
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
16
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
19
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
20
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 import json
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 import os
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from sat.core.i18n import _
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from sat.tools.common import data_format
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from sat_frontends.jp import common
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from sat_frontends.jp.constants import Const as C
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from . import base
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
28
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 __commands__ = ["Blocking"]
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
30
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
31
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 class List(base.CommandBase):
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 def __init__(self, host):
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 super().__init__(
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 host,
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 "list",
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 use_output=C.OUTPUT_LIST,
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 help=_("list blocked entities"),
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 )
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
40
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 def add_parser_options(self):
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 pass
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
43
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 async def start(self):
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 try:
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 blocked_jids = await self.host.bridge.blockingList(
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 self.profile,
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 )
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 except Exception as e:
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 self.disp(f"can't get blocked entities: {e}", error=True)
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 else:
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 await self.output(blocked_jids)
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 self.host.quit(C.EXIT_OK)
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
55
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
56
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 class Block(base.CommandBase):
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 def __init__(self, host):
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 super().__init__(
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 host,
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 "block",
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 help=_("block one or more entities"),
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 )
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
64
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 def add_parser_options(self):
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 self.parser.add_argument(
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 "entities",
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 nargs="+",
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 metavar="JID",
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 help=_("JIDs of entities to block"),
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 )
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
72
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 async def start(self):
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 try:
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 await self.host.bridge.blockingBlock(
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 self.args.entities,
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 self.profile
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 )
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 except Exception as e:
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 self.disp(f"can't block entities: {e}", error=True)
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 else:
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 self.host.quit(C.EXIT_OK)
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
84
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
85
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 class Unblock(base.CommandBase):
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 def __init__(self, host):
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 super().__init__(
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
89 host,
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 "unblock",
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 help=_("unblock one or more entities"),
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 )
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
93
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
94 def add_parser_options(self):
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
95 self.parser.add_argument(
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 "entities",
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 nargs="+",
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 metavar="JID",
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 help=_("JIDs of entities to unblock"),
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 )
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 self.parser.add_argument(
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 "-f",
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
103 "--force",
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
104 action="store_true",
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 help=_('when "all" is used, unblock all entities without confirmation'),
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
106 )
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
107
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 async def start(self):
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
109 if self.args.entities == ["all"]:
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
110 if not self.args.force:
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 await self.host.confirmOrQuit(
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 _("All entities will be unblocked, are you sure"),
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
113 _("unblock cancelled")
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
114 )
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
115 self.args.entities.clear()
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
116 elif self.args.force:
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
117 self.parser.error(_('--force is only allowed when "all" is used as target'))
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
118
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
119 try:
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
120 await self.host.bridge.blockingUnblock(
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
121 self.args.entities,
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
122 self.profile
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 )
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
124 except Exception as e:
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 self.disp(f"can't unblock entities: {e}", error=True)
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
126 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
127 else:
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
128 self.host.quit(C.EXIT_OK)
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
129
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
130
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
131 class Blocking(base.CommandBase):
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 subcommands = (List, Block, Unblock)
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
133
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
134 def __init__(self, host):
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
135 super().__init__(
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
136 host, "blocking", use_profile=False, help=_("entities blocking")
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
137 )