summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMadhur Garg <madhurgarg96@gmail.com>2019-06-23 05:23:18 +0530
committerMadhur Garg <madhurgarg96@gmail.com>2019-08-22 00:54:25 +0530
commitd909adcb5cdb18a85b7042794c8f6ca1aed52d77 (patch)
tree45c5037917a25ee809c92947ea8e47bae89399a7
parent3ea2ea338f321c44b6db7ed932f6f6eb3de63b9a (diff)
downloadpoezio-d909adcb5cdb18a85b7042794c8f6ca1aed52d77.tar.gz
poezio-d909adcb5cdb18a85b7042794c8f6ca1aed52d77.tar.bz2
poezio-d909adcb5cdb18a85b7042794c8f6ca1aed52d77.tar.xz
poezio-d909adcb5cdb18a85b7042794c8f6ca1aed52d77.zip
Client to fetch an archive of messages and add them in the current window
-rw-r--r--poezio/mam.py73
1 files changed, 73 insertions, 0 deletions
diff --git a/poezio/mam.py b/poezio/mam.py
new file mode 100644
index 00000000..6fc9f8e1
--- /dev/null
+++ b/poezio/mam.py
@@ -0,0 +1,73 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+
+"""
+ Query and control an archive of messages stored on a server using
+ XEP-0313: Message Archive Management(MAM).
+"""
+
+from getpass import getpass
+from argparse import ArgumentParser
+
+import slixmpp
+from datetime import datetime, timezone
+from poezio.theming import get_theme
+from slixmpp.exceptions import XMPPError
+from poezio.text_buffer import Message, TextBuffer
+
+
+def add_line(text_buffer: TextBuffer, text: str, str_time: str, nick: str):
+ """Adds a textual entry in the TextBuffer"""
+
+ time = datetime.strftime(str_time, '%Y-%m-%d %H:%M:%S')
+ time = datetime.strptime(time, '%Y-%m-%d %H:%M:%S')
+ time = time.replace(tzinfo=timezone.utc).astimezone(tz=None)
+ nick = nick.split('/')[1]
+ color = get_theme().COLOR_OWN_NICK
+ text_buffer.add_message(
+ text,
+ time,
+ nick,
+ color,
+ True, # History
+ None, # User
+ False, # Highlight
+ None, # Identifier
+ None, # str_time
+ None, # Jid
+ )
+
+
+class MAM(slixmpp.ClientXMPP):
+ """
+ A basic client fetching mam archive messages
+ """
+
+ def __init__(self, jid, password, remote_jid, start, end, tab):
+ slixmpp.ClientXMPP.__init__(self, jid, password)
+ self.remote_jid = remote_jid
+ self.start_date = start
+ self.end_date = end
+ self.tab = tab
+
+ self.add_event_handler("session_start", self.start)
+
+ async def start(self, *args):
+ """
+ Fetches mam results for the specified JID.
+ """
+
+ text_buffer = self.tab._text_buffer
+
+ results = self.plugin['xep_0313'].retrieve(jid=self.remote_jid,
+ iterator=True, start=self.start_date, end=self.end_date)
+ async for rsm in results:
+ for msg in rsm['mam']['results']:
+ forwarded = msg['mam_result']['forwarded']
+ timestamp = forwarded['delay']['stamp']
+ message = forwarded['stanza']
+ add_line(text_buffer, '%s' % message['body'], timestamp, str(message['from']))
+
+ self.tab.text_win.pos = 0
+ self.tab.core.refresh_window()
+ self.disconnect()