Commit 8b800f46 authored by Philipp Hörist's avatar Philipp Hörist

Refactor VCard code into own modules

parent 71a82b5c
......@@ -397,6 +397,9 @@ def account_is_connected(account):
else:
return False
def is_invisible(account):
return SHOW_LIST[connections[account].connected] == 'invisible'
def account_is_disconnected(account):
return not account_is_connected(account)
......
......@@ -71,6 +71,8 @@ from gajim.common.modules.annotations import Annotations
from gajim.common.modules.roster_item_exchange import RosterItemExchange
from gajim.common.modules.last_activity import LastActivity
from gajim.common.modules.http_auth import HTTPAuth
from gajim.common.modules.vcard_temp import VCardTemp
from gajim.common.modules.vcard_avatars import VCardAvatars
from gajim.common.connection_handlers import *
from gajim.common.contacts import GC_Contact
from gajim.gtkgui_helpers import get_action
......@@ -666,6 +668,8 @@ class Connection(CommonConnection, ConnectionHandlers):
self.register_module('RosterItemExchange', RosterItemExchange, self)
self.register_module('LastActivity', LastActivity, self)
self.register_module('HTTPAuth', HTTPAuth, self)
self.register_module('VCardTemp', VCardTemp, self)
self.register_module('VCardAvatars', VCardAvatars, self)
app.ged.register_event_handler('privacy-list-received', ged.CORE,
self._nec_privacy_list_received)
......@@ -756,7 +760,7 @@ class Connection(CommonConnection, ConnectionHandlers):
self.connected = 0
self.time_to_reconnect = None
self.privacy_rules_supported = False
self.avatar_presence_sent = False
self.get_module('VCardAvatars').avatar_advertised = False
if on_purpose:
self.sm = Smacks(self)
if self.connection:
......@@ -1768,7 +1772,7 @@ class Connection(CommonConnection, ConnectionHandlers):
show='invisible'))
if initial:
# ask our VCard
self.request_vcard(self._on_own_avatar_received)
self.get_module('VCardTemp').request_vcard()
# Get bookmarks from private namespace
self.get_bookmarks()
......
This diff is collapsed.
......@@ -550,16 +550,6 @@ PresenceHelperEvent):
tim = helpers.datetime_tuple(time_str)
self.idle_time = timegm(tim)
# Check if presence is from the room itself, used when the room
# sends a avatar hash
contact = app.contacts.get_groupchat_contact(self.conn.name, self.fjid)
if contact:
app.nec.push_incoming_event(
RoomAvatarReceivedEvent(
None, conn=self.conn, stanza=self.stanza,
contact=contact, jid=self.jid))
return
xtags = self.stanza.getTags('x')
for x in xtags:
namespace = x.getNamespace()
......@@ -567,9 +557,6 @@ PresenceHelperEvent):
self.is_gc = True
elif namespace == nbxmpp.NS_SIGNED:
sig_tag = x
elif namespace == nbxmpp.NS_VCARD_UPDATE:
self.avatar_sha = x.getTagData('photo')
self.contact_nickname = x.getTagData('nickname')
elif namespace == nbxmpp.NS_DELAY and not self.timestamp:
# XEP-0091
self._generate_timestamp(self.stanza.timestamp)
......@@ -1770,14 +1757,6 @@ class ConnectionTypeEvent(nec.NetworkIncomingEvent):
name = 'connection-type'
base_network_events = []
class VcardPublishedEvent(nec.NetworkIncomingEvent):
name = 'vcard-published'
base_network_events = []
class VcardNotPublishedEvent(nec.NetworkIncomingEvent):
name = 'vcard-not-published'
base_network_events = []
class StanzaReceivedEvent(nec.NetworkIncomingEvent):
name = 'stanza-received'
base_network_events = []
......@@ -1967,13 +1946,6 @@ class NonAnonymousServerErrorEvent(nec.NetworkIncomingEvent):
name = 'non-anonymous-server-error'
base_network_events = []
class VcardReceivedEvent(nec.NetworkIncomingEvent):
name = 'vcard-received'
base_network_events = []
def generate(self):
return True
class UpdateGCAvatarEvent(nec.NetworkIncomingEvent):
name = 'update-gc-avatar'
base_network_events = []
......@@ -1995,19 +1967,6 @@ class UpdateRoomAvatarEvent(nec.NetworkIncomingEvent):
def generate(self):
return True
class RoomAvatarReceivedEvent(nec.NetworkIncomingEvent):
name = 'room-avatar-received'
base_network_events = []
def generate(self):
vcard = self.stanza.getTag('x', namespace=nbxmpp.NS_VCARD_UPDATE)
if vcard is None:
app.log('avatar').info(
'%s has no avatar published (vCard)', self.jid)
return
self.avatar_sha = vcard.getTagData('photo')
return True
class PEPConfigReceivedEvent(nec.NetworkIncomingEvent):
name = 'pep-config-received'
base_network_events = []
......
......@@ -113,6 +113,12 @@ class IdleState(IntEnum):
AWAY = 2
AWAKE = 3
@unique
class RequestAvatar(IntEnum):
SELF = 0
ROOM = 1
USER = 2
SSLError = {
2: _("Unable to get issuer certificate"),
3: _("Unable to get certificate CRL"),
......
......@@ -1398,15 +1398,7 @@ def get_subscription_request_msg(account=None):
s = _('I would like to add you to my contact list.')
if account:
s = _('Hello, I am $name.') + ' ' + s
our_jid = app.get_jid_from_account(account)
vcard = app.connections[account].own_vcard
name = ''
if vcard:
if 'N' in vcard:
if 'GIVEN' in vcard['N'] and 'FAMILY' in vcard['N']:
name = vcard['N']['GIVEN'] + ' ' + vcard['N']['FAMILY']
if not name and 'FN' in vcard:
name = vcard['FN']
name = app.connections[account].get_module('VCardTemp').get_vard_name()
nick = app.nicks[account]
if name and nick:
name += ' (%s)' % nick
......
# 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-0153: vCard-Based Avatars
import os
import logging
import nbxmpp
from gajim.common import app
from gajim.common import helpers
from gajim.common import configpaths
from gajim.common.const import RequestAvatar
log = logging.getLogger('gajim.c.m.vcard.avatars')
class VCardAvatars:
def __init__(self, con):
self._con = con
self._account = con.name
self._requested_shas = []
self.handlers = [
('presence', self._presence_received, '', nbxmpp.NS_VCARD_UPDATE),
]
self.avatar_advertised = False
def _presence_received(self, con, stanza):
update = stanza.getTag('x', namespace=nbxmpp.NS_VCARD_UPDATE)
if update is None:
return
jid = stanza.getFrom()
avatar_sha = update.getTagData('photo')
if avatar_sha is None:
log.info('%s is not ready to promote an avatar', jid)
# Empty update element, ignore
return
if self._con.get_own_jid().bareMatch(jid):
if self._con.get_own_jid() == jid:
# Reflection of our own presence
return
self._self_update_received(jid, avatar_sha)
return
# Check if presence is from a MUC service
contact = app.contacts.get_groupchat_contact(self._account, str(jid))
if contact is not None:
self._update_received(jid, avatar_sha)
elif stanza.getTag('x', namespace=nbxmpp.NS_MUC_USER):
show = stanza.getShow()
type_ = stanza.getType()
self._gc_update_received(jid, avatar_sha, show, type_)
else:
self._update_received(jid, avatar_sha)
def _self_update_received(self, jid, avatar_sha):
jid = jid.getStripped()
full_jid = jid
if avatar_sha == '':
# Empty <photo/> tag, means no avatar is advertised
log.info('%s has no avatar published', full_jid)
return
log.info('Update: %s %s', jid, avatar_sha)
current_sha = app.config.get_per(
'accounts', self._account, 'avatar_sha')
if avatar_sha != current_sha:
log.info('Request : %s', jid)
self._con.get_module('VCardTemp').request_vcard(RequestAvatar.SELF)
else:
log.info('Avatar already known: %s %s',
jid, avatar_sha)
def _update_received(self, jid, avatar_sha, room=False):
jid = jid.getStripped()
full_jid = jid
if avatar_sha == '':
# Empty <photo/> tag, means no avatar is advertised
log.info('%s has no avatar published', full_jid)
# Remove avatar
log.debug('Remove: %s', jid)
app.contacts.set_avatar(self._account, jid, None)
acc_jid = self._con.get_own_jid().getStripped()
if not room:
app.logger.set_avatar_sha(acc_jid, jid, None)
app.interface.update_avatar(
self._account, jid, room_avatar=room)
else:
log.info('Update: %s %s', full_jid, avatar_sha)
current_sha = app.contacts.get_avatar_sha(self._account, jid)
if avatar_sha == current_sha:
log.info('Avatar already known: %s %s', jid, avatar_sha)
return
if room:
# We dont save the room avatar hash in our DB, so check
# if we previously downloaded it
if app.interface.avatar_exists(avatar_sha):
app.contacts.set_avatar(self._account, jid, avatar_sha)
app.interface.update_avatar(
self._account, jid, room_avatar=room)
return
if avatar_sha not in self._requested_shas:
self._requested_shas.append(avatar_sha)
if room:
self._con.get_module('VCardTemp').request_vcard(
RequestAvatar.ROOM, jid, sha=avatar_sha)
else:
self._con.get_module('VCardTemp').request_vcard(
RequestAvatar.USER, jid, sha=avatar_sha)
def _gc_update_received(self, jid, avatar_sha, show, type_):
if show == 'offline' or type_ == 'unavailable':
return
nick = jid.getResource()
gc_contact = app.contacts.get_gc_contact(
self._account, jid.getStripped(), nick)
if gc_contact is None:
log.error('no gc contact found: %s', nick)
return
if avatar_sha == '':
# Empty <photo/> tag, means no avatar is advertised, remove avatar
log.info('%s has no avatar published', nick)
log.debug('Remove: %s', nick)
gc_contact.avatar_sha = None
app.interface.update_avatar(contact=gc_contact)
else:
log.info('Update: %s %s', nick, avatar_sha)
path = os.path.join(configpaths.get('AVATAR'), avatar_sha)
if not os.path.isfile(path):
if avatar_sha not in self._requested_shas:
app.log('avatar').info('Request: %s', nick)
self._requested_shas.append(avatar_sha)
self._con.get_module('VCardTemp').request_vcard(
RequestAvatar.USER, str(jid),
room=True, sha=avatar_sha)
return
if gc_contact.avatar_sha != avatar_sha:
log.info('%s changed his Avatar: %s', nick, avatar_sha)
gc_contact.avatar_sha = avatar_sha
app.interface.update_avatar(contact=gc_contact)
else:
log.info('Avatar already known: %s', nick)
def send_avatar_presence(self, force=False):
if self.avatar_advertised and not force:
log.debug('Avatar already advertised')
return
show = helpers.get_xmpp_show(app.SHOW_LIST[self._con.connected])
pres = nbxmpp.Presence(typ=None, priority=self._con.priority,
show=show, status=self._con.status)
pres = self._con.add_sha(pres)
self._con.connection.send(pres)
self.avatar_advertised = True
app.interface.update_avatar(self._account,
self._con.get_own_jid().getStripped())
def add_update_node(self, node):
update = node.setTag('x', namespace=nbxmpp.NS_VCARD_UPDATE)
if self._con.get_module('VCardTemp').own_vcard_received:
sha = app.config.get_per('accounts', self._account, 'avatar_sha')
own_jid = self._con.get_own_jid()
log.info('Send avatar presence to: %s %s',
node.getTo() or own_jid, sha or 'no sha advertised')
update.setTagData('photo', sha)
return node
This diff is collapsed.
......@@ -40,19 +40,13 @@ AGENT_REMOVED = 'agent_removed'
from gajim.common import connection_handlers
class ConnectionVcard(connection_handlers.ConnectionVcard):
class ConnectionVcard:
def add_sha(self, p, *args):
return p
def add_caps(self, p):
return p
def request_vcard(self, *args):
pass
def send_vcard(self, *args):
pass
class ConnectionHandlersZeroconf(ConnectionVcard,
ConnectionSocks5BytestreamZeroconf, ConnectionCommands,
......
......@@ -762,8 +762,8 @@ class GroupchatControl(ChatControlBase):
publish = app.interface.get_avatar(sha, publish=True)
avatar = base64.b64encode(publish).decode('utf-8')
app.connections[self.account].upload_room_avatar(
con = app.connections[self.account]
con.get_module('VCardTemp').upload_room_avatar(
self.room_jid, avatar)
AvatarChooserDialog(_on_accept,
......
......@@ -77,7 +77,7 @@ class ProfileWindow:
self._nec_vcard_not_published)
self.window.show_all()
self.xml.get_object('ok_button').grab_focus()
app.connections[account].request_vcard(
app.connections[account].get_module('VCardTemp').request_vcard(
self._nec_vcard_received, self.jid)
def on_information_notebook_switch_page(self, widget, page, page_num):
......@@ -261,7 +261,7 @@ class ProfileWindow:
self.progressbar.set_fraction(0)
self.update_progressbar_timeout_id = None
def _nec_vcard_received(self, jid, resource, room, vcard_):
def _nec_vcard_received(self, jid, resource, room, vcard_, *args):
self.set_values(vcard_)
def add_to_vcard(self, vcard_, entry, txt):
......@@ -339,7 +339,8 @@ class ProfileWindow:
app.connections[self.account].retract_nickname()
nick = app.config.get_per('accounts', self.account, 'name')
app.nicks[self.account] = nick
app.connections[self.account].send_vcard(vcard_, sha)
app.connections[self.account].get_module('VCardTemp').send_vcard(
vcard_, sha)
self.message_id = self.statusbar.push(self.context_id,
_('Sending profile…'))
self.progressbar.show()
......
......@@ -840,10 +840,12 @@ class SignalObject(dbus.service.Object):
if avatar_mime_type:
vcard['PHOTO']['TYPE'] = avatar_mime_type
if account:
app.connections[account].send_vcard(vcard, sha)
app.connections[account].get_module('VCardTemp').send_vcard(
vcard, sha)
else:
for acc in app.connections:
app.connections[acc].send_vcard(vcard, sha)
app.connections[acc].get_module('VCardTemp').send_vcard(
vcard, sha)
@dbus.service.method(INTERFACE, in_signature='ssss', out_signature='')
def join_room(self, room_jid, nick, password, account):
......
......@@ -268,7 +268,7 @@ class VcardWindow:
widget.set_text('')
self.xml.get_object('DESC_textview').get_buffer().set_text('')
def _nec_vcard_received(self, jid, resource, room, vcard):
def _nec_vcard_received(self, jid, resource, room, vcard, *args):
self.clear_values()
self._set_values(vcard, jid)
......@@ -477,10 +477,13 @@ class VcardWindow:
self.fill_status_label()
if self.gc_contact:
con.request_vcard(self._nec_vcard_received,
self.gc_contact.get_full_jid(), room=True)
con.get_module('VCardTemp').request_vcard(
self._nec_vcard_received,
self.gc_contact.get_full_jid(),
room=True)
else:
con.request_vcard(self._nec_vcard_received, self.contact.jid)
con.get_module('VCardTemp').request_vcard(
self._nec_vcard_received, self.contact.jid)
def on_close_button_clicked(self, widget):
self.window.destroy()
......
......@@ -47,9 +47,6 @@ class MockConnection(Mock, ConnectionHandlers):
app.connections[account] = self
def request_vcard(self, *args):
pass
class MockWindow(Mock):
def __init__(self, *args):
Mock.__init__(self, *args)
......
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