diff --git a/sat_frontends/jp/cmd_message.py b/sat_frontends/jp/cmd_message.py index 08133a04..bf6267af 100644 --- a/sat_frontends/jp/cmd_message.py +++ b/sat_frontends/jp/cmd_message.py @@ -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 @@ class Send(base.CommandBase): "--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 @@ class Send(base.CommandBase): "--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 @@ class Send(base.CommandBase): 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,