diff 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
line wrap: on
line diff
--- a/sat_frontends/jp/cmd_message.py	Sat Dec 01 10:48:01 2018 +0100
+++ b/sat_frontends/jp/cmd_message.py	Sat Dec 01 10:48:01 2018 +0100
@@ -17,11 +17,13 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+import sys
 from sat_frontends.jp import base
 from sat_frontends.jp.constants import Const as C
-import sys
+from sat_frontends.tools import jid
 from sat.core.i18n import _
 from sat.tools.utils import clean_ustr
+from sat.tools.common import data_format
 from functools import partial
 
 __commands__ = ["Message"]
@@ -151,6 +153,57 @@
             )
 
 
+class MAM(base.CommandBase):
+
+    def __init__(self, host):
+        super(MAM, self).__init__(
+            host, "mam", use_output=C.OUTPUT_MESS, help=_(u"query archives using MAM"))
+        self.need_loop=True
+
+    def add_parser_options(self):
+        self.parser.add_argument(
+            "-s", "--service", type=base.unicode_decoder, default=u"",
+            help=_(u"jid of the service (default: profile's server"))
+        self.parser.add_argument(
+            "-S", "--start", dest="mam_start", type=base.date_decoder,
+            help=_(u"start fetching archive from this date (default: 1 day ago)"))
+        self.parser.add_argument(
+            "-E", "--end", dest="mam_end", type=base.date_decoder,
+            help=_(u"end fetching archive after this date (default: no limit)"))
+        self.parser.add_argument(
+            "-W", "--with", dest="mam_with", type=base.unicode_decoder,
+            help=_(u"retrieve only archives with this jid"))
+        self.parser.add_argument(
+            "-M", "--max", dest="rsm_max", type=int, default=20,
+            help=_(u"maximum number of items to retrieve, using RSM (default: 20))"))
+
+    def _sessionInfosGetCb(self, session_info, data):
+        self.host.own_jid = jid.JID(session_info[u"jid"])
+        self.output(data)
+        self.host.quit()
+
+    def _MAMGetCb(self, result):
+        data, profile = result
+        self.host.bridge.sessionInfosGet(self.profile,
+            callback=partial(self._sessionInfosGetCb, data=data), errback=self.errback)
+
+    def start(self):
+        if self.args.mam_start is None:
+            self.args.mam_start = base.date_decoder(u"-1 day")
+        extra = {
+            u"mam_start": float(self.args.mam_start),
+        }
+        if self.args.mam_end is not None:
+            extra[u"mam_end"] = float(self.args.mam_end)
+        if self.args.mam_with is not None:
+            extra[u"mam_with"] = self.args.mam_with
+        if self.args.rsm_max is not None:
+            extra[u"rsm_max"] = self.args.rsm_max
+        self.host.bridge.MAMGet(
+            self.args.service, data_format.serialise(extra), self.profile,
+            callback=self._MAMGetCb, errback=self.errback)
+
+
 class EncryptionAlgorithms(base.CommandBase):
 
     def __init__(self, host):
@@ -200,7 +253,7 @@
 
 
 class Message(base.CommandBase):
-    subcommands = (Send, Encryption)
+    subcommands = (Send, MAM, Encryption)
 
     def __init__(self, host):
         super(Message, self).__init__(