~ubuntu-branches/ubuntu/karmic/sugar-presence-service/karmic

« back to all changes in this revision

Viewing changes to src/server_plugin.py

  • Committer: Bazaar Package Importer
  • Author(s): Jani Monoses
  • Date: 2008-02-08 13:15:42 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080208131542-ck7vnmt74j3ph765
Tags: 0.75.0-0ubuntu1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
18
 
19
19
# Standard library
 
20
import base64
20
21
import logging
 
22
import os
21
23
from itertools import izip
22
24
from string import hexdigits
23
25
 
24
26
# Other libraries
25
27
import dbus
26
28
import gobject
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)
 
36
import sugar.profile
34
37
 
35
38
# Presence Service local modules
36
39
import psutils
57
60
    def __init__(self, registry, owner):
58
61
        TelepathyPlugin.__init__(self, registry, owner)
59
62
 
 
63
        self._friends_channel = None
 
64
 
60
65
    def _ip4_address_changed_cb(self, ip4am, address):
61
66
        TelepathyPlugin._ip4_address_changed_cb(self, ip4am, address)
62
67
 
235
240
 
236
241
        TelepathyPlugin._connected_cb(self)
237
242
 
 
243
        # request Friends group channel
 
244
        def friends_channel_requested_cb(friends_chan_path):
 
245
            self._friends_channel = Channel(self._conn.service_name,
 
246
                    friends_chan_path)
 
247
 
 
248
        def error_requesting_friends_channel(e):
 
249
            _logger.debug('error requesting friends channel: %r' % e)
 
250
 
 
251
        handles = self._conn[CONN_INTERFACE].RequestHandles(HANDLE_TYPE_GROUP,
 
252
                ["Friends"])
 
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)
 
257
 
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, '')
 
360
 
 
361
    def sync_friends(self, keys):
 
362
        if self._friends_channel is None or self._subscribe_channel is None:
 
363
            # not ready yet
 
364
            return
 
365
 
 
366
        config_path = os.path.join(sugar.env.get_profile_path(), 'config')
 
367
        profile = sugar.profile.Profile(config_path)
 
368
 
 
369
        friends_handles = set()
 
370
        friends = set()
 
371
        for key in keys:
 
372
            try:
 
373
               decoded = base64.b64decode(key)
 
374
            except TypeError:
 
375
                # key is invalid; skip this friend
 
376
                _logger.debug('skipping friend with invalid key')
 
377
                continue
 
378
 
 
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)
 
382
            friends.add(jid)
 
383
 
 
384
        def error_syncing_friends(e):
 
385
            _logger.debug('error syncing friends: %r' % e)
 
386
 
 
387
        def friends_group_synced():
 
388
            _logger.debug('friends group synced')
 
389
 
 
390
        def friends_subscribed():
 
391
            _logger.debug('friends subscribed')
 
392
 
 
393
        def got_friends_handles(handles):
 
394
            friends_handles.update(handles)
 
395
 
 
396
            # subscribe friends
 
397
            self._subscribe_channel[CHANNEL_INTERFACE_GROUP].AddMembers(
 
398
                friends_handles, "New friend",
 
399
                reply_handler=friends_subscribed,
 
400
                error_handler=error_syncing_friends)
 
401
 
 
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)
 
407
 
 
408
        self._conn[CONN_INTERFACE].RequestHandles(
 
409
            HANDLE_TYPE_CONTACT, friends,
 
410
            reply_handler=got_friends_handles,
 
411
            error_handler=error_syncing_friends)