#!/usr/bin/env pythonimport dbusimport dbus.glibimport gobjectimport datetimeimport telepathyfrom telepathy.interfaces import *class ExampleObserver(telepathy.server.Observer, telepathy.server.DBusProperties): def __init__(self, client_name): bus_name = '.'.join ([CLIENT, client_name]) object_path = '/' + bus_name.replace('.', '/') bus_name = dbus.service.BusName(bus_name, bus=dbus.SessionBus()) telepathy.server.Observer.__init__(self, bus_name, object_path) telepathy.server.DBusProperties.__init__(self) self._implement_property_get(CLIENT, { 'Interfaces': lambda: [ CLIENT_OBSERVER ], }) self._implement_property_get(CLIENT_OBSERVER, { 'ObserverChannelFilter': lambda: dbus.Array([ dbus.Dictionary({ CHANNEL + '.ChannelType': CHANNEL_TYPE_TEXT, }, signature='sv') ], signature='a{sv}') }) def ObserveChannels(self, account_path, connection_path, channels, dispatch_operation, requests_satisfied, observer_info):# Callback functions for getting contact information# THIS IS WHERE WE GET PENDING MESSAGES def list_pending_messages(messages): if messages: for message in messages: time = datetime.datetime.fromtimestamp(message[1]) print "%s: Pending message" % time else: print "No pending messages"# THIS IS WHERE WE GET THE HANDLE + ALIAS OF THE PERSON WE'RE TALKING WITH def get_contact_attributes(attributes): if attributes: for handle, properties in attributes.items(): print "Identifier: %s" % \ properties[CONNECTION + '/contact-id'] print "Alias: %s" % \ properties[CONNECTION_INTERFACE_ALIASING + '/alias'] else: print "No attributes known"# THIS IS WHERE WE GET THE LOCATION OF THE PERSON WE'RE TALKING WITH def get_locations(locations): if locations: print "Printing location:" for key, value in locations.items(): print key, value else: print "Location unknown"# THIS IS WHERE WE GET THE CHAT PROTOCOL def get_protocol(protocol): print "Protocol:", protocol# THIS IS WHERE WE LISTEN TO OUTGOING MESSAGES def message_sent(timestamp, contenttype, content): time = datetime.datetime.fromtimestamp(timestamp) print "%s: Message sent" % time# THIS IS WHERE WE LISTEN TO INCOMING MESSAGES def message_received(identification, timestamp, sender, contenttype, flags, content): time = datetime.datetime.fromtimestamp(timestamp) print "%s: Message received" % time# THIS IS WHERE WE LISTEN FOR THE WINDOW CLOSE SIGNAL def channel_closed(): print "Channel closed" #TODO: Send conv_end to Zeitgeist#TODO: more specific errors def error(*args): print "error", args# Once a connection is ready, query some methods for immediate information,# then connect to relevant signals def connection_ready(connection): print "Connection ready:", connection handles = [ properties[CHANNEL + '.TargetHandle'] for channel, properties in channels ]# Get contact name and handle if CONNECTION_INTERFACE_CONTACTS in connection: connection[CONNECTION_INTERFACE_CONTACTS].GetContactAttributes( handles, # assuming this is available is technically wrong [ CONNECTION_INTERFACE_ALIASING ], False, reply_handler=get_contact_attributes, error_handler=error)# Get contact location (rarely available...yet) if CONNECTION_INTERFACE_LOCATION in connection: connection[CONNECTION_INTERFACE_LOCATION].GetLocations( handles, reply_handler=get_locations, error_handler=error) else: print "Location: unsupported"# Get conversation protocol if CONNECTION in connection: connection[CONNECTION].GetProtocol( reply_handler=get_protocol, error_handler=error)# List pending messages for textchannel in textchannels: if CHANNEL_TYPE_TEXT in textchannel: textchannel[CHANNEL_TYPE_TEXT].ListPendingMessages( False, reply_handler=list_pending_messages, error_handler=error) else: print "it doesn't work :(" def channel_ready(channel): print "Channel ready", channel channel[CHANNEL_TYPE_TEXT].connect_to_signal('Sent', message_sent) channel[CHANNEL_TYPE_TEXT].connect_to_signal('Received', message_received) channel[CHANNEL].connect_to_signal('Closed', channel_closed) bus_name = connection_path.replace('/', '.')[1:] connection = telepathy.client.Connection(bus_name, connection_path, ready_handler=connection_ready) textchannels = [] for channel_path, properties in channels: print "Target handle:", properties[CHANNEL + '.TargetHandle'] textchannels.append(telepathy.client.Channel(bus_name, channel_path, ready_handler=channel_ready))def start(): ExampleObserver("ExampleObserver") return Falseif __name__ == '__main__': gobject.timeout_add(0, start) loop = gobject.MainLoop() loop.run()