summaryrefslogtreecommitdiff
path: root/plugins/embed.py
blob: 3ce2a9d385da2a765e4cbff52839e2be41a75048 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""
Display an image URL as an embedded image in some clients like Conversations.
Uses: https://xmpp.org/extensions/xep-0066.html#x-oob

Usage
-----

.. glossary::

    /embed <image_url>

        Run this command to send the <image_url> as an
        embedded image in your contact's client.
"""

from poezio import tabs
from poezio.plugin import BasePlugin
from poezio.theming import get_theme
from poezio.ui.types import Message


class Plugin(BasePlugin):
    def init(self):
        for tab_t in [tabs.MucTab, tabs.StaticConversationTab, tabs.DynamicConversationTab, tabs.PrivateTab]:
            self.api.add_tab_command(
                tab_t,
                'embed',
                self.embed_image_url,
                help='Embed an image url into the contact\'s client',
                usage='<image_url>')

    def embed_image_url(self, url):
        tab = self.api.current_tab()
        message = self.core.xmpp.make_message(tab.jid)
        message['body'] = url
        message['oob']['url'] = url
        message['type'] = 'groupchat'
        if not isinstance(tab, tabs.MucTab):
            message['type'] = 'chat'
            tab.add_message(
                Message(
                    message['body'],
                    nickname=tab.core.own_nick,
                    nick_color=get_theme().COLOR_OWN_NICK,
                    identifier=message['id'],
                    jid=tab.core.xmpp.boundjid,
                ),
            )
        message.send()
        self.core.refresh_window()