mam.py 6.74 KB
Newer Older
1 2 3 4 5 6 7 8
#!/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).
"""

9
import asyncio
10
import random
11
from datetime import datetime, timedelta, timezone
12
from slixmpp.exceptions import IqError, IqTimeout
13
from poezio.theming import get_theme
14
from poezio import tabs
15
from poezio import xhtml, colors
16
from poezio.config import config
17 18
from poezio.text_buffer import Message, TextBuffer

19
def add_line(tab, text_buffer: TextBuffer, text: str, str_time: str, nick: str, top: bool):
20 21 22 23
    """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')
24 25
    time = time.replace(tzinfo=timezone.utc).astimezone(tz=None)
    time = time.replace(tzinfo=None)
26 27
    deterministic = config.get_by_tabname('deterministic_nick_colors',
                                              tab.jid.bare)
28 29 30
    if isinstance(tab, tabs.MucTab):
        nick = nick.split('/')[1]
        user = tab.get_user_by_name(nick)
31 32 33 34 35 36 37 38 39 40 41 42 43
        if deterministic:
            if user:
                color = user.color
            else:
                theme = get_theme()
                if theme.ccg_palette:
                    fg_color = colors.ccg_text_to_color(theme.ccg_palette, nick)
                    color = fg_color, -1
                else:
                    mod = len(theme.LIST_COLOR_NICKNAMES)
                    nick_pos = int(md5(nick.encode('utf-8')).hexdigest(),
                                16) % mod
                    color = theme.LIST_COLOR_NICKNAMES[nick_pos]
44 45 46 47 48 49 50
        else:
            color = random.choice(list(xhtml.colors))
            color = xhtml.colors.get(color)
            color = (color, -1)
    else:
        nick = nick.split('/')[0]
        color = get_theme().COLOR_OWN_NICK
51 52 53 54 55 56 57 58 59 60 61 62 63 64
    text_buffer.add_message(
        text,
        time,
        nick,
        color,
        True,  # History
        None,  # User
        False,  # Highlight
        top, #Top
        None,  # Identifier
        None,  # str_time
        None,  # Jid
    )

65
async def query(tab, remote_jid, top, start=None, end=None, before=None):
66
    text_buffer = tab._text_buffer
67
    try:
68
        iq = await tab.core.xmpp.plugin['xep_0030'].get_info(jid=remote_jid)
69
    except (IqError, IqTimeout):
70
        return tab.information('Failed to retrieve messages', 'Error')
71
    if 'urn:xmpp:mam:2' not in iq['disco_info'].get_features():
72
        return tab.core.information("%s doesn't support MAM." % remote_jid, "Info")
73
    if top:
74
        if isinstance(tab, tabs.MucTab):
75
            try:
76
                if before is not None:
77
                    results = tab.core.xmpp['xep_0313'].retrieve(jid=remote_jid,
78
                    iterator=True, reverse=before, rsm={'before':before})
79
                else:
80
                    results = tab.core.xmpp['xep_0313'].retrieve(jid=remote_jid,
81
                    iterator=True, reverse=top, end=end)
82
            except (IqError, IqTimeout):
83
                return tab.core.information('Failed to retrieve messages', 'Error')
84
        else:
85
            try:
86
                if before is not None:
87
                    results = tab.core.xmpp['xep_0313'].retrieve(with_jid=remote_jid,
88
                    iterator=True, reverse=before, rsm={'before':before})
89
                else:
90
                    results = tab.core.xmpp['xep_0313'].retrieve(with_jid=remote_jid,
91
                    iterator=True, reverse=top, end=end)
92
            except (IqError, IqTimeout):
93
                return tab.core.information('Failed to retrieve messages', 'Error')
94
    else:
95
        if 'conference' in list(iq['disco_info']['identities'])[0]:
96
            try:
97
                results = tab.core.xmpp['xep_0313'].retrieve(jid=remote_jid,
98
                iterator=True, reverse=top, start=start_date, end=end)
99
            except (IqError, IqTimeout):
100
                return tab.core.information('Failed to retrieve messages', 'Error')
101
        else:
102
            try:
103
                results = tab.core.xmpp['xep_0313'].retrieve(with_jid=remote_jid,
104
                iterator=True, reverse=top, start=start_date, end=end)
105
            except (IqError, IqTimeout):
106
                return tab.core.information('Failed to retrieve messages', 'Error')
107 108
    msg_count = 0
    msgs = []
109
    async for rsm in results:
110 111
        if top:
            for msg in rsm['mam']['results']:
112 113
                if msg['mam_result']['forwarded']['stanza'].xml.find(
                    '{%s}%s' % ('jabber:client', 'body')) is not None:
114
                    msgs.append(msg)
115
                if msg_count == 10:
116 117
                    tab.query_id = 0
                    tab.core.refresh_window()
118 119 120 121 122 123 124
                    return
                msg_count += 1
            msgs.reverse()
            for msg in msgs:
                forwarded = msg['mam_result']['forwarded']
                timestamp = forwarded['delay']['stamp']
                message = forwarded['stanza']
125
                tab.stanza_id = msg['mam_result']['id']
126
                nick = str(message['from'])
127
                add_line(tab, text_buffer, message['body'], timestamp, nick, top)
128
                tab.text_win.scroll_up(len(tab.text_win.built_lines))
129 130 131 132 133
        else:
            for msg in rsm['mam']['results']:
                forwarded = msg['mam_result']['forwarded']
                timestamp = forwarded['delay']['stamp']
                message = forwarded['stanza']
134
                nick = str(message['from'])
135
                add_line(tab, text_buffer, message['body'], timestamp, nick, top)
136
                tab.core.refresh_window()
137
    if len(msgs) == 0:
138 139
        return tab.core.information('No more messages left to retrieve', 'Info')
    tab.query_id = 0
140

141 142 143
def mam_scroll(tab):
    remote_jid = tab.jid
    text_buffer = tab._text_buffer
144 145 146 147 148 149 150
    before = tab.stanza_id
    end = datetime.now()
    if isinstance(tab, tabs.MucTab) is False:
        for message in text_buffer.messages:
            time = message.time
            if time < end:
                end = time
151
        end = end + timedelta(seconds=-1)
152 153 154 155
    tzone = datetime.now().astimezone().tzinfo
    end = end.replace(tzinfo=tzone).astimezone(tz=timezone.utc)
    end = end.replace(tzinfo=None)
    end = datetime.strftime(end, '%Y-%m-%dT%H:%M:%SZ')
156 157 158
    pos = tab.text_win.pos
    tab.text_win.pos += tab.text_win.height - 1
    if tab.text_win.pos + tab.text_win.height > len(tab.text_win.built_lines):
159
        if before is None:
160
            asyncio.ensure_future(query(tab, remote_jid, top=True, end=end))
161
        else:
162
            asyncio.ensure_future(query(tab, remote_jid, top=True, before=before))
163 164 165 166 167
        tab.query_id = 1
        tab.text_win.pos = len(tab.text_win.built_lines) - tab.text_win.height
        if tab.text_win.pos < 0:
            tab.text_win.pos = 0
    return tab.text_win.pos != pos