structs.py 4.64 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# This file is part of Gajim.
#
# Gajim is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation; version 3 only.
#
# Gajim 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Gajim.  If not, see <http://www.gnu.org/licenses/>.

Philipp Hörist's avatar
Philipp Hörist committed
15
import time
16 17
from collections import namedtuple

Philipp Hörist's avatar
Philipp Hörist committed
18 19
from nbxmpp.protocol import JID

Philipp Hörist's avatar
Philipp Hörist committed
20
from gajim.common.const import MUCJoinedState
Philipp Hörist's avatar
Philipp Hörist committed
21
from gajim.common.const import KindConstant
Philipp Hörist's avatar
Philipp Hörist committed
22

23 24
URI = namedtuple('URI', 'type action data')
URI.__new__.__defaults__ = (None, None)  # type: ignore
Philipp Hörist's avatar
Philipp Hörist committed
25 26 27


class MUCData:
28
    def __init__(self, room_jid, nick, password, config=None):
Philipp Hörist's avatar
Philipp Hörist committed
29
        self._room_jid = JID(room_jid)
30
        self._config = config
Philipp Hörist's avatar
Philipp Hörist committed
31 32 33
        self.nick = nick
        self.password = password
        self.state = MUCJoinedState.NOT_JOINED
34 35
        # Message id of the captcha challenge
        self.captcha_id = None
Philipp Hörist's avatar
Philipp Hörist committed
36 37 38 39 40

    @property
    def jid(self):
        return self._room_jid

Philipp Hörist's avatar
Philipp Hörist committed
41 42 43
    @property
    def occupant_jid(self):
        jid = self._room_jid.copy()
Philipp Hörist's avatar
Philipp Hörist committed
44
        jid.setResource(self.nick)
Philipp Hörist's avatar
Philipp Hörist committed
45 46
        return jid

47 48 49
    @property
    def config(self):
        return self._config
Philipp Hörist's avatar
Philipp Hörist committed
50 51 52 53 54


class OutgoingMessage:
    def __init__(self,
                 account,
55
                 contact,
Philipp Hörist's avatar
Philipp Hörist committed
56 57 58 59 60 61 62 63 64 65 66 67
                 message,
                 type_,
                 subject=None,
                 chatstate=None,
                 resource=None,
                 user_nick=None,
                 label=None,
                 control=None,
                 attention=None,
                 correct_id=None,
                 automatic_message=False,
                 oob_url=None,
68 69
                 xhtml=None,
                 nodes=None):
Philipp Hörist's avatar
Philipp Hörist committed
70 71 72 73 74 75 76 77

        if type_ not in ('chat', 'groupchat', 'normal', 'headline'):
            raise ValueError('Unknown message type: %s' % type_)

        if not message and chatstate is None:
            raise ValueError('Trying to send message without content')

        self.account = account
78
        self.contact = contact
Philipp Hörist's avatar
Philipp Hörist committed
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
        self.message = message
        self.type_ = type_

        if type_ == 'chat':
            self.kind = KindConstant.CHAT_MSG_SENT
        elif type_ == 'groupchat':
            self.kind = KindConstant.GC_MSG
        elif type_ == 'normal':
            self.kind = KindConstant.SINGLE_MSG_SENT
        else:
            raise ValueError('Unknown message type')

        from gajim.common.helpers import AdditionalDataDict
        self.additional_data = AdditionalDataDict()

        self.subject = subject
        self.chatstate = chatstate
        self.resource = resource
        self.user_nick = user_nick
        self.label = label
        self.control = control
        self.attention = attention
        self.correct_id = correct_id
        self.automatic_message = automatic_message

        self.oob_url = oob_url

        if oob_url is not None:
            self.additional_data.set_value('gajim', 'oob_url', oob_url)

        self.xhtml = xhtml

        if xhtml is not None:
            self.additional_data.set_value('gajim', 'xhtml', xhtml)

114 115
        self.nodes = nodes

Philipp Hörist's avatar
Philipp Hörist committed
116 117 118 119 120 121 122 123 124 125
        self.timestamp = None
        self.message_id = None
        self.stanza = None
        self.session = None
        self.delayed = None # TODO never set

        self.is_loggable = True

    def copy(self):
        message = OutgoingMessage(self.account,
126
                                  self.contact,
Philipp Hörist's avatar
Philipp Hörist committed
127 128 129 130 131 132 133
                                  self.message,
                                  self.type_)
        for name, value in vars(self).items():
            setattr(message, name, value)
        message.additional_data = self.additional_data.copy()
        return message

134 135 136 137
    @property
    def jid(self):
        return self.contact.jid

Philipp Hörist's avatar
Philipp Hörist committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
    @property
    def is_groupchat(self):
        return self.type_ == 'groupchat'

    @property
    def is_chat(self):
        return self.type_ == 'chat'

    @property
    def is_normal(self):
        return self.type_ == 'normal'

    def set_sent_timestamp(self):
        if self.is_groupchat:
            return
        self.timestamp = time.time()

    @property
    def is_encrypted(self):
        return bool(self.additional_data.get_value('encrypted', 'name', False))

    @property
    def msg_iq(self):
        # Backwards compatibility for plugins
        return self.stanza

    @msg_iq.setter
    def msg_iq(self, value):
        # Backwards compatibility for plugins
        self.stanza = value