annotate sat_frontends/jp/cmd_message.py @ 3011:93da7c6f8e0c

plugin XEP-0198: retrieve missing messages + send buffered ones on hot reconnection: Missing one2one messages are now retrieved with MAM on hot reconnection, and buffered ones (which have most probably not been received by the server) are resent at the end of the reconnection workflow. IQ results are not re-sent on hot reconnection, as they don't make sense anymore with a new session. fix 330
author Goffi <goffi@goffi.org>
date Wed, 17 Jul 2019 09:28:35 +0200
parents 0ae25883e223
children ab2696e34d29
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(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
40 "-l", "--lang", type=str, default="", help=_(u"language of the message")
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=_(
2663
32b5f68a23b4 jp (message): new encryption/algorithms command to retrieve available encryptions algorithms.
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
47 u"separate xmpp messages: send one message per line instead of one "
32b5f68a23b4 jp (message): new encryption/algorithms command to retrieve available encryptions algorithms.
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
48 u"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=_(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
56 u"add a new line at the beginning of the input (usefull for ascii art ;))"
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",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
62 type=base.unicode_decoder,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
63 help=_(u"subject of the message"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
64 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
65 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
66 "-L", "--subject_lang", type=str, default="", help=_(u"language of subject")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
67 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
68 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
69 "-t",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
70 "--type",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
71 choices=C.MESS_TYPE_STANDARD + (C.MESS_TYPE_AUTO,),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
72 default=C.MESS_TYPE_AUTO,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
73 help=_("type of the message"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
74 )
2740
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
75 self.parser.add_argument("-e", "--encrypt", metavar="ALGORITHM",
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
76 help=_(u"encrypt message using given algorithm"))
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
77 self.parser.add_argument(
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
78 "--encrypt-noreplace",
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
79 action="store_true",
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
80 help=_(u"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
81 syntax = self.parser.add_mutually_exclusive_group()
2265d9df4cfb jp (message/send): message/send can now send XHTML (-x) or rich (-r) messages
Goffi <goffi@goffi.org>
parents: 1963
diff changeset
82 syntax.add_argument("-x", "--xhtml", action="store_true", help=_(u"XHTML body"))
2265d9df4cfb jp (message/send): message/send can now send XHTML (-x) or rich (-r) messages
Goffi <goffi@goffi.org>
parents: 1963
diff changeset
83 syntax.add_argument("-r", "--rich", action="store_true", help=_(u"rich body"))
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
84 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
85 "jid", type=base.unicode_decoder, help=_(u"the destination jid")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
86 )
401
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents: 393
diff changeset
87
2740
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
88 def multi_send_cb(self):
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
89 self.sent += 1
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
90 if self.sent == self.to_send:
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
91 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
92
2740
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
93 def multi_send_eb(self, failure_, msg):
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
94 self.disp(_(u"Can't send message [{msg}]: {reason}").format(
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
95 msg=msg, reason=failure_))
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
96 self.errcode = C.EXIT_BRIDGE_ERRBACK
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
97 self.multi_send_cb()
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
98
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
99 def sendStdin(self, dest_jid):
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
100 """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
101
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 @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
103 """
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
104 header = "\n" if self.args.new_line else ""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
105 stdin_lines = [
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
106 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
107 ]
2081
2265d9df4cfb jp (message/send): message/send can now send XHTML (-x) or rich (-r) messages
Goffi <goffi@goffi.org>
parents: 1963
diff changeset
108 extra = {}
1963
a2bc5089c2eb backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents: 1960
diff changeset
109 if self.args.subject is None:
a2bc5089c2eb backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents: 1960
diff changeset
110 subject = {}
a2bc5089c2eb backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents: 1960
diff changeset
111 else:
a2bc5089c2eb backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents: 1960
diff changeset
112 subject = {self.args.subject_lang: self.args.subject}
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
113
2081
2265d9df4cfb jp (message/send): message/send can now send XHTML (-x) or rich (-r) messages
Goffi <goffi@goffi.org>
parents: 1963
diff changeset
114 if self.args.xhtml or self.args.rich:
2265d9df4cfb jp (message/send): message/send can now send XHTML (-x) or rich (-r) messages
Goffi <goffi@goffi.org>
parents: 1963
diff changeset
115 key = u"xhtml" if self.args.xhtml else u"rich"
2265d9df4cfb jp (message/send): message/send can now send XHTML (-x) or rich (-r) messages
Goffi <goffi@goffi.org>
parents: 1963
diff changeset
116 if self.args.lang:
2265d9df4cfb jp (message/send): message/send can now send XHTML (-x) or rich (-r) messages
Goffi <goffi@goffi.org>
parents: 1963
diff changeset
117 key = u"{}_{}".format(key, self.args.lang)
2265d9df4cfb jp (message/send): message/send can now send XHTML (-x) or rich (-r) messages
Goffi <goffi@goffi.org>
parents: 1963
diff changeset
118 extra[key] = clean_ustr(u"".join(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 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
120
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
121 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
122 self.to_send = 0
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
123 self.sent = 0
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
124 self.errcode = 0
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
125
0
goffi@necton2
parents:
diff changeset
126 if header:
2740
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
127 self.to_send += 1
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
128 self.host.bridge.messageSend(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
129 dest_jid,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
130 {self.args.lang: header},
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
131 subject,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
132 self.args.type,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
133 profile_key=self.profile,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
134 callback=lambda: None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
135 errback=lambda ignore: ignore,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
136 )
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
137
2740
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
138 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
139 for line in stdin_lines:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
140 self.host.bridge.messageSend(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
141 dest_jid,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
142 {self.args.lang: line.replace("\n", "")},
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
143 subject,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
144 self.args.type,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
145 extra,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
146 profile_key=self.host.profile,
2740
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
147 callback=self.multi_send_cb,
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
148 errback=partial(self.multi_send_eb, msg=line),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
149 )
0
goffi@necton2
parents:
diff changeset
150
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 815
diff changeset
151 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
152 msg = (
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
153 {self.args.lang: header + clean_ustr(u"".join(stdin_lines))}
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
154 if not (self.args.xhtml or self.args.rich)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
155 else {}
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
156 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
157 self.host.bridge.messageSend(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
158 dest_jid,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
159 msg,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
160 subject,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
161 self.args.type,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
162 extra,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
163 profile_key=self.host.profile,
2740
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
164 callback=self.host.quit,
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
165 errback=partial(self.errback,
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
166 msg=_(u"Can't send message: {}")))
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
167
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
168 def encryptionNamespaceGetCb(self, namespace, jid_):
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
169 self.host.bridge.messageEncryptionStart(
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
170 jid_, namespace, not self.args.encrypt_noreplace,
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
171 self.profile,
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
172 callback=lambda: self.sendStdin(jid_),
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
173 errback=partial(self.errback,
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
174 msg=_(u"Can't start encryption session: {}"),
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
175 exit_code=C.EXIT_BRIDGE_ERRBACK,
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
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
179 def start(self):
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
180 if self.args.xhtml and self.args.separate:
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
181 self.disp(
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
182 u"argument -s/--separate is not compatible yet with argument -x/--xhtml",
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
183 error=True,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
184 )
2740
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
185 self.host.quit(2)
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
186
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
187 jids = self.host.check_jids([self.args.jid])
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
188 jid_ = jids[0]
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
189
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
190 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
191 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
192
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
193 if self.args.encrypt is not None:
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
194 self.host.bridge.encryptionNamespaceGet(self.args.encrypt,
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
195 callback=partial(self.encryptionNamespaceGetCb, jid_=jid_),
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
196 errback=partial(self.errback,
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
197 msg=_(u"Can't get encryption namespace: {}"),
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
198 exit_code=C.EXIT_BRIDGE_ERRBACK,
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
199 ))
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
200 else:
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
201 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
202
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
203
2708
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
204 class MAM(base.CommandBase):
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
205
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
206 def __init__(self, host):
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
207 super(MAM, self).__init__(
2719
45189c8bd165 jp (message/mam): display metadata when verbose
Goffi <goffi@goffi.org>
parents: 2708
diff changeset
208 host, "mam", use_output=C.OUTPUT_MESS, use_verbose=True, help=_(u"query archives using MAM"))
2708
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
209 self.need_loop=True
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
210
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
211 def add_parser_options(self):
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
212 self.parser.add_argument(
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
213 "-s", "--service", type=base.unicode_decoder, default=u"",
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
214 help=_(u"jid of the service (default: profile's server"))
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
215 self.parser.add_argument(
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
216 "-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
217 help=_(
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
218 u"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
219 self.parser.add_argument(
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
220 "-E", "--end", dest="mam_end", type=base.date_decoder,
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
221 help=_(u"end fetching archive after this date (default: no limit)"))
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
222 self.parser.add_argument(
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
223 "-W", "--with", dest="mam_with", type=base.unicode_decoder,
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
224 help=_(u"retrieve only archives with this jid"))
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
225 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
226 "-m", "--max", dest="rsm_max", type=int, default=20,
2708
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
227 help=_(u"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
228 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
229 rsm_page_group.add_argument(
fd45089b3a92 jp (message/mam): added missing RSM options
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
230 "-a", "--after", dest="rsm_after", type=base.unicode_decoder,
fd45089b3a92 jp (message/mam): added missing RSM options
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
231 help=_(u"find page after this item"), metavar='ITEM_ID')
fd45089b3a92 jp (message/mam): added missing RSM options
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
232 rsm_page_group.add_argument(
fd45089b3a92 jp (message/mam): added missing RSM options
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
233 "-b", "--before", dest="rsm_before", type=base.unicode_decoder,
fd45089b3a92 jp (message/mam): added missing RSM options
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
234 help=_(u"find page before this item"), metavar='ITEM_ID')
fd45089b3a92 jp (message/mam): added missing RSM options
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
235 rsm_page_group.add_argument(
fd45089b3a92 jp (message/mam): added missing RSM options
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
236 "--index", dest="rsm_index", type=int,
fd45089b3a92 jp (message/mam): added missing RSM options
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
237 help=_(u"index of the page to retrieve"))
2708
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
238
2719
45189c8bd165 jp (message/mam): display metadata when verbose
Goffi <goffi@goffi.org>
parents: 2708
diff changeset
239 def _sessionInfosGetCb(self, session_info, data, metadata):
2708
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
240 self.host.own_jid = jid.JID(session_info[u"jid"])
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
241 self.output(data)
2719
45189c8bd165 jp (message/mam): display metadata when verbose
Goffi <goffi@goffi.org>
parents: 2708
diff changeset
242 # 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
243 # they should be added to output data somehow
45189c8bd165 jp (message/mam): display metadata when verbose
Goffi <goffi@goffi.org>
parents: 2708
diff changeset
244 if self.verbosity:
45189c8bd165 jp (message/mam): display metadata when verbose
Goffi <goffi@goffi.org>
parents: 2708
diff changeset
245 for value in (u"rsm_first", u"rsm_last", u"rsm_index", u"rsm_count",
45189c8bd165 jp (message/mam): display metadata when verbose
Goffi <goffi@goffi.org>
parents: 2708
diff changeset
246 u"mam_complete", u"mam_stable"):
45189c8bd165 jp (message/mam): display metadata when verbose
Goffi <goffi@goffi.org>
parents: 2708
diff changeset
247 if value in metadata:
45189c8bd165 jp (message/mam): display metadata when verbose
Goffi <goffi@goffi.org>
parents: 2708
diff changeset
248 label = value.split(u"_")[1]
45189c8bd165 jp (message/mam): display metadata when verbose
Goffi <goffi@goffi.org>
parents: 2708
diff changeset
249 self.disp(A.color(
45189c8bd165 jp (message/mam): display metadata when verbose
Goffi <goffi@goffi.org>
parents: 2708
diff changeset
250 C.A_HEADER, label, u': ' , A.RESET, metadata[value]))
45189c8bd165 jp (message/mam): display metadata when verbose
Goffi <goffi@goffi.org>
parents: 2708
diff changeset
251
2708
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
252 self.host.quit()
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
253
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
254 def _MAMGetCb(self, result):
2719
45189c8bd165 jp (message/mam): display metadata when verbose
Goffi <goffi@goffi.org>
parents: 2708
diff changeset
255 data, metadata, profile = result
2708
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
256 self.host.bridge.sessionInfosGet(self.profile,
2719
45189c8bd165 jp (message/mam): display metadata when verbose
Goffi <goffi@goffi.org>
parents: 2708
diff changeset
257 callback=partial(self._sessionInfosGetCb, data=data, metadata=metadata),
45189c8bd165 jp (message/mam): display metadata when verbose
Goffi <goffi@goffi.org>
parents: 2708
diff changeset
258 errback=self.errback)
2708
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
259
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
260 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
261 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
262 if self.args.mam_start is not None:
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
263 extra[u"mam_start"] = float(self.args.mam_start)
2708
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
264 if self.args.mam_end is not None:
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
265 extra[u"mam_end"] = float(self.args.mam_end)
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
266 if self.args.mam_with is not None:
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
267 extra[u"mam_with"] = self.args.mam_with
2819
fd45089b3a92 jp (message/mam): added missing RSM options
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
268 for suff in ('max', 'after', 'before', 'index'):
fd45089b3a92 jp (message/mam): added missing RSM options
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
269 key = u'rsm_' + suff
fd45089b3a92 jp (message/mam): added missing RSM options
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
270 value = getattr(self.args,key)
fd45089b3a92 jp (message/mam): added missing RSM options
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
271 if value is not None:
fd45089b3a92 jp (message/mam): added missing RSM options
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
272 extra[key] = unicode(value)
2708
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
273 self.host.bridge.MAMGet(
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
274 self.args.service, data_format.serialise(extra), self.profile,
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
275 callback=self._MAMGetCb, errback=self.errback)
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
276
0b5deb9a35fd jp (message): new mam subcommand:
Goffi <goffi@goffi.org>
parents: 2663
diff changeset
277
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
278 class Message(base.CommandBase):
2740
8fd8ce5a5855 jp (message/send, encryption): encryption handling:
Goffi <goffi@goffi.org>
parents: 2719
diff changeset
279 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
280
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
281 def __init__(self, host):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
282 super(Message, self).__init__(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
283 host, "message", use_profile=False, help=_("messages handling")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
284 )