annotate sat_frontends/jp/cmd_message.py @ 3211:4252176ad993

plugin download: clean unfinished files and re-raise exception in case of download error
author Goffi <goffi@goffi.org>
date Fri, 06 Mar 2020 18:19:03 +0100
parents 84a94b385760
children 569f4cf7183b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3137
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
1 #!/usr/bin/env python3
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
2
815
f8d534ed1d1e jp: added missing license headers
Goffi <goffi@goffi.org>
parents: 814
diff changeset
3
f8d534ed1d1e jp: added missing license headers
Goffi <goffi@goffi.org>
parents: 814
diff changeset
4 # jp: a SAT command line tool
3136
9d0df638c8b4 dates update
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
5 # Copyright (C) 2009-2020 Jérôme Poisson (goffi@goffi.org)
815
f8d534ed1d1e jp: added missing license headers
Goffi <goffi@goffi.org>
parents: 814
diff changeset
6
f8d534ed1d1e jp: added missing license headers
Goffi <goffi@goffi.org>
parents: 814
diff changeset
7 # This program is free software: you can redistribute it and/or modify
f8d534ed1d1e jp: added missing license headers
Goffi <goffi@goffi.org>
parents: 814
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
f8d534ed1d1e jp: added missing license headers
Goffi <goffi@goffi.org>
parents: 814
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
f8d534ed1d1e jp: added missing license headers
Goffi <goffi@goffi.org>
parents: 814
diff changeset
10 # (at your option) any later version.
f8d534ed1d1e jp: added missing license headers
Goffi <goffi@goffi.org>
parents: 814
diff changeset
11
f8d534ed1d1e jp: added missing license headers
Goffi <goffi@goffi.org>
parents: 814
diff changeset
12 # This program is distributed in the hope that it will be useful,
f8d534ed1d1e jp: added missing license headers
Goffi <goffi@goffi.org>
parents: 814
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
f8d534ed1d1e jp: added missing license headers
Goffi <goffi@goffi.org>
parents: 814
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f8d534ed1d1e jp: added missing license headers
Goffi <goffi@goffi.org>
parents: 814
diff changeset
15 # GNU Affero General Public License for more details.
f8d534ed1d1e jp: added missing license headers
Goffi <goffi@goffi.org>
parents: 814
diff changeset
16
f8d534ed1d1e jp: added missing license headers
Goffi <goffi@goffi.org>
parents: 814
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
f8d534ed1d1e jp: added missing license headers
Goffi <goffi@goffi.org>
parents: 814
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
f8d534ed1d1e jp: added missing license headers
Goffi <goffi@goffi.org>
parents: 814
diff changeset
19
2708
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
20 import sys
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
21 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
22 from sat_frontends.jp.constants import Const as C
2708
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
23 from sat_frontends.tools import jid
771
bfabeedbf32e core: i18n refactoring:
Goffi <goffi@goffi.org>
parents: 721
diff changeset
24 from sat.core.i18n import _
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
25 from sat.tools.utils import clean_ustr
2708
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
26 from sat.tools.common import data_format
2719
45189c8bd165 jp (message/mam): display metadata when verbose
Goffi <goffi@goffi.org>
parents: 2708
diff changeset
27 from sat.tools.common.ansi import ANSI as A
0
goffi@necton2
parents:
diff changeset
28
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
29 __commands__ = ["Message"]
393
393b35aa86d2 jp: added --connect option
Goffi <goffi@goffi.org>
parents: 391
diff changeset
30
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
31
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
32 class Send(base.CommandBase):
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
33 def __init__(self, host):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
34 super(Send, self).__init__(host, "send", help=_("send a message to a contact"))
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
35
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
36 def add_parser_options(self):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
37 self.parser.add_argument(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
38 "-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
39 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
40 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
41 "-s",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
42 "--separate",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
43 action="store_true",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
44 help=_(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
45 "separate xmpp messages: send one message per line instead of one "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
46 "message alone."
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
47 ),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
48 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
49 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
50 "-n",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
51 "--new-line",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
52 action="store_true",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
53 help=_(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
54 "add a new line at the beginning of the input (usefull for ascii art ;))"
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
55 ),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
56 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
57 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
58 "-S",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
59 "--subject",
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
60 help=_("subject of the message"),
2624
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(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
63 "-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
64 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
65 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
66 "-t",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
67 "--type",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
68 choices=C.MESS_TYPE_STANDARD + (C.MESS_TYPE_AUTO,),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
69 default=C.MESS_TYPE_AUTO,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
70 help=_("type of the message"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
71 )
2740
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
72 self.parser.add_argument("-e", "--encrypt", metavar="ALGORITHM",
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
73 help=_("encrypt message using given algorithm"))
2740
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
74 self.parser.add_argument(
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
75 "--encrypt-noreplace",
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
76 action="store_true",
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
77 help=_("don't replace encryption algorithm if an other one is already used"))
2081
2265d9df4cfb jp (message/send): message/send can now send XHTML (-x) or rich (-r) messages
Goffi <goffi@goffi.org>
parents: 1963
diff changeset
78 syntax = self.parser.add_mutually_exclusive_group()
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
79 syntax.add_argument("-x", "--xhtml", action="store_true", help=_("XHTML body"))
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
80 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
81 self.parser.add_argument(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
82 "jid", help=_("the destination jid")
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
83 )
401
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents: 393
diff changeset
84
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
85 async def sendStdin(self, dest_jid):
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
86 """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
87
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
88 @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
89 """
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
90 header = "\n" if self.args.new_line else ""
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
91 # FIXME: stdin is not read asynchronously at the moment
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
92 stdin_lines = [
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
93 stream for stream in sys.stdin.readlines()
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
94 ]
2081
2265d9df4cfb jp (message/send): message/send can now send XHTML (-x) or rich (-r) messages
Goffi <goffi@goffi.org>
parents: 1963
diff changeset
95 extra = {}
1963
a2bc5089c2eb backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents: 1960
diff changeset
96 if self.args.subject is None:
a2bc5089c2eb backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents: 1960
diff changeset
97 subject = {}
a2bc5089c2eb backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents: 1960
diff changeset
98 else:
a2bc5089c2eb backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents: 1960
diff changeset
99 subject = {self.args.subject_lang: self.args.subject}
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
100
2081
2265d9df4cfb jp (message/send): message/send can now send XHTML (-x) or rich (-r) messages
Goffi <goffi@goffi.org>
parents: 1963
diff changeset
101 if self.args.xhtml or self.args.rich:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
102 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
103 if self.args.lang:
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
104 key = f"{key}_{self.args.lang}"
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
105 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
106 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
107
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
108 to_send = []
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
109
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
110 error = False
2740
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
111
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
112 if self.args.separate:
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
113 # we send stdin in several messages
0
goffi@necton2
parents:
diff changeset
114 if header:
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
115 # first we sent the header
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
116 try:
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
117 await self.host.bridge.messageSend(
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
118 dest_jid,
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
119 {self.args.lang: header},
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
120 subject,
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
121 self.args.type,
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
122 profile_key=self.profile,
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
123 )
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
124 except Exception as e:
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
125 self.disp(f"can't send header: {e}", error=True)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
126 error = True
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
127
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
128 to_send.extend({self.args.lang: clean_ustr(l.replace("\n", ""))}
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
129 for l in stdin_lines)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
130 else:
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
131 # we sent all in a single message
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
132 if not (self.args.xhtml or self.args.rich):
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
133 msg = {self.args.lang: header + clean_ustr("".join(stdin_lines))}
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
134 else:
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
135 msg = {}
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
136 to_send.append(msg)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
137
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
138 for msg in to_send:
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
139 try:
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
140 await self.host.bridge.messageSend(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
141 dest_jid,
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
142 msg,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
143 subject,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
144 self.args.type,
3179
84a94b385760 bridge: messageSend's extra is now serialised
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
145 data_format.serialise(extra),
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
146 profile_key=self.host.profile)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
147 except Exception as e:
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
148 self.disp(f"can't send message {msg!r}: {e}", error=True)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
149 error = True
0
goffi@necton2
parents:
diff changeset
150
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
151 if error:
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
152 # at least one message sending failed
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
153 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
2740
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
154
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
155 self.host.quit()
2740
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
156
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
157 async def start(self):
2740
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
158 if self.args.xhtml and self.args.separate:
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
159 self.disp(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
160 "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
161 error=True,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
162 )
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
163 self.host.quit(C.EXIT_BAD_ARG)
2740
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
164
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
165 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
166 jid_ = jids[0]
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
167
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
168 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
169 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
170
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
171 if self.args.encrypt is not None:
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
172 try:
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
173 namespace = await self.host.bridge.encryptionNamespaceGet(
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
174 self.args.encrypt)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
175 except Exception as e:
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
176 self.disp(f"can't get encryption namespace: {e}", error=True)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
177 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
178
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
179 try:
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
180 await self.host.bridge.messageEncryptionStart(
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
181 jid_, namespace, not self.args.encrypt_noreplace, self.profile
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
182 )
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
183 except Exception as e:
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
184 self.disp(f"can't start encryption session: {e}", error=True)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
185 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
186
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
187 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
188
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
189
2708
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
190 class MAM(base.CommandBase):
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
191
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
192 def __init__(self, host):
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
193 super(MAM, self).__init__(
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
194 host, "mam", use_output=C.OUTPUT_MESS, use_verbose=True,
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
195 help=_("query archives using MAM"))
2708
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
196
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
197 def add_parser_options(self):
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
198 self.parser.add_argument(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
199 "-s", "--service", default="",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
200 help=_("jid of the service (default: profile's server"))
2708
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
201 self.parser.add_argument(
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
202 "-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
203 help=_(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
204 "start fetching archive from this date (default: from the beginning)"))
2708
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
205 self.parser.add_argument(
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
206 "-E", "--end", dest="mam_end", type=base.date_decoder,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
207 help=_("end fetching archive after this date (default: no limit)"))
2708
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
208 self.parser.add_argument(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
209 "-W", "--with", dest="mam_with",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
210 help=_("retrieve only archives with this jid"))
2708
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
211 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
212 "-m", "--max", dest="rsm_max", type=int, default=20,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
213 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
214 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
215 rsm_page_group.add_argument(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
216 "-a", "--after", dest="rsm_after",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
217 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
218 rsm_page_group.add_argument(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
219 "-b", "--before", dest="rsm_before",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
220 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
221 rsm_page_group.add_argument(
fd45089b3a92 jp (message/mam): added missing RSM options
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
222 "--index", dest="rsm_index", type=int,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
223 help=_("index of the page to retrieve"))
2708
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
224
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
225 async def start(self):
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
226 extra = {}
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
227 if self.args.mam_start is not None:
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
228 extra["mam_start"] = float(self.args.mam_start)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
229 if self.args.mam_end is not None:
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
230 extra["mam_end"] = float(self.args.mam_end)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
231 if self.args.mam_with is not None:
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
232 extra["mam_with"] = self.args.mam_with
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
233 for suff in ('max', 'after', 'before', 'index'):
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
234 key = 'rsm_' + suff
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
235 value = getattr(self.args,key)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
236 if value is not None:
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
237 extra[key] = str(value)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
238 try:
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
239 data, metadata, profile = await self.host.bridge.MAMGet(
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
240 self.args.service, data_format.serialise(extra), self.profile)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
241 except Exception as e:
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
242 self.disp(f"can't retrieve MAM archives: {e}", error=True)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
243 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
244
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
245 try:
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
246 session_info = await self.host.bridge.sessionInfosGet(self.profile)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
247 except Exception as e:
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
248 self.disp(f"can't get session infos: {e}", error=True)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
249 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
250
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
251 # we need to fill own_jid for message output
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
252 self.host.own_jid = jid.JID(session_info["jid"])
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
253
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
254 await self.output(data)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
255
2719
45189c8bd165 jp (message/mam): display metadata when verbose
Goffi <goffi@goffi.org>
parents: 2708
diff changeset
256 # 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
257 # they should be added to output data somehow
45189c8bd165 jp (message/mam): display metadata when verbose
Goffi <goffi@goffi.org>
parents: 2708
diff changeset
258 if self.verbosity:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
259 for value in ("rsm_first", "rsm_last", "rsm_index", "rsm_count",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
260 "mam_complete", "mam_stable"):
2719
45189c8bd165 jp (message/mam): display metadata when verbose
Goffi <goffi@goffi.org>
parents: 2708
diff changeset
261 if value in metadata:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
262 label = value.split("_")[1]
2719
45189c8bd165 jp (message/mam): display metadata when verbose
Goffi <goffi@goffi.org>
parents: 2708
diff changeset
263 self.disp(A.color(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
264 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
265
2708
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
266 self.host.quit()
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
267
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
268
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
269 class Message(base.CommandBase):
2740
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
270 subcommands = (Send, 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
271
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
272 def __init__(self, host):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
273 super(Message, self).__init__(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
274 host, "message", use_profile=False, help=_("messages handling")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
275 )