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

muc.py « modules « common « gajim - dev.gajim.org/gajim/gajim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4521cacbdb7f050de9a993c2f60927f0820fc613 (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
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
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
# 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/>.

# XEP-0045: Multi-User Chat
# XEP-0249: Direct MUC Invitations

from __future__ import annotations

from typing import Any
from typing import Optional

import logging
from collections import defaultdict
import time

import nbxmpp
from nbxmpp.const import InviteType
from nbxmpp.const import PresenceType
from nbxmpp.const import StatusCode
from nbxmpp.errors import StanzaError
from nbxmpp.modules.dataforms import SimpleDataForm
from nbxmpp.namespaces import Namespace
from nbxmpp.protocol import JID
from nbxmpp.protocol import Message
from nbxmpp.protocol import Presence
from nbxmpp.structs import CommonError
from nbxmpp.structs import DiscoInfo
from nbxmpp.structs import MessageProperties
from nbxmpp.structs import PresenceProperties
from nbxmpp.structs import StanzaHandler
from nbxmpp.task import Task

from gi.repository import GLib

from gajim.common import app
from gajim.common import helpers
from gajim.common import types
from gajim.common.const import ClientState, KindConstant
from gajim.common.const import MUCJoinedState
from gajim.common.events import MessageModerated
from gajim.common.events import MucAdded
from gajim.common.events import MucDecline
from gajim.common.events import MucInvitation
from gajim.common.helpers import AdditionalDataDict
from gajim.common.helpers import get_default_muc_config
from gajim.common.helpers import get_group_chat_nick
from gajim.common.structs import MUCData
from gajim.common.structs import MUCPresenceData
from gajim.common.modules.bits_of_binary import store_bob_data
from gajim.common.modules.base import BaseModule
from gajim.common.modules.contacts import GroupchatContact

log = logging.getLogger('gajim.c.m.muc')


class MUC(BaseModule):

    _nbxmpp_extends = 'MUC'
    _nbxmpp_methods = [
        'get_affiliation',
        'set_role',
        'set_affiliation',
        'set_config',
        'set_subject',
        'cancel_config',
        'send_captcha',
        'cancel_captcha',
        'decline',
        'invite',
        'request_config',
        'request_voice',
        'approve_voice_request',
        'destroy',
        'request_disco_info',
        'retract_message'
    ]

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

        self.handlers = [
            StanzaHandler(name='presence',
                          callback=self._on_muc_user_presence,
                          typ='available',
                          ns=Namespace.MUC_USER,
                          priority=49),
            StanzaHandler(name='presence',
                          callback=self._on_muc_user_presence,
                          typ='unavailable',
                          ns=Namespace.MUC_USER,
                          priority=49),
            StanzaHandler(name='presence',
                          callback=self._on_error_presence,
                          typ='error',
                          priority=49),
            StanzaHandler(name='message',
                          callback=self._on_subject_change,
                          typ='groupchat',
                          priority=49),
            StanzaHandler(name='message',
                          callback=self._on_moderation,
                          ns=Namespace.FASTEN,
                          typ='groupchat',
                          priority=49),
            StanzaHandler(name='message',
                          callback=self._on_config_change,
                          ns=Namespace.MUC_USER,
                          priority=49),
            StanzaHandler(name='message',
                          callback=self._on_invite_or_decline,
                          typ='normal',
                          ns=Namespace.MUC_USER,
                          priority=49),
            StanzaHandler(name='message',
                          callback=self._on_invite_or_decline,
                          ns=Namespace.CONFERENCE,
                          priority=49),
            StanzaHandler(name='message',
                          callback=self._on_captcha_challenge,
                          ns=Namespace.CAPTCHA,
                          priority=49),
            StanzaHandler(name='message',
                          callback=self._on_voice_request,
                          ns=Namespace.DATA,
                          priority=49)
        ]

        self._con.connect_signal('state-changed',
                                 self._on_client_state_changed)
        self._con.connect_signal('resume-failed',
                                 self._on_client_resume_failed)

        self._rejoin_muc: set[str] = set()
        self._join_timeouts: dict[str, int] = {}
        self._rejoin_timeouts: dict[str, int] = {}
        self._muc_service_jid = None
        self._joined_users: defaultdict[
            str, dict[str, MUCPresenceData]] = defaultdict(dict)
        self._mucs: dict[str, MUCData] = {}
        self._muc_nicknames = {}

    def _on_resume_failed(self,
                          _client: types.Client,
                          _signal_name: str
                          ) -> None:
        self._reset_presence()

    def _on_state_changed(self,
                          _client: types.Client,
                          _signal_name: str,
                          state: ClientState
                          ) -> None:
        if state.is_disconnected:
            self._reset_presence()

    @property
    def supported(self) -> bool:
        return self._muc_service_jid is not None

    @property
    def service_jid(self):
        return self._muc_service_jid

    def pass_disco(self, info: DiscoInfo) -> None:
        if info.is_gateway:
            return

        for identity in info.identities:
            if identity.category != 'conference':
                continue
            if identity.type != 'text':
                continue
            if Namespace.MUC in info.features:
                self._log.info('Discovered MUC: %s', info.jid)
                self._muc_service_jid = info.jid
                raise nbxmpp.NodeProcessed

    def get_muc_data(self, room_jid: str) -> Optional[MUCData]:
        return self._mucs.get(room_jid)

    def set_password(self, room_jid: str, password: str) -> None:
        muc_data = self.get_muc_data(room_jid)
        muc_data.password = password

    def _get_mucs_with_state(self, states: list[MUCJoinedState]):
        return [muc for muc in self._mucs.values() if muc.state in states]

    def _set_muc_state(self, room_jid: str, state: MUCJoinedState) -> None:
        try:
            muc = self._mucs[room_jid]
        except KeyError:
            raise ValueError('set_muc_state() called '
                             'on unknown muc: %s' % room_jid)

        if muc.state == state:
            return

        self._log.info('Set MUC state: %s %s', room_jid, state)

        muc.state = state
        contact = self._get_contact(room_jid, groupchat=True)
        contact.notify('state-changed')

    def _reset_state(self) -> None:
        for room_jid in list(self._rejoin_timeouts.keys()):
            self._remove_rejoin_timeout(room_jid)

        for room_jid in list(self._join_timeouts.keys()):
            self._remove_join_timeout(room_jid)

        for muc in self._mucs.values():
            self._joined_users.pop(muc.jid, None)
            self._set_muc_state(muc.jid, MUCJoinedState.NOT_JOINED)
            room = self._get_contact(muc.jid)
            room.set_not_joined()
            room.notify('room-left')

        self._joined_users.clear()

    def _create_muc_data(self,
                         room_jid: str,
                         nick: Optional[str],
                         password: Optional[str],
                         config: Optional[dict[str, Any]]
                         ) -> MUCData:
        if not nick:
            nick = get_group_chat_nick(self._account, room_jid)

        # Fetch data from bookmarks
        bookmark = self._con.get_module('Bookmarks').get_bookmark(room_jid)
        if bookmark is not None:
            if bookmark.password is not None:
                password = bookmark.password

        return MUCData(room_jid, nick, password, config)

    def join(self,
             jid: str,
             nick: Optional[str] = None,
             password: Optional[str] = None,
             config: Optional[dict[str, Any]] = None
             ) -> None:
        if not app.account_is_available(self._account):
            return

        self._con.get_module('Contacts').add_contact(jid, groupchat=True)

        muc_data = self._mucs.get(jid)
        if muc_data is None:
            muc_data = self._create_muc_data(jid, nick, password, config)
            self._mucs[jid] = muc_data
            self._push_muc_added_event(jid)

        elif nick is not None:
            # Currently MUCData is never discarded so if it exists it contains
            # the nickname of a previous join. The user may chose now on a new
            # join a different nickname, so update MUCData here.
            muc_data.nick = nick

        if muc_data.state not in (MUCJoinedState.NOT_JOINED,
                                  MUCJoinedState.PASSWORD_REQUEST):
            self._log.warning('Can’t join MUC %s, state: %s',
                              jid, muc_data.state)
            return

        disco_info = app.storage.cache.get_last_disco_info(muc_data.jid,
                                                           max_age=60)
        if disco_info is None:
            self._set_muc_state(muc_data.jid, MUCJoinedState.JOINING)
            self._con.get_module('Discovery').disco_muc(
                muc_data.jid,
                callback=self._on_disco_result)
        else:
            self._join(muc_data)

    def create(self, jid: str, config: dict[str, Any]) -> None:
        if not app.account_is_available(self._account):
            return

        self._con.get_module('Contacts').add_contact(jid, groupchat=True)
        muc_data = self._create_muc_data(jid, None, None, config)
        self._mucs[jid] = muc_data
        self._create(muc_data)
        self._push_muc_added_event(jid)

    def _push_muc_added_event(self, jid: str) -> None:
        app.ged.raise_event(MucAdded(account=self._account,
                                     jid=JID.from_string(jid)))

    def _on_disco_result(self, task: Task) -> None:
        try:
            result = task.finish()
        except StanzaError as error:
            self._log.info('Disco %s failed: %s', error.jid, error.get_text())

            room = self._get_contact(error.jid.bare)
            room.notify('room-join-failed', error)
            return

        muc_data = self._mucs.get(result.info.jid)
        if muc_data is None:
            self._log.warning('MUC Data not found, join aborted')
            return
        self._join(muc_data)

    def _join(self, muc_data: MUCData) -> None:
        presence = self._con.get_module('Presence').get_presence(
            muc_data.occupant_jid,
            show=self._con.status,
            status=self._con.status_message)

        muc_x = presence.setTag(Namespace.MUC + ' x')
        muc_x.setTag('history', {'maxchars': '0'})

        if muc_data.password is not None:
            muc_x.setTagData('password', muc_data.password)

        self._log.info('Join MUC: %s', muc_data.jid)
        self._set_muc_state(muc_data.jid, MUCJoinedState.JOINING)
        self._con.send_stanza(presence)

    def _rejoin(self, room_jid: str) -> bool:
        muc_data = self._mucs[room_jid]
        if muc_data.state.is_not_joined:
            self._log.info('Rejoin %s', room_jid)
            self._join(muc_data)
        return True

    def _create(self, muc_data: MUCData) -> None:
        presence = self._con.get_module('Presence').get_presence(
            muc_data.occupant_jid,
            show=self._con.status,
            status=self._con.status_message)

        presence.setTag(Namespace.MUC + ' x')

        self._log.info('Create MUC: %s', muc_data.jid)
        self._set_muc_state(muc_data.jid, MUCJoinedState.CREATING)
        self._con.send_stanza(presence)

    def leave(self,
              room_jid: str,
              reason: Optional[str] = None
              ) -> None:
        self._log.info('Leave MUC: %s', room_jid)

        self._con.get_module('Bookmarks').modify(room_jid, autojoin=False)

        muc_data = self._mucs.get(room_jid)
        if muc_data is None:
            return

        if muc_data.state.is_not_joined:
            return

        self._remove_join_timeout(room_jid)
        self._remove_rejoin_timeout(room_jid)

        self._con.get_module('Presence').send_presence(
            muc_data.occupant_jid,
            typ='unavailable',
            status=reason,
            caps=False)

        self._set_muc_state(room_jid, MUCJoinedState.NOT_JOINED)
        room = self._get_contact(room_jid)
        room.set_not_joined()
        room.notify('room-left')

    def configure_room(self, room_jid: str) -> None:
        self._nbxmpp('MUC').request_config(room_jid,
                                           callback=self._on_room_config)

    def _on_room_config(self, task: Task) -> None:
        try:
            result = task.finish()
        except StanzaError as error:
            self._log.info(error)

            room = self._get_contact(error.jid.bare)
            room.notfiy('room-config-failed', error)
            return

        self._log.info('Configure room: %s', result.jid)

        muc_data = self._mucs[result.jid]
        self._apply_config(result.form, muc_data.config)
        self.set_config(result.jid,
                        result.form,
                        callback=self._on_config_result)

    @staticmethod
    def _apply_config(form: SimpleDataForm,
                      config: Optional[dict[str, Any]] = None
                      ) -> None:
        default_config = get_default_muc_config()
        if config is not None:
            default_config.update(config)
        for var, value in default_config.items():
            try:
                field = form[var]
            except KeyError:
                pass
            else:
                field.value = value

    def _on_config_result(self, task: Task) -> None:
        try:
            result = task.finish()
        except StanzaError as error:
            self._log.info(error)

            room = self._get_contact(error.jid.bare)
            room.notfiy('room-config-failed', error)
            return

        self._con.get_module('Discovery').disco_muc(
            result.jid, callback=self._on_disco_result_after_config)

        # If this is an automatic room creation
        try:
            invites = app.automatic_rooms[self._account][result.jid]['invities']
        except KeyError:
            return

        user_list = {}
        for jid in invites:
            user_list[jid] = {'affiliation': 'member'}
        self.set_affiliation(result.jid, user_list)

        for jid in invites:
            self.invite(result.jid, JID.from_string(jid))

    def _on_disco_result_after_config(self, task: Task) -> None:
        try:
            result = task.finish()
        except StanzaError as error:
            self._log.info('Disco %s failed: %s', error.jid, error.get_text())
            return

        jid = result.info.jid
        muc_data = self._mucs[jid]
        self._room_join_complete(muc_data)

        self._log.info('Configuration finished: %s', jid)

        room = self._get_contact(jid.bare)
        room.notify('room-config-finished')

    def update_presence(self) -> None:
        mucs = self._get_mucs_with_state([MUCJoinedState.JOINED,
                                          MUCJoinedState.JOINING])

        status, message, idle = self._con.get_presence_state()
        for muc_data in mucs:
            self._con.get_module('Presence').send_presence(
                muc_data.occupant_jid,
                show=status,
                status=message,
                idle_time=idle)

    def change_nick(self, room_jid: str, new_nick: str) -> None:
        status, message, _idle = self._con.get_presence_state()
        self._con.get_module('Presence').send_presence(
            f'{room_jid}/{new_nick}',
            show=status,
            status=message)

    def _on_error_presence(self,
                           _con: types.xmppClient,
                           stanza: Presence,
                           properties: PresenceProperties
                           ) -> None:

        room_jid = properties.jid.bare
        muc_data = self._mucs.get(room_jid)
        if muc_data is None:
            return

        if properties.jid.resource != muc_data.nick:
            self._log.warning('Unknown error presence')
            self._log.warning(stanza)
            return

        room = self._get_contact(room_jid)

        if muc_data.state == MUCJoinedState.JOINING:
            if properties.error.condition == 'conflict':
                self._remove_rejoin_timeout(room_jid)
                muc_data.nick += '_'
                self._log.info('Nickname conflict: %s change to %s',
                               muc_data.jid, muc_data.nick)
                self._join(muc_data)

            elif properties.error.condition == 'not-authorized':
                self._remove_rejoin_timeout(room_jid)
                self._set_muc_state(room_jid, MUCJoinedState.PASSWORD_REQUEST)
                room.notify('room-password-required', properties)

            else:
                self._set_muc_state(room_jid, MUCJoinedState.NOT_JOINED)
                if room_jid not in self._rejoin_muc:
                    muc_data.error = 'join-failed'
                    assert isinstance(properties.error, CommonError)
                    muc_data.error_text = helpers.to_user_string(
                        properties.error)
                    room.notify('room-join-failed', properties.error)

        elif muc_data.state == MUCJoinedState.CREATING:
            self._set_muc_state(room_jid, MUCJoinedState.NOT_JOINED)
            muc_data.error = 'creation-failed'
            assert isinstance(properties.error, CommonError)
            muc_data.error_text = helpers.to_user_string(properties.error)
            room.notify('room-creation-failed', properties)

        elif muc_data.state == MUCJoinedState.CAPTCHA_REQUEST:
            self._set_muc_state(room_jid, MUCJoinedState.CAPTCHA_FAILED)
            self._set_muc_state(room_jid, MUCJoinedState.NOT_JOINED)
            muc_data.error = 'captcha-failed'
            assert isinstance(properties.error, CommonError)
            muc_data.error_text = helpers.to_user_string(properties.error)
            room.notify('room-captcha-error', properties.error)

        elif muc_data.state == MUCJoinedState.CAPTCHA_FAILED:
            self._set_muc_state(room_jid, MUCJoinedState.NOT_JOINED)

        else:
            room.notify('room-presence-error', properties)

    def _on_muc_user_presence(self,
                              _con: types.xmppClient,
                              stanza: Presence,
                              properties: PresenceProperties
                              ) -> None:

        room_jid = str(properties.muc_jid)
        if room_jid not in self._mucs:
            self._log.warning('Presence from unknown MUC')
            self._log.warning(stanza)
            return

        muc_data = self._mucs[room_jid]
        occupant = self._get_contact(properties.jid, groupchat=True)
        room = self._get_contact(properties.jid.bare)

        if properties.is_muc_destroyed:
            self._log.info('MUC destroyed: %s', room_jid)
            self._remove_join_timeout(room_jid)
            self._set_muc_state(room_jid, MUCJoinedState.NOT_JOINED)
            self._con.get_module('Bookmarks').remove(room_jid)
            room.set_not_joined()
            room.notify('room-destroyed', properties)
            return

        if properties.is_nickname_changed:
            if properties.is_muc_self_presence:
                muc_data.nick = properties.muc_user.nick
                self._con.get_module('Bookmarks').modify(muc_data.jid,
                                                         nick=muc_data.nick)

            initiator = 'Server' if properties.is_nickname_modified else 'User'
            self._log.info('%s nickname changed: %s to %s',
                           initiator,
                           properties.jid,
                           properties.muc_user.nick)

            # We receive only the unavailable presence here, so we take
            # the current presence and create a new contact with it, before we
            # update the presence.
            nickname = properties.muc_user.nick
            new_occupant = room.add_resource(nickname)
            new_occupant.set_presence(occupant.presence)
            self._joined_users[room.jid][nickname] = occupant.presence

            presence = self._process_user_presence(properties)
            occupant.update_presence(presence, properties)

            room.notify('user-nickname-changed',
                        occupant,
                        new_occupant,
                        properties)
            return

        is_joined = self._is_user_joined(properties.jid)
        if not is_joined and properties.type.is_available:
            if properties.is_muc_self_presence:
                self._log.info('Self presence: %s', properties.jid)
                if muc_data.state == MUCJoinedState.JOINING:
                    if (properties.is_nickname_modified or
                            muc_data.nick != properties.muc_nickname):
                        muc_data.nick = properties.muc_nickname
                        self._log.info('Server modified nickname to: %s',
                                       properties.muc_nickname)

                elif muc_data.state == MUCJoinedState.CREATING:
                    if properties.is_new_room:
                        self.configure_room(room_jid)

                self._start_join_timeout(room_jid)

            presence = self._process_user_presence(properties)
            occupant.update_presence(presence, properties)
            return

        if properties.is_muc_self_presence and properties.is_kicked:
            self._set_muc_state(room_jid, MUCJoinedState.NOT_JOINED)
            room.set_not_joined()
            room.notify('room-kicked', properties)
            status_codes = properties.muc_status_codes or []
            if StatusCode.REMOVED_SERVICE_SHUTDOWN in status_codes:
                self._start_rejoin_timeout(room_jid)
            return

        if properties.is_muc_self_presence and properties.type.is_unavailable:
            # Its not a kick, so this is the reflection of our own
            # unavailable presence, because we left the MUC
            return

        try:
            presence = self._process_user_presence(properties)
        except KeyError:
            # Sometimes it seems to happen that we get unavailable presence
            # from occupants we don’t know
            log.warning('Unexpected presence received')
            log.warning(stanza)
            return

        occupant.update_presence(presence, properties)

    def _process_user_presence(self,
                               properties: PresenceProperties
                               ) -> MUCPresenceData:
        jid = properties.jid
        muc_presence = MUCPresenceData.from_presence(properties)
        if not muc_presence.available:
            self._joined_users[jid.bare].pop(jid.resource)
        else:
            self._joined_users[jid.bare][jid.resource] = muc_presence
        return muc_presence

    def _is_user_joined(self, jid: Optional[JID]) -> bool:
        try:
            self._joined_users[jid.bare][jid.resource]
        except KeyError:
            return False
        return True

    def get_joined_users(self, jid: str) -> list[str]:
        return list(self._joined_users[jid].keys())

    def _start_rejoin_timeout(self, room_jid: str) -> None:
        self._remove_rejoin_timeout(room_jid)
        self._rejoin_muc.add(room_jid)
        self._log.info('Start rejoin timeout for: %s', room_jid)
        id_ = GLib.timeout_add_seconds(2, self._rejoin, room_jid)
        self._rejoin_timeouts[room_jid] = id_

    def _remove_rejoin_timeout(self, room_jid: str) -> None:
        self._rejoin_muc.discard(room_jid)
        id_ = self._rejoin_timeouts.get(room_jid)
        if id_ is not None:
            self._log.info('Remove rejoin timeout for: %s', room_jid)
            GLib.source_remove(id_)
            del self._rejoin_timeouts[room_jid]

    def _start_join_timeout(self, room_jid: str) -> None:
        self._remove_join_timeout(room_jid)
        self._log.info('Start join timeout for: %s', room_jid)
        id_ = GLib.timeout_add_seconds(
            10, self._fake_subject_change, room_jid)
        self._join_timeouts[room_jid] = id_

    def _remove_join_timeout(self, room_jid: str) -> None:
        id_ = self._join_timeouts.get(room_jid)
        if id_ is not None:
            self._log.info('Remove join timeout for: %s', room_jid)
            GLib.source_remove(id_)
            del self._join_timeouts[room_jid]

    def _log_muc_event(self,
                       event_name: str,
                       properties: PresenceProperties
                       ) -> None:
        # TODO CURRENTLY NOT USED
        if event_name not in ['muc-user-joined',
                              'muc-user-left',
                              'muc-user-status-show-changed']:
            return

        if not app.settings.get('log_contact_status_changes'):
            return

        additional_data = AdditionalDataDict()
        if properties.muc_user is not None:
            if properties.muc_user.jid is not None:
                additional_data.set_value(
                    'gajim', 'real_jid', str(properties.muc_user.jid))

        # TODO: Refactor
        if properties.type == PresenceType.UNAVAILABLE:
            show = 'offline'
        else:
            show = properties.show.value
        show = app.storage.archive.convert_show_values_to_db_api_values(show)

        app.storage.archive.insert_into_logs(
            self._account,
            properties.jid.bare,
            properties.timestamp,
            KindConstant.GCSTATUS,
            contact_name=properties.muc_nickname,
            message=properties.status or None,
            show=show,
            additional_data=additional_data)

    def _on_subject_change(self,
                           _con: types.xmppClient,
                           _stanza: Message,
                           properties: MessageProperties
                           ) -> None:
        if not properties.is_muc_subject:
            return

        room_jid = properties.jid.bare

        muc_data = self._mucs.get(room_jid)
        if muc_data is None:
            self._log.warning('No MUCData found for %s', room_jid)
            return

        muc_subject = properties.muc_subject
        if muc_subject is not None and muc_subject.timestamp is None:
            muc_subject = muc_subject._replace(timestamp=time.time())

        muc_data.subject = muc_subject
        room = self._get_contact(room_jid)
        room.notify('room-subject', muc_subject)

        if muc_data.state == MUCJoinedState.JOINING:
            self._room_join_complete(muc_data)
            room.notify('room-joined')

        raise nbxmpp.NodeProcessed

    def _on_moderation(self,
                       _con: types.xmppClient,
                       _stanza: Message,
                       properties: MessageProperties
                       ) -> None:
        if not properties.is_moderation:
            return

        app.storage.archive.update_additional_data(
            self._account, properties.moderation.stanza_id, properties)

        app.ged.raise_event(
            MessageModerated(account=self._account,
                             jid=properties.jid,
                             moderation=properties.moderation))

        raise nbxmpp.NodeProcessed

    def _fake_subject_change(self, room_jid: str) -> None:
        # This is for servers which don’t send empty subjects as part of the
        # event order on joining a MUC. For example jabber.ru
        self._log.warning('Fake subject received for %s', room_jid)
        del self._join_timeouts[room_jid]
        room = self._get_contact(room_jid)
        room.notify('room-joined')

    def cancel_password_request(self, room_jid: str) -> None:
        self._set_muc_state(room_jid, MUCJoinedState.NOT_JOINED)

    def _room_join_complete(self, muc_data: MUCData):
        self._remove_join_timeout(muc_data.jid)
        self._set_muc_state(muc_data.jid, MUCJoinedState.JOINED)
        self._remove_rejoin_timeout(muc_data.jid)

        # We successfully joined a MUC, set add bookmark with autojoin
        self._con.get_module('Bookmarks').add_or_modify(
            muc_data.jid,
            autojoin=True,
            password=muc_data.password,
            nick=muc_data.nick)

        disco_info = app.storage.cache.get_last_disco_info(muc_data.jid)
        if disco_info.has_mam_2:
            self._con.get_module('MAM').request_archive_on_muc_join(
                muc_data.jid)

    def _on_voice_request(self,
                          _con: types.xmppClient,
                          _stanza: Message,
                          properties: MessageProperties
                          ) -> None:
        if not properties.is_voice_request:
            return

        room = self._get_contact(properties.jid.bare)
        room.notify('room-voice-request', properties)

        raise nbxmpp.NodeProcessed

    def _on_captcha_challenge(self,
                              _con: types.xmppClient,
                              _stanza: Message,
                              properties: MessageProperties
                              ) -> None:
        if not properties.is_captcha_challenge:
            return

        if properties.is_mam_message:
            # Some servers store captcha challenges in MAM, don’t process them
            self._log.warning('Ignore captcha challenge received from MAM')
            raise nbxmpp.NodeProcessed

        muc_data = self._mucs.get(properties.jid)
        if muc_data is None:
            return

        if muc_data.state != MUCJoinedState.JOINING:
            self._log.warning('Received captcha request but state != %s',
                              MUCJoinedState.JOINING)
            return

        self._log.info('Captcha challenge received from %s', properties.jid)

        assert properties.captcha is not None
        store_bob_data(properties.captcha.bob_data)
        muc_data.captcha_id = properties.id
        muc_data.captcha_form = properties.captcha.form

        self._set_muc_state(properties.jid, MUCJoinedState.CAPTCHA_REQUEST)
        self._remove_rejoin_timeout(properties.jid)

        room = self._get_contact(properties.jid.bare)
        room.notify('room-captcha-challenge', properties)

        raise nbxmpp.NodeProcessed

    def cancel_captcha(self, room_jid: str) -> None:
        muc_data = self._mucs.get(room_jid)
        if muc_data is None:
            return

        if muc_data.captcha_id is None:
            self._log.warning('No captcha message id available')
            return
        self._nbxmpp('MUC').cancel_captcha(room_jid, muc_data.captcha_id)
        self._set_muc_state(room_jid, MUCJoinedState.CAPTCHA_FAILED)
        self._set_muc_state(room_jid, MUCJoinedState.NOT_JOINED)

    def send_captcha(self,
                     room_jid: str,
                     form_node: SimpleDataForm
                     ) -> None:
        self._set_muc_state(room_jid, MUCJoinedState.JOINING)
        self._nbxmpp('MUC').send_captcha(room_jid,
                                         form_node,
                                         callback=self._on_captcha_result)

    def _on_captcha_result(self, task: Task) -> None:
        try:
            task.finish()
        except StanzaError as error:
            muc_data = self._mucs.get(error.jid)
            if muc_data is None:
                return
            self._set_muc_state(error.jid, MUCJoinedState.CAPTCHA_FAILED)
            room = self._get_contact(error.jid)
            room.notify('room-captcha-error', error)

    def _on_config_change(self,
                          _con: types.xmppClient,
                          _stanza: Message,
                          properties: MessageProperties
                          ) -> None:
        if not properties.is_muc_config_change:
            return

        room_jid = str(properties.muc_jid)
        self._log.info('Received config change: %s %s',
                       room_jid, properties.muc_status_codes)

        room = self._get_contact(room_jid)
        room.notify('room-config-changed', properties)

        raise nbxmpp.NodeProcessed

    def _on_invite_or_decline(self,
                              _con: types.xmppClient,
                              _stanza: Message,
                              properties: MessageProperties
                              ) -> None:
        if properties.muc_decline is not None:
            data = properties.muc_decline
            if helpers.ignore_contact(self._account, data.from_):
                raise nbxmpp.NodeProcessed

            self._log.info('Invite declined from: %s, reason: %s',
                           data.from_, data.reason)

            app.ged.raise_event(
                MucDecline(account=self._account,
                           **data._asdict()))
            raise nbxmpp.NodeProcessed

        if properties.muc_invite is not None:
            data = properties.muc_invite
            if helpers.ignore_contact(self._account, data.from_):
                raise nbxmpp.NodeProcessed

            self._log.info('Invite from: %s, to: %s', data.from_, data.muc)

            contact = self._get_contact(data.muc, groupchat=True)
            if contact.is_joined:
                # We are already in groupchat. Ignore invitation
                self._log.info('We are already in this room')
                raise nbxmpp.NodeProcessed

            self._con.get_module('Discovery').disco_muc(
                data.muc,
                request_vcard=True,
                callback=self._on_disco_result_after_invite,
                user_data=data)

            raise nbxmpp.NodeProcessed

    def _on_disco_result_after_invite(self, task: Task) -> None:
        try:
            result = task.finish()
        except StanzaError as error:
            self._log.warning(error)
            return

        invite_data = task.get_user_data()
        app.ged.raise_event(
            MucInvitation(account=self._account,
                          info=result.info,
                          **invite_data._asdict()))

    def invite(self,
               room: JID,
               jid: JID,
               reason: Optional[str] = None,
               continue_: bool = False
               ) -> str:
        if helpers.get_muc_context(room) == 'private':
            room_contact = self._get_contact(room)
            assert isinstance(room_contact, GroupchatContact)
            self_contact = room_contact.get_self()
            affiliation = self_contact.affiliation
            admin = affiliation.is_owner or affiliation.is_admin
            if admin:
                self.set_affiliation(
                    room, {jid: {'affiliation': 'member'}})
                type_ = InviteType.DIRECT
            else:
                type_ = InviteType.MEDIATED
        else:
            type_ = InviteType.DIRECT

        password = self._mucs[str(room)].password
        self._log.info('Invite %s to %s', jid, room)
        return self._nbxmpp('MUC').invite(
            room, jid, password, reason, continue_, type_)

    def _on_client_state_changed(self,
                                 _client: types.Client,
                                 _signal_name: str,
                                 state: ClientState
                                 ) -> None:
        if state.is_disconnected:
            self._reset_state()

    def _on_client_resume_failed(self,
                                 _client: types.Client,
                                 _signal_name: str
                                 ) -> None:
        self._reset_state()