comparison sat_frontends/jp/cmd_message.py @ 2708:0b5deb9a35fd

jp (message): new mam subcommand: this subcommand allow to retrieve message history from MAM archive. Note that the name may change in the future, it can be integrated to a "message/get" or "message/log" command.
author Goffi <goffi@goffi.org>
date Sat, 01 Dec 2018 10:48:01 +0100
parents 32b5f68a23b4
children 45189c8bd165
comparison
equal deleted inserted replaced
2707:b156b78b8f9a 2708:0b5deb9a35fd
15 # GNU Affero General Public License for more details. 15 # GNU Affero General Public License for more details.
16 16
17 # You should have received a copy of the GNU Affero General Public License 17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 import sys
20 from sat_frontends.jp import base 21 from sat_frontends.jp import base
21 from sat_frontends.jp.constants import Const as C 22 from sat_frontends.jp.constants import Const as C
22 import sys 23 from sat_frontends.tools import jid
23 from sat.core.i18n import _ 24 from sat.core.i18n import _
24 from sat.tools.utils import clean_ustr 25 from sat.tools.utils import clean_ustr
26 from sat.tools.common import data_format
25 from functools import partial 27 from functools import partial
26 28
27 __commands__ = ["Message"] 29 __commands__ = ["Message"]
28 30
29 31
149 callback=lambda: None, 151 callback=lambda: None,
150 errback=lambda ignore: ignore, 152 errback=lambda ignore: ignore,
151 ) 153 )
152 154
153 155
156 class MAM(base.CommandBase):
157
158 def __init__(self, host):
159 super(MAM, self).__init__(
160 host, "mam", use_output=C.OUTPUT_MESS, help=_(u"query archives using MAM"))
161 self.need_loop=True
162
163 def add_parser_options(self):
164 self.parser.add_argument(
165 "-s", "--service", type=base.unicode_decoder, default=u"",
166 help=_(u"jid of the service (default: profile's server"))
167 self.parser.add_argument(
168 "-S", "--start", dest="mam_start", type=base.date_decoder,
169 help=_(u"start fetching archive from this date (default: 1 day ago)"))
170 self.parser.add_argument(
171 "-E", "--end", dest="mam_end", type=base.date_decoder,
172 help=_(u"end fetching archive after this date (default: no limit)"))
173 self.parser.add_argument(
174 "-W", "--with", dest="mam_with", type=base.unicode_decoder,
175 help=_(u"retrieve only archives with this jid"))
176 self.parser.add_argument(
177 "-M", "--max", dest="rsm_max", type=int, default=20,
178 help=_(u"maximum number of items to retrieve, using RSM (default: 20))"))
179
180 def _sessionInfosGetCb(self, session_info, data):
181 self.host.own_jid = jid.JID(session_info[u"jid"])
182 self.output(data)
183 self.host.quit()
184
185 def _MAMGetCb(self, result):
186 data, profile = result
187 self.host.bridge.sessionInfosGet(self.profile,
188 callback=partial(self._sessionInfosGetCb, data=data), errback=self.errback)
189
190 def start(self):
191 if self.args.mam_start is None:
192 self.args.mam_start = base.date_decoder(u"-1 day")
193 extra = {
194 u"mam_start": float(self.args.mam_start),
195 }
196 if self.args.mam_end is not None:
197 extra[u"mam_end"] = float(self.args.mam_end)
198 if self.args.mam_with is not None:
199 extra[u"mam_with"] = self.args.mam_with
200 if self.args.rsm_max is not None:
201 extra[u"rsm_max"] = self.args.rsm_max
202 self.host.bridge.MAMGet(
203 self.args.service, data_format.serialise(extra), self.profile,
204 callback=self._MAMGetCb, errback=self.errback)
205
206
154 class EncryptionAlgorithms(base.CommandBase): 207 class EncryptionAlgorithms(base.CommandBase):
155 208
156 def __init__(self, host): 209 def __init__(self, host):
157 extra_outputs = {"default": self.default_output} 210 extra_outputs = {"default": self.default_output}
158 super(EncryptionAlgorithms, self).__init__( 211 super(EncryptionAlgorithms, self).__init__(
198 host, "encryption", use_profile=False, help=_("encryption sessions handling") 251 host, "encryption", use_profile=False, help=_("encryption sessions handling")
199 ) 252 )
200 253
201 254
202 class Message(base.CommandBase): 255 class Message(base.CommandBase):
203 subcommands = (Send, Encryption) 256 subcommands = (Send, MAM, Encryption)
204 257
205 def __init__(self, host): 258 def __init__(self, host):
206 super(Message, self).__init__( 259 super(Message, self).__init__(
207 host, "message", use_profile=False, help=_("messages handling") 260 host, "message", use_profile=False, help=_("messages handling")
208 ) 261 )