view cagou/plugins/plugin_wid_chat.py @ 332:13bc00b9743a

chat: use `bind` for EncryptionButton events: binding them on init doesn't seem to work anymore
author Goffi <goffi@goffi.org>
date Fri, 20 Dec 2019 12:29:37 +0100
parents 51520ce98154
children 69d2a96ce26f
line wrap: on
line source

#!/usr/bin/python

# Cagou: desktop/mobile frontend for Salut à Toi XMPP client
# Copyright (C) 2016-2019 Jérôme Poisson (goffi@goffi.org)

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.

# 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/>.


from functools import partial
import mimetypes
import sys
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.metrics import sp, dp
from kivy.clock import Clock
from kivy import properties
from kivy.uix.dropdown import DropDown
from kivy.core.window import Window
from sat.core import log as logging
from sat.core.i18n import _
from sat.core import exceptions
from sat_frontends.quick_frontend import quick_widgets
from sat_frontends.quick_frontend import quick_chat
from sat_frontends.tools import jid
from cagou import G
from cagou.core.constants import Const as C
from cagou.core import cagou_widget
from cagou.core import xmlui
from cagou.core.image import Image
from cagou.core.common import SymbolButton, JidButton
from cagou.core import menu

log = logging.getLogger(__name__)

PLUGIN_INFO = {
    "name": _("chat"),
    "main": "Chat",
    "description": _("instant messaging with one person or a group"),
    "icon_symbol": "chat",
}

# FIXME: OTR specific code is legacy, and only used nowadays for lock color
# we can probably get rid of them.
OTR_STATE_UNTRUSTED = 'untrusted'
OTR_STATE_TRUSTED = 'trusted'
OTR_STATE_TRUST = (OTR_STATE_UNTRUSTED, OTR_STATE_TRUSTED)
OTR_STATE_UNENCRYPTED = 'unencrypted'
OTR_STATE_ENCRYPTED = 'encrypted'
OTR_STATE_ENCRYPTION = (OTR_STATE_UNENCRYPTED, OTR_STATE_ENCRYPTED)

SYMBOL_UNENCRYPTED = 'lock-open'
SYMBOL_ENCRYPTED = 'lock'
SYMBOL_ENCRYPTED_TRUSTED = 'lock-filled'
COLOR_UNENCRYPTED = (0.4, 0.4, 0.4, 1)
COLOR_ENCRYPTED = (0.4, 0.4, 0.4, 1)
COLOR_ENCRYPTED_TRUSTED = (0.29,0.87,0.0,1)

# below this limit, new messages will be prepended
INFINITE_SCROLL_LIMIT = dp(600)


class MessAvatar(Image):
    pass


class MessageWidget(quick_chat.MessageWidget, BoxLayout):
    mess_data = properties.ObjectProperty()
    mess_xhtml = properties.ObjectProperty()
    mess_padding = (dp(5), dp(5))
    avatar = properties.ObjectProperty()
    delivery = properties.ObjectProperty()
    font_size = properties.NumericProperty(sp(12))
    right_part = properties.ObjectProperty()
    header_box = properties.ObjectProperty()

    def on_mess_data(self, wid, mess_data):
        mess_data.widgets.add(self)

    @property
    def chat(self):
        """return parent Chat instance"""
        return self.mess_data.parent

    def _get_from_mess_data(self, name, default):
        if self.mess_data is None:
            return default
        return getattr(self.mess_data, name)

    def _get_message(self):
        """Return currently displayed message"""
        if self.mess_data is None:
            return ""
        return self.mess_data.main_message

    def _set_message(self, message):
        if self.mess_data is None:
            return False
        if message == self.mess_data.message.get(""):
            return False
        self.mess_data.message = {"": message}
        return True

    message = properties.AliasProperty(
        partial(_get_from_mess_data, name="main_message", default=""),
        _set_message,
        bind=['mess_data'],
    )
    message_xhtml = properties.AliasProperty(
        partial(_get_from_mess_data, name="main_message_xhtml", default=""),
        bind=['mess_data'])
    mess_type = properties.AliasProperty(
        partial(_get_from_mess_data, name="type", default=""), bind=['mess_data'])
    own_mess = properties.AliasProperty(
        partial(_get_from_mess_data, name="own_mess", default=False), bind=['mess_data'])
    nick = properties.AliasProperty(
        partial(_get_from_mess_data, name="nick", default=""), bind=['mess_data'])
    time_text = properties.AliasProperty(
        partial(_get_from_mess_data, name="time_text", default=""), bind=['mess_data'])

    @property
    def info_type(self):
        return self.mess_data.info_type

    def update(self, update_dict):
        if 'avatar' in update_dict:
            self.avatar.source = update_dict['avatar']
        if 'status' in update_dict:
            status = update_dict['status']
            self.delivery.text =  '\u2714' if status == 'delivered' else ''


class SendButton(SymbolButton):
    message_input_box = properties.ObjectProperty()


class MessageInputBox(BoxLayout):
    message_input = properties.ObjectProperty()

    def __init__(self, *args, **kwargs):
        super(MessageInputBox, self).__init__(*args, **kwargs)
        Clock.schedule_once(self.post_init, 0)

    def post_init(self, *args):
        if sys.platform == 'android':
            self.add_widget(SendButton(message_input_box=self), 0)

    def send_text(self):
        self.message_input.send_text()


class MessageInputWidget(TextInput):

    def keyboard_on_key_down(self, window, keycode, text, modifiers):
        # We don't send text when shift is pressed to be able to add line feeds
        # (i.e. multi-lines messages). We don't send on Android either as the
        # send button appears on this platform.
        if (keycode[-1] == "enter"
            and "shift" not in modifiers
            and sys.platform != 'android'):
            self.send_text()
        else:
            return super(MessageInputWidget, self).keyboard_on_key_down(
                window, keycode, text, modifiers)

    def send_text(self):
        self.dispatch('on_text_validate')


class TransferButton(SymbolButton):
    chat = properties.ObjectProperty()

    def on_release(self, *args):
        menu.TransferMenu(callback=self.chat.transferFile).show(self)


class ExtraMenu(DropDown):
    chat = properties.ObjectProperty()

    def on_select(self, menu):
        if menu == 'bookmark':
            G.host.bridge.menuLaunch(C.MENU_GLOBAL, ("groups", "bookmarks"),
                                     {}, C.NO_SECURITY_LIMIT, self.chat.profile,
                                     callback=partial(
                                        G.host.actionManager, profile=self.chat.profile),
                                     errback=G.host.errback)
        else:
            raise exceptions.InternalError("Unknown menu: {}".format(menu))


class ExtraButton(SymbolButton):
    chat = properties.ObjectProperty()


class EncryptionMainButton(SymbolButton):

    def __init__(self, chat, **kwargs):
        """
        @param chat(Chat): Chat instance
        """
        self.chat = chat
        self.encryption_menu = EncryptionMenu(chat)
        super(EncryptionMainButton, self).__init__(**kwargs)
        self.bind(on_release=self.encryption_menu.open)

    def selectAlgo(self, name):
        """Mark an encryption algorithm as selected.

        This will also deselect all other button
        @param name(unicode, None): encryption plugin name
            None for plain text
        """
        buttons = self.encryption_menu.container.children
        buttons[-1].selected = name is None
        for button in buttons[:-1]:
            button.selected = button.text == name

    def getColor(self):
        if self.chat.otr_state_encryption == OTR_STATE_UNENCRYPTED:
            return  (0.4, 0.4, 0.4, 1)
        elif self.chat.otr_state_trust == OTR_STATE_TRUSTED:
            return (0.29,0.87,0.0,1)
        else:
            return  (0.4, 0.4, 0.4, 1)

    def getSymbol(self):
        if self.chat.otr_state_encryption == OTR_STATE_UNENCRYPTED:
            return 'lock-open'
        elif self.chat.otr_state_trust == OTR_STATE_TRUSTED:
            return 'lock-filled'
        else:
            return 'lock'


class TrustManagementButton(SymbolButton):
    pass


class EncryptionButton(BoxLayout):
    selected = properties.BooleanProperty(False)
    text = properties.StringProperty()
    trust_button = properties.BooleanProperty(False)
    best_width = properties.NumericProperty(0)
    bold = properties.BooleanProperty(True)

    def __init__(self, **kwargs):
        super(EncryptionButton, self).__init__(**kwargs)
        self.register_event_type('on_release')
        self.register_event_type('on_trust_release')
        if self.trust_button:
            self.add_widget(TrustManagementButton())

    def on_release(self):
        pass

    def on_trust_release(self):
        pass


class EncryptionMenu(DropDown):
    # best with to display all algorithms buttons + trust buttons
    best_width = properties.NumericProperty(0)

    def __init__(self, chat, **kwargs):
        """
        @param chat(Chat): Chat instance
        """
        self.chat = chat
        super(EncryptionMenu, self).__init__(**kwargs)
        btn = EncryptionButton(
            text=_("unencrypted (plain text)"),
            on_release=self.unencrypted,
            selected=True,
            bold=False,
            )
        btn.bind(
            on_release=self.unencrypted,
        )
        self.add_widget(btn)
        for plugin in G.host.encryption_plugins:
            btn = EncryptionButton(
                text=plugin['name'],
                trust_button=True,
                )
            btn.bind(
                on_release=partial(self.startEncryption, plugin=plugin),
                on_trust_release=partial(self.getTrustUI, plugin=plugin),
            )
            self.add_widget(btn)
            log.info("added encryption: {}".format(plugin['name']))

    def messageEncryptionStopCb(self):
        log.info(_("Session with {destinee} is now in plain text").format(
            destinee = self.chat.target))

    def messageEncryptionStopEb(self, failure_):
        msg = _("Error while stopping encryption with {destinee}: {reason}").format(
            destinee = self.chat.target,
            reason = failure_)
        log.warning(msg)
        G.host.addNote(_("encryption problem"), msg, C.XMLUI_DATA_LVL_ERROR)

    def unencrypted(self, button):
        self.dismiss()
        G.host.bridge.messageEncryptionStop(
            str(self.chat.target),
            self.chat.profile,
            callback=self.messageEncryptionStopCb,
            errback=self.messageEncryptionStopEb)

    def messageEncryptionStartCb(self, plugin):
        log.info(_("Session with {destinee} is now encrypted with {encr_name}").format(
            destinee = self.chat.target,
            encr_name = plugin['name']))

    def messageEncryptionStartEb(self, failure_):
        msg = _("Session can't be encrypted with {destinee}: {reason}").format(
            destinee = self.chat.target,
            reason = failure_)
        log.warning(msg)
        G.host.addNote(_("encryption problem"), msg, C.XMLUI_DATA_LVL_ERROR)

    def startEncryption(self, button, plugin):
        """Request encryption with given plugin for this session

        @param button(EncryptionButton): button which has been pressed
        @param plugin(dict): plugin data
        """
        self.dismiss()
        G.host.bridge.messageEncryptionStart(
            str(self.chat.target),
            plugin['namespace'],
            True,
            self.chat.profile,
            callback=partial(self.messageEncryptionStartCb, plugin=plugin),
            errback=self.messageEncryptionStartEb)

    def encryptionTrustUIGetCb(self, xmlui_raw):
        xml_ui = xmlui.create(
            G.host, xmlui_raw, profile=self.chat.profile)
        xml_ui.show()

    def encryptionTrustUIGetEb(self, failure_):
        msg = _("Trust manager interface can't be retrieved: {reason}").format(
            reason = failure_)
        log.warning(msg)
        G.host.addNote(_("encryption trust management problem"), msg,
                       C.XMLUI_DATA_LVL_ERROR)

    def getTrustUI(self, button, plugin):
        """Request and display trust management UI

        @param button(EncryptionButton): button which has been pressed
        @param plugin(dict): plugin data
        """
        self.dismiss()
        G.host.bridge.encryptionTrustUIGet(
            str(self.chat.target),
            plugin['namespace'],
            self.chat.profile,
            callback=self.encryptionTrustUIGetCb,
            errback=self.encryptionTrustUIGetEb)

    def otr_start(self):
        self.dismiss()
        G.host.launchMenu(
            C.MENU_SINGLE,
            ("otr", "start/refresh"),
            {'jid': str(self.chat.target)},
            None,
            C.NO_SECURITY_LIMIT,
            self.chat.profile
            )

    def otr_end(self):
        self.dismiss()
        G.host.launchMenu(
            C.MENU_SINGLE,
            ("otr", "end session"),
            {'jid': str(self.chat.target)},
            None,
            C.NO_SECURITY_LIMIT,
            self.chat.profile
            )

    def otr_authenticate(self):
        self.dismiss()
        G.host.launchMenu(
            C.MENU_SINGLE,
            ("otr", "authenticate"),
            {'jid': str(self.chat.target)},
            None,
            C.NO_SECURITY_LIMIT,
            self.chat.profile
            )


class Chat(quick_chat.QuickChat, cagou_widget.CagouWidget):
    message_input = properties.ObjectProperty()
    messages_widget = properties.ObjectProperty()
    history_scroll = properties.ObjectProperty()

    def __init__(self, host, target, type_=C.CHAT_ONE2ONE, nick=None, occupants=None,
                 subject=None, profiles=None):
        quick_chat.QuickChat.__init__(
            self, host, target, type_, nick, occupants, subject, profiles=profiles)
        self.otr_state_encryption = OTR_STATE_UNENCRYPTED
        self.otr_state_trust = OTR_STATE_UNTRUSTED
        # completion attributes
        self._hi_comp_data = None
        self._hi_comp_last = None
        self._hi_comp_dropdown = DropDown()
        self._hi_comp_allowed = True
        cagou_widget.CagouWidget.__init__(self)
        transfer_btn = TransferButton(chat=self)
        self.headerInputAddExtra(transfer_btn)
        if type_ == C.CHAT_ONE2ONE:
            self.encryption_btn = EncryptionMainButton(self)
            self.headerInputAddExtra(self.encryption_btn)
        self.extra_menu = ExtraMenu(chat=self)
        extra_btn = ExtraButton(chat=self)
        self.headerInputAddExtra(extra_btn)
        self.header_input.hint_text = target
        self._history_prepend_lock = False
        Clock.schedule_once(lambda dt: self.postInit(), 0)
        self.history_count = 0

    def __str__(self):
        return "Chat({})".format(self.target)

    def __repr__(self):
        return self.__str__()

    @classmethod
    def factory(cls, plugin_info, target, profiles):
        profiles = list(profiles)
        if len(profiles) > 1:
            raise NotImplementedError("Multi-profiles is not available yet for chat")
        if target is None:
            target = G.host.profiles[profiles[0]].whoami
        return G.host.widgets.getOrCreateWidget(cls, target, on_new_widget=None,
                                                on_existing_widget=G.host.getOrClone,
                                                profiles=profiles)

    @property
    def message_widgets_rev(self):
        return self.messages_widget.children

    ## header ##

    def changeWidget(self, jid_):
        """change current widget for a new one with given jid

        @param jid_(jid.JID): jid of the widget to create
        """
        plugin_info = G.host.getPluginInfo(main=Chat)
        factory = plugin_info['factory']
        G.host.switchWidget(self, factory(plugin_info, jid_, profiles=[self.profile]))
        self.header_input.text = ''

    def onHeaderInput(self):
        text = self.header_input.text.strip()
        try:
            if text.count('@') != 1 or text.count(' '):
                raise ValueError
            jid_ = jid.JID(text)
        except ValueError:
            log.info("entered text is not a jid")
            return

        def discoCb(disco):
            # TODO: check if plugin XEP-0045 is activated
            if "conference" in [i[0] for i in disco[1]]:
                G.host.bridge.mucJoin(str(jid_), "", "", self.profile,
                                      callback=self._mucJoinCb, errback=self._mucJoinEb)
            else:
                self.changeWidget(jid_)

        def discoEb(failure):
            log.warning("Disco failure, ignore this text: {}".format(failure))

        G.host.bridge.discoInfos(jid_.domain, self.profile, callback=discoCb,
                                 errback=discoEb)

    def onHeaderInputCompleted(self, input_wid, completed_text):
        self._hi_comp_allowed = False
        input_wid.text = completed_text
        self._hi_comp_allowed = True
        self._hi_comp_dropdown.dismiss()
        self.onHeaderInput()

    def onHeaderInputComplete(self, wid, text):
        if not self._hi_comp_allowed:
            return
        text = text.lstrip()
        if not text:
            self._hi_comp_data = None
            self._hi_comp_last = None
            self._hi_comp_dropdown.dismiss()
            return

        profile = list(self.profiles)[0]

        if self._hi_comp_data is None:
            # first completion, we build the initial list
            comp_data = self._hi_comp_data = []
            self._hi_comp_last = ''
            for jid_, jid_data in G.host.contact_lists[profile].all_iter:
                comp_data.append((jid_, jid_data))
            comp_data.sort(key=lambda datum: datum[0])
        else:
            comp_data = self._hi_comp_data

        # XXX: dropdown is rebuilt each time backspace is pressed or if the text is changed,
        #      it works OK, but some optimisation may be done here
        dropdown = self._hi_comp_dropdown

        if not text.startswith(self._hi_comp_last) or not self._hi_comp_last:
            # text has changed or backspace has been pressed, we restart
            dropdown.clear_widgets()

            for jid_, jid_data in comp_data:
                nick = jid_data.get('nick', '')
                if text in jid_.bare or text in nick.lower():
                    btn = JidButton(
                        jid = jid_.bare,
                        profile = profile,
                        size_hint = (0.5, None),
                        nick = nick,
                        on_release=lambda __, txt=jid_.bare: self.onHeaderInputCompleted(wid, txt)
                        )
                    dropdown.add_widget(btn)
        else:
            # more chars, we continue completion by removing unwanted widgets
            to_remove = []
            for c in dropdown.children[0].children:
                if text not in c.jid and text not in (c.nick or ''):
                    to_remove.append(c)
            for c in to_remove:
                dropdown.remove_widget(c)
        if dropdown.attach_to is None:
            dropdown.open(wid)
        self._hi_comp_last = text

    def messageDataConverter(self, idx, mess_id):
        return {"mess_data": self.messages[mess_id]}

    def _onHistoryPrinted(self):
        """Refresh or scroll down the focus after the history is printed"""
        # self.adapter.data = self.messages
        for mess_data in self.messages.values():
            self.appendMessage(mess_data)
        super(Chat, self)._onHistoryPrinted()

    def createMessage(self, message):
        self.appendMessage(message)
        # we need to render immediatly next 2 layouts to avoid an unpleasant flickering
        # when sending or receiving a message
        self.messages_widget.dont_delay_next_layouts = 2

    def appendMessage(self, mess_data):
        """Append a message Widget to the history

        @param mess_data(quick_chat.Message): message data
        """
        if self.handleUserMoved(mess_data):
            return
        self.messages_widget.add_widget(MessageWidget(mess_data=mess_data))
        self.notify(mess_data)

    def prependMessage(self, mess_data):
        """Prepend a message Widget to the history

        @param mess_data(quick_chat.Message): message data
        """
        mess_wid = self.messages_widget
        last_idx = len(mess_wid.children)
        mess_wid.add_widget(MessageWidget(mess_data=mess_data), index=last_idx)

    def _get_notif_msg(self, mess_data):
        return _("{nick}: {message}").format(
            nick=mess_data.nick,
            message=mess_data.main_message)

    def notify(self, mess_data):
        """Notify user when suitable

        For one2one chat, notification will happen when window has not focus
        or when one2one chat is not visible. A note is also there when widget
        is not visible.
        For group chat, note will be added on mention, with a desktop notification if
        window has not focus.
        """
        visible_clones = [w for w in G.host.getVisibleList(self.__class__)
                          if w.target == self.target]
        if len(visible_clones) > 1 and visible_clones.index(self) > 0:
            # to avoid multiple notifications in case of multiple cloned widgets
            # we only handle first clone
            return
        is_visible = bool(visible_clones)
        if self.type == C.CHAT_ONE2ONE:
            if (not Window.focus or not is_visible) and not mess_data.history:
                notif_msg = self._get_notif_msg(mess_data)
                G.host.desktop_notif(
                    notif_msg,
                    title=_("private message"))
                if not is_visible:
                    G.host.addNote(
                        _("private message"),
                        notif_msg,
                        symbol = "chat",
                        action = {
                            "action": 'chat',
                            "target": self.target,
                            "profiles": self.profiles}
                        )
        else:
            if mess_data.mention and not mess_data.history:
                notif_msg = self._get_notif_msg(mess_data)
                G.host.addNote(
                    _("mention"),
                    notif_msg,
                    symbol = "chat",
                    action = {
                        "action": 'chat',
                        "target": self.target,
                        "profiles": self.profiles}
                    )
                if not Window.focus:
                    G.host.desktop_notif(
                        notif_msg,
                        title=_("mention ({room_jid})").format(
                            room_jid=self.target)
                        )

    def onSend(self, input_widget):
        G.host.messageSend(
            self.target,
            {'': input_widget.text}, # TODO: handle language
            mess_type = (C.MESS_TYPE_GROUPCHAT
                if self.type == C.CHAT_GROUP else C.MESS_TYPE_CHAT), # TODO: put this in QuickChat
            profile_key=self.profile
            )
        input_widget.text = ''

    def fileTransferEb(self, err_msg, cleaning_cb, profile):
        if cleaning_cb is not None:
            cleaning_cb()
        msg = _("can't transfer file: {reason}").format(reason=err_msg)
        log.warning(msg)
        G.host.addNote(_("File transfer error"),
                       msg,
                       level=C.XMLUI_DATA_LVL_WARNING)

    def fileTransferCb(self, metadata, cleaning_cb, profile):
        log.debug("file transfered: {}".format(metadata))
        extra = {}

        # FIXME: Q&D way of getting file type, upload plugins shouls give it
        mime_type = mimetypes.guess_type(metadata['url'])[0]
        if mime_type is not None:
            if mime_type.split('/')[0] == 'image':
                # we generate url ourselves, so this formatting is safe
                extra['xhtml'] = "<img src='{url}' />".format(**metadata)

        G.host.messageSend(
            self.target,
            {'': metadata['url']},
            mess_type = (C.MESS_TYPE_GROUPCHAT
                if self.type == C.CHAT_GROUP else C.MESS_TYPE_CHAT),
            extra = extra,
            profile_key=profile
            )

        if cleaning_cb is not None:
            cleaning_cb()


    def transferFile(self, file_path, transfer_type=C.TRANSFER_UPLOAD, cleaning_cb=None):
        if transfer_type == C.TRANSFER_UPLOAD:
            G.host.bridge.fileUpload(
                str(file_path),
                "",
                "",
                {"ignore_tls_errors": C.boolConst(not G.host.tls_validation)},
                self.profile,
                callback = partial(
                    G.host.actionManager,
                    progress_cb = partial(self.fileTransferCb, cleaning_cb=cleaning_cb),
                    progress_eb = partial(self.fileTransferEb, cleaning_cb=cleaning_cb),
                    profile = self.profile,
                    ),
                errback = partial(G.host.errback,
                                  message=_("can't upload file: {msg}"))
            )
        elif transfer_type == C.TRANSFER_SEND:
            if self.type == C.CHAT_GROUP:
                log.warning("P2P transfer is not possible for group chat")
                # TODO: show an error dialog to user, or better hide the send button for
                #       MUC
            else:
                jid_ = self.target
                if not jid_.resource:
                    jid_ = G.host.contact_lists[self.profile].getFullJid(jid_)
                G.host.bridge.fileSend(str(jid_), str(file_path), "", "", {},
                                       profile=self.profile)
                # TODO: notification of sending/failing
        else:
            raise log.error("transfer of type {} are not handled".format(transfer_type))

    def messageEncryptionStarted(self, plugin_data):
        quick_chat.QuickChat.messageEncryptionStarted(self, plugin_data)
        self.encryption_btn.symbol = SYMBOL_ENCRYPTED
        self.encryption_btn.color = COLOR_ENCRYPTED
        self.encryption_btn.selectAlgo(plugin_data['name'])

    def messageEncryptionStopped(self, plugin_data):
        quick_chat.QuickChat.messageEncryptionStopped(self, plugin_data)
        self.encryption_btn.symbol = SYMBOL_UNENCRYPTED
        self.encryption_btn.color = COLOR_UNENCRYPTED
        self.encryption_btn.selectAlgo(None)

    def _mucJoinCb(self, joined_data):
        joined, room_jid_s, occupants, user_nick, subject, profile = joined_data
        self.host.mucRoomJoinedHandler(*joined_data[1:])
        jid_ = jid.JID(room_jid_s)
        self.changeWidget(jid_)

    def _mucJoinEb(self, failure):
        log.warning("Can't join room: {}".format(failure))

    def onOTRState(self, state, dest_jid, profile):
        assert profile in self.profiles
        if state in OTR_STATE_ENCRYPTION:
            self.otr_state_encryption = state
        elif state in OTR_STATE_TRUST:
            self.otr_state_trust = state
        else:
            log.error(_("Unknown OTR state received: {}".format(state)))
            return
        self.encryption_btn.symbol = self.encryption_btn.getSymbol()
        self.encryption_btn.color = self.encryption_btn.getColor()

    def onVisible(self):
        if not self.sync:
            self.resync()

    def onDelete(self):
        # we always keep one widget, so it's available when swiping
        # TODO: delete all widgets when chat is closed
        nb_instances = sum(1 for _ in self.host.widgets.getWidgetInstances(self))
        if nb_instances > 1:
            return super(Chat, self).onDelete()
        else:
            return False

    def _history_unlock(self, __):
        self._history_prepend_lock = False
        log.debug("history prepend unlocked")
        # we call manually onScroll, to check if we are still in the scrolling zone
        self.onScroll(self.history_scroll, self.history_scroll.scroll_y)

    def _history_scroll_adjust(self, __, scroll_start_height):
        # history scroll position must correspond to where it was before new messages
        # have been appended
        self.history_scroll.scroll_y = (
            scroll_start_height / self.messages_widget.height
        )

        # we want a small delay before unlocking, to avoid re-fetching history
        # again
        Clock.schedule_once(self._history_unlock, 1.5)

    def _backHistoryGetCb_post(self, __, history, scroll_start_height):
        if len(history) == 0:
            # we don't unlock self._history_prepend_lock if there is no history, as there
            # is no sense to try to retrieve more in this case.
            log.debug(f"we've reached top of history for {self.target.bare} chat")
        else:
            # we have to schedule again for _history_scroll_adjust, else messages_widget
            # is not resized (self.messages_widget.height is not yet updated)
            # as a result, the scroll_to can't work correctly
            Clock.schedule_once(partial(
                self._history_scroll_adjust,
                scroll_start_height=scroll_start_height))
            log.debug(
                f"{len(history)} messages prepended to history (last: {history[0][0]})")

    def _backHistoryGetCb(self, history):
        # TODO: factorise with QuickChat._historyGetCb
        scroll_start_height = self.messages_widget.height * self.history_scroll.scroll_y
        for data in reversed(history):
            uid, timestamp, from_jid, to_jid, message, subject, type_, extra = data
            from_jid = jid.JID(from_jid)
            to_jid = jid.JID(to_jid)
            extra["history"] = True
            self.messages[uid] = message = quick_chat.Message(
                self,
                uid,
                timestamp,
                from_jid,
                to_jid,
                message,
                subject,
                type_,
                extra,
                self.profile,
            )
            self.messages.move_to_end(uid, last=False)
            self.prependMessage(message)
        Clock.schedule_once(partial(
            self._backHistoryGetCb_post,
            history=history,
            scroll_start_height=scroll_start_height))

    def _backHistoryGetEb(self, failure_):
        G.host.addNote(
            _("Problem while getting back history"),
            _("Can't back history for {target}: {problem}").format(
                target=self.target, problem=failure_),
            C.XMLUI_DATA_LVL_ERROR)
        # we don't unlock self._history_prepend_lock on purpose, no need
        # to try to get more history if something is wrong

    def onScroll(self, scroll_view, scroll_y):
        if self._history_prepend_lock:
            return
        if (1-scroll_y) * self.messages_widget.height < INFINITE_SCROLL_LIMIT:
            self._history_prepend_lock = True
            log.debug(f"Retrieving back history for {self} [{self.history_count}]")
            self.history_count += 1
            first_uid = next(iter(self.messages.keys()))
            filters = self.history_filters.copy()
            filters['before_uid'] = first_uid
            self.host.bridge.historyGet(
                str(self.host.profiles[self.profile].whoami.bare),
                str(self.target),
                30,
                True,
                {k: str(v) for k,v in filters.items()},
                self.profile,
                callback=self._backHistoryGetCb,
                errback=self._backHistoryGetEb,
            )


PLUGIN_INFO["factory"] = Chat.factory
quick_widgets.register(quick_chat.QuickChat, Chat)