File: message_renderer.py

package info (click to toggle)
weechat-matrix 0.3.0-3.3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 808 kB
  • sloc: python: 7,992; makefile: 27
file content (120 lines) | stat: -rw-r--r-- 4,134 bytes parent folder | download | duplicates (2)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# -*- coding: utf-8 -*-

# Copyright © 2018, 2019 Damir Jelić <poljar@termina.org.uk>
#
# Permission to use, copy, modify, and/or distribute this software for
# any purpose with or without fee is hereby granted, provided that the
# above copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
# RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
# CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.


"""Module for rendering matrix messages in Weechat."""

from __future__ import unicode_literals
from nio import Api
from .globals import W
from .colors import Formatted


class Render(object):
    """Class collecting methods for rendering matrix messages in Weechat."""

    @staticmethod
    def _media(url, description):
        return ("{del_color}<{ncolor}{desc}{del_color}>{ncolor} "
                "{del_color}[{ncolor}{url}{del_color}]{ncolor}").format(
                    del_color=W.color("chat_delimiters"),
                    ncolor=W.color("reset"),
                    desc=description, url=url)

    @staticmethod
    def media(mxc, body, homeserver=None):
        """Render a mxc media URI."""
        url = Api.mxc_to_http(mxc, homeserver)
        description = "{}".format(body) if body else "file"
        return Render._media(url, description)

    @staticmethod
    def encrypted_media(mxc, body, key, hash,  iv, homeserver=None):
        """Render a mxc media URI of an encrypted file."""
        http_url = Api.encrypted_mxc_to_plumb(
            mxc,
            key,
            hash,
            iv,
            homeserver
        )
        url = http_url if http_url else mxc
        description = "{}".format(body) if body else "file"
        return Render._media(url, description)

    @staticmethod
    def message(body, formatted_body):
        """Render a room message."""
        if formatted_body:
            formatted = Formatted.from_html(formatted_body)
            return formatted.to_weechat()

        return body

    @staticmethod
    def redacted(censor, reason=None):
        """Render a redacted event message."""
        reason = (
            ', reason: "{reason}"'.format(reason=reason)
            if reason
            else ""
        )

        data = (
            "{del_color}<{log_color}Message redacted by: "
            "{censor}{log_color}{reason}{del_color}>{ncolor}"
        ).format(
            del_color=W.color("chat_delimiters"),
            ncolor=W.color("reset"),
            log_color=W.color("logger.color.backlog_line"),
            censor=censor,
            reason=reason,
        )

        return data

    @staticmethod
    def room_encryption(nick):
        """Render a room encryption event."""
        return "{nick} has enabled encryption in this room".format(nick=nick)

    @staticmethod
    def unknown(message_type, content=None):
        """Render a message of an unknown type."""
        content = (
            ': "{content}"'.format(content=content)
            if content
            else ""
        )
        return "Unknown message of type {t}{c}".format(
            t=message_type,
            c=content
        )

    @staticmethod
    def megolm():
        """Render an undecrypted megolm event."""
        return ("{del_color}<{log_color}Unable to decrypt: "
                "The sender's device has not sent us "
                "the keys for this message{del_color}>{ncolor}").format(
                    del_color=W.color("chat_delimiters"),
                    log_color=W.color("logger.color.backlog_line"),
                    ncolor=W.color("reset"))

    @staticmethod
    def bad(event):
        """Render a malformed event of a known type"""
        return "Bad event received, event type: {t}".format(t=event.type)