comparison libervia/backend/plugins/plugin_xep_0045.py @ 4245:a7d4007a8fa5

plugin XEP-0272: implement XEP-0272: Multiparty Jingle (Muji) rel 429
author Goffi <goffi@goffi.org>
date Wed, 15 May 2024 17:34:46 +0200
parents 8b673bb307c1
children 0d7bb4df2343
comparison
equal deleted inserted replaced
4244:05f01ac1d5b2 4245:a7d4007a8fa5
19 19
20 import time 20 import time
21 from typing import Optional 21 from typing import Optional
22 import uuid 22 import uuid
23 23
24 import shortuuid
24 from twisted.internet import defer 25 from twisted.internet import defer
25 from twisted.python import failure 26 from twisted.python import failure
26 from twisted.words.protocols.jabber import jid 27 from twisted.words.protocols.jabber import jid
27 from twisted.words.protocols.jabber import error as xmpp_error 28 from twisted.words.protocols.jabber import error as xmpp_error
28 from wokkel import disco, iwokkel, muc 29 from wokkel import disco, iwokkel, muc
78 def __init__(self, room): 79 def __init__(self, room):
79 super(AlreadyJoined, self).__init__() 80 super(AlreadyJoined, self).__init__()
80 self.room = room 81 self.room = room
81 82
82 83
83 class XEP_0045(object): 84 class XEP_0045:
84 # TODO: handle invitations 85 # TODO: handle invitations
85 # FIXME: this plugin need a good cleaning, join method is messy 86 # FIXME: this plugin need a good cleaning, join method is messy
86 87
87 def __init__(self, host): 88 def __init__(self, host):
88 log.info(_("Plugin XEP_0045 initialization")) 89 log.info(_("Plugin XEP_0045 initialization"))
510 511
511 def _get_unique_name(self, muc_service="", profile_key=C.PROF_KEY_NONE): 512 def _get_unique_name(self, muc_service="", profile_key=C.PROF_KEY_NONE):
512 client = self.host.get_client(profile_key) 513 client = self.host.get_client(profile_key)
513 return self.get_unique_name(client, muc_service or None).full() 514 return self.get_unique_name(client, muc_service or None).full()
514 515
515 def get_unique_name(self, client, muc_service=None): 516 def get_unique_name(
517 self,
518 client: SatXMPPEntity,
519 muc_service: jid.JID|None = None,
520 prefix: str = ""
521 ) -> jid.JID:
516 """Return unique name for a room, avoiding collision 522 """Return unique name for a room, avoiding collision
517 523
518 @param muc_service (jid.JID) : leave empty string to use the default service 524 @param client: Client instance.
519 @return: jid.JID (unique room bare JID) 525 @param muc_service: leave empty string to use the default service
520 """ 526 @param prefix: prefix to use in room name.
521 # TODO: we should use #RFC-0045 10.1.4 when available here 527 @return: unique room bare JID
522 room_name = str(uuid.uuid4()) 528 """
529 room_name = f"{prefix}{shortuuid.uuid()}"
523 if muc_service is None: 530 if muc_service is None:
524 try: 531 try:
525 muc_service = client.muc_service 532 muc_service = client.muc_service
526 except AttributeError: 533 except AttributeError:
527 raise exceptions.NotReady("Main server MUC service has not been checked yet") 534 raise exceptions.NotReady(
535 "Main server MUC service has not been checked yet"
536 )
528 if muc_service is None: 537 if muc_service is None:
529 log.warning(_("No MUC service found on main server")) 538 log.warning(_("No MUC service found on main server"))
530 raise exceptions.FeatureNotFound 539 raise exceptions.FeatureNotFound
531 540
532 muc_service = muc_service.userhost() 541 muc_service = muc_service.userhost()
533 return jid.JID("{}@{}".format(room_name, muc_service)) 542 return jid.JID(f"{room_name}@{muc_service}")
534 543
535 def get_default_muc(self): 544 def get_default_muc(self):
536 """Return the default MUC. 545 """Return the default MUC.
537 546
538 @return: unicode 547 @return: unicode
598 except Exception as e: 607 except Exception as e:
599 room = await utils.as_deferred( 608 room = await utils.as_deferred(
600 self._join_eb(failure.Failure(e), client, room_jid, nick, password) 609 self._join_eb(failure.Failure(e), client, room_jid, nick, password)
601 ) 610 )
602 else: 611 else:
612 room.on_joined_callbacks = []
613 room.on_left_callbacks = []
603 await defer.ensureDeferred( 614 await defer.ensureDeferred(
604 self._join_cb(room, client, room_jid, nick) 615 self._join_cb(room, client, room_jid, nick)
605 ) 616 )
606 return room 617 return room
607 618
1256 nick=user.nick)) 1267 nick=user.nick))
1257 return 1268 return
1258 else: 1269 else:
1259 if not room.fully_joined.called: 1270 if not room.fully_joined.called:
1260 return 1271 return
1272 for cb in room.on_joined_callbacks:
1273 defer.ensureDeferred(cb(room, user))
1261 try: 1274 try:
1262 self._changing_nicks.remove(user.nick) 1275 self._changing_nicks.remove(user.nick)
1263 except KeyError: 1276 except KeyError:
1264 # this is a new user 1277 # this is a new user
1265 log.debug(_("user {nick} has joined room {room_id}").format( 1278 log.debug(_("user {nick} has joined room {room_id}").format(
1311 return 1324 return
1312 else: 1325 else:
1313 if not room.fully_joined.called: 1326 if not room.fully_joined.called:
1314 return 1327 return
1315 log.debug(_("user {nick} left room {room_id}").format(nick=user.nick, room_id=room.occupantJID.userhost())) 1328 log.debug(_("user {nick} left room {room_id}").format(nick=user.nick, room_id=room.occupantJID.userhost()))
1329 for cb in room.on_left_callbacks:
1330 defer.ensureDeferred(cb(room, user))
1316 extra = {'info_type': ROOM_USER_LEFT, 1331 extra = {'info_type': ROOM_USER_LEFT,
1317 'user_affiliation': user.affiliation, 1332 'user_affiliation': user.affiliation,
1318 'user_role': user.role, 1333 'user_role': user.role,
1319 'user_nick': user.nick 1334 'user_nick': user.nick
1320 } 1335 }