# HG changeset patch # User Goffi # Date 1665064925 -7200 # Node ID 900bf04d87c8832309b8fbd3c4962b76c08196df # Parent e4631d073c8bbac9e38dde040dc0d25f2a18ab29 cli (message/send): new `--attach` argument to send an attachment: rel 379 diff -r e4631d073c8b -r 900bf04d87c8 sat_frontends/jp/cmd_message.py --- a/sat_frontends/jp/cmd_message.py Thu Oct 06 16:02:05 2022 +0200 +++ b/sat_frontends/jp/cmd_message.py Thu Oct 06 16:02:05 2022 +0200 @@ -17,14 +17,19 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +from pathlib import Path import sys + +from twisted.python import filepath + +from sat.core.i18n import _ +from sat.tools.common import data_format +from sat.tools.common.ansi import ANSI as A +from sat.tools.utils import clean_ustr from sat_frontends.jp import base from sat_frontends.jp.constants import Const as C from sat_frontends.tools import jid -from sat.core.i18n import _ -from sat.tools.utils import clean_ustr -from sat.tools.common import data_format -from sat.tools.common.ansi import ANSI as A + __commands__ = ["Message"] @@ -51,7 +56,7 @@ "--new-line", action="store_true", help=_( - "add a new line at the beginning of the input (usefull for ascii art ;))" + "add a new line at the beginning of the input" ), ) self.parser.add_argument( @@ -75,6 +80,10 @@ "--encrypt-noreplace", action="store_true", help=_("don't replace encryption algorithm if an other one is already used")) + self.parser.add_argument( + "-a", "--attach", dest="attachments", action="append", metavar="FILE_PATH", + help=_("add a file as an attachment") + ) syntax = self.parser.add_mutually_exclusive_group() syntax.add_argument("-x", "--xhtml", action="store_true", help=_("XHTML body")) syntax.add_argument("-r", "--rich", action="store_true", help=_("rich body")) @@ -135,7 +144,21 @@ msg = {} to_send.append(msg) - for msg in to_send: + if self.args.attachments: + attachments = extra[C.MESS_KEY_ATTACHMENTS] = [] + for attachment in self.args.attachments: + try: + file_path = str(Path(attachment).resolve(strict=True)) + except FileNotFoundError: + self.disp("file {attachment} doesn't exists, ignoring", error=True) + else: + attachments.append({"path": file_path}) + + for idx, msg in enumerate(to_send): + if idx > 0 and C.MESS_KEY_ATTACHMENTS in extra: + # if we send several messages, we only want to send attachments with the + # first one + del extra[C.MESS_KEY_ATTACHMENTS] try: await self.host.bridge.messageSend( dest_jid,