Mercurial > libervia-backend
annotate sat_frontends/jp/cmd_message.py @ 4023:78b5f356900c
component AP gateway: handle attachments
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 23 Mar 2023 15:42:21 +0100 |
parents | 900bf04d87c8 |
children | 524856bd7b19 |
rev | line source |
---|---|
3137 | 1 #!/usr/bin/env python3 |
2 | |
815 | 3 |
4 # jp: a SAT command line tool | |
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 |
900bf04d87c8
cli (message/send): new `--attach` argument to send an attachment:
Goffi <goffi@goffi.org>
parents:
3887
diff
changeset
|
25 from sat.core.i18n import _ |
900bf04d87c8
cli (message/send): new `--attach` argument to send an attachment:
Goffi <goffi@goffi.org>
parents:
3887
diff
changeset
|
26 from sat.tools.common import data_format |
900bf04d87c8
cli (message/send): new `--attach` argument to send an attachment:
Goffi <goffi@goffi.org>
parents:
3887
diff
changeset
|
27 from sat.tools.common.ansi import ANSI as A |
900bf04d87c8
cli (message/send): new `--attach` argument to send an attachment:
Goffi <goffi@goffi.org>
parents:
3887
diff
changeset
|
28 from sat.tools.utils import clean_ustr |
817 | 29 from sat_frontends.jp import base |
2663
32b5f68a23b4
jp (message): new encryption/algorithms command to retrieve available encryptions algorithms.
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
30 from sat_frontends.jp.constants import Const as C |
2708 | 31 from sat_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", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
58 help=_( |
3925
900bf04d87c8
cli (message/send): new `--attach` argument to send an attachment:
Goffi <goffi@goffi.org>
parents:
3887
diff
changeset
|
59 "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
|
60 ), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
61 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
62 self.parser.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
63 "-S", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
64 "--subject", |
3028 | 65 help=_("subject of the message"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
66 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
67 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
|
68 "-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
|
69 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
70 self.parser.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
71 "-t", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
72 "--type", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
73 choices=C.MESS_TYPE_STANDARD + (C.MESS_TYPE_AUTO,), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
74 default=C.MESS_TYPE_AUTO, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
75 help=_("type of the message"), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
76 ) |
2740
8fd8ce5a5855
jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents:
2719
diff
changeset
|
77 self.parser.add_argument("-e", "--encrypt", metavar="ALGORITHM", |
3028 | 78 help=_("encrypt message using given algorithm")) |
2740
8fd8ce5a5855
jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents:
2719
diff
changeset
|
79 self.parser.add_argument( |
8fd8ce5a5855
jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents:
2719
diff
changeset
|
80 "--encrypt-noreplace", |
8fd8ce5a5855
jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents:
2719
diff
changeset
|
81 action="store_true", |
3028 | 82 help=_("don't replace encryption algorithm if an other one is already used")) |
3925
900bf04d87c8
cli (message/send): new `--attach` argument to send an attachment:
Goffi <goffi@goffi.org>
parents:
3887
diff
changeset
|
83 self.parser.add_argument( |
900bf04d87c8
cli (message/send): new `--attach` argument to send an attachment:
Goffi <goffi@goffi.org>
parents:
3887
diff
changeset
|
84 "-a", "--attach", dest="attachments", action="append", metavar="FILE_PATH", |
900bf04d87c8
cli (message/send): new `--attach` argument to send an attachment:
Goffi <goffi@goffi.org>
parents:
3887
diff
changeset
|
85 help=_("add a file as an attachment") |
900bf04d87c8
cli (message/send): new `--attach` argument to send an attachment:
Goffi <goffi@goffi.org>
parents:
3887
diff
changeset
|
86 ) |
2081
2265d9df4cfb
jp (message/send): message/send can now send XHTML (-x) or rich (-r) messages
Goffi <goffi@goffi.org>
parents:
1963
diff
changeset
|
87 syntax = self.parser.add_mutually_exclusive_group() |
3028 | 88 syntax.add_argument("-x", "--xhtml", action="store_true", help=_("XHTML body")) |
89 syntax.add_argument("-r", "--rich", action="store_true", help=_("rich body")) | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
90 self.parser.add_argument( |
3028 | 91 "jid", help=_("the destination jid") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
92 ) |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
93 |
3040 | 94 async def sendStdin(self, dest_jid): |
817 | 95 """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
|
96 |
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
|
97 @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
|
98 """ |
817 | 99 header = "\n" if self.args.new_line else "" |
3040 | 100 # FIXME: stdin is not read asynchronously at the moment |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
101 stdin_lines = [ |
3040 | 102 stream for stream in sys.stdin.readlines() |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
103 ] |
2081
2265d9df4cfb
jp (message/send): message/send can now send XHTML (-x) or rich (-r) messages
Goffi <goffi@goffi.org>
parents:
1963
diff
changeset
|
104 extra = {} |
1963
a2bc5089c2eb
backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents:
1960
diff
changeset
|
105 if self.args.subject is None: |
a2bc5089c2eb
backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents:
1960
diff
changeset
|
106 subject = {} |
a2bc5089c2eb
backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents:
1960
diff
changeset
|
107 else: |
a2bc5089c2eb
backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents:
1960
diff
changeset
|
108 subject = {self.args.subject_lang: self.args.subject} |
817 | 109 |
2081
2265d9df4cfb
jp (message/send): message/send can now send XHTML (-x) or rich (-r) messages
Goffi <goffi@goffi.org>
parents:
1963
diff
changeset
|
110 if self.args.xhtml or self.args.rich: |
3028 | 111 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
|
112 if self.args.lang: |
3040 | 113 key = f"{key}_{self.args.lang}" |
3028 | 114 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
|
115 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
|
116 |
3040 | 117 to_send = [] |
118 | |
119 error = False | |
2740
8fd8ce5a5855
jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents:
2719
diff
changeset
|
120 |
3040 | 121 if self.args.separate: |
122 # we send stdin in several messages | |
0 | 123 if header: |
3040 | 124 # first we sent the header |
125 try: | |
126 await self.host.bridge.messageSend( | |
127 dest_jid, | |
128 {self.args.lang: header}, | |
129 subject, | |
130 self.args.type, | |
131 profile_key=self.profile, | |
132 ) | |
133 except Exception as e: | |
134 self.disp(f"can't send header: {e}", error=True) | |
135 error = True | |
136 | |
137 to_send.extend({self.args.lang: clean_ustr(l.replace("\n", ""))} | |
138 for l in stdin_lines) | |
139 else: | |
140 # we sent all in a single message | |
141 if not (self.args.xhtml or self.args.rich): | |
142 msg = {self.args.lang: header + clean_ustr("".join(stdin_lines))} | |
143 else: | |
144 msg = {} | |
145 to_send.append(msg) | |
146 | |
3925
900bf04d87c8
cli (message/send): new `--attach` argument to send an attachment:
Goffi <goffi@goffi.org>
parents:
3887
diff
changeset
|
147 if self.args.attachments: |
4023
78b5f356900c
component AP gateway: handle attachments
Goffi <goffi@goffi.org>
parents:
3925
diff
changeset
|
148 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
|
149 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
|
150 try: |
900bf04d87c8
cli (message/send): new `--attach` argument to send an attachment:
Goffi <goffi@goffi.org>
parents:
3887
diff
changeset
|
151 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
|
152 except FileNotFoundError: |
900bf04d87c8
cli (message/send): new `--attach` argument to send an attachment:
Goffi <goffi@goffi.org>
parents:
3887
diff
changeset
|
153 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
|
154 else: |
900bf04d87c8
cli (message/send): new `--attach` argument to send an attachment:
Goffi <goffi@goffi.org>
parents:
3887
diff
changeset
|
155 attachments.append({"path": file_path}) |
900bf04d87c8
cli (message/send): new `--attach` argument to send an attachment:
Goffi <goffi@goffi.org>
parents:
3887
diff
changeset
|
156 |
900bf04d87c8
cli (message/send): new `--attach` argument to send an attachment:
Goffi <goffi@goffi.org>
parents:
3887
diff
changeset
|
157 for idx, msg in enumerate(to_send): |
4023
78b5f356900c
component AP gateway: handle attachments
Goffi <goffi@goffi.org>
parents:
3925
diff
changeset
|
158 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
|
159 # 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
|
160 # first one |
4023
78b5f356900c
component AP gateway: handle attachments
Goffi <goffi@goffi.org>
parents:
3925
diff
changeset
|
161 del extra[C.KEY_ATTACHMENTS] |
3040 | 162 try: |
163 await self.host.bridge.messageSend( | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
164 dest_jid, |
3040 | 165 msg, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
166 subject, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
167 self.args.type, |
3179
84a94b385760
bridge: messageSend's extra is now serialised
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
168 data_format.serialise(extra), |
3040 | 169 profile_key=self.host.profile) |
170 except Exception as e: | |
171 self.disp(f"can't send message {msg!r}: {e}", error=True) | |
172 error = True | |
0 | 173 |
3040 | 174 if error: |
175 # at least one message sending failed | |
176 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
2740
8fd8ce5a5855
jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents:
2719
diff
changeset
|
177 |
3040 | 178 self.host.quit() |
2740
8fd8ce5a5855
jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents:
2719
diff
changeset
|
179 |
3040 | 180 async def start(self): |
2740
8fd8ce5a5855
jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents:
2719
diff
changeset
|
181 if self.args.xhtml and self.args.separate: |
8fd8ce5a5855
jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents:
2719
diff
changeset
|
182 self.disp( |
3028 | 183 "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
|
184 error=True, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
185 ) |
3040 | 186 self.host.quit(C.EXIT_BAD_ARG) |
2740
8fd8ce5a5855
jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents:
2719
diff
changeset
|
187 |
3040 | 188 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
|
189 jid_ = jids[0] |
8fd8ce5a5855
jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents:
2719
diff
changeset
|
190 |
8fd8ce5a5855
jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents:
2719
diff
changeset
|
191 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
|
192 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
|
193 |
8fd8ce5a5855
jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents:
2719
diff
changeset
|
194 if self.args.encrypt is not None: |
3040 | 195 try: |
196 namespace = await self.host.bridge.encryptionNamespaceGet( | |
197 self.args.encrypt) | |
198 except Exception as e: | |
199 self.disp(f"can't get encryption namespace: {e}", error=True) | |
200 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
201 | |
202 try: | |
203 await self.host.bridge.messageEncryptionStart( | |
204 jid_, namespace, not self.args.encrypt_noreplace, self.profile | |
205 ) | |
206 except Exception as e: | |
207 self.disp(f"can't start encryption session: {e}", error=True) | |
208 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
209 | |
210 await self.sendStdin(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
|
211 |
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
|
212 |
3805
33ab258df0de
cli (message/retract): message retractation command:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
213 class Retract(base.CommandBase): |
33ab258df0de
cli (message/retract): message retractation command:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
214 |
33ab258df0de
cli (message/retract): message retractation command:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
215 def __init__(self, host): |
33ab258df0de
cli (message/retract): message retractation command:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
216 super().__init__(host, "retract", help=_("retract a message")) |
33ab258df0de
cli (message/retract): message retractation command:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
217 |
33ab258df0de
cli (message/retract): message retractation command:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
218 def add_parser_options(self): |
33ab258df0de
cli (message/retract): message retractation command:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
219 self.parser.add_argument( |
33ab258df0de
cli (message/retract): message retractation command:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
220 "message_id", |
33ab258df0de
cli (message/retract): message retractation command:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
221 help=_("ID of the message (internal ID)") |
33ab258df0de
cli (message/retract): message retractation command:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
222 ) |
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 async def start(self): |
33ab258df0de
cli (message/retract): message retractation command:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
225 try: |
3887
6090141b1b70
plugin XEP-0424: rename bridge method:
Goffi <goffi@goffi.org>
parents:
3805
diff
changeset
|
226 await self.host.bridge.messageRetract( |
3805
33ab258df0de
cli (message/retract): message retractation command:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
227 self.args.message_id, |
33ab258df0de
cli (message/retract): message retractation command:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
228 self.profile |
33ab258df0de
cli (message/retract): message retractation command:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
229 ) |
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 " |
33ab258df0de
cli (message/retract): message retractation command:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
236 "request which can't be enforced (see documentation for details).") |
33ab258df0de
cli (message/retract): message retractation command:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
237 self.host.quit(C.EXIT_OK) |
33ab258df0de
cli (message/retract): message retractation command:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
238 |
33ab258df0de
cli (message/retract): message retractation command:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
239 |
2708 | 240 class MAM(base.CommandBase): |
241 | |
242 def __init__(self, host): | |
243 super(MAM, self).__init__( | |
3040 | 244 host, "mam", use_output=C.OUTPUT_MESS, use_verbose=True, |
245 help=_("query archives using MAM")) | |
2708 | 246 |
247 def add_parser_options(self): | |
248 self.parser.add_argument( | |
3028 | 249 "-s", "--service", default="", |
250 help=_("jid of the service (default: profile's server")) | |
2708 | 251 self.parser.add_argument( |
252 "-S", "--start", dest="mam_start", type=base.date_decoder, | |
2824
0ae25883e223
jp (message/mam): --start doesn't defaut anymore to "1 day ago", it is now unset by default
Goffi <goffi@goffi.org>
parents:
2819
diff
changeset
|
253 help=_( |
3028 | 254 "start fetching archive from this date (default: from the beginning)")) |
2708 | 255 self.parser.add_argument( |
256 "-E", "--end", dest="mam_end", type=base.date_decoder, | |
3028 | 257 help=_("end fetching archive after this date (default: no limit)")) |
2708 | 258 self.parser.add_argument( |
3028 | 259 "-W", "--with", dest="mam_with", |
260 help=_("retrieve only archives with this jid")) | |
2708 | 261 self.parser.add_argument( |
2764
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2740
diff
changeset
|
262 "-m", "--max", dest="rsm_max", type=int, default=20, |
3028 | 263 help=_("maximum number of items to retrieve, using RSM (default: 20))")) |
2819
fd45089b3a92
jp (message/mam): added missing RSM options
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
264 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
|
265 rsm_page_group.add_argument( |
3028 | 266 "-a", "--after", dest="rsm_after", |
267 help=_("find page after this item"), metavar='ITEM_ID') | |
2819
fd45089b3a92
jp (message/mam): added missing RSM options
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
268 rsm_page_group.add_argument( |
3028 | 269 "-b", "--before", dest="rsm_before", |
270 help=_("find page before this item"), metavar='ITEM_ID') | |
2819
fd45089b3a92
jp (message/mam): added missing RSM options
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
271 rsm_page_group.add_argument( |
fd45089b3a92
jp (message/mam): added missing RSM options
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
272 "--index", dest="rsm_index", type=int, |
3028 | 273 help=_("index of the page to retrieve")) |
2708 | 274 |
3040 | 275 async def start(self): |
276 extra = {} | |
277 if self.args.mam_start is not None: | |
278 extra["mam_start"] = float(self.args.mam_start) | |
279 if self.args.mam_end is not None: | |
280 extra["mam_end"] = float(self.args.mam_end) | |
281 if self.args.mam_with is not None: | |
282 extra["mam_with"] = self.args.mam_with | |
283 for suff in ('max', 'after', 'before', 'index'): | |
284 key = 'rsm_' + suff | |
285 value = getattr(self.args,key) | |
286 if value is not None: | |
287 extra[key] = str(value) | |
288 try: | |
3356
569f4cf7183b
plugin XEP-0313: fixed `MAMGet` signature
Goffi <goffi@goffi.org>
parents:
3179
diff
changeset
|
289 data, metadata_s, profile = await self.host.bridge.MAMGet( |
3040 | 290 self.args.service, data_format.serialise(extra), self.profile) |
291 except Exception as e: | |
292 self.disp(f"can't retrieve MAM archives: {e}", error=True) | |
293 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
294 | |
3356
569f4cf7183b
plugin XEP-0313: fixed `MAMGet` signature
Goffi <goffi@goffi.org>
parents:
3179
diff
changeset
|
295 metadata = data_format.deserialise(metadata_s) |
569f4cf7183b
plugin XEP-0313: fixed `MAMGet` signature
Goffi <goffi@goffi.org>
parents:
3179
diff
changeset
|
296 |
3040 | 297 try: |
298 session_info = await self.host.bridge.sessionInfosGet(self.profile) | |
299 except Exception as e: | |
300 self.disp(f"can't get session infos: {e}", error=True) | |
301 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
302 | |
303 # we need to fill own_jid for message output | |
3028 | 304 self.host.own_jid = jid.JID(session_info["jid"]) |
3040 | 305 |
306 await self.output(data) | |
307 | |
2719
45189c8bd165
jp (message/mam): display metadata when verbose
Goffi <goffi@goffi.org>
parents:
2708
diff
changeset
|
308 # 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
|
309 # they should be added to output data somehow |
45189c8bd165
jp (message/mam): display metadata when verbose
Goffi <goffi@goffi.org>
parents:
2708
diff
changeset
|
310 if self.verbosity: |
3028 | 311 for value in ("rsm_first", "rsm_last", "rsm_index", "rsm_count", |
312 "mam_complete", "mam_stable"): | |
2719
45189c8bd165
jp (message/mam): display metadata when verbose
Goffi <goffi@goffi.org>
parents:
2708
diff
changeset
|
313 if value in metadata: |
3028 | 314 label = value.split("_")[1] |
2719
45189c8bd165
jp (message/mam): display metadata when verbose
Goffi <goffi@goffi.org>
parents:
2708
diff
changeset
|
315 self.disp(A.color( |
3028 | 316 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
|
317 |
2708 | 318 self.host.quit() |
319 | |
320 | |
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
|
321 class Message(base.CommandBase): |
3805
33ab258df0de
cli (message/retract): message retractation command:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
322 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
|
323 |
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
|
324 def __init__(self, host): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
325 super(Message, self).__init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
326 host, "message", use_profile=False, help=_("messages handling") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
327 ) |