diff libervia/cli/cmd_message.py @ 4308:472a938a46e3

cli (message/send): add arguments for message addressing: commands have been added to add `to`, `cc` and `bcc` metadata for sending copy of the message to several entities. Metadata such as `reply-to` `reply-room` and `no-reply` are also supported. rel 450
author Goffi <goffi@goffi.org>
date Thu, 26 Sep 2024 16:12:01 +0200
parents 0d7bb4df2343
children 1795bfcc38e7
line wrap: on
line diff
--- a/libervia/cli/cmd_message.py	Thu Sep 26 16:12:01 2024 +0200
+++ b/libervia/cli/cmd_message.py	Thu Sep 26 16:12:01 2024 +0200
@@ -33,6 +33,9 @@
 
 __commands__ = ["Message"]
 
+RECIPIENTS_ARGS = ["to", "cc", "bcc"]
+REPLY_ARGS = ["reply-to", "reply-room"]
+
 
 class Send(base.CommandBase):
     def __init__(self, host):
@@ -91,6 +94,35 @@
             metavar="FILE_PATH",
             help=_("add a file as an attachment"),
         )
+
+        addressing_group = self.parser.add_argument_group(
+            "addressing commands",
+            description="Commands to add addressing metadata, and/or to send message to "
+            "multiple recipients."
+        )
+        for arg_name in RECIPIENTS_ARGS:
+            addressing_group.add_argument(
+                f"--{arg_name}",
+                nargs="+",
+                action="append",
+                metavar=("JID", "DESCRIPTION"),
+                help=f'extra "{arg_name.upper()}" recipient(s), may be used several '
+                'times',
+            )
+        for arg_name in REPLY_ARGS:
+            addressing_group.add_argument(
+                f"--{arg_name}",
+                nargs="+",
+                action="append",
+                metavar=("JID", "DESCRIPTION"),
+                help=f'ask to reply to this JID, may be used several '
+                'times',
+            )
+        addressing_group.add_argument(
+            "--no-reply",
+            action="store_true",
+            help="flag this message as not requiring replies"
+        )
         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"))
@@ -117,6 +149,24 @@
             extra[key] = clean_ustr("".join(stdin_lines))
             stdin_lines = []
 
+        addresses = {}
+        for arg_name in RECIPIENTS_ARGS + [a.replace("-", "_") for a in REPLY_ARGS]:
+            values = getattr(self.args, arg_name)
+            if values:
+                for value in values:
+                    address_jid = value[0]
+                    address_desc = " ".join(value[1:]).strip()
+                    address = {"jid": address_jid}
+                    if address_desc:
+                        address["desc"] = address_desc
+                    addresses.setdefault(arg_name.replace("_", ""), []).append(address)
+
+        if self.args.no_reply:
+            addresses["noreply"] = True
+
+        if addresses:
+            extra["addresses"] = addresses
+
         to_send = []
 
         error = False