Philipp Hörist pushed to branch master at gajim / gajim
Commits: e0557fda by Philipp Hörist at 2019-07-05T21:51:34Z Pass only JID to bookmark module
Fixes #9754
- - - - - 4acecfef by Philipp Hörist at 2019-07-05T21:56:29Z MUC: Automatically set bookmarks on join/leave
- When we join a MUC add a bookmark with autojoin enabled - When we leave a MUC modify the bookmark and disable autojoin
- - - - - d369ff91 by Philipp Hörist at 2019-07-05T22:53:39Z Refactor MUCData
- Make jid property a JID - Add occupant_jid property - nbxmpp: Use new muc_jid attribute
- - - - -
8 changed files:
- gajim/command_system/implementation/standard.py - gajim/common/modules/bookmarks.py - gajim/common/modules/muc.py - gajim/common/structs.py - gajim/groupchat_control.py - gajim/gtk/discovery.py - gajim/gtk/join_groupchat.py - gajim/roster_window.py
Changes:
===================================== gajim/command_system/implementation/standard.py ===================================== @@ -308,6 +308,7 @@ class StandardGroupChatCommands(CommandContainer): @command('part', 'close', raw=True, empty=True) @doc(_("Leave the groupchat, optionally giving a reason, and close tab or window")) def leave(self, reason): + self.connection.get_module('MUC').leave(self.room_jid) self.parent_win.remove_tab(self, self.parent_win.CLOSE_COMMAND, reason)
@command(raw=True, empty=True)
===================================== gajim/common/modules/bookmarks.py ===================================== @@ -21,6 +21,7 @@ from typing import Optional import copy
import nbxmpp +from nbxmpp.protocol import JID from nbxmpp.util import is_error_result from nbxmpp.structs import BookmarkData from nbxmpp.const import BookmarkStoreType @@ -129,7 +130,7 @@ class Bookmarks(BaseModule): if not bookmark_copy.name: # No name was given for this bookmark # Use the first part of JID instead - name = bookmark_copy.jid.split("@")[0] + name = bookmark_copy.jid.getNode() bookmark_copy = bookmark_copy._replace(name=name)
if short_name: @@ -206,12 +207,33 @@ class Bookmarks(BaseModule): self._account, str(bookmark.jid), bookmark.nick, bookmark.password, minimize=minimize)
- def add_bookmark(self, name, jid, autojoin, password, nick): - bookmark = BookmarkData(jid=jid, - name=name, - autojoin=autojoin, - password=password, - nick=nick) + def add_bookmark(self, + name: str, + jid: JID, + autojoin: bool, + password: str, + nick: str) -> None: + + bookmark = self.get_bookmark_from_jid(jid) + if bookmark is None: + bookmark = BookmarkData(jid=jid, + name=name, + autojoin=autojoin, + password=password, + nick=nick) + self._bookmarks.append(bookmark) + self.store_bookmarks() + + elif not bookmark.autojoin: + self.set_autojoin(jid, True) + + def set_autojoin(self, jid, enabled): + bookmark = self.get_bookmark_from_jid(jid) + if bookmark is None: + return + self._log.info('Set autojoin for: %s to %s', jid, enabled) + self._bookmarks.remove(bookmark) + bookmark = bookmark._replace(autojoin=enabled) self._bookmarks.append(bookmark) self.store_bookmarks()
===================================== gajim/common/modules/muc.py ===================================== @@ -134,39 +134,39 @@ class MUC(BaseModule): if not app.account_is_connected(self._account): return
- self._muc_data[room_jid] = MUCData(room_jid, nick, - password, rejoin, config) + muc_data = MUCData(room_jid, nick, password, rejoin, config) + self._muc_data[room_jid] = muc_data
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]) - muc = self._get_muc_data(room_jid)
presence = self._con.get_module('Presence').get_presence( - '%s/%s' % (room_jid, muc.nick), + muc_data.occupant_jid, show=show, status=self._con.status)
muc_x = presence.setTag(nbxmpp.NS_MUC + ' x') - if room_jid is not None: - self._add_history_query(muc_x, room_jid, muc.rejoin) + self._add_history_query(muc_x, str(muc_data.jid), muc_data.rejoin)
- if muc.password is not None: - muc_x.setTagData('password', muc.password) + if muc_data.password is not None: + muc_x.setTagData('password', muc_data.password)
- self._log.info('Join MUC: %s', room_jid) - self._set_muc_state(room_jid, MUCJoinedState.JOINING) + self._log.info('Join MUC: %s', muc_data.jid) + self._set_muc_state(muc_data.jid, MUCJoinedState.JOINING) self._con.connection.send(presence)
def leave(self, room_jid): self._log.info('Leave MUC: %s', room_jid) 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( - '%s/%s' % (room_jid, muc.nick), + muc_data.occupant_jid, typ='unavailable') + # We leave a group chat, disable bookmark autojoin + self._con.get_module('Bookmarks').set_autojoin(room_jid, False)
def configure_room(self, room_jid): self._nbxmpp('MUC').request_config(room_jid, @@ -184,8 +184,8 @@ class MUC(BaseModule):
self._log.info('Configure room: %s', result.jid)
- muc = self._get_muc_data(result.jid) - self._apply_config(result.form, muc.config) + muc_data = self._get_muc_data(result.jid) + self._apply_config(result.form, muc_data.config) self.set_config(result.jid, result.form, callback=self._on_config_result) @@ -245,16 +245,15 @@ class MUC(BaseModule): def update_presence(self, auto=False): mucs = self.get_mucs_with_state([MUCJoinedState.JOINED, MUCJoinedState.JOINING]) - for muc in mucs: - self._send_presence(muc.jid, auto) + for muc_data in mucs: + 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] if show in ('invisible', 'offline'): # FIXME: Check if this return
- muc = self._get_muc_data(room_jid) status = self._con.status
xmpp_show = helpers.get_xmpp_show(show) @@ -265,13 +264,11 @@ class MUC(BaseModule): idle_time = time.strftime('%Y-%m-%dT%H:%M:%SZ', 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', - full_jid, xmpp_show, status, idle_time) + muc_data.occupant_jid, xmpp_show, status, idle_time)
self._con.get_module('Presence').send_presence( - full_jid, + muc_data.occupant_jid, show=xmpp_show, status=status, caps=True, @@ -317,13 +314,13 @@ class MUC(BaseModule): muc_x.setTag('history', tags)
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 - muc.state == MUCJoinedState.JOINING): - muc.nick += '_' + muc_data.state == MUCJoinedState.JOINING): + muc_data.nick += '_' self._log.info('Nickname conflict: %s change to %s', - muc.jid, muc.nick) - self._join(muc.jid) + muc_data.jid, muc_data.nick) + self._join(muc_data) return self._raise_muc_event('muc-presence-error', properties)
@@ -331,7 +328,7 @@ class MUC(BaseModule): if properties.type == PresenceType.ERROR: return
- room_jid = properties.jid.getBare() + room_jid = str(properties.muc_jid) if room_jid not in self._muc_data: self._log.warning('Presence from unknown MUC') self._log.warning(stanza) @@ -347,7 +344,7 @@ class MUC(BaseModule): return
contact = app.contacts.get_gc_contact(self._account, - properties.jid.getBare(), + room_jid, properties.muc_nickname)
if properties.is_nickname_changed: @@ -498,7 +495,7 @@ class MUC(BaseModule): if not properties.is_muc_subject: return
- jid = properties.jid.getBare() + jid = str(properties.muc_jid) contact = app.contacts.get_groupchat_contact(self._account, jid) if contact is None: return @@ -513,8 +510,15 @@ class MUC(BaseModule): nickname=properties.muc_nickname, user_timestamp=properties.user_timestamp))
- if self._get_muc_state(jid) == MUCJoinedState.JOINING: + muc_data = self._get_muc_data(jid) + if muc_data.state == MUCJoinedState.JOINING: self._set_muc_state(jid, MUCJoinedState.JOINED) + # We successfully joined a MUC, set autojoin bookmark + self._con.get_module('Bookmarks').add_bookmark(None, + muc_data.jid, + True, + muc_data.password, + muc_data.nick)
raise nbxmpp.NodeProcessed
@@ -530,7 +534,7 @@ class MUC(BaseModule): app.nec.push_incoming_event( NetworkEvent('muc-voice-approval', account=self._account, - room_jid=properties.jid.getBare(), + room_jid=str(properties.muc_jid), form=properties.voice_request.form)) raise nbxmpp.NodeProcessed
@@ -557,7 +561,7 @@ class MUC(BaseModule): if not properties.is_muc_config_change: return
- room_jid = properties.jid.getBare() + room_jid = str(properties.muc_jid) self._log.info('Received config change: %s %s', room_jid, properties.muc_status_codes) app.nec.push_incoming_event(
===================================== gajim/common/structs.py ===================================== @@ -14,6 +14,8 @@
from collections import namedtuple
+from nbxmpp.protocol import JID + from gajim.common.const import MUCJoinedState
URI = namedtuple('URI', 'type action data') @@ -25,7 +27,7 @@ CapsIdentity = namedtuple('CapsIdentity', 'category type name lang')
class MUCData: 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._password = password self._rejoin = rejoin @@ -36,6 +38,12 @@ class MUCData: def jid(self): return self._room_jid
+ @property + def occupant_jid(self): + jid = self._room_jid.copy() + jid.setResource(self._nick) + return jid + @property def nick(self): return self._nick
===================================== gajim/groupchat_control.py ===================================== @@ -33,6 +33,7 @@ import logging from enum import IntEnum, unique
import nbxmpp +from nbxmpp.protocol import JID from nbxmpp.const import StatusCode from nbxmpp.const import Affiliation from nbxmpp.const import Role @@ -642,6 +643,7 @@ class GroupchatControl(ChatControlBase): prompt, change_nick=True, transient_for=self.parent_win.window)
def _on_disconnect(self, action, param): + app.connections[self.account].get_module('MUC').leave(self.room_jid) self.force_non_minimizable = True self.parent_win.remove_tab(self, self.parent_win.CLOSE_COMMAND) self.force_non_minimizable = False @@ -705,7 +707,7 @@ class GroupchatControl(ChatControlBase): password = app.gc_passwords.get(self.room_jid, '') con = app.connections[self.account] con.get_module('Bookmarks').add_bookmark(self.name, - self.room_jid, + JID(self.room_jid), True, password, self.nick) @@ -2229,9 +2231,6 @@ class GroupchatControl(ChatControlBase): for handler in self._event_handlers: app.ged.remove_event_handler(*handler)
- if self.is_connected: - app.connections[self.account].get_module('MUC').leave(self.room_jid) - nick_list = app.contacts.get_nick_list(self.account, self.room_jid) for nick in nick_list: # Update pm chat window
===================================== gajim/gtk/discovery.py ===================================== @@ -46,6 +46,7 @@ import weakref import uuid
import nbxmpp +from nbxmpp.protocol import JID from nbxmpp.util import is_error_result from nbxmpp.structs import DiscoIdentity
@@ -1740,7 +1741,7 @@ class MucBrowser(AgentBrowser): return
con.get_module('Bookmarks').add_bookmark(room_jid.split('@')[0], - room_jid, + JID(room_jid), False, '', '')
===================================== gajim/gtk/join_groupchat.py ===================================== @@ -17,6 +17,7 @@ from gi.repository import Gtk from gi.repository import Gdk
import nbxmpp +from nbxmpp.protocol import JID from nbxmpp.util import is_error_result from nbxmpp.structs import DiscoIdentity
@@ -267,7 +268,7 @@ class JoinGroupchatWindow(Gtk.ApplicationWindow): # Add as bookmark, with autojoin and not minimized name = app.get_nick_from_jid(self.room_jid) con.get_module('Bookmarks').add_bookmark(name, - self.room_jid, + JID(self.room_jid), autojoin, password, nickname)
===================================== gajim/roster_window.py ===================================== @@ -2965,6 +2965,7 @@ class RosterWindow: """ When disconnect menuitem is activated: disconnect from room """ + app.connections[account].get_module('MUC').leave(jid) if jid in app.interface.minimized_controls[account]: ctrl = app.interface.minimized_controls[account][jid] ctrl.shutdown()
View it on GitLab: https://dev.gajim.org/gajim/gajim/compare/0c9cd26a0111976f17b32f59fda533cee0...