comparison sat_frontends/jp/cmd_message.py @ 3925:900bf04d87c8

cli (message/send): new `--attach` argument to send an attachment: rel 379
author Goffi <goffi@goffi.org>
date Thu, 06 Oct 2022 16:02:05 +0200
parents 6090141b1b70
children 78b5f356900c
comparison
equal deleted inserted replaced
3924:e4631d073c8b 3925:900bf04d87c8
15 # GNU Affero General Public License for more details. 15 # GNU Affero General Public License for more details.
16 16
17 # You should have received a copy of the GNU Affero General Public License 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/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 from pathlib import Path
20 import sys 21 import sys
22
23 from twisted.python import filepath
24
25 from sat.core.i18n import _
26 from sat.tools.common import data_format
27 from sat.tools.common.ansi import ANSI as A
28 from sat.tools.utils import clean_ustr
21 from sat_frontends.jp import base 29 from sat_frontends.jp import base
22 from sat_frontends.jp.constants import Const as C 30 from sat_frontends.jp.constants import Const as C
23 from sat_frontends.tools import jid 31 from sat_frontends.tools import jid
24 from sat.core.i18n import _ 32
25 from sat.tools.utils import clean_ustr
26 from sat.tools.common import data_format
27 from sat.tools.common.ansi import ANSI as A
28 33
29 __commands__ = ["Message"] 34 __commands__ = ["Message"]
30 35
31 36
32 class Send(base.CommandBase): 37 class Send(base.CommandBase):
49 self.parser.add_argument( 54 self.parser.add_argument(
50 "-n", 55 "-n",
51 "--new-line", 56 "--new-line",
52 action="store_true", 57 action="store_true",
53 help=_( 58 help=_(
54 "add a new line at the beginning of the input (usefull for ascii art ;))" 59 "add a new line at the beginning of the input"
55 ), 60 ),
56 ) 61 )
57 self.parser.add_argument( 62 self.parser.add_argument(
58 "-S", 63 "-S",
59 "--subject", 64 "--subject",
73 help=_("encrypt message using given algorithm")) 78 help=_("encrypt message using given algorithm"))
74 self.parser.add_argument( 79 self.parser.add_argument(
75 "--encrypt-noreplace", 80 "--encrypt-noreplace",
76 action="store_true", 81 action="store_true",
77 help=_("don't replace encryption algorithm if an other one is already used")) 82 help=_("don't replace encryption algorithm if an other one is already used"))
83 self.parser.add_argument(
84 "-a", "--attach", dest="attachments", action="append", metavar="FILE_PATH",
85 help=_("add a file as an attachment")
86 )
78 syntax = self.parser.add_mutually_exclusive_group() 87 syntax = self.parser.add_mutually_exclusive_group()
79 syntax.add_argument("-x", "--xhtml", action="store_true", help=_("XHTML body")) 88 syntax.add_argument("-x", "--xhtml", action="store_true", help=_("XHTML body"))
80 syntax.add_argument("-r", "--rich", action="store_true", help=_("rich body")) 89 syntax.add_argument("-r", "--rich", action="store_true", help=_("rich body"))
81 self.parser.add_argument( 90 self.parser.add_argument(
82 "jid", help=_("the destination jid") 91 "jid", help=_("the destination jid")
133 msg = {self.args.lang: header + clean_ustr("".join(stdin_lines))} 142 msg = {self.args.lang: header + clean_ustr("".join(stdin_lines))}
134 else: 143 else:
135 msg = {} 144 msg = {}
136 to_send.append(msg) 145 to_send.append(msg)
137 146
138 for msg in to_send: 147 if self.args.attachments:
148 attachments = extra[C.MESS_KEY_ATTACHMENTS] = []
149 for attachment in self.args.attachments:
150 try:
151 file_path = str(Path(attachment).resolve(strict=True))
152 except FileNotFoundError:
153 self.disp("file {attachment} doesn't exists, ignoring", error=True)
154 else:
155 attachments.append({"path": file_path})
156
157 for idx, msg in enumerate(to_send):
158 if idx > 0 and C.MESS_KEY_ATTACHMENTS in extra:
159 # if we send several messages, we only want to send attachments with the
160 # first one
161 del extra[C.MESS_KEY_ATTACHMENTS]
139 try: 162 try:
140 await self.host.bridge.messageSend( 163 await self.host.bridge.messageSend(
141 dest_jid, 164 dest_jid,
142 msg, 165 msg,
143 subject, 166 subject,