annotate sat_frontends/jp/cmd_message.py @ 3035:25af53e2990a

install (setup.py): updated `python_requires`
author Goffi <goffi@goffi.org>
date Sun, 25 Aug 2019 09:53:03 +0200
parents ab2696e34d29
children fee60f17ebac
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1960
3e168cde7a7d jp: fixed shebang python call
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
1 #!/usr/bin/env python2
815
f8d534ed1d1e jp: added missing license headers
Goffi <goffi@goffi.org>
parents: 814
diff changeset
2 # -*- coding: utf-8 -*-
f8d534ed1d1e jp: added missing license headers
Goffi <goffi@goffi.org>
parents: 814
diff changeset
3
f8d534ed1d1e jp: added missing license headers
Goffi <goffi@goffi.org>
parents: 814
diff changeset
4 # jp: a SAT command line tool
2771
003b8b4b56a7 date update
Goffi <goffi@goffi.org>
parents: 2764
diff changeset
5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org)
815
f8d534ed1d1e jp: added missing license headers
Goffi <goffi@goffi.org>
parents: 814
diff changeset
6
f8d534ed1d1e jp: added missing license headers
Goffi <goffi@goffi.org>
parents: 814
diff changeset
7 # This program is free software: you can redistribute it and/or modify
f8d534ed1d1e jp: added missing license headers
Goffi <goffi@goffi.org>
parents: 814
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
f8d534ed1d1e jp: added missing license headers
Goffi <goffi@goffi.org>
parents: 814
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
f8d534ed1d1e jp: added missing license headers
Goffi <goffi@goffi.org>
parents: 814
diff changeset
10 # (at your option) any later version.
f8d534ed1d1e jp: added missing license headers
Goffi <goffi@goffi.org>
parents: 814
diff changeset
11
f8d534ed1d1e jp: added missing license headers
Goffi <goffi@goffi.org>
parents: 814
diff changeset
12 # This program is distributed in the hope that it will be useful,
f8d534ed1d1e jp: added missing license headers
Goffi <goffi@goffi.org>
parents: 814
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
f8d534ed1d1e jp: added missing license headers
Goffi <goffi@goffi.org>
parents: 814
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f8d534ed1d1e jp: added missing license headers
Goffi <goffi@goffi.org>
parents: 814
diff changeset
15 # GNU Affero General Public License for more details.
f8d534ed1d1e jp: added missing license headers
Goffi <goffi@goffi.org>
parents: 814
diff changeset
16
f8d534ed1d1e jp: added missing license headers
Goffi <goffi@goffi.org>
parents: 814
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
f8d534ed1d1e jp: added missing license headers
Goffi <goffi@goffi.org>
parents: 814
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
f8d534ed1d1e jp: added missing license headers
Goffi <goffi@goffi.org>
parents: 814
diff changeset
19
2708
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
20 import sys
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
21 from sat_frontends.jp import base
2663
32b5f68a23b4 jp (message): new encryption/algorithms command to retrieve available encryptions algorithms.
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
22 from sat_frontends.jp.constants import Const as C
2708
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
23 from sat_frontends.tools import jid
771
bfabeedbf32e core: i18n refactoring:
Goffi <goffi@goffi.org>
parents: 721
diff changeset
24 from sat.core.i18n import _
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
25 from sat.tools.utils import clean_ustr
2708
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
26 from sat.tools.common import data_format
2719
45189c8bd165 jp (message/mam): display metadata when verbose
Goffi <goffi@goffi.org>
parents: 2708
diff changeset
27 from sat.tools.common.ansi import ANSI as A
2663
32b5f68a23b4 jp (message): new encryption/algorithms command to retrieve available encryptions algorithms.
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
28 from functools import partial
0
goffi@necton2
parents:
diff changeset
29
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
30 __commands__ = ["Message"]
393
393b35aa86d2 jp: added --connect option
Goffi <goffi@goffi.org>
parents: 391
diff changeset
31
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
32
1828
cb4be663a4a7 jp (message): message is now a subcommand, and the sending command is moved to message/send
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
33 class Send(base.CommandBase):
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
34 def __init__(self, host):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
35 super(Send, self).__init__(host, "send", help=_("send a message to a contact"))
2740
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
36 self.need_loop=True
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
37
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
38 def add_parser_options(self):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
39 self.parser.add_argument(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
40 "-l", "--lang", type=str, default="", help=_("language of the message")
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
41 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
42 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
43 "-s",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
44 "--separate",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
45 action="store_true",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
46 help=_(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
47 "separate xmpp messages: send one message per line instead of one "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
48 "message alone."
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
49 ),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
50 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
51 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
52 "-n",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
53 "--new-line",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
54 action="store_true",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
55 help=_(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
56 "add a new line at the beginning of the input (usefull for ascii art ;))"
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
57 ),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
58 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
59 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
60 "-S",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
61 "--subject",
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
62 help=_("subject of the message"),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
63 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
64 self.parser.add_argument(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
65 "-L", "--subject_lang", type=str, default="", help=_("language of subject")
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
66 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
67 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
68 "-t",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
69 "--type",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
70 choices=C.MESS_TYPE_STANDARD + (C.MESS_TYPE_AUTO,),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
71 default=C.MESS_TYPE_AUTO,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
72 help=_("type of the message"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
73 )
2740
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
74 self.parser.add_argument("-e", "--encrypt", metavar="ALGORITHM",
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
75 help=_("encrypt message using given algorithm"))
2740
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
76 self.parser.add_argument(
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
77 "--encrypt-noreplace",
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
78 action="store_true",
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
79 help=_("don't replace encryption algorithm if an other one is already used"))
2081
2265d9df4cfb jp (message/send): message/send can now send XHTML (-x) or rich (-r) messages
Goffi <goffi@goffi.org>
parents: 1963
diff changeset
80 syntax = self.parser.add_mutually_exclusive_group()
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
81 syntax.add_argument("-x", "--xhtml", action="store_true", help=_("XHTML body"))
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
82 syntax.add_argument("-r", "--rich", action="store_true", help=_("rich body"))
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
83 self.parser.add_argument(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
84 "jid", help=_("the destination jid")
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
85 )
401
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents: 393
diff changeset
86
2740
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
87 def multi_send_cb(self):
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
88 self.sent += 1
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
89 if self.sent == self.to_send:
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
90 self.host.quit(self.errcode)
2081
2265d9df4cfb jp (message/send): message/send can now send XHTML (-x) or rich (-r) messages
Goffi <goffi@goffi.org>
parents: 1963
diff changeset
91
2740
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
92 def multi_send_eb(self, failure_, msg):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
93 self.disp(_("Can't send message [{msg}]: {reason}").format(
2740
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
94 msg=msg, reason=failure_))
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
95 self.errcode = C.EXIT_BRIDGE_ERRBACK
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
96 self.multi_send_cb()
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
97
1828
cb4be663a4a7 jp (message): message is now a subcommand, and the sending command is moved to message/send
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
98 def sendStdin(self, dest_jid):
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
99 """Send incomming data on stdin to jabber contact
1828
cb4be663a4a7 jp (message): message is now a subcommand, and the sending command is moved to message/send
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
100
cb4be663a4a7 jp (message): message is now a subcommand, and the sending command is moved to message/send
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
101 @param dest_jid: destination jid
cb4be663a4a7 jp (message): message is now a subcommand, and the sending command is moved to message/send
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
102 """
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
103 header = "\n" if self.args.new_line else ""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
104 stdin_lines = [
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
105 stream.decode("utf-8", "ignore") for stream in sys.stdin.readlines()
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
106 ]
2081
2265d9df4cfb jp (message/send): message/send can now send XHTML (-x) or rich (-r) messages
Goffi <goffi@goffi.org>
parents: 1963
diff changeset
107 extra = {}
1963
a2bc5089c2eb backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents: 1960
diff changeset
108 if self.args.subject is None:
a2bc5089c2eb backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents: 1960
diff changeset
109 subject = {}
a2bc5089c2eb backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents: 1960
diff changeset
110 else:
a2bc5089c2eb backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents: 1960
diff changeset
111 subject = {self.args.subject_lang: self.args.subject}
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
112
2081
2265d9df4cfb jp (message/send): message/send can now send XHTML (-x) or rich (-r) messages
Goffi <goffi@goffi.org>
parents: 1963
diff changeset
113 if self.args.xhtml or self.args.rich:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
114 key = "xhtml" if self.args.xhtml else "rich"
2081
2265d9df4cfb jp (message/send): message/send can now send XHTML (-x) or rich (-r) messages
Goffi <goffi@goffi.org>
parents: 1963
diff changeset
115 if self.args.lang:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
116 key = "{}_{}".format(key, self.args.lang)
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
117 extra[key] = clean_ustr("".join(stdin_lines))
2081
2265d9df4cfb jp (message/send): message/send can now send XHTML (-x) or rich (-r) messages
Goffi <goffi@goffi.org>
parents: 1963
diff changeset
118 stdin_lines = []
2265d9df4cfb jp (message/send): message/send can now send XHTML (-x) or rich (-r) messages
Goffi <goffi@goffi.org>
parents: 1963
diff changeset
119
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
120 if self.args.separate: # we send stdin in several messages
2740
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
121 self.to_send = 0
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
122 self.sent = 0
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
123 self.errcode = 0
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
124
0
goffi@necton2
parents:
diff changeset
125 if header:
2740
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
126 self.to_send += 1
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
127 self.host.bridge.messageSend(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
128 dest_jid,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
129 {self.args.lang: header},
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
130 subject,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
131 self.args.type,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
132 profile_key=self.profile,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
133 callback=lambda: None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
134 errback=lambda ignore: ignore,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
135 )
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
136
2740
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
137 self.to_send += len(stdin_lines)
2081
2265d9df4cfb jp (message/send): message/send can now send XHTML (-x) or rich (-r) messages
Goffi <goffi@goffi.org>
parents: 1963
diff changeset
138 for line in stdin_lines:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
139 self.host.bridge.messageSend(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
140 dest_jid,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
141 {self.args.lang: line.replace("\n", "")},
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
142 subject,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
143 self.args.type,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
144 extra,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
145 profile_key=self.host.profile,
2740
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
146 callback=self.multi_send_cb,
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
147 errback=partial(self.multi_send_eb, msg=line),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
148 )
0
goffi@necton2
parents:
diff changeset
149
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
150 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
151 msg = (
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
152 {self.args.lang: header + clean_ustr("".join(stdin_lines))}
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
153 if not (self.args.xhtml or self.args.rich)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
154 else {}
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
155 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
156 self.host.bridge.messageSend(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
157 dest_jid,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
158 msg,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
159 subject,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
160 self.args.type,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
161 extra,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
162 profile_key=self.host.profile,
2740
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
163 callback=self.host.quit,
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
164 errback=partial(self.errback,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
165 msg=_("Can't send message: {}")))
2740
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
166
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
167 def encryptionNamespaceGetCb(self, namespace, jid_):
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
168 self.host.bridge.messageEncryptionStart(
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
169 jid_, namespace, not self.args.encrypt_noreplace,
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
170 self.profile,
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
171 callback=lambda: self.sendStdin(jid_),
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
172 errback=partial(self.errback,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
173 msg=_("Can't start encryption session: {}"),
2740
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
174 exit_code=C.EXIT_BRIDGE_ERRBACK,
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
175 ))
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
176
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
177
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
178 def start(self):
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
179 if self.args.xhtml and self.args.separate:
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
180 self.disp(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
181 "argument -s/--separate is not compatible yet with argument -x/--xhtml",
2740
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
182 error=True,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
183 )
2740
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
184 self.host.quit(2)
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
185
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
186 jids = self.host.check_jids([self.args.jid])
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
187 jid_ = jids[0]
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
188
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
189 if self.args.encrypt_noreplace and self.args.encrypt is None:
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
190 self.parser.error("You need to use --encrypt if you use --encrypt-noreplace")
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
191
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
192 if self.args.encrypt is not None:
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
193 self.host.bridge.encryptionNamespaceGet(self.args.encrypt,
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
194 callback=partial(self.encryptionNamespaceGetCb, jid_=jid_),
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
195 errback=partial(self.errback,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
196 msg=_("Can't get encryption namespace: {}"),
2740
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
197 exit_code=C.EXIT_BRIDGE_ERRBACK,
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
198 ))
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
199 else:
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
200 self.sendStdin(jid_)
1828
cb4be663a4a7 jp (message): message is now a subcommand, and the sending command is moved to message/send
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
201
cb4be663a4a7 jp (message): message is now a subcommand, and the sending command is moved to message/send
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
202
2708
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
203 class MAM(base.CommandBase):
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
204
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
205 def __init__(self, host):
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
206 super(MAM, self).__init__(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
207 host, "mam", use_output=C.OUTPUT_MESS, use_verbose=True, help=_("query archives using MAM"))
2708
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
208 self.need_loop=True
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
209
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
210 def add_parser_options(self):
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
211 self.parser.add_argument(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
212 "-s", "--service", default="",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
213 help=_("jid of the service (default: profile's server"))
2708
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
214 self.parser.add_argument(
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
215 "-S", "--start", dest="mam_start", type=base.date_decoder,
2824
0ae25883e223 jp (message/mam): --start doesn't defaut anymore to "1 day ago", it is now unset by default
Goffi <goffi@goffi.org>
parents: 2819
diff changeset
216 help=_(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
217 "start fetching archive from this date (default: from the beginning)"))
2708
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
218 self.parser.add_argument(
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
219 "-E", "--end", dest="mam_end", type=base.date_decoder,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
220 help=_("end fetching archive after this date (default: no limit)"))
2708
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
221 self.parser.add_argument(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
222 "-W", "--with", dest="mam_with",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
223 help=_("retrieve only archives with this jid"))
2708
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
224 self.parser.add_argument(
2764
92af49cde255 jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents: 2740
diff changeset
225 "-m", "--max", dest="rsm_max", type=int, default=20,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
226 help=_("maximum number of items to retrieve, using RSM (default: 20))"))
2819
fd45089b3a92 jp (message/mam): added missing RSM options
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
227 rsm_page_group = self.parser.add_mutually_exclusive_group()
fd45089b3a92 jp (message/mam): added missing RSM options
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
228 rsm_page_group.add_argument(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
229 "-a", "--after", dest="rsm_after",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
230 help=_("find page after this item"), metavar='ITEM_ID')
2819
fd45089b3a92 jp (message/mam): added missing RSM options
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
231 rsm_page_group.add_argument(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
232 "-b", "--before", dest="rsm_before",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
233 help=_("find page before this item"), metavar='ITEM_ID')
2819
fd45089b3a92 jp (message/mam): added missing RSM options
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
234 rsm_page_group.add_argument(
fd45089b3a92 jp (message/mam): added missing RSM options
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
235 "--index", dest="rsm_index", type=int,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
236 help=_("index of the page to retrieve"))
2708
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
237
2719
45189c8bd165 jp (message/mam): display metadata when verbose
Goffi <goffi@goffi.org>
parents: 2708
diff changeset
238 def _sessionInfosGetCb(self, session_info, data, metadata):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
239 self.host.own_jid = jid.JID(session_info["jid"])
2708
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
240 self.output(data)
2719
45189c8bd165 jp (message/mam): display metadata when verbose
Goffi <goffi@goffi.org>
parents: 2708
diff changeset
241 # FIXME: metadata are not displayed correctly and don't play nice with output
45189c8bd165 jp (message/mam): display metadata when verbose
Goffi <goffi@goffi.org>
parents: 2708
diff changeset
242 # they should be added to output data somehow
45189c8bd165 jp (message/mam): display metadata when verbose
Goffi <goffi@goffi.org>
parents: 2708
diff changeset
243 if self.verbosity:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
244 for value in ("rsm_first", "rsm_last", "rsm_index", "rsm_count",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
245 "mam_complete", "mam_stable"):
2719
45189c8bd165 jp (message/mam): display metadata when verbose
Goffi <goffi@goffi.org>
parents: 2708
diff changeset
246 if value in metadata:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
247 label = value.split("_")[1]
2719
45189c8bd165 jp (message/mam): display metadata when verbose
Goffi <goffi@goffi.org>
parents: 2708
diff changeset
248 self.disp(A.color(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
249 C.A_HEADER, label, ': ' , A.RESET, metadata[value]))
2719
45189c8bd165 jp (message/mam): display metadata when verbose
Goffi <goffi@goffi.org>
parents: 2708
diff changeset
250
2708
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
251 self.host.quit()
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
252
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
253 def _MAMGetCb(self, result):
2719
45189c8bd165 jp (message/mam): display metadata when verbose
Goffi <goffi@goffi.org>
parents: 2708
diff changeset
254 data, metadata, profile = result
2708
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
255 self.host.bridge.sessionInfosGet(self.profile,
2719
45189c8bd165 jp (message/mam): display metadata when verbose
Goffi <goffi@goffi.org>
parents: 2708
diff changeset
256 callback=partial(self._sessionInfosGetCb, data=data, metadata=metadata),
45189c8bd165 jp (message/mam): display metadata when verbose
Goffi <goffi@goffi.org>
parents: 2708
diff changeset
257 errback=self.errback)
2708
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
258
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
259 def start(self):
2824
0ae25883e223 jp (message/mam): --start doesn't defaut anymore to "1 day ago", it is now unset by default
Goffi <goffi@goffi.org>
parents: 2819
diff changeset
260 extra = {}
0ae25883e223 jp (message/mam): --start doesn't defaut anymore to "1 day ago", it is now unset by default
Goffi <goffi@goffi.org>
parents: 2819
diff changeset
261 if self.args.mam_start is not None:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
262 extra["mam_start"] = float(self.args.mam_start)
2708
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
263 if self.args.mam_end is not None:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
264 extra["mam_end"] = float(self.args.mam_end)
2708
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
265 if self.args.mam_with is not None:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
266 extra["mam_with"] = self.args.mam_with
2819
fd45089b3a92 jp (message/mam): added missing RSM options
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
267 for suff in ('max', 'after', 'before', 'index'):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
268 key = 'rsm_' + suff
2819
fd45089b3a92 jp (message/mam): added missing RSM options
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
269 value = getattr(self.args,key)
fd45089b3a92 jp (message/mam): added missing RSM options
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
270 if value is not None:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2824
diff changeset
271 extra[key] = str(value)
2708
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
272 self.host.bridge.MAMGet(
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
273 self.args.service, data_format.serialise(extra), self.profile,
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
274 callback=self._MAMGetCb, errback=self.errback)
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
275
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
276
1828
cb4be663a4a7 jp (message): message is now a subcommand, and the sending command is moved to message/send
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
277 class Message(base.CommandBase):
2740
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
278 subcommands = (Send, MAM)
1828
cb4be663a4a7 jp (message): message is now a subcommand, and the sending command is moved to message/send
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
279
cb4be663a4a7 jp (message): message is now a subcommand, and the sending command is moved to message/send
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
280 def __init__(self, host):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
281 super(Message, self).__init__(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
282 host, "message", use_profile=False, help=_("messages handling")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
283 )