7 import dbus.mainloop.glib
8 dbus.mainloop.glib.DBusGMainLoop(set_as_default = True)
12 import telepathy.client
13 from telepathy.interfaces import CONNECTION_MANAGER, \
15 CONNECTION_INTERFACE_REQUESTS, \
16 CONNECTION_INTERFACE_ALIASING, \
17 CONNECTION_INTERFACE_SIMPLE_PRESENCE, \
18 CONNECTION_INTERFACE_CONTACTS, \
20 CHANNEL_TYPE_CONTACT_LIST, \
21 CHANNEL_INTERFACE_GROUP
22 from telepathy.constants import CONNECTION_STATUS_CONNECTED, \
23 CONNECTION_STATUS_DISCONNECTED, \
27 DBUS_PROPERTIES = 'org.freedesktop.DBus.Properties'
30 class Example (object):
32 def __init__ (self, account, password, forward):
33 """e.g. account = 'bob@example.com/test'
38 reg = telepathy.client.ManagerRegistry()
41 # get the gabble Connection Manager
42 self.cm = cm = reg.GetManager('theonering')
44 # get the parameters required to make a Jabber connection
46 cm[CONNECTION_MANAGER].RequestConnection('sip',
52 reply_handler = self.request_connection_cb,
53 error_handler = self.error_cb)
57 self.loop = gobject.MainLoop()
60 except KeyboardInterrupt:
61 print "Terminating connection..."
63 # reengage the mainloop so that we can disconnect cleanly
66 def generic_reply (self, *args): pass
68 def error_cb (self, error):
72 def disconnect (self):
75 self.conn[CONNECTION].Disconnect(reply_handler = self.generic_reply,
76 error_handler = self.error_cb)
79 def request_connection_cb (self, bus_name, object_path):
80 print bus_name, object_path
82 self.conn = conn = telepathy.client.Connection(bus_name, object_path)
84 conn[CONNECTION].connect_to_signal('StatusChanged',
85 self.status_changed_cb)
87 print "Establishing connection..."
88 conn[CONNECTION].Connect(reply_handler = self.generic_reply,
89 error_handler = self.error_cb)
91 def status_changed_cb (self, status, reason):
94 if status == CONNECTION_STATUS_DISCONNECTED:
98 if status != CONNECTION_STATUS_CONNECTED: return
100 print 'Carrier Detected' # remember dialup modems?
101 print 'Ctrl-C to disconnect'
103 # get a list of interfaces on this connection
104 conn[CONNECTION].GetInterfaces(reply_handler = self.get_interfaces_cb,
105 error_handler = self.error_cb)
107 def request_contact_list (self, *groups):
110 class ensure_channel_cb (object):
111 def __init__ (self, parent, group):
115 def __call__ (self, yours, path, properties):
116 print "got channel for %s -> %s, yours = %s" % (
117 self.group, path, yours)
119 channel = telepathy.client.Channel(conn.service_name, path)
120 self.channel = channel
123 # request the list of members
124 channel[DBUS_PROPERTIES].Get(CHANNEL_INTERFACE_GROUP,
126 reply_handler = self.members_cb,
127 error_handler = self.parent.error_cb)
129 def members_cb (self, handles):
130 # request information for this list of handles using the
132 conn[CONNECTION_INTERFACE_CONTACTS].GetContactAttributes(
135 CONNECTION_INTERFACE_ALIASING,
136 CONNECTION_INTERFACE_SIMPLE_PRESENCE,
139 reply_handler = self.get_contact_attributes_cb,
140 error_handler = self.parent.error_cb)
143 def get_contact_attributes_cb (self, attributes):
149 for member in attributes.values():
150 for key, value in member.iteritems():
151 print '%s: %s' % (key, value)
155 def no_channel_available (error):
158 # we can either use TargetID if we know a name, or TargetHandle
159 # if we already have a handle
162 print "Ensuring channel to %s..." % group
163 conn[CONNECTION_INTERFACE_REQUESTS].EnsureChannel({
164 CHANNEL + '.ChannelType' : CHANNEL_TYPE_CONTACT_LIST,
165 CHANNEL + '.TargetHandleType': HANDLE_TYPE_LIST,
166 CHANNEL + '.TargetID' : group,
168 reply_handler = ensure_channel_cb(self, group),
169 error_handler = no_channel_available)
172 def get_interfaces_cb (self, interfaces):
176 for interface in interfaces:
177 print " - %s" % interface
179 if CONNECTION_INTERFACE_REQUESTS in interfaces:
180 self.request_contact_list('subscribe',
187 # get the open channelsA
189 conn[CONNECTION_INTERFACE_REQUESTS].connect_to_signal(
191 self.get_channels_cb)
194 conn[DBUS_PROPERTIES].Get(CONNECTION_INTERFACE_REQUESTS,
196 reply_handler = self.get_channels_cb,
197 error_handler = self.error_cb)
200 if CONNECTION_INTERFACE_SIMPLE_PRESENCE in interfaces:
202 # request the statuses
203 print 'Requesting statuses...'
204 conn[DBUS_PROPERTIES].Get(CONNECTION_INTERFACE_SIMPLE_PRESENCE,
206 reply_handler = self.get_statuses_cb,
207 error_handler = self.error_cb)
210 #print 'Setting presence...'
211 #conn[CONNECTION_INTERFACE_SIMPLE_PRESENCE].SetPresence(
214 # reply_handler = self.generic_reply,
215 # error_handler = self.error_cb)
218 if CONNECTION_INTERFACE_CONTACTS in interfaces:
219 print 'Requesting contact attribute interfaces...'
220 conn[DBUS_PROPERTIES].Get(CONNECTION_INTERFACE_CONTACTS,
221 'ContactAttributeInterfaces',
222 reply_handler = self.get_contact_ifaces_cb,
223 error_handler = self.error_cb)
225 def get_channels_cb (self, channels):
226 for channel, props in channels:
227 # find the group channels
228 if props[CHANNEL + '.ChannelType'] != CHANNEL_TYPE_CONTACT_LIST or \
229 props[CHANNEL + '.TargetHandleType'] != HANDLE_TYPE_GROUP:
232 print 'GROUP: Got channel %s: %s' % (channel, props[CHANNEL + '.TargetID'])
234 def get_statuses_cb (self, value):
237 for (key, value) in value.iteritems():
240 def get_contact_ifaces_cb (self, interfaces):
241 print "Contact Attribute Interfaces:"
242 for interface in interfaces:
243 print " - %s" % interface
245 if __name__ == '__main__':
246 username = sys.argv[1]
247 password = sys.argv[2]
248 forward = sys.argv[3]
249 e = Example(username, password, forward)