File: dispatcher.py

package info (click to toggle)
python-nbxmpp 6.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,340 kB
  • sloc: python: 19,639; makefile: 4
file content (815 lines) | stat: -rw-r--r-- 27,897 bytes parent folder | download
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
# Copyright (C) 2019 Philipp Hörist <philipp AT hoerist.com>
#
# This file is part of nbxmpp.
#
# SPDX-License-Identifier: GPL-3.0-or-later

from __future__ import annotations

from typing import Any
from typing import Literal
from typing import overload
from typing import TYPE_CHECKING

import logging
import time
from collections.abc import Callable
from xml.parsers.expat import ExpatError

from gi.repository import GLib

from nbxmpp.exceptions import StanzaDecrypted
from nbxmpp.modules.activity import Activity
from nbxmpp.modules.adhoc import AdHoc
from nbxmpp.modules.annotations import Annotations
from nbxmpp.modules.attention import Attention
from nbxmpp.modules.blocking import Blocking
from nbxmpp.modules.bookmarks.native_bookmarks import NativeBookmarks
from nbxmpp.modules.bookmarks.pep_bookmarks import PEPBookmarks
from nbxmpp.modules.bookmarks.private_bookmarks import PrivateBookmarks
from nbxmpp.modules.captcha import Captcha
from nbxmpp.modules.chat_markers import ChatMarkers
from nbxmpp.modules.chatstates import Chatstates
from nbxmpp.modules.correction import Correction
from nbxmpp.modules.delay import Delay
from nbxmpp.modules.delimiter import Delimiter
from nbxmpp.modules.discovery import Discovery
from nbxmpp.modules.eme import EME
from nbxmpp.modules.entity_caps import EntityCaps
from nbxmpp.modules.entity_time import EntityTime
from nbxmpp.modules.http_auth import HTTPAuth
from nbxmpp.modules.http_upload import HTTPUpload
from nbxmpp.modules.ibb import IBB
from nbxmpp.modules.idle import Idle
from nbxmpp.modules.iq import BaseIq
from nbxmpp.modules.last_activity import LastActivity
from nbxmpp.modules.location import Location
from nbxmpp.modules.mam import MAM
from nbxmpp.modules.mds import MDS
from nbxmpp.modules.message import BaseMessage
from nbxmpp.modules.misc import unwrap_carbon
from nbxmpp.modules.misc import unwrap_mam
from nbxmpp.modules.mood import Mood
from nbxmpp.modules.muc import MUC
from nbxmpp.modules.muc.hats import Hats
from nbxmpp.modules.muc.moderation import Moderation
from nbxmpp.modules.muclumbus import Muclumbus
from nbxmpp.modules.nickname import Nickname
from nbxmpp.modules.omemo import OMEMO
from nbxmpp.modules.oob import OOB
from nbxmpp.modules.openpgp import OpenPGP
from nbxmpp.modules.pgplegacy import PGPLegacy
from nbxmpp.modules.ping import Ping
from nbxmpp.modules.presence import BasePresence
from nbxmpp.modules.pubsub import PubSub
from nbxmpp.modules.reactions import Reactions
from nbxmpp.modules.receipts import Receipts
from nbxmpp.modules.register import Register
from nbxmpp.modules.replies import Replies
from nbxmpp.modules.retraction import Retraction
from nbxmpp.modules.roster import Roster
from nbxmpp.modules.security_labels import SecurityLabels
from nbxmpp.modules.software_version import SoftwareVersion
from nbxmpp.modules.tune import Tune
from nbxmpp.modules.user_avatar import UserAvatar
from nbxmpp.modules.vcard4 import VCard4
from nbxmpp.modules.vcard_avatar import VCardAvatar
from nbxmpp.modules.vcard_temp import VCardTemp
from nbxmpp.namespaces import Namespace
from nbxmpp.protocol import ERR_FEATURE_NOT_IMPLEMENTED
from nbxmpp.protocol import Error
from nbxmpp.protocol import InvalidFrom
from nbxmpp.protocol import InvalidJid
from nbxmpp.protocol import InvalidStanza
from nbxmpp.protocol import Iq
from nbxmpp.protocol import Message
from nbxmpp.protocol import NodeProcessed
from nbxmpp.protocol import Presence
from nbxmpp.protocol import Protocol
from nbxmpp.protocol import StreamErrorNode
from nbxmpp.simplexml import Node
from nbxmpp.simplexml import NodeBuilder
from nbxmpp.structs import StanzaHandler
from nbxmpp.util import get_properties_struct
from nbxmpp.util import INVALID_XML_RX
from nbxmpp.util import is_websocket_close
from nbxmpp.util import is_websocket_stream_error
from nbxmpp.util import LogAdapter
from nbxmpp.util import Observable

if TYPE_CHECKING:
    from nbxmpp.client import Client

log = logging.getLogger("nbxmpp.dispatcher")

NBXMPPModuleNameT = Literal[
    "Activity",
    "Activity",
    "AdHoc",
    "Annotations",
    "Attention",
    "BasePresence",
    "BaseMessage",
    "BaseIq",
    "Blocking",
    "Captcha",
    "ChatMarkers",
    "Chatstates",
    "Correction",
    "Delay",
    "Delimiter",
    "Discovery",
    "EME",
    "EntityCaps",
    "EntityTime",
    "Hats",
    "HTTPAuth",
    "HTTPUpload",
    "IBB",
    "Idle",
    "LastActivity",
    "Location",
    "MAM",
    "MDS",
    "Moderation",
    "Mood",
    "MUC",
    "Muclumbus",
    "NativeBookmarks",
    "Nickname",
    "OMEMO",
    "OOB",
    "OpenPGP",
    "PEPBookmarks",
    "PGPLegacy",
    "Ping",
    "PrivateBookmarks",
    "PubSub",
    "Reactions",
    "Receipts",
    "Register",
    "Replies",
    "Retraction",
    "Roster",
    "SecurityLabels",
    "SoftwareVersion",
    "Tune",
    "UserAvatar",
    "VCardAvatar",
    "VCardTemp",
    "VCard4",
]
NBXMPPModuleT = (
    Activity
    | AdHoc
    | Annotations
    | Attention
    | BasePresence
    | BaseMessage
    | BaseIq
    | Blocking
    | Captcha
    | ChatMarkers
    | Chatstates
    | Correction
    | Delay
    | Delimiter
    | Discovery
    | EME
    | EntityCaps
    | EntityTime
    | Hats
    | HTTPAuth
    | HTTPUpload
    | IBB
    | Idle
    | LastActivity
    | Location
    | MAM
    | MDS
    | Moderation
    | Mood
    | MUC
    | Muclumbus
    | NativeBookmarks
    | Nickname
    | OMEMO
    | OOB
    | OpenPGP
    | PEPBookmarks
    | PGPLegacy
    | Ping
    | PrivateBookmarks
    | PubSub
    | Reactions
    | Receipts
    | Register
    | Replies
    | Retraction
    | Roster
    | SecurityLabels
    | SoftwareVersion
    | Tune
    | UserAvatar
    | VCardAvatar
    | VCardTemp
    | VCard4
)


class StanzaDispatcher(Observable):
    """
    Dispatches stanzas to handlers

    Signals:
        before-dispatch
        parsing-error
        stream-end

    """

    def __init__(self, client: Client) -> None:
        Observable.__init__(self, log)
        self._client = client
        self._modules: dict[str, NBXMPPModuleT] = {}
        self._parser: NodeBuilder | None = None
        self._websocket_stream_error: str | None = None

        self._log = LogAdapter(log, {"context": client.log_context})

        self._handlers: dict[str, dict[str, dict[str, Any]]] = {}

        self._id_callbacks: dict[str, tuple[Callable[..., Any], float | None, Any]] = {}
        self._dispatch_callback: Callable[..., Any] | None = None
        self._timeout_id: int | None = None

        self._stanza_types = {
            "iq": Iq,
            "message": Message,
            "presence": Presence,
            "error": StreamErrorNode,
        }

        self._register_namespace("unknown")
        self._register_namespace(Namespace.STREAMS)
        self._register_namespace(Namespace.CLIENT)
        self._register_protocol("iq", Iq)
        self._register_protocol("presence", Presence)
        self._register_protocol("message", Message)

        self._register_modules()

    def set_dispatch_callback(self, callback: Callable[..., Any]) -> None:
        self._log.info("Set dispatch callback: %s", callback)
        self._dispatch_callback = callback

    @overload
    def get_module(self, name: Literal["Activity"]) -> Activity: ...
    @overload
    def get_module(self, name: Literal["AdHoc"]) -> AdHoc: ...
    @overload
    def get_module(self, name: Literal["Annotations"]) -> Annotations: ...
    @overload
    def get_module(self, name: Literal["Attention"]) -> Attention: ...
    @overload
    def get_module(self, name: Literal["Blocking"]) -> Blocking: ...
    @overload
    def get_module(self, name: Literal["NativeBookmarks"]) -> NativeBookmarks: ...
    @overload
    def get_module(self, name: Literal["PEPBookmarks"]) -> PEPBookmarks: ...
    @overload
    def get_module(self, name: Literal["PrivateBookmarks"]) -> PrivateBookmarks: ...
    @overload
    def get_module(self, name: Literal["Captcha"]) -> Captcha: ...
    @overload
    def get_module(self, name: Literal["ChatMarkers"]) -> ChatMarkers: ...
    @overload
    def get_module(self, name: Literal["Chatstates"]) -> Chatstates: ...
    @overload
    def get_module(self, name: Literal["Correction"]) -> Correction: ...
    @overload
    def get_module(self, name: Literal["Delay"]) -> Delay: ...
    @overload
    def get_module(self, name: Literal["Delimiter"]) -> Delimiter: ...
    @overload
    def get_module(self, name: Literal["Discovery"]) -> Discovery: ...
    @overload
    def get_module(self, name: Literal["EME"]) -> EME: ...
    @overload
    def get_module(self, name: Literal["EntityCaps"]) -> EntityCaps: ...
    @overload
    def get_module(self, name: Literal["EntityTime"]) -> EntityTime: ...
    @overload
    def get_module(self, name: Literal["Hats"]) -> Hats: ...
    @overload
    def get_module(self, name: Literal["HTTPAuth"]) -> HTTPAuth: ...
    @overload
    def get_module(self, name: Literal["HTTPUpload"]) -> HTTPUpload: ...
    @overload
    def get_module(self, name: Literal["IBB"]) -> IBB: ...
    @overload
    def get_module(self, name: Literal["Idle"]) -> Idle: ...
    @overload
    def get_module(self, name: Literal["BaseIq"]) -> BaseIq: ...
    @overload
    def get_module(self, name: Literal["LastActivity"]) -> LastActivity: ...
    @overload
    def get_module(self, name: Literal["Location"]) -> Location: ...
    @overload
    def get_module(self, name: Literal["MAM"]) -> MAM: ...
    @overload
    def get_module(self, name: Literal["MDS"]) -> MDS: ...
    @overload
    def get_module(self, name: Literal["BaseMessage"]) -> BaseMessage: ...
    @overload
    def get_module(self, name: Literal["Mood"]) -> Mood: ...
    @overload
    def get_module(self, name: Literal["MUC"]) -> MUC: ...
    @overload
    def get_module(self, name: Literal["Moderation"]) -> Moderation: ...
    @overload
    def get_module(self, name: Literal["Muclumbus"]) -> Muclumbus: ...
    @overload
    def get_module(self, name: Literal["Nickname"]) -> Nickname: ...
    @overload
    def get_module(self, name: Literal["OMEMO"]) -> OMEMO: ...
    @overload
    def get_module(self, name: Literal["OOB"]) -> OOB: ...
    @overload
    def get_module(self, name: Literal["OpenPGP"]) -> OpenPGP: ...
    @overload
    def get_module(self, name: Literal["PGPLegacy"]) -> PGPLegacy: ...
    @overload
    def get_module(self, name: Literal["Ping"]) -> Ping: ...
    @overload
    def get_module(self, name: Literal["BasePresence"]) -> BasePresence: ...
    @overload
    def get_module(self, name: Literal["PubSub"]) -> PubSub: ...
    @overload
    def get_module(self, name: Literal["Reactions"]) -> Reactions: ...
    @overload
    def get_module(self, name: Literal["Receipts"]) -> Receipts: ...
    @overload
    def get_module(self, name: Literal["Register"]) -> Register: ...
    @overload
    def get_module(self, name: Literal["Replies"]) -> Replies: ...
    @overload
    def get_module(self, name: Literal["Retraction"]) -> Retraction: ...
    @overload
    def get_module(self, name: Literal["Roster"]) -> Roster: ...
    @overload
    def get_module(self, name: Literal["SecurityLabels"]) -> SecurityLabels: ...
    @overload
    def get_module(self, name: Literal["SoftwareVersion"]) -> SoftwareVersion: ...
    @overload
    def get_module(self, name: Literal["Tune"]) -> Tune: ...
    @overload
    def get_module(self, name: Literal["UserAvatar"]) -> UserAvatar: ...
    @overload
    def get_module(self, name: Literal["VCard4"]) -> VCard4: ...
    @overload
    def get_module(self, name: Literal["VCardAvatar"]) -> VCardAvatar: ...
    @overload
    def get_module(self, name: Literal["VCardTemp"]) -> VCardTemp: ...

    def get_module(self, name: NBXMPPModuleNameT) -> NBXMPPModuleT:
        return self._modules[name]

    def _register_modules(self):
        assert self._client is not None
        self._modules["Activity"] = Activity(self._client)
        self._modules["AdHoc"] = AdHoc(self._client)
        self._modules["Annotations"] = Annotations(self._client)
        self._modules["Attention"] = Attention(self._client)
        self._modules["BasePresence"] = BasePresence(self._client)
        self._modules["BaseMessage"] = BaseMessage(self._client)
        self._modules["BaseIq"] = BaseIq(self._client)
        self._modules["Blocking"] = Blocking(self._client)
        self._modules["Captcha"] = Captcha(self._client)
        self._modules["ChatMarkers"] = ChatMarkers(self._client)
        self._modules["Chatstates"] = Chatstates(self._client)
        self._modules["Correction"] = Correction(self._client)
        self._modules["Delay"] = Delay(self._client)
        self._modules["Delimiter"] = Delimiter(self._client)
        self._modules["Discovery"] = Discovery(self._client)
        self._modules["EME"] = EME(self._client)
        self._modules["EntityCaps"] = EntityCaps(self._client)
        self._modules["EntityTime"] = EntityTime(self._client)
        self._modules["Hats"] = Hats(self._client)
        self._modules["HTTPAuth"] = HTTPAuth(self._client)
        self._modules["HTTPUpload"] = HTTPUpload(self._client)
        self._modules["IBB"] = IBB(self._client)
        self._modules["Idle"] = Idle(self._client)
        self._modules["LastActivity"] = LastActivity(self._client)
        self._modules["Location"] = Location(self._client)
        self._modules["MAM"] = MAM(self._client)
        self._modules["MDS"] = MDS(self._client)
        self._modules["Moderation"] = Moderation(self._client)
        self._modules["Mood"] = Mood(self._client)
        self._modules["MUC"] = MUC(self._client)
        self._modules["Muclumbus"] = Muclumbus(self._client)
        self._modules["NativeBookmarks"] = NativeBookmarks(self._client)
        self._modules["Nickname"] = Nickname(self._client)
        self._modules["OMEMO"] = OMEMO(self._client)
        self._modules["OOB"] = OOB(self._client)
        self._modules["OpenPGP"] = OpenPGP(self._client)
        self._modules["PEPBookmarks"] = PEPBookmarks(self._client)
        self._modules["PGPLegacy"] = PGPLegacy(self._client)
        self._modules["Ping"] = Ping(self._client)
        self._modules["PrivateBookmarks"] = PrivateBookmarks(self._client)
        self._modules["PubSub"] = PubSub(self._client)
        self._modules["Reactions"] = Reactions(self._client)
        self._modules["Receipts"] = Receipts(self._client)
        self._modules["Register"] = Register(self._client)
        self._modules["Replies"] = Replies(self._client)
        self._modules["Retraction"] = Retraction(self._client)
        self._modules["Roster"] = Roster(self._client)
        self._modules["SecurityLabels"] = SecurityLabels(self._client)
        self._modules["SoftwareVersion"] = SoftwareVersion(self._client)
        self._modules["Tune"] = Tune(self._client)
        self._modules["UserAvatar"] = UserAvatar(self._client)
        self._modules["VCardAvatar"] = VCardAvatar(self._client)
        self._modules["VCardTemp"] = VCardTemp(self._client)
        self._modules["VCard4"] = VCard4(self._client)

        for instance in self._modules.values():
            for handler in instance.handlers:
                self.register_handler(handler)

    def reset_parser(self) -> None:
        if self._parser is not None:
            self._parser.dispatch = None
            self._parser.destroy()
            self._parser = None

        self._parser = NodeBuilder(dispatch_depth=2, finished=False)
        self._parser.dispatch = self.dispatch

    def replace_non_character(self, data: str) -> str:
        return INVALID_XML_RX.sub("\ufffd", data)

    def process_data(self, data: str) -> None:
        # Parse incoming data

        data = self.replace_non_character(data)

        if self._client.is_websocket:
            stanza = Node(node=data)
            if is_websocket_stream_error(stanza):
                for tag in stanza.getChildren():
                    name = tag.getName()
                    if name != "text" and tag.getNamespace() == Namespace.XMPP_STREAMS:
                        self._websocket_stream_error = name

            elif is_websocket_close(stanza):
                self._log.info("Stream <close> received")
                self.notify("stream-end", self._websocket_stream_error)
                return

            self.dispatch(stanza)
            return

        try:
            self._parser.Parse(data)
        except (ExpatError, ValueError) as error:
            self._log.error("XML parsing error: %s", error)
            self.notify("parsing-error", str(error))
            return

        # end stream:stream tag received
        if self._parser.has_received_endtag():
            self._log.info("End of stream: %s", self._parser.stream_error)
            self.notify("stream-end", self._parser.stream_error)
            return

    def _register_namespace(self, xmlns: str) -> None:
        """
        Setup handler structure for namespace
        """
        self._log.debug('Register namespace "%s"', xmlns)
        self._handlers[xmlns] = {}
        self._register_protocol("error", Protocol, xmlns=xmlns)
        self._register_protocol("unknown", Protocol, xmlns=xmlns)
        self._register_protocol("default", Protocol, xmlns=xmlns)

    def _register_protocol(
        self, tag_name: str, protocol: Any, xmlns: str | None = None
    ) -> None:
        """
        Register protocol for top level tag names
        """
        if xmlns is None:
            xmlns = Namespace.CLIENT
        self._log.debug('Register protocol "%s (%s)" as %s', tag_name, xmlns, protocol)
        self._handlers[xmlns][tag_name] = {"type": protocol, "default": []}

    def register_handler(self, handler: StanzaHandler) -> None:
        """
        Register handler
        """

        xmlns = handler.xmlns or Namespace.CLIENT

        typ = handler.typ
        if not typ and not handler.ns:
            typ = "default"

        self._log.debug(
            'Register handler %s for "%s" type->%s ns->%s(%s) priority->%s',
            handler.callback,
            handler.name,
            typ,
            handler.ns,
            xmlns,
            handler.priority,
        )

        if xmlns not in self._handlers:
            self._register_namespace(xmlns)
        if handler.name not in self._handlers[xmlns]:
            self._register_protocol(handler.name, Protocol, xmlns)

        specific = typ + handler.ns
        if specific not in self._handlers[xmlns][handler.name]:
            self._handlers[xmlns][handler.name][specific] = []

        self._handlers[xmlns][handler.name][specific].append(
            {
                "func": handler.callback,
                "priority": handler.priority,
                "specific": specific,
            }
        )

    def unregister_handler(self, handler: StanzaHandler) -> None:
        """
        Unregister handler
        """

        xmlns = handler.xmlns or Namespace.CLIENT

        typ = handler.typ
        if not typ and not handler.ns:
            typ = "default"

        specific = typ + handler.ns
        try:
            self._handlers[xmlns][handler.name][specific]
        except KeyError:
            return

        for handler_dict in self._handlers[xmlns][handler.name][specific]:
            if handler_dict["func"] != handler.callback:
                continue

            try:
                self._handlers[xmlns][handler.name][specific].remove(handler_dict)
            except ValueError:
                self._log.warning(
                    'Unregister failed: %s for "%s" type->%s ns->%s(%s)',
                    handler.callback,
                    handler.name,
                    typ,
                    handler.ns,
                    xmlns,
                )
            else:
                self._log.debug(
                    'Unregister handler %s for "%s" type->%s ns->%s(%s)',
                    handler.callback,
                    handler.name,
                    typ,
                    handler.ns,
                    xmlns,
                )

    def _default_handler(self, stanza: Protocol) -> None:
        """
        Return stanza back to the sender with <feature-not-implemented/> error
        """
        if stanza.getType() in ("get", "set"):
            self._client.send_stanza(Error(stanza, ERR_FEATURE_NOT_IMPLEMENTED))

    def dispatch(self, stanza: Protocol) -> None:
        self.notify("before-dispatch", stanza)

        if self._dispatch_callback is not None:
            name = stanza.getName()
            protocol_class = self._stanza_types.get(name)
            if protocol_class is not None:
                stanza = protocol_class(node=stanza)
            self._dispatch_callback(stanza)
            return

        # Count stanza
        self._client._smacks.count_incoming(stanza.getName())

        name = stanza.getName()
        xmlns = stanza.getNamespace()

        if xmlns not in self._handlers:
            self._log.warning("Unknown namespace: %s", xmlns)
            xmlns = "unknown"

        if name not in self._handlers[xmlns]:
            self._log.warning("Unknown stanza: %s", stanza)
            name = "unknown"

        # Convert simplexml to Protocol object
        try:
            stanza = self._handlers[xmlns][name]["type"](node=stanza)
        except InvalidJid:
            self._log.warning("Invalid JID, ignoring stanza")
            self._log.warning(stanza)
            return

        own_jid = self._client.get_bound_jid()
        properties = get_properties_struct(name, own_jid)

        if name == "iq":
            if stanza.getFrom() is None and own_jid is not None:
                stanza.setFrom(own_jid.bare)

        if name == "message":
            # https://tools.ietf.org/html/rfc6120#section-8.1.1.1
            # If the stanza does not include a 'to' address then the client MUST
            # treat it as if the 'to' address were included with a value of the
            # client's full JID.

            to = stanza.getTo()
            if to is None:
                stanza.setTo(own_jid)

            elif not to.bare_match(own_jid):
                self._log.warning("Message addressed to someone else: %s", stanza)
                return

            if stanza.getFrom() is None:
                stanza.setFrom(own_jid.bare)

            # Unwrap carbon
            try:
                stanza, properties.carbon = unwrap_carbon(stanza, own_jid)
            except (InvalidFrom, InvalidJid) as exc:
                self._log.warning(exc)
                self._log.warning(stanza)
                return
            except NodeProcessed as exc:
                self._log.info(exc)
                return

            # Unwrap mam
            try:
                stanza, properties.mam = unwrap_mam(stanza, own_jid)
            except (InvalidStanza, InvalidJid) as exc:
                self._log.warning(exc)
                self._log.warning(stanza)
                return

        typ = stanza.getType()
        if not typ:
            if name == "message":
                typ = "normal"
            elif name == "presence":
                typ = "available"
            else:
                typ = ""

        # Process callbacks
        _id = stanza.getID()
        func, _timeout, user_data = self._id_callbacks.pop(_id, (None, None, {}))
        if user_data is None:
            user_data = {}

        if func is not None:
            try:
                func(self._client, stanza, **user_data)
            except Exception:
                self._log.exception("Error while handling stanza")
            return

        props = stanza.getProperties()
        self._log.debug("type: %s, properties: %s", typ, props)

        chain = self._build_handler_chain(xmlns, name, typ, props)

        try:
            self._execute_handler_chain(chain, stanza, properties)
        except StanzaDecrypted:
            props = stanza.getProperties()
            self._log.debug("type: %s, properties after decryption: %s", typ, props)
            chain = self._build_handler_chain(
                xmlns, name, typ, props, after_decryption=True
            )
            self._execute_handler_chain(chain, stanza, properties)

    def _build_handler_chain(
        self,
        xmlns: str,
        name: str,
        typ: str,
        props: Any,
        *,
        after_decryption: bool = False,
    ) -> list[dict[str, Any]]:

        # Gather specifics depending on stanza properties
        specifics = ["default"]
        if typ and typ in self._handlers[xmlns][name]:
            specifics.append(typ)

        for prop in props:
            if prop in self._handlers[xmlns][name]:
                specifics.append(prop)

            if typ and typ + prop in self._handlers[xmlns][name]:
                specifics.append(typ + prop)

        # Create the handler chain
        chain: list[dict[str, Any]] = []
        chain += self._handlers[xmlns]["default"]["default"]
        for specific in specifics:
            chain += self._handlers[xmlns][name][specific]

        # Sort chain with priority
        chain.sort(key=lambda x: x["priority"])

        if after_decryption:
            # Filter everything out which was executed before decryption
            # so it is not executed again
            chain = list(filter(lambda x: x["priority"] > 9, chain))

        return chain

    def _execute_handler_chain(
        self, chain: Any, stanza: Protocol, properties: Any
    ) -> None:

        for handler in chain:
            self._log.info("Call handler: %s", handler["func"].__qualname__)
            try:
                handler["func"](self._client, stanza, properties)
            except NodeProcessed:
                return
            except StanzaDecrypted:
                raise
            except Exception:
                self._log.exception("Handler exception:")
                return

        # Stanza was not processed call default handler
        self._default_handler(stanza)

    def add_callback_for_id(
        self, id_: str, func: Callable[..., Any], timeout: float | None, user_data: Any
    ) -> None:
        if timeout is not None and self._timeout_id is None:
            self._log.info("Add timeout check")
            self._timeout_id = GLib.timeout_add_seconds(1, self._timeout_check)
            timeout = time.monotonic() + timeout
        self._id_callbacks[id_] = (func, timeout, user_data)

    def _timeout_check(self) -> bool:
        self._log.info("Run timeout check")
        timeouts: dict[str, tuple[Callable[..., Any], float | None, Any]] = {}
        for id_, data in self._id_callbacks.items():
            if data[1] is not None:
                timeouts[id_] = data

        if not timeouts:
            self._log.info("Remove timeout check, no timeouts scheduled")
            self._timeout_id = None
            return False

        for id_, data in timeouts.items():
            func, timeout, user_data = data

            if user_data is None:
                user_data = {}

            if timeout < time.monotonic():
                self._id_callbacks.pop(id_)
                func(self._client, None, **user_data)
        return True

    def _remove_timeout_source(self) -> None:
        if self._timeout_id is not None:
            GLib.source_remove(self._timeout_id)
            self._timeout_id = None

    def remove_iq_callback(self, id_: str) -> None:
        self._id_callbacks.pop(id_, None)

    def clear_iq_callbacks(self) -> None:
        self._log.info("Clear IQ callbacks")
        self._id_callbacks.clear()

    def cleanup(self) -> None:
        self._client = None
        self._modules = {}
        self._parser = None
        self.clear_iq_callbacks()
        self._dispatch_callback = None
        self._handlers.clear()
        self._remove_timeout_source()
        self.remove_subscriptions()