Mercurial > libervia-backend
annotate libervia/cli/cmd_message.py @ 4296:ffc43219e0b2
core (models): add new models for `DiscoIdentity` and `JID`:
`JIDType` `StrictJIDType` can be used when a Twisted JID is expected in a Pydantic model.
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 06 Sep 2024 17:40:32 +0200 |
parents | 0d7bb4df2343 |
children | 472a938a46e3 |
rev | line source |
---|---|
3137 | 1 #!/usr/bin/env python3 |
2 | |
815 | 3 |
4075
47401850dec6
refactoring: rename `libervia.frontends.jp` to `libervia.cli`
Goffi <goffi@goffi.org>
parents:
4074
diff
changeset
|
4 # Libervia CLI |
3479 | 5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org) |
815 | 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 | |
3925
900bf04d87c8
cli (message/send): new `--attach` argument to send an attachment:
Goffi <goffi@goffi.org>
parents:
3887
diff
changeset
|
20 from pathlib import Path |
2708 | 21 import sys |
3925
900bf04d87c8
cli (message/send): new `--attach` argument to send an attachment:
Goffi <goffi@goffi.org>
parents:
3887
diff
changeset
|
22 |
900bf04d87c8
cli (message/send): new `--attach` argument to send an attachment:
Goffi <goffi@goffi.org>
parents:
3887
diff
changeset
|
23 from twisted.python import filepath |
900bf04d87c8
cli (message/send): new `--attach` argument to send an attachment:
Goffi <goffi@goffi.org>
parents:
3887
diff
changeset
|
24 |
4071
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
25 from libervia.backend.core.i18n import _ |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
26 from libervia.backend.tools.common import data_format |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
27 from libervia.backend.tools.common.ansi import ANSI as A |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
28 from libervia.backend.tools.utils import clean_ustr |
4075
47401850dec6
refactoring: rename `libervia.frontends.jp` to `libervia.cli`
Goffi <goffi@goffi.org>
parents:
4074
diff
changeset
|
29 from libervia.cli import base |
47401850dec6
refactoring: rename `libervia.frontends.jp` to `libervia.cli`
Goffi <goffi@goffi.org>
parents:
4074
diff
changeset
|
30 from libervia.cli.constants import Const as C |
4074
26b7ed2817da
refactoring: rename `sat_frontends` to `libervia.frontends`
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
31 from libervia.frontends.tools import jid |
3925
900bf04d87c8
cli (message/send): new `--attach` argument to send an attachment:
Goffi <goffi@goffi.org>
parents:
3887
diff
changeset
|
32 |
0 | 33 |
817 | 34 __commands__ = ["Message"] |
393 | 35 |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
36 |
1828
cb4be663a4a7
jp (message): message is now a subcommand, and the sending command is moved to message/send
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
37 class Send(base.CommandBase): |
817 | 38 def __init__(self, host): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
39 super(Send, self).__init__(host, "send", help=_("send a message to a contact")) |
817 | 40 |
41 def add_parser_options(self): | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
42 self.parser.add_argument( |
3028 | 43 "-l", "--lang", type=str, default="", help=_("language of the message") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
44 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
45 self.parser.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
46 "-s", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
47 "--separate", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
48 action="store_true", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
49 help=_( |
3028 | 50 "separate xmpp messages: send one message per line instead of one " |
51 "message alone." | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
52 ), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
53 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
54 self.parser.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
55 "-n", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
56 "--new-line", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
57 action="store_true", |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
58 help=_("add a new line at the beginning of the input"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
59 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
60 self.parser.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
61 "-S", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
62 "--subject", |
3028 | 63 help=_("subject of the message"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
64 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
65 self.parser.add_argument( |
3411
f30b238d9c45
jp: follow best practices and use a dash (`-`) instead of underscore (`_`) for long options.
Goffi <goffi@goffi.org>
parents:
3356
diff
changeset
|
66 "-L", "--subject-lang", type=str, default="", help=_("language of subject") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
67 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
68 self.parser.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
69 "-t", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
70 "--type", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
71 choices=C.MESS_TYPE_STANDARD + (C.MESS_TYPE_AUTO,), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
72 default=C.MESS_TYPE_AUTO, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
73 help=_("type of the message"), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
74 ) |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
75 self.parser.add_argument( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
76 "-e", |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
77 "--encrypt", |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
78 metavar="ALGORITHM", |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
79 help=_("encrypt message using given algorithm"), |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
80 ) |
2740
8fd8ce5a5855
jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents:
2719
diff
changeset
|
81 self.parser.add_argument( |
8fd8ce5a5855
jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents:
2719
diff
changeset
|
82 "--encrypt-noreplace", |
8fd8ce5a5855
jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents:
2719
diff
changeset
|
83 action="store_true", |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
84 help=_("don't replace encryption algorithm if an other one is already used"), |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
85 ) |
3925
900bf04d87c8
cli (message/send): new `--attach` argument to send an attachment:
Goffi <goffi@goffi.org>
parents:
3887
diff
changeset
|
86 self.parser.add_argument( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
87 "-a", |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
88 "--attach", |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
89 dest="attachments", |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
90 action="append", |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
91 metavar="FILE_PATH", |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
92 help=_("add a file as an attachment"), |
3925
900bf04d87c8
cli (message/send): new `--attach` argument to send an attachment:
Goffi <goffi@goffi.org>
parents:
3887
diff
changeset
|
93 ) |
2081
2265d9df4cfb
jp (message/send): message/send can now send XHTML (-x) or rich (-r) messages
Goffi <goffi@goffi.org>
parents:
1963
diff
changeset
|
94 syntax = self.parser.add_mutually_exclusive_group() |
3028 | 95 syntax.add_argument("-x", "--xhtml", action="store_true", help=_("XHTML body")) |
96 syntax.add_argument("-r", "--rich", action="store_true", help=_("rich body")) | |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
97 self.parser.add_argument("jid", help=_("the destination jid")) |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
98 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
99 async def send_stdin(self, dest_jid): |
817 | 100 """Send incomming data on stdin to jabber contact |
1828
cb4be663a4a7
jp (message): message is now a subcommand, and the sending command is moved to message/send
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
101 |
cb4be663a4a7
jp (message): message is now a subcommand, and the sending command is moved to message/send
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
102 @param dest_jid: destination jid |
cb4be663a4a7
jp (message): message is now a subcommand, and the sending command is moved to message/send
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
103 """ |
817 | 104 header = "\n" if self.args.new_line else "" |
3040 | 105 # FIXME: stdin is not read asynchronously at the moment |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
106 stdin_lines = [stream for stream in sys.stdin.readlines()] |
2081
2265d9df4cfb
jp (message/send): message/send can now send XHTML (-x) or rich (-r) messages
Goffi <goffi@goffi.org>
parents:
1963
diff
changeset
|
107 extra = {} |
1963
a2bc5089c2eb
backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents:
1960
diff
changeset
|
108 if self.args.subject is None: |
a2bc5089c2eb
backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents:
1960
diff
changeset
|
109 subject = {} |
a2bc5089c2eb
backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents:
1960
diff
changeset
|
110 else: |
a2bc5089c2eb
backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents:
1960
diff
changeset
|
111 subject = {self.args.subject_lang: self.args.subject} |
817 | 112 |
2081
2265d9df4cfb
jp (message/send): message/send can now send XHTML (-x) or rich (-r) messages
Goffi <goffi@goffi.org>
parents:
1963
diff
changeset
|
113 if self.args.xhtml or self.args.rich: |
3028 | 114 key = "xhtml" if self.args.xhtml else "rich" |
2081
2265d9df4cfb
jp (message/send): message/send can now send XHTML (-x) or rich (-r) messages
Goffi <goffi@goffi.org>
parents:
1963
diff
changeset
|
115 if self.args.lang: |
3040 | 116 key = f"{key}_{self.args.lang}" |
3028 | 117 extra[key] = clean_ustr("".join(stdin_lines)) |
2081
2265d9df4cfb
jp (message/send): message/send can now send XHTML (-x) or rich (-r) messages
Goffi <goffi@goffi.org>
parents:
1963
diff
changeset
|
118 stdin_lines = [] |
2265d9df4cfb
jp (message/send): message/send can now send XHTML (-x) or rich (-r) messages
Goffi <goffi@goffi.org>
parents:
1963
diff
changeset
|
119 |
3040 | 120 to_send = [] |
121 | |
122 error = False | |
2740
8fd8ce5a5855
jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents:
2719
diff
changeset
|
123 |
3040 | 124 if self.args.separate: |
125 # we send stdin in several messages | |
0 | 126 if header: |
3040 | 127 # first we sent the header |
128 try: | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
129 await self.host.bridge.message_send( |
3040 | 130 dest_jid, |
131 {self.args.lang: header}, | |
132 subject, | |
133 self.args.type, | |
134 profile_key=self.profile, | |
135 ) | |
136 except Exception as e: | |
137 self.disp(f"can't send header: {e}", error=True) | |
138 error = True | |
139 | |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
140 to_send.extend( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
141 {self.args.lang: clean_ustr(l.replace("\n", ""))} for l in stdin_lines |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
142 ) |
3040 | 143 else: |
144 # we sent all in a single message | |
145 if not (self.args.xhtml or self.args.rich): | |
146 msg = {self.args.lang: header + clean_ustr("".join(stdin_lines))} | |
147 else: | |
148 msg = {} | |
149 to_send.append(msg) | |
150 | |
3925
900bf04d87c8
cli (message/send): new `--attach` argument to send an attachment:
Goffi <goffi@goffi.org>
parents:
3887
diff
changeset
|
151 if self.args.attachments: |
4023
78b5f356900c
component AP gateway: handle attachments
Goffi <goffi@goffi.org>
parents:
3925
diff
changeset
|
152 attachments = extra[C.KEY_ATTACHMENTS] = [] |
3925
900bf04d87c8
cli (message/send): new `--attach` argument to send an attachment:
Goffi <goffi@goffi.org>
parents:
3887
diff
changeset
|
153 for attachment in self.args.attachments: |
900bf04d87c8
cli (message/send): new `--attach` argument to send an attachment:
Goffi <goffi@goffi.org>
parents:
3887
diff
changeset
|
154 try: |
900bf04d87c8
cli (message/send): new `--attach` argument to send an attachment:
Goffi <goffi@goffi.org>
parents:
3887
diff
changeset
|
155 file_path = str(Path(attachment).resolve(strict=True)) |
900bf04d87c8
cli (message/send): new `--attach` argument to send an attachment:
Goffi <goffi@goffi.org>
parents:
3887
diff
changeset
|
156 except FileNotFoundError: |
900bf04d87c8
cli (message/send): new `--attach` argument to send an attachment:
Goffi <goffi@goffi.org>
parents:
3887
diff
changeset
|
157 self.disp("file {attachment} doesn't exists, ignoring", error=True) |
900bf04d87c8
cli (message/send): new `--attach` argument to send an attachment:
Goffi <goffi@goffi.org>
parents:
3887
diff
changeset
|
158 else: |
900bf04d87c8
cli (message/send): new `--attach` argument to send an attachment:
Goffi <goffi@goffi.org>
parents:
3887
diff
changeset
|
159 attachments.append({"path": file_path}) |
900bf04d87c8
cli (message/send): new `--attach` argument to send an attachment:
Goffi <goffi@goffi.org>
parents:
3887
diff
changeset
|
160 |
900bf04d87c8
cli (message/send): new `--attach` argument to send an attachment:
Goffi <goffi@goffi.org>
parents:
3887
diff
changeset
|
161 for idx, msg in enumerate(to_send): |
4023
78b5f356900c
component AP gateway: handle attachments
Goffi <goffi@goffi.org>
parents:
3925
diff
changeset
|
162 if idx > 0 and C.KEY_ATTACHMENTS in extra: |
3925
900bf04d87c8
cli (message/send): new `--attach` argument to send an attachment:
Goffi <goffi@goffi.org>
parents:
3887
diff
changeset
|
163 # if we send several messages, we only want to send attachments with the |
900bf04d87c8
cli (message/send): new `--attach` argument to send an attachment:
Goffi <goffi@goffi.org>
parents:
3887
diff
changeset
|
164 # first one |
4023
78b5f356900c
component AP gateway: handle attachments
Goffi <goffi@goffi.org>
parents:
3925
diff
changeset
|
165 del extra[C.KEY_ATTACHMENTS] |
3040 | 166 try: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
167 await self.host.bridge.message_send( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
168 dest_jid, |
3040 | 169 msg, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
170 subject, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
171 self.args.type, |
3179
84a94b385760
bridge: messageSend's extra is now serialised
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
172 data_format.serialise(extra), |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
173 profile_key=self.host.profile, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
174 ) |
3040 | 175 except Exception as e: |
176 self.disp(f"can't send message {msg!r}: {e}", error=True) | |
177 error = True | |
0 | 178 |
3040 | 179 if error: |
180 # at least one message sending failed | |
181 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
2740
8fd8ce5a5855
jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents:
2719
diff
changeset
|
182 |
3040 | 183 self.host.quit() |
2740
8fd8ce5a5855
jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents:
2719
diff
changeset
|
184 |
3040 | 185 async def start(self): |
2740
8fd8ce5a5855
jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents:
2719
diff
changeset
|
186 if self.args.xhtml and self.args.separate: |
8fd8ce5a5855
jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents:
2719
diff
changeset
|
187 self.disp( |
3028 | 188 "argument -s/--separate is not compatible yet with argument -x/--xhtml", |
2740
8fd8ce5a5855
jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents:
2719
diff
changeset
|
189 error=True, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
190 ) |
3040 | 191 self.host.quit(C.EXIT_BAD_ARG) |
2740
8fd8ce5a5855
jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents:
2719
diff
changeset
|
192 |
3040 | 193 jids = await self.host.check_jids([self.args.jid]) |
2740
8fd8ce5a5855
jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents:
2719
diff
changeset
|
194 jid_ = jids[0] |
8fd8ce5a5855
jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents:
2719
diff
changeset
|
195 |
8fd8ce5a5855
jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents:
2719
diff
changeset
|
196 if self.args.encrypt_noreplace and self.args.encrypt is None: |
8fd8ce5a5855
jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents:
2719
diff
changeset
|
197 self.parser.error("You need to use --encrypt if you use --encrypt-noreplace") |
8fd8ce5a5855
jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents:
2719
diff
changeset
|
198 |
8fd8ce5a5855
jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents:
2719
diff
changeset
|
199 if self.args.encrypt is not None: |
3040 | 200 try: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
201 namespace = await self.host.bridge.encryption_namespace_get( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
202 self.args.encrypt |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
203 ) |
3040 | 204 except Exception as e: |
205 self.disp(f"can't get encryption namespace: {e}", error=True) | |
206 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
207 | |
208 try: | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
209 await self.host.bridge.message_encryption_start( |
3040 | 210 jid_, namespace, not self.args.encrypt_noreplace, self.profile |
211 ) | |
212 except Exception as e: | |
213 self.disp(f"can't start encryption session: {e}", error=True) | |
214 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
215 | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
216 await self.send_stdin(jid_) |
1828
cb4be663a4a7
jp (message): message is now a subcommand, and the sending command is moved to message/send
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
217 |
cb4be663a4a7
jp (message): message is now a subcommand, and the sending command is moved to message/send
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
218 |
3805
33ab258df0de
cli (message/retract): message retractation command:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
219 class Retract(base.CommandBase): |
33ab258df0de
cli (message/retract): message retractation command:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
220 |
33ab258df0de
cli (message/retract): message retractation command:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
221 def __init__(self, host): |
33ab258df0de
cli (message/retract): message retractation command:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
222 super().__init__(host, "retract", help=_("retract a message")) |
33ab258df0de
cli (message/retract): message retractation command:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
223 |
33ab258df0de
cli (message/retract): message retractation command:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
224 def add_parser_options(self): |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
225 self.parser.add_argument("message_id", help=_("ID of the message (internal ID)")) |
3805
33ab258df0de
cli (message/retract): message retractation command:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
226 |
33ab258df0de
cli (message/retract): message retractation command:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
227 async def start(self): |
33ab258df0de
cli (message/retract): message retractation command:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
228 try: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
229 await self.host.bridge.message_retract(self.args.message_id, self.profile) |
3805
33ab258df0de
cli (message/retract): message retractation command:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
230 except Exception as e: |
33ab258df0de
cli (message/retract): message retractation command:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
231 self.disp(f"can't retract message: {e}", error=True) |
33ab258df0de
cli (message/retract): message retractation command:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
232 self.host.quit(C.EXIT_BRIDGE_ERRBACK) |
33ab258df0de
cli (message/retract): message retractation command:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
233 else: |
33ab258df0de
cli (message/retract): message retractation command:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
234 self.disp( |
33ab258df0de
cli (message/retract): message retractation command:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
235 "message retraction has been requested, please note that this is a " |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
236 "request which can't be enforced (see documentation for details)." |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
237 ) |
3805
33ab258df0de
cli (message/retract): message retractation command:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
238 self.host.quit(C.EXIT_OK) |
33ab258df0de
cli (message/retract): message retractation command:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
239 |
33ab258df0de
cli (message/retract): message retractation command:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
240 |
2708 | 241 class MAM(base.CommandBase): |
242 | |
243 def __init__(self, host): | |
244 super(MAM, self).__init__( | |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
245 host, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
246 "mam", |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
247 use_output=C.OUTPUT_MESS, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
248 use_verbose=True, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
249 help=_("query archives using MAM"), |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
250 ) |
2708 | 251 |
252 def add_parser_options(self): | |
253 self.parser.add_argument( | |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
254 "-s", |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
255 "--service", |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
256 default="", |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
257 help=_("jid of the service (default: profile's server"), |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
258 ) |
2708 | 259 self.parser.add_argument( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
260 "-S", |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
261 "--start", |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
262 dest="mam_start", |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
263 type=base.date_decoder, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
264 help=_("start fetching archive from this date (default: from the beginning)"), |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
265 ) |
2708 | 266 self.parser.add_argument( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
267 "-E", |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
268 "--end", |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
269 dest="mam_end", |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
270 type=base.date_decoder, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
271 help=_("end fetching archive after this date (default: no limit)"), |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
272 ) |
2708 | 273 self.parser.add_argument( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
274 "-W", |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
275 "--with", |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
276 dest="mam_with", |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
277 help=_("retrieve only archives with this jid"), |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
278 ) |
2708 | 279 self.parser.add_argument( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
280 "-m", |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
281 "--max", |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
282 dest="rsm_max", |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
283 type=int, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
284 default=20, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
285 help=_("maximum number of items to retrieve, using RSM (default: 20))"), |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
286 ) |
2819
fd45089b3a92
jp (message/mam): added missing RSM options
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
287 rsm_page_group = self.parser.add_mutually_exclusive_group() |
fd45089b3a92
jp (message/mam): added missing RSM options
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
288 rsm_page_group.add_argument( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
289 "-a", |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
290 "--after", |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
291 dest="rsm_after", |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
292 help=_("find page after this item"), |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
293 metavar="ITEM_ID", |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
294 ) |
2819
fd45089b3a92
jp (message/mam): added missing RSM options
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
295 rsm_page_group.add_argument( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
296 "-b", |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
297 "--before", |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
298 dest="rsm_before", |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
299 help=_("find page before this item"), |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
300 metavar="ITEM_ID", |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
301 ) |
2819
fd45089b3a92
jp (message/mam): added missing RSM options
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
302 rsm_page_group.add_argument( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
303 "--index", dest="rsm_index", type=int, help=_("index of the page to retrieve") |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
304 ) |
2708 | 305 |
3040 | 306 async def start(self): |
307 extra = {} | |
308 if self.args.mam_start is not None: | |
309 extra["mam_start"] = float(self.args.mam_start) | |
310 if self.args.mam_end is not None: | |
311 extra["mam_end"] = float(self.args.mam_end) | |
312 if self.args.mam_with is not None: | |
313 extra["mam_with"] = self.args.mam_with | |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
314 for suff in ("max", "after", "before", "index"): |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
315 key = "rsm_" + suff |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
316 value = getattr(self.args, key) |
3040 | 317 if value is not None: |
318 extra[key] = str(value) | |
319 try: | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
320 data, metadata_s, profile = await self.host.bridge.mam_get( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
321 self.args.service, data_format.serialise(extra), self.profile |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
322 ) |
3040 | 323 except Exception as e: |
324 self.disp(f"can't retrieve MAM archives: {e}", error=True) | |
325 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
326 | |
3356
569f4cf7183b
plugin XEP-0313: fixed `MAMGet` signature
Goffi <goffi@goffi.org>
parents:
3179
diff
changeset
|
327 metadata = data_format.deserialise(metadata_s) |
569f4cf7183b
plugin XEP-0313: fixed `MAMGet` signature
Goffi <goffi@goffi.org>
parents:
3179
diff
changeset
|
328 |
3040 | 329 try: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
330 session_info = await self.host.bridge.session_infos_get(self.profile) |
3040 | 331 except Exception as e: |
332 self.disp(f"can't get session infos: {e}", error=True) | |
333 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
334 | |
335 # we need to fill own_jid for message output | |
3028 | 336 self.host.own_jid = jid.JID(session_info["jid"]) |
3040 | 337 |
338 await self.output(data) | |
339 | |
2719
45189c8bd165
jp (message/mam): display metadata when verbose
Goffi <goffi@goffi.org>
parents:
2708
diff
changeset
|
340 # FIXME: metadata are not displayed correctly and don't play nice with output |
45189c8bd165
jp (message/mam): display metadata when verbose
Goffi <goffi@goffi.org>
parents:
2708
diff
changeset
|
341 # they should be added to output data somehow |
45189c8bd165
jp (message/mam): display metadata when verbose
Goffi <goffi@goffi.org>
parents:
2708
diff
changeset
|
342 if self.verbosity: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
343 for value in ( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
344 "rsm_first", |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
345 "rsm_last", |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
346 "rsm_index", |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
347 "rsm_count", |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
348 "mam_complete", |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
349 "mam_stable", |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
350 ): |
2719
45189c8bd165
jp (message/mam): display metadata when verbose
Goffi <goffi@goffi.org>
parents:
2708
diff
changeset
|
351 if value in metadata: |
3028 | 352 label = value.split("_")[1] |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
353 self.disp(A.color(C.A_HEADER, label, ": ", A.RESET, metadata[value])) |
2719
45189c8bd165
jp (message/mam): display metadata when verbose
Goffi <goffi@goffi.org>
parents:
2708
diff
changeset
|
354 |
2708 | 355 self.host.quit() |
356 | |
357 | |
1828
cb4be663a4a7
jp (message): message is now a subcommand, and the sending command is moved to message/send
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
358 class Message(base.CommandBase): |
3805
33ab258df0de
cli (message/retract): message retractation command:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
359 subcommands = (Send, Retract, MAM) |
1828
cb4be663a4a7
jp (message): message is now a subcommand, and the sending command is moved to message/send
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
360 |
cb4be663a4a7
jp (message): message is now a subcommand, and the sending command is moved to message/send
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
361 def __init__(self, host): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
362 super(Message, self).__init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
363 host, "message", use_profile=False, help=_("messages handling") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
364 ) |