Welcome to mirror list, hosted at ThFree Co, Russian Federation.

message.py « modules « common « gajim - dev.gajim.org/gajim/gajim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c94214f100166225dcbf3136fadac9e9769be4d7 (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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# 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/>.

# Message handler

from __future__ import annotations

from typing import Any

import time

import nbxmpp
from nbxmpp.namespaces import Namespace
from nbxmpp.protocol import JID
from nbxmpp.structs import MessageProperties
from nbxmpp.structs import StanzaHandler
from nbxmpp.util import generate_id

from gajim.common import app
from gajim.common import types
from gajim.common.const import KindConstant
from gajim.common.events import GcMessageReceived
from gajim.common.events import MessageError
from gajim.common.events import MessageReceived
from gajim.common.events import RawMessageReceived
from gajim.common.helpers import AdditionalDataDict
from gajim.common.modules.base import BaseModule
from gajim.common.modules.contacts import GroupchatParticipant
from gajim.common.modules.misc import parse_oob
from gajim.common.modules.misc import parse_xhtml
from gajim.common.modules.util import check_if_message_correction
from gajim.common.modules.util import get_eme_message
from gajim.common.structs import OutgoingMessage


class Message(BaseModule):
    def __init__(self, con: types.Client) -> None:
        BaseModule.__init__(self, con)

        self.handlers = [
            StanzaHandler(name='message',
                          callback=self._check_if_unknown_contact,
                          priority=41),
            StanzaHandler(name='message',
                          callback=self._message_received,
                          priority=50),
            StanzaHandler(name='message',
                          typ='error',
                          callback=self._message_error_received,
                          priority=50),
        ]

        # XEPs for which this message module should not be executed
        self._message_namespaces = {Namespace.ROSTERX, Namespace.IBB}

    def _check_if_unknown_contact(self,
                                  _con: types.xmppClient,
                                  stanza: nbxmpp.Message,
                                  properties: MessageProperties
                                  ) -> None:
        if (properties.type.is_groupchat or
                properties.is_muc_pm or
                properties.is_self_message or
                properties.is_mam_message):
            return

        if self._con.get_own_jid().domain == str(properties.jid):
            # Server message
            return

        if not app.settings.get_account_setting(self._account,
                                                'ignore_unknown_contacts'):
            return

        jid = properties.jid.bare
        if self._con.get_module('Roster').get_item(jid) is None:
            self._log.warning('Ignore message from unknown contact: %s', jid)
            self._log.warning(stanza)
            raise nbxmpp.NodeProcessed

    def _message_received(self,
                          _con: types.xmppClient,
                          stanza: nbxmpp.Message,
                          properties: MessageProperties
                          ) -> None:
        if (properties.is_mam_message or
                properties.is_pubsub or
                properties.type.is_error):
            return
        # Check if a child of the message contains any
        # namespaces that we handle in other modules.
        # nbxmpp executes less common handlers last
        if self._message_namespaces & set(stanza.getProperties()):
            return

        self._log.info('Received from %s', stanza.getFrom())

        app.ged.raise_event(RawMessageReceived(
            conn=self._con,
            stanza=stanza,
            account=self._account))

        if properties.is_carbon_message and properties.carbon.is_sent:
            # Ugly, we treat the from attr as the remote jid,
            # to make that work with sent carbons we have to do this.
            # TODO: Check where in Gajim and plugins we depend on that behavior
            stanza.setFrom(stanza.getTo())

        from_ = stanza.getFrom()
        fjid = str(from_)
        jid = from_.bare
        resource = from_.resource

        type_ = properties.type

        stanza_id, message_id = self._get_unique_id(properties)

        if (properties.is_self_message or properties.is_muc_pm):
            archive_jid = self._con.get_own_jid().bare
            if app.storage.archive.find_stanza_id(
                    self._account,
                    archive_jid,
                    stanza_id,
                    message_id,
                    properties.type.is_groupchat):
                return

        msgtxt = properties.body

        additional_data = AdditionalDataDict()

        if properties.has_user_delay:
            additional_data.set_value(
                'gajim', 'user_timestamp', properties.user_timestamp)

        parse_oob(properties, additional_data)
        parse_xhtml(properties, additional_data)

        if properties.is_encrypted:
            additional_data['encrypted'] = properties.encrypted.additional_data
        else:
            if properties.eme is not None:
                msgtxt = get_eme_message(properties.eme)

        displaymarking = None
        if properties.has_security_label:
            displaymarking = properties.security_label.displaymarking

        event_attr: dict[str, Any] = {
            'conn': self._con,
            'stanza': stanza,
            'account': self._account,
            'additional_data': additional_data,
            'fjid': fjid,
            'jid': from_ if properties.is_muc_pm else from_.new_as_bare(),
            'resource': resource,
            'stanza_id': stanza_id,
            'unique_id': stanza_id or message_id,
            'msgtxt': msgtxt,
            'delayed': properties.user_timestamp is not None,
            'msg_log_id': None,
            'displaymarking': displaymarking,
            'properties': properties,
        }

        if type_.is_groupchat:
            kind = KindConstant.GC_MSG
        elif properties.is_sent_carbon:
            kind = KindConstant.CHAT_MSG_SENT
        else:
            kind = KindConstant.CHAT_MSG_RECV

        if check_if_message_correction(properties,
                                       self._account,
                                       from_,
                                       msgtxt,
                                       kind,
                                       properties.timestamp,
                                       self._log):
            return

        if type_.is_groupchat:
            if not msgtxt:
                return

            occupant_id = None
            group_contact = self._client.get_module('Contacts').get_contact(
                jid, groupchat=True)
            if group_contact.supports(Namespace.OCCUPANT_ID):
                # Only store occupant-id if MUC announces support
                occupant_id = properties.occupant_id

            real_jid = self._get_real_jid(properties)

            event_attr.update({
                'room_jid': jid,
                'real_jid': real_jid,
                'occupant_id': occupant_id,
            })

            event = GcMessageReceived(**event_attr)

            msg_log_id = self._log_muc_message(event)
            event.msg_log_id = msg_log_id
            app.ged.raise_event(event)
            return

        event = MessageReceived(**event_attr)
        if not msgtxt:
            app.ged.raise_event(event)
            return

        msg_log_id = app.storage.archive.insert_into_logs(
            self._account,
            fjid if properties.is_muc_pm else jid,
            properties.timestamp,
            kind,
            message=msgtxt,
            subject=properties.subject,
            additional_data=additional_data,
            stanza_id=stanza_id or message_id,
            message_id=properties.id)

        event.msg_log_id = msg_log_id
        app.ged.raise_event(event)

    def _message_error_received(self,
                                _con: types.xmppClient,
                                _stanza: nbxmpp.Message,
                                properties: MessageProperties
                                ) -> None:
        jid = properties.jid
        if not properties.is_muc_pm:
            jid = jid.new_as_bare()

        self._log.info(properties.error)

        app.storage.archive.set_message_error(
            app.get_jid_from_account(self._account),
            jid,
            properties.id,
            properties.error)

        app.ged.raise_event(
            MessageError(account=self._account,
                         jid=jid,
                         room_jid=jid,
                         message_id=properties.id,
                         error=properties.error))

    def _log_muc_message(self, event: GcMessageReceived) -> int | None:
        if not event.properties.muc_nickname:
            return None

        if not event.msgtxt:
            return None

        self._check_for_mam_compliance(event.room_jid, event.stanza_id)

        return app.storage.archive.insert_into_logs(
            self._account,
            event.jid,
            event.properties.timestamp,
            KindConstant.GC_MSG,
            message=event.msgtxt,
            contact_name=event.properties.muc_nickname,
            additional_data=event.additional_data,
            stanza_id=event.stanza_id,
            message_id=event.properties.id,
            occupant_id=event.occupant_id,
            real_Jid=event.real_jid)

    def _check_for_mam_compliance(self, room_jid: str, stanza_id: str) -> None:
        disco_info = app.storage.cache.get_last_disco_info(room_jid)
        if stanza_id is None and disco_info.mam_namespace == Namespace.MAM_2:
            self._log.warning('%s announces mam:2 without stanza-id', room_jid)

    def _get_real_jid(self, properties: MessageProperties) -> JID | None:
        if not properties.type.is_groupchat:
            return None

        if not properties.jid.is_full:
            return None

        participant = self._client.get_module('Contacts').get_contact(
                properties.jid, groupchat=True)
        assert isinstance(participant, GroupchatParticipant)
        return participant.real_jid

    def _get_unique_id(self,
                       properties: MessageProperties
                       ) -> tuple[str | None, str | None]:
        if properties.is_self_message:
            # Deduplicate self message with message-id
            return None, properties.id

        if not properties.stanza_ids:
            return None, None

        if properties.type.is_groupchat:
            disco_info = app.storage.cache.get_last_disco_info(
                properties.jid.bare)

            if disco_info.mam_namespace != Namespace.MAM_2:
                return None, None

            archive = properties.jid
        else:
            if not self._con.get_module('MAM').available:
                return None, None

            archive = self._con.get_own_jid()

        for stanza_id in properties.stanza_ids:
            if archive.bare_match(stanza_id.by):
                return stanza_id.id, None

        # stanza-id not added by the archive, ignore it.
        return None, None

    def build_message_stanza(self, message: OutgoingMessage) -> nbxmpp.Message:
        own_jid = self._con.get_own_jid()

        stanza = nbxmpp.Message(to=message.jid,
                                body=message.message,
                                typ=message.type_,
                                subject=message.subject,
                                xhtml=message.xhtml)

        if message.correct_id:
            stanza.setTag('replace', attrs={'id': message.correct_id},
                          namespace=Namespace.CORRECT)

        # XEP-0359
        message.message_id = generate_id()
        stanza.setID(message.message_id)
        stanza.setOriginID(message.message_id)

        if message.label:
            stanza.addChild(node=message.label.to_node())

        # XEP-0172: user_nickname
        if message.user_nick:
            stanza.setTag('nick', namespace=Namespace.NICK).setData(
                message.user_nick)

        # XEP-0203
        # TODO: Seems delayed is not set anywhere
        if message.delayed:
            timestamp = time.strftime('%Y-%m-%dT%H:%M:%SZ',
                                      time.gmtime(message.delayed))
            stanza.addChild('delay',
                            namespace=Namespace.DELAY2,
                            attrs={'from': str(own_jid), 'stamp': timestamp})

        # XEP-0224
        if message.attention:
            stanza.setTag('attention', namespace=Namespace.ATTENTION)

        # XEP-0066
        if message.oob_url is not None:
            oob = stanza.addChild('x', namespace=Namespace.X_OOB)
            oob.addChild('url').setData(message.oob_url)

        # XEP-0184
        if not own_jid.bare_match(message.jid):
            if message.message and not message.is_groupchat:
                stanza.setReceiptRequest()

        # Mark Message as MUC PM
        if message.contact.is_pm_contact:
            stanza.setTag('x', namespace=Namespace.MUC_USER)

        # XEP-0085
        if message.chatstate is not None:
            stanza.setTag(message.chatstate, namespace=Namespace.CHATSTATES)
            if not message.message:
                stanza.setTag('no-store',
                              namespace=Namespace.MSG_HINTS)

        # XEP-0333
        if message.message:
            stanza.setMarkable()
        if message.marker:
            marker, id_ = message.marker
            stanza.setMarker(marker, id_)

        # Add other nodes
        if message.nodes is not None:
            for node in message.nodes:
                stanza.addChild(node=node)

        return stanza

    def log_message(self, message: OutgoingMessage) -> int | None:
        if not message.is_loggable:
            return None

        if message.message is None:
            return None

        if message.correct_id is not None:
            app.storage.archive.try_message_correction(
                self._account,
                message.jid,
                None,
                message.message,
                message.correct_id,
                KindConstant.CHAT_MSG_SENT,
                message.timestamp)
            return None

        msg_log_id = app.storage.archive.insert_into_logs(
            self._account,
            message.jid,
            message.timestamp,
            message.kind,
            message=message.message,
            subject=message.subject,
            additional_data=message.additional_data,
            message_id=message.message_id,
            stanza_id=message.message_id)
        return msg_log_id