File: dispatcher.py

package info (click to toggle)
python-aioxmpp 0.12.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,152 kB
  • sloc: python: 96,969; xml: 215; makefile: 155; sh: 72
file content (149 lines) | stat: -rw-r--r-- 5,072 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
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
########################################################################
# File name: dispatcher.py
# This file is part of: aioxmpp
#
# LICENSE
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program.  If not, see
# <http://www.gnu.org/licenses/>.
#
########################################################################
import asyncio
import enum

import aioxmpp.callbacks
import aioxmpp.carbons
import aioxmpp.service
import aioxmpp.stream


class MessageSource(enum.Enum):
    STREAM = 0
    CARBONS = 1


class IMDispatcher(aioxmpp.service.Service):
    """
    Dispatches messages, taking into account carbons.

    .. function:: message_filter(message, peer, sent, source)

       A message was received or sent.

       :param message: Message stanza
       :type message: :class:`aioxmpp.Message`
       :param peer: The peer from/to which the stanza was received/sent
       :type peer: :class:`aioxmpp.JID`
       :param sent: Whether the mesasge was sent or received.
       :type sent: :class:`bool`
       :param source: The source of the message.
       :type source: :class:`MessageSource`

       `message` is the message stanza which was sent or received.

       `peer` is the JID of the peer involved in the message. If the message
       was sent, this is the :attr:`~.StanzaBase.to` and otherwise it is the
       :attr:`~.StanzaBase.from_` attribute of the stanza.

       If `sent` is true, the message was sent from this resource *or* another
       resource of the same account, if Message Carbons are enabled.

       `source` indicates how the message was sent or received. It may be one
       of the values of the :class:`MessageSource` enumeration.

    """

    ORDER_AFTER = [
        # we want to be loaded after the SimplePresenceDispatcher to ensure
        # that PresenceClient has updated its data structures before the
        # dispatch_presence handler runs.
        # this helps one-to-one conversations a lot, because they can simply
        # re-use the PresenceClient state
        aioxmpp.dispatcher.SimplePresenceDispatcher,

        aioxmpp.carbons.CarbonsClient,
    ]

    def __init__(self, client, **kwargs):
        super().__init__(client, **kwargs)
        self.message_filter = aioxmpp.callbacks.Filter()
        self.presence_filter = aioxmpp.callbacks.Filter()

    @aioxmpp.service.depsignal(
        aioxmpp.node.Client,
        "before_stream_established")
    async def enable_carbons(self, *args):
        carbons = self.dependencies[aioxmpp.carbons.CarbonsClient]
        try:
            await carbons.enable()
        except (RuntimeError, aioxmpp.errors.XMPPError):
            self.logger.info(
                "remote server does not support message carbons"
            )
        else:
            self.logger.info(
                "message carbons enabled successfully"
            )
        return True

    @aioxmpp.service.depsignal(
        aioxmpp.stream.StanzaStream,
        "on_message_received")
    def dispatch_message(self, message, *,
                         sent=False,
                         source=MessageSource.STREAM):
        if message.xep0280_received is not None:
            if (message.from_ is not None and
                    message.from_ != self.client.local_jid.bare()):
                return
            message = message.xep0280_received.stanza
            source = MessageSource.CARBONS
        elif message.xep0280_sent is not None:
            if (message.from_ is not None and
                    message.from_ != self.client.local_jid.bare()):
                return
            message = message.xep0280_sent.stanza
            sent = True
            source = MessageSource.CARBONS

        peer = message.to if sent else message.from_

        filtered = self.message_filter.filter(
            message,
            peer,
            sent,
            source,
        )

        if filtered is not None:
            self.logger.debug(
                "message was not processed by any IM handler: %s",
                filtered,
            )

    @aioxmpp.service.depsignal(
        aioxmpp.stream.StanzaStream,
        "on_presence_received")
    def dispatch_presence(self, presence, *, sent=False):
        filtered = self.presence_filter.filter(
            presence,
            presence.from_,
            sent,
        )

        if filtered is not None:
            self.logger.debug(
                "presence was not processed by any IM handler: %s",
                filtered,
            )