comparison 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
comparison
equal deleted inserted replaced
4307:6a0155f410bd 4308:472a938a46e3
31 from libervia.frontends.tools import jid 31 from libervia.frontends.tools import jid
32 32
33 33
34 __commands__ = ["Message"] 34 __commands__ = ["Message"]
35 35
36 RECIPIENTS_ARGS = ["to", "cc", "bcc"]
37 REPLY_ARGS = ["reply-to", "reply-room"]
38
36 39
37 class Send(base.CommandBase): 40 class Send(base.CommandBase):
38 def __init__(self, host): 41 def __init__(self, host):
39 super(Send, self).__init__(host, "send", help=_("send a message to a contact")) 42 super(Send, self).__init__(host, "send", help=_("send a message to a contact"))
40 43
88 "--attach", 91 "--attach",
89 dest="attachments", 92 dest="attachments",
90 action="append", 93 action="append",
91 metavar="FILE_PATH", 94 metavar="FILE_PATH",
92 help=_("add a file as an attachment"), 95 help=_("add a file as an attachment"),
96 )
97
98 addressing_group = self.parser.add_argument_group(
99 "addressing commands",
100 description="Commands to add addressing metadata, and/or to send message to "
101 "multiple recipients."
102 )
103 for arg_name in RECIPIENTS_ARGS:
104 addressing_group.add_argument(
105 f"--{arg_name}",
106 nargs="+",
107 action="append",
108 metavar=("JID", "DESCRIPTION"),
109 help=f'extra "{arg_name.upper()}" recipient(s), may be used several '
110 'times',
111 )
112 for arg_name in REPLY_ARGS:
113 addressing_group.add_argument(
114 f"--{arg_name}",
115 nargs="+",
116 action="append",
117 metavar=("JID", "DESCRIPTION"),
118 help=f'ask to reply to this JID, may be used several '
119 'times',
120 )
121 addressing_group.add_argument(
122 "--no-reply",
123 action="store_true",
124 help="flag this message as not requiring replies"
93 ) 125 )
94 syntax = self.parser.add_mutually_exclusive_group() 126 syntax = self.parser.add_mutually_exclusive_group()
95 syntax.add_argument("-x", "--xhtml", action="store_true", help=_("XHTML body")) 127 syntax.add_argument("-x", "--xhtml", action="store_true", help=_("XHTML body"))
96 syntax.add_argument("-r", "--rich", action="store_true", help=_("rich body")) 128 syntax.add_argument("-r", "--rich", action="store_true", help=_("rich body"))
97 self.parser.add_argument("jid", help=_("the destination jid")) 129 self.parser.add_argument("jid", help=_("the destination jid"))
114 key = "xhtml" if self.args.xhtml else "rich" 146 key = "xhtml" if self.args.xhtml else "rich"
115 if self.args.lang: 147 if self.args.lang:
116 key = f"{key}_{self.args.lang}" 148 key = f"{key}_{self.args.lang}"
117 extra[key] = clean_ustr("".join(stdin_lines)) 149 extra[key] = clean_ustr("".join(stdin_lines))
118 stdin_lines = [] 150 stdin_lines = []
151
152 addresses = {}
153 for arg_name in RECIPIENTS_ARGS + [a.replace("-", "_") for a in REPLY_ARGS]:
154 values = getattr(self.args, arg_name)
155 if values:
156 for value in values:
157 address_jid = value[0]
158 address_desc = " ".join(value[1:]).strip()
159 address = {"jid": address_jid}
160 if address_desc:
161 address["desc"] = address_desc
162 addresses.setdefault(arg_name.replace("_", ""), []).append(address)
163
164 if self.args.no_reply:
165 addresses["noreply"] = True
166
167 if addresses:
168 extra["addresses"] = addresses
119 169
120 to_send = [] 170 to_send = []
121 171
122 error = False 172 error = False
123 173