2018-07-01 02:16:33 +02:00
|
|
|
# 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-0048: Bookmarks
|
|
|
|
|
2018-12-12 23:28:30 +01:00
|
|
|
from typing import Any
|
|
|
|
from typing import List
|
|
|
|
from typing import Optional
|
|
|
|
|
2018-09-04 23:00:29 +02:00
|
|
|
import copy
|
2018-07-01 02:16:33 +02:00
|
|
|
|
|
|
|
import nbxmpp
|
2019-02-10 00:39:10 +01:00
|
|
|
from nbxmpp.util import is_error_result
|
2019-02-03 23:02:32 +01:00
|
|
|
from nbxmpp.structs import BookmarkData
|
|
|
|
from nbxmpp.const import BookmarkStoreType
|
2018-12-12 23:28:30 +01:00
|
|
|
from gi.repository import GLib
|
2018-07-01 02:16:33 +02:00
|
|
|
|
|
|
|
from gajim.common import app
|
2018-12-12 23:28:30 +01:00
|
|
|
from gajim.common.nec import NetworkEvent
|
2019-02-03 23:02:32 +01:00
|
|
|
from gajim.common.modules.base import BaseModule
|
|
|
|
from gajim.common.modules.util import event_node
|
2018-07-01 02:16:33 +02:00
|
|
|
|
2018-12-12 23:28:30 +01:00
|
|
|
|
2019-02-03 23:02:32 +01:00
|
|
|
class Bookmarks(BaseModule):
|
2018-12-12 23:28:30 +01:00
|
|
|
|
2019-02-03 23:02:32 +01:00
|
|
|
_nbxmpp_extends = 'Bookmarks'
|
|
|
|
_nbxmpp_methods = [
|
|
|
|
'request_bookmarks',
|
|
|
|
'store_bookmarks',
|
|
|
|
]
|
2018-12-12 23:28:30 +01:00
|
|
|
|
2018-07-01 02:16:33 +02:00
|
|
|
def __init__(self, con):
|
2019-02-03 23:02:32 +01:00
|
|
|
BaseModule.__init__(self, con)
|
|
|
|
self._register_pubsub_handler(self._bookmark_event_received)
|
|
|
|
self._conversion = False
|
|
|
|
self._bookmarks = []
|
2018-12-12 23:28:30 +01:00
|
|
|
self._join_timeouts = []
|
|
|
|
self._request_in_progress = False
|
2018-07-01 02:16:33 +02:00
|
|
|
|
2019-02-03 23:02:32 +01:00
|
|
|
@property
|
|
|
|
def conversion(self):
|
|
|
|
return self._conversion
|
2018-12-04 20:41:30 +01:00
|
|
|
|
2019-02-03 23:02:32 +01:00
|
|
|
@property
|
|
|
|
def bookmarks(self):
|
|
|
|
return self._bookmarks
|
2018-12-12 23:28:30 +01:00
|
|
|
|
2019-02-03 23:02:32 +01:00
|
|
|
@bookmarks.setter
|
|
|
|
def bookmarks(self, value):
|
|
|
|
self._bookmarks = value
|
|
|
|
|
|
|
|
@event_node(nbxmpp.NS_BOOKMARKS)
|
|
|
|
def _bookmark_event_received(self, _con, _stanza, properties):
|
|
|
|
bookmarks = properties.pubsub_event.data
|
2018-12-12 23:28:30 +01:00
|
|
|
|
2019-02-03 23:02:32 +01:00
|
|
|
if not properties.is_self_message:
|
2019-03-09 23:16:27 +01:00
|
|
|
self._log.warning('%s has an open access bookmarks node',
|
|
|
|
properties.jid)
|
2018-12-21 19:10:18 +01:00
|
|
|
return
|
|
|
|
|
2018-12-12 23:28:30 +01:00
|
|
|
if not self._pubsub_support() or not self.conversion:
|
|
|
|
return
|
|
|
|
|
2019-02-03 23:02:32 +01:00
|
|
|
if self._request_in_progress:
|
2019-03-09 23:16:27 +01:00
|
|
|
self._log.info('Ignore update, pubsub request in progress')
|
2019-02-03 23:02:32 +01:00
|
|
|
return
|
|
|
|
|
|
|
|
old_bookmarks = self._convert_to_set(self._bookmarks)
|
|
|
|
self._bookmarks = bookmarks
|
2018-12-12 23:28:30 +01:00
|
|
|
self._act_on_changed_bookmarks(old_bookmarks)
|
|
|
|
app.nec.push_incoming_event(
|
|
|
|
NetworkEvent('bookmarks-received', account=self._account))
|
|
|
|
|
2019-02-03 23:02:32 +01:00
|
|
|
def pass_disco(self, from_, _identities, features, _data, _node):
|
|
|
|
if nbxmpp.NS_BOOKMARK_CONVERSION not in features:
|
|
|
|
return
|
|
|
|
self._conversion = True
|
2019-03-09 23:16:27 +01:00
|
|
|
self._log.info('Discovered Bookmarks Conversion: %s', from_)
|
2019-02-03 23:02:32 +01:00
|
|
|
|
2018-12-12 23:28:30 +01:00
|
|
|
def _act_on_changed_bookmarks(self, old_bookmarks):
|
2019-02-03 23:02:32 +01:00
|
|
|
new_bookmarks = self._convert_to_set(self._bookmarks)
|
2018-12-12 23:28:30 +01:00
|
|
|
changed = new_bookmarks - old_bookmarks
|
|
|
|
if not changed:
|
|
|
|
return
|
|
|
|
|
|
|
|
join = [jid for jid, autojoin in changed if autojoin]
|
2019-02-03 23:02:32 +01:00
|
|
|
bookmarks = []
|
2018-12-12 23:28:30 +01:00
|
|
|
for jid in join:
|
2019-03-09 23:16:27 +01:00
|
|
|
self._log.info('Schedule autojoin in 10s for: %s', jid)
|
2019-02-03 23:02:32 +01:00
|
|
|
bookmarks.append(self.get_bookmark_from_jid(jid))
|
2018-12-12 23:28:30 +01:00
|
|
|
# If another client creates a MUC, the MUC is locked until the
|
|
|
|
# configuration is finished. Give the user some time to finish
|
|
|
|
# the configuration.
|
|
|
|
timeout_id = GLib.timeout_add_seconds(
|
2019-02-03 23:02:32 +01:00
|
|
|
10, self._join_with_timeout, bookmarks)
|
2018-12-12 23:28:30 +01:00
|
|
|
self._join_timeouts.append(timeout_id)
|
|
|
|
|
|
|
|
# TODO: leave mucs
|
|
|
|
# leave = [jid for jid, autojoin in changed if not autojoin]
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _convert_to_set(bookmarks):
|
|
|
|
set_ = set()
|
2019-02-03 23:02:32 +01:00
|
|
|
for bookmark in bookmarks:
|
|
|
|
set_.add((bookmark.jid, bookmark.autojoin))
|
2018-12-12 23:28:30 +01:00
|
|
|
return set_
|
|
|
|
|
2019-02-03 23:02:32 +01:00
|
|
|
def get_bookmark_from_jid(self, jid):
|
|
|
|
for bookmark in self._bookmarks:
|
|
|
|
if bookmark.jid == jid:
|
|
|
|
return bookmark
|
|
|
|
|
2018-09-04 23:00:29 +02:00
|
|
|
def get_sorted_bookmarks(self, short_name=False):
|
|
|
|
# This returns a sorted by name copy of the bookmarks
|
2019-02-03 23:02:32 +01:00
|
|
|
sorted_bookmarks = []
|
|
|
|
for bookmark in self._bookmarks:
|
|
|
|
bookmark_copy = copy.deepcopy(bookmark)
|
|
|
|
if not bookmark_copy.name:
|
2018-09-04 23:00:29 +02:00
|
|
|
# No name was given for this bookmark
|
|
|
|
# Use the first part of JID instead
|
2019-02-03 23:02:32 +01:00
|
|
|
name = bookmark_copy.jid.split("@")[0]
|
|
|
|
bookmark_copy = bookmark_copy._replace(name=name)
|
2018-09-04 23:00:29 +02:00
|
|
|
|
|
|
|
if short_name:
|
2019-02-03 23:02:32 +01:00
|
|
|
name = bookmark_copy.name
|
2018-09-04 23:00:29 +02:00
|
|
|
name = (name[:42] + '..') if len(name) > 42 else name
|
2019-02-03 23:02:32 +01:00
|
|
|
bookmark_copy = bookmark_copy._replace(name=name)
|
|
|
|
|
|
|
|
sorted_bookmarks.append(bookmark_copy)
|
2018-09-04 23:00:29 +02:00
|
|
|
|
2019-02-03 23:02:32 +01:00
|
|
|
sorted_bookmarks.sort(key=lambda x: x.name.lower())
|
|
|
|
return sorted_bookmarks
|
2018-09-04 23:00:29 +02:00
|
|
|
|
2018-09-12 21:07:59 +02:00
|
|
|
def _pubsub_support(self) -> bool:
|
2018-07-22 12:18:24 +02:00
|
|
|
return (self._con.get_module('PEP').supported and
|
|
|
|
self._con.get_module('PubSub').publish_options)
|
2018-07-01 02:16:33 +02:00
|
|
|
|
2019-02-03 23:02:32 +01:00
|
|
|
def request_bookmarks(self):
|
2018-07-01 02:16:33 +02:00
|
|
|
if not app.account_is_connected(self._account):
|
|
|
|
return
|
|
|
|
|
2019-02-03 23:02:32 +01:00
|
|
|
self._request_in_progress = True
|
|
|
|
type_ = BookmarkStoreType.PRIVATE
|
2018-12-12 23:28:30 +01:00
|
|
|
if self._pubsub_support() and self.conversion:
|
2019-02-03 23:02:32 +01:00
|
|
|
type_ = BookmarkStoreType.PUBSUB
|
2018-07-01 02:16:33 +02:00
|
|
|
|
2019-02-03 23:02:32 +01:00
|
|
|
self._nbxmpp('Bookmarks').request_bookmarks(
|
|
|
|
type_, callback=self._bookmarks_received)
|
2018-07-01 02:16:33 +02:00
|
|
|
|
2019-02-03 23:02:32 +01:00
|
|
|
def _bookmarks_received(self, bookmarks):
|
2019-02-10 00:39:10 +01:00
|
|
|
if is_error_result(bookmarks):
|
2019-03-09 23:16:27 +01:00
|
|
|
self._log.info('Error: %s', bookmarks)
|
2019-02-10 00:39:10 +01:00
|
|
|
bookmarks = []
|
|
|
|
|
2018-12-12 23:28:30 +01:00
|
|
|
self._request_in_progress = False
|
2019-02-03 23:02:32 +01:00
|
|
|
self._bookmarks = bookmarks
|
|
|
|
self.auto_join_bookmarks()
|
2018-12-12 23:28:30 +01:00
|
|
|
app.nec.push_incoming_event(
|
|
|
|
NetworkEvent('bookmarks-received', account=self._account))
|
2018-07-01 02:16:33 +02:00
|
|
|
|
2018-12-12 23:28:30 +01:00
|
|
|
def store_bookmarks(self):
|
2018-07-01 02:16:33 +02:00
|
|
|
if not app.account_is_connected(self._account):
|
|
|
|
return
|
|
|
|
|
2019-02-03 23:02:32 +01:00
|
|
|
type_ = BookmarkStoreType.PRIVATE
|
2018-12-12 23:28:30 +01:00
|
|
|
if self._pubsub_support() and self.conversion:
|
2019-02-03 23:02:32 +01:00
|
|
|
type_ = BookmarkStoreType.PUBSUB
|
|
|
|
|
|
|
|
self._nbxmpp('Bookmarks').store_bookmarks(self._bookmarks, type_)
|
2018-07-01 02:16:33 +02:00
|
|
|
|
2019-01-19 23:02:29 +01:00
|
|
|
app.nec.push_incoming_event(
|
|
|
|
NetworkEvent('bookmarks-received', account=self._account))
|
|
|
|
|
2019-02-03 23:02:32 +01:00
|
|
|
def _join_with_timeout(self, bookmarks: List[Any]) -> None:
|
2018-12-12 23:28:30 +01:00
|
|
|
self._join_timeouts.pop(0)
|
|
|
|
self.auto_join_bookmarks(bookmarks)
|
|
|
|
|
2019-02-03 23:02:32 +01:00
|
|
|
def auto_join_bookmarks(self, bookmarks: Optional[List[Any]] = None) -> None:
|
2018-07-01 02:16:33 +02:00
|
|
|
if app.is_invisible(self._account):
|
|
|
|
return
|
2019-02-03 23:02:32 +01:00
|
|
|
|
2018-12-12 23:28:30 +01:00
|
|
|
if bookmarks is None:
|
2019-02-03 23:02:32 +01:00
|
|
|
bookmarks = self._bookmarks
|
2018-12-12 23:28:30 +01:00
|
|
|
|
2019-02-03 23:02:32 +01:00
|
|
|
for bookmark in bookmarks:
|
|
|
|
if bookmark.autojoin:
|
2018-07-01 02:16:33 +02:00
|
|
|
# Only join non-opened groupchats. Opened one are already
|
|
|
|
# auto-joined on re-connection
|
2019-02-03 23:02:32 +01:00
|
|
|
if bookmark.jid not in app.gc_connected[self._account]:
|
2018-07-01 02:16:33 +02:00
|
|
|
# we are not already connected
|
2019-03-09 23:16:27 +01:00
|
|
|
self._log.info('Autojoin Bookmark: %s', bookmark.jid)
|
2019-02-03 23:02:32 +01:00
|
|
|
minimize = app.config.get_per('rooms', bookmark.jid,
|
2019-01-05 23:59:30 +01:00
|
|
|
'minimize_on_autojoin', True)
|
2018-07-01 02:16:33 +02:00
|
|
|
app.interface.join_gc_room(
|
2019-02-03 23:02:32 +01:00
|
|
|
self._account, bookmark.jid, bookmark.nick,
|
|
|
|
bookmark.password, minimize=minimize)
|
2018-07-01 02:16:33 +02:00
|
|
|
|
2019-01-05 23:59:30 +01:00
|
|
|
def add_bookmark(self, name, jid, autojoin, password, nick):
|
2019-02-03 23:02:32 +01:00
|
|
|
bookmark = BookmarkData(jid=jid,
|
|
|
|
name=name,
|
|
|
|
autojoin=autojoin,
|
|
|
|
password=password,
|
|
|
|
nick=nick)
|
|
|
|
self._bookmarks.append(bookmark)
|
2018-07-01 02:16:33 +02:00
|
|
|
self.store_bookmarks()
|
2019-01-19 23:02:29 +01:00
|
|
|
|
|
|
|
def remove(self, jid: str, publish: bool = True) -> None:
|
2019-02-03 23:02:32 +01:00
|
|
|
bookmark = self.get_bookmark_from_jid(jid)
|
|
|
|
if bookmark is None:
|
2019-01-19 23:02:29 +01:00
|
|
|
return
|
2019-02-11 20:32:41 +01:00
|
|
|
self._bookmarks.remove(bookmark)
|
2019-01-19 23:02:29 +01:00
|
|
|
if publish:
|
|
|
|
self.store_bookmarks()
|
2018-07-01 02:16:33 +02:00
|
|
|
|
2018-09-12 21:07:59 +02:00
|
|
|
def get_name_from_bookmark(self, jid: str) -> str:
|
2018-09-08 23:53:12 +02:00
|
|
|
fallback = jid.split('@')[0]
|
2019-02-03 23:02:32 +01:00
|
|
|
bookmark = self.get_bookmark_from_jid(jid)
|
|
|
|
if bookmark is None:
|
2018-09-08 23:53:12 +02:00
|
|
|
return fallback
|
2019-02-03 23:02:32 +01:00
|
|
|
return bookmark.name or fallback
|
2018-07-01 02:16:33 +02:00
|
|
|
|
2019-01-19 23:02:29 +01:00
|
|
|
def is_bookmark(self, jid: str) -> bool:
|
2019-02-03 23:02:32 +01:00
|
|
|
return self.get_bookmark_from_jid(jid) is not None
|
2019-01-19 23:02:29 +01:00
|
|
|
|
2018-09-12 21:07:59 +02:00
|
|
|
def purge_pubsub_bookmarks(self) -> None:
|
2019-03-09 23:16:27 +01:00
|
|
|
self._log.info('Purge/Delete Bookmarks on PubSub, '
|
|
|
|
'because publish options are not available')
|
2018-07-01 02:16:33 +02:00
|
|
|
self._con.get_module('PubSub').send_pb_purge('', 'storage:bookmarks')
|
|
|
|
self._con.get_module('PubSub').send_pb_delete('', 'storage:bookmarks')
|
|
|
|
|
2018-12-12 23:28:30 +01:00
|
|
|
def _remove_timeouts(self):
|
|
|
|
for _id in self._join_timeouts:
|
|
|
|
GLib.source_remove(_id)
|
2018-07-01 02:16:33 +02:00
|
|
|
|
2018-12-12 23:28:30 +01:00
|
|
|
def cleanup(self):
|
|
|
|
self._remove_timeouts()
|
2018-07-07 13:52:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_instance(*args, **kwargs):
|
|
|
|
return Bookmarks(*args, **kwargs), 'Bookmarks'
|