17
17
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
23
from itertools import izip
22
24
from string import hexdigits
27
from telepathy.client import (ConnectionManager, Connection)
29
from telepathy.client import (ConnectionManager, Connection, Channel)
28
30
from telepathy.interfaces import (CONN_MGR_INTERFACE, CONN_INTERFACE,
29
CHANNEL_INTERFACE_GROUP)
30
from telepathy.constants import (HANDLE_TYPE_CONTACT,
31
CHANNEL_INTERFACE_GROUP, CHANNEL_TYPE_CONTACT_LIST)
32
from telepathy.constants import (HANDLE_TYPE_CONTACT, HANDLE_TYPE_GROUP,
31
33
CONNECTION_STATUS_CONNECTED, CONNECTION_STATUS_DISCONNECTED,
32
34
CHANNEL_GROUP_FLAG_CHANNEL_SPECIFIC_HANDLES,
33
35
CONNECTION_STATUS_REASON_NAME_IN_USE)
35
38
# Presence Service local modules
57
60
def __init__(self, registry, owner):
58
61
TelepathyPlugin.__init__(self, registry, owner)
63
self._friends_channel = None
60
65
def _ip4_address_changed_cb(self, ip4am, address):
61
66
TelepathyPlugin._ip4_address_changed_cb(self, ip4am, address)
236
241
TelepathyPlugin._connected_cb(self)
243
# request Friends group channel
244
def friends_channel_requested_cb(friends_chan_path):
245
self._friends_channel = Channel(self._conn.service_name,
248
def error_requesting_friends_channel(e):
249
_logger.debug('error requesting friends channel: %r' % e)
251
handles = self._conn[CONN_INTERFACE].RequestHandles(HANDLE_TYPE_GROUP,
253
self._conn[CONN_INTERFACE].RequestChannel(CHANNEL_TYPE_CONTACT_LIST,
254
HANDLE_TYPE_GROUP, handles[0], True,
255
reply_handler=friends_channel_requested_cb,
256
error_handler=error_requesting_friends_channel)
238
258
def _filter_trusted_server(self, handles):
239
259
"""Filter a list of contact handles removing the one which aren't hosted
240
260
on a trusted server.
337
357
if not_subscribed:
338
358
self._subscribe_channel[CHANNEL_INTERFACE_GROUP].AddMembers(
339
359
not_subscribed, '')
361
def sync_friends(self, keys):
362
if self._friends_channel is None or self._subscribe_channel is None:
366
config_path = os.path.join(sugar.env.get_profile_path(), 'config')
367
profile = sugar.profile.Profile(config_path)
369
friends_handles = set()
373
decoded = base64.b64decode(key)
375
# key is invalid; skip this friend
376
_logger.debug('skipping friend with invalid key')
379
id = psutils.pubkey_to_keyid(decoded)
380
# this assumes that all our friends are on the same server as us
381
jid = '%s@%s' % (id, profile.jabber_server)
384
def error_syncing_friends(e):
385
_logger.debug('error syncing friends: %r' % e)
387
def friends_group_synced():
388
_logger.debug('friends group synced')
390
def friends_subscribed():
391
_logger.debug('friends subscribed')
393
def got_friends_handles(handles):
394
friends_handles.update(handles)
397
self._subscribe_channel[CHANNEL_INTERFACE_GROUP].AddMembers(
398
friends_handles, "New friend",
399
reply_handler=friends_subscribed,
400
error_handler=error_syncing_friends)
402
# add friends to the "Friends" group
403
self._friends_channel[CHANNEL_INTERFACE_GROUP].AddMembers(
404
friends_handles, "New friend",
405
reply_handler=friends_group_synced,
406
error_handler=error_syncing_friends)
408
self._conn[CONN_INTERFACE].RequestHandles(
409
HANDLE_TYPE_CONTACT, friends,
410
reply_handler=got_friends_handles,
411
error_handler=error_syncing_friends)