changeset 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 e4631d073c8b
children 1877c5c477ec
files sat_frontends/jp/cmd_message.py
diffstat 1 files changed, 29 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- 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 <http://www.gnu.org/licenses/>.
 
+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,