Commit d369ff91 authored by Philipp Hörist's avatar Philipp Hörist

Refactor MUCData

- Make jid property a JID
- Add occupant_jid property
- nbxmpp: Use new muc_jid attribute
parent 4acecfef
Pipeline #3879 passed with stages
in 3 minutes and 2 seconds
...@@ -134,38 +134,36 @@ class MUC(BaseModule): ...@@ -134,38 +134,36 @@ class MUC(BaseModule):
if not app.account_is_connected(self._account): if not app.account_is_connected(self._account):
return return
self._muc_data[room_jid] = MUCData(room_jid, nick, muc_data = MUCData(room_jid, nick, password, rejoin, config)
password, rejoin, config) self._muc_data[room_jid] = muc_data
self._con.get_module('Discovery').disco_muc( self._con.get_module('Discovery').disco_muc(
room_jid, partial(self._join, room_jid)) room_jid, partial(self._join, muc_data))
def _join(self, room_jid): def _join(self, muc_data):
show = helpers.get_xmpp_show(app.SHOW_LIST[self._con.connected]) show = helpers.get_xmpp_show(app.SHOW_LIST[self._con.connected])
muc = self._get_muc_data(room_jid)
presence = self._con.get_module('Presence').get_presence( presence = self._con.get_module('Presence').get_presence(
'%s/%s' % (room_jid, muc.nick), muc_data.occupant_jid,
show=show, show=show,
status=self._con.status) status=self._con.status)
muc_x = presence.setTag(nbxmpp.NS_MUC + ' x') muc_x = presence.setTag(nbxmpp.NS_MUC + ' x')
if room_jid is not None: self._add_history_query(muc_x, str(muc_data.jid), muc_data.rejoin)
self._add_history_query(muc_x, room_jid, muc.rejoin)
if muc.password is not None: if muc_data.password is not None:
muc_x.setTagData('password', muc.password) muc_x.setTagData('password', muc_data.password)
self._log.info('Join MUC: %s', room_jid) self._log.info('Join MUC: %s', muc_data.jid)
self._set_muc_state(room_jid, MUCJoinedState.JOINING) self._set_muc_state(muc_data.jid, MUCJoinedState.JOINING)
self._con.connection.send(presence) self._con.connection.send(presence)
def leave(self, room_jid): def leave(self, room_jid):
self._log.info('Leave MUC: %s', room_jid) self._log.info('Leave MUC: %s', room_jid)
self._set_muc_state(room_jid, MUCJoinedState.NOT_JOINED) self._set_muc_state(room_jid, MUCJoinedState.NOT_JOINED)
muc = self._get_muc_data(room_jid) muc_data = self._get_muc_data(room_jid)
self._con.get_module('Presence').send_presence( self._con.get_module('Presence').send_presence(
'%s/%s' % (room_jid, muc.nick), muc_data.occupant_jid,
typ='unavailable') typ='unavailable')
# We leave a group chat, disable bookmark autojoin # We leave a group chat, disable bookmark autojoin
self._con.get_module('Bookmarks').set_autojoin(room_jid, False) self._con.get_module('Bookmarks').set_autojoin(room_jid, False)
...@@ -186,8 +184,8 @@ class MUC(BaseModule): ...@@ -186,8 +184,8 @@ class MUC(BaseModule):
self._log.info('Configure room: %s', result.jid) self._log.info('Configure room: %s', result.jid)
muc = self._get_muc_data(result.jid) muc_data = self._get_muc_data(result.jid)
self._apply_config(result.form, muc.config) self._apply_config(result.form, muc_data.config)
self.set_config(result.jid, self.set_config(result.jid,
result.form, result.form,
callback=self._on_config_result) callback=self._on_config_result)
...@@ -247,16 +245,15 @@ class MUC(BaseModule): ...@@ -247,16 +245,15 @@ class MUC(BaseModule):
def update_presence(self, auto=False): def update_presence(self, auto=False):
mucs = self.get_mucs_with_state([MUCJoinedState.JOINED, mucs = self.get_mucs_with_state([MUCJoinedState.JOINED,
MUCJoinedState.JOINING]) MUCJoinedState.JOINING])
for muc in mucs: for muc_data in mucs:
self._send_presence(muc.jid, auto) self._send_presence(muc_data, auto)
def _send_presence(self, room_jid, auto): def _send_presence(self, muc_data, auto):
show = app.SHOW_LIST[self._con.connected] show = app.SHOW_LIST[self._con.connected]
if show in ('invisible', 'offline'): if show in ('invisible', 'offline'):
# FIXME: Check if this # FIXME: Check if this
return return
muc = self._get_muc_data(room_jid)
status = self._con.status status = self._con.status
xmpp_show = helpers.get_xmpp_show(show) xmpp_show = helpers.get_xmpp_show(show)
...@@ -267,13 +264,11 @@ class MUC(BaseModule): ...@@ -267,13 +264,11 @@ class MUC(BaseModule):
idle_time = time.strftime('%Y-%m-%dT%H:%M:%SZ', idle_time = time.strftime('%Y-%m-%dT%H:%M:%SZ',
time.gmtime(time.time() - idle_sec)) time.gmtime(time.time() - idle_sec))
full_jid = '%s/%s' % (room_jid, muc.nick)
self._log.info('Send presence: %s, show: %s, status: %s, idle_time: %s', self._log.info('Send presence: %s, show: %s, status: %s, idle_time: %s',
full_jid, xmpp_show, status, idle_time) muc_data.occupant_jid, xmpp_show, status, idle_time)
self._con.get_module('Presence').send_presence( self._con.get_module('Presence').send_presence(
full_jid, muc_data.occupant_jid,
show=xmpp_show, show=xmpp_show,
status=status, status=status,
caps=True, caps=True,
...@@ -319,13 +314,13 @@ class MUC(BaseModule): ...@@ -319,13 +314,13 @@ class MUC(BaseModule):
muc_x.setTag('history', tags) muc_x.setTag('history', tags)
def _on_muc_presence(self, _con, _stanza, properties): def _on_muc_presence(self, _con, _stanza, properties):
muc = self._get_muc_data(properties.jid.getBare()) muc_data = self._get_muc_data(properties.muc_jid)
if (properties.error.type == Error.CONFLICT and if (properties.error.type == Error.CONFLICT and
muc.state == MUCJoinedState.JOINING): muc_data.state == MUCJoinedState.JOINING):
muc.nick += '_' muc_data.nick += '_'
self._log.info('Nickname conflict: %s change to %s', self._log.info('Nickname conflict: %s change to %s',
muc.jid, muc.nick) muc_data.jid, muc_data.nick)
self._join(muc.jid) self._join(muc_data)
return return
self._raise_muc_event('muc-presence-error', properties) self._raise_muc_event('muc-presence-error', properties)
...@@ -333,7 +328,7 @@ class MUC(BaseModule): ...@@ -333,7 +328,7 @@ class MUC(BaseModule):
if properties.type == PresenceType.ERROR: if properties.type == PresenceType.ERROR:
return return
room_jid = properties.jid.getBare() room_jid = str(properties.muc_jid)
if room_jid not in self._muc_data: if room_jid not in self._muc_data:
self._log.warning('Presence from unknown MUC') self._log.warning('Presence from unknown MUC')
self._log.warning(stanza) self._log.warning(stanza)
...@@ -349,7 +344,7 @@ class MUC(BaseModule): ...@@ -349,7 +344,7 @@ class MUC(BaseModule):
return return
contact = app.contacts.get_gc_contact(self._account, contact = app.contacts.get_gc_contact(self._account,
properties.jid.getBare(), room_jid,
properties.muc_nickname) properties.muc_nickname)
if properties.is_nickname_changed: if properties.is_nickname_changed:
...@@ -500,7 +495,7 @@ class MUC(BaseModule): ...@@ -500,7 +495,7 @@ class MUC(BaseModule):
if not properties.is_muc_subject: if not properties.is_muc_subject:
return return
jid = properties.jid.getBare() jid = str(properties.muc_jid)
contact = app.contacts.get_groupchat_contact(self._account, jid) contact = app.contacts.get_groupchat_contact(self._account, jid)
if contact is None: if contact is None:
return return
...@@ -519,9 +514,11 @@ class MUC(BaseModule): ...@@ -519,9 +514,11 @@ class MUC(BaseModule):
if muc_data.state == MUCJoinedState.JOINING: if muc_data.state == MUCJoinedState.JOINING:
self._set_muc_state(jid, MUCJoinedState.JOINED) self._set_muc_state(jid, MUCJoinedState.JOINED)
# We successfully joined a MUC, set autojoin bookmark # We successfully joined a MUC, set autojoin bookmark
self._con.get_module('Bookmarks').add_bookmark( self._con.get_module('Bookmarks').add_bookmark(None,
None, properties.muc_jid, muc_data.jid,
True, muc_data.password, muc_data.nick) True,
muc_data.password,
muc_data.nick)
raise nbxmpp.NodeProcessed raise nbxmpp.NodeProcessed
...@@ -537,7 +534,7 @@ class MUC(BaseModule): ...@@ -537,7 +534,7 @@ class MUC(BaseModule):
app.nec.push_incoming_event( app.nec.push_incoming_event(
NetworkEvent('muc-voice-approval', NetworkEvent('muc-voice-approval',
account=self._account, account=self._account,
room_jid=properties.jid.getBare(), room_jid=str(properties.muc_jid),
form=properties.voice_request.form)) form=properties.voice_request.form))
raise nbxmpp.NodeProcessed raise nbxmpp.NodeProcessed
...@@ -564,7 +561,7 @@ class MUC(BaseModule): ...@@ -564,7 +561,7 @@ class MUC(BaseModule):
if not properties.is_muc_config_change: if not properties.is_muc_config_change:
return return
room_jid = properties.jid.getBare() room_jid = str(properties.muc_jid)
self._log.info('Received config change: %s %s', self._log.info('Received config change: %s %s',
room_jid, properties.muc_status_codes) room_jid, properties.muc_status_codes)
app.nec.push_incoming_event( app.nec.push_incoming_event(
......
...@@ -14,6 +14,8 @@ ...@@ -14,6 +14,8 @@
from collections import namedtuple from collections import namedtuple
from nbxmpp.protocol import JID
from gajim.common.const import MUCJoinedState from gajim.common.const import MUCJoinedState
URI = namedtuple('URI', 'type action data') URI = namedtuple('URI', 'type action data')
...@@ -25,7 +27,7 @@ CapsIdentity = namedtuple('CapsIdentity', 'category type name lang') ...@@ -25,7 +27,7 @@ CapsIdentity = namedtuple('CapsIdentity', 'category type name lang')
class MUCData: class MUCData:
def __init__(self, room_jid, nick, password, rejoin, config=None): def __init__(self, room_jid, nick, password, rejoin, config=None):
self._room_jid = room_jid self._room_jid = JID(room_jid)
self._nick = nick self._nick = nick
self._password = password self._password = password
self._rejoin = rejoin self._rejoin = rejoin
...@@ -36,6 +38,12 @@ class MUCData: ...@@ -36,6 +38,12 @@ class MUCData:
def jid(self): def jid(self):
return self._room_jid return self._room_jid
@property
def occupant_jid(self):
jid = self._room_jid.copy()
jid.setResource(self._nick)
return jid
@property @property
def nick(self): def nick(self):
return self._nick return self._nick
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment