mam.py 6.98 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
    text_buffer.add_message(
52 53 54 55 56 57 58 59 60 61 62
        txt=text,
        time=time,
        nickname=nick,
        nick_color=color,
        history=True,
        user=None,
        highlight=False,
        top=top,
        identifier=None,
        str_time=None,
        jid=None,
63 64
    )

65
async def query(tab, remote_jid, action, 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.core.information('Failed to retrieve messages', 'Error')
71
    if 'urn:xmpp:mam:2' not in iq['disco_info'].get_features() and action is 'scroll':
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=top, 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 84
                if action is 'scroll':
                    return tab.core.information('Failed to retrieve messages', 'Error')
85
        else:
86
            try:
87
                if before is not None:
88
                    results = tab.core.xmpp['xep_0313'].retrieve(with_jid=remote_jid,
89
                    iterator=True, reverse=top, rsm={'before':before})
90
                else:
91
                    results = tab.core.xmpp['xep_0313'].retrieve(with_jid=remote_jid,
92
                    iterator=True, reverse=top, end=end)
93
            except (IqError, IqTimeout):
94 95
                if action is 'scroll':
                    return tab.core.information('Failed to retrieve messages', 'Error')
96
    else:
97
        if 'conference' in list(iq['disco_info']['identities'])[0]:
98
            try:
99
                results = tab.core.xmpp['xep_0313'].retrieve(jid=remote_jid,
100
                iterator=True, reverse=top, start=start, end=end)
101
            except (IqError, IqTimeout):
102
                return tab.core.information('Failed to retrieve messages', 'Error')
103
        else:
104
            try:
105
                results = tab.core.xmpp['xep_0313'].retrieve(with_jid=remote_jid,
106
                iterator=True, reverse=top, start=start, end=end)
107
            except (IqError, IqTimeout):
108
                return tab.core.information('Failed to retrieve messages', 'Error')
109 110
    msg_count = 0
    msgs = []
111
    async for rsm in results:
112 113
        if top:
            for msg in rsm['mam']['results']:
114 115
                if msg['mam_result']['forwarded']['stanza'].xml.find(
                    '{%s}%s' % ('jabber:client', 'body')) is not None:
116
                    msgs.append(msg)
117
                if msg_count == 10:
118
                    tab.query_status = False
119
                    tab.core.refresh_window()
120 121 122 123 124 125 126
                    return
                msg_count += 1
            msgs.reverse()
            for msg in msgs:
                forwarded = msg['mam_result']['forwarded']
                timestamp = forwarded['delay']['stamp']
                message = forwarded['stanza']
127
                tab.last_stanza_id = msg['mam_result']['id']
128
                nick = str(message['from'])
129
                add_line(tab, text_buffer, message['body'], timestamp, nick, top)
130 131
                if action is 'scroll':
                    tab.text_win.scroll_up(len(tab.text_win.built_lines))
132 133 134 135 136
        else:
            for msg in rsm['mam']['results']:
                forwarded = msg['mam_result']['forwarded']
                timestamp = forwarded['delay']['stamp']
                message = forwarded['stanza']
137
                nick = str(message['from'])
138
                add_line(tab, text_buffer, message['body'], timestamp, nick, top)
139
                tab.core.refresh_window()
140
    if len(msgs) == 0 and action is 'scroll':
141
        return tab.core.information('No more messages left to retrieve', 'Info')
142
    tab.query_status = False
143

144
def mam_scroll(tab, action):
145 146
    remote_jid = tab.jid
    text_buffer = tab._text_buffer
147
    before = tab.last_stanza_id
148 149 150 151 152 153
    end = datetime.now()
    if isinstance(tab, tabs.MucTab) is False:
        for message in text_buffer.messages:
            time = message.time
            if time < end:
                end = time
154
        end = end + timedelta(seconds=-1)
155 156 157 158
    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')
159 160 161
    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):
162
        if before is None:
163
            asyncio.ensure_future(query(tab, remote_jid, action, top=True, end=end))
164
        else:
165
            asyncio.ensure_future(query(tab, remote_jid, action, top=True, before=before))
166
        tab.query_status = True
167 168 169 170
        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