6 import dbus.mainloop.glib
7 dbus.mainloop.glib.DBusGMainLoop(set_as_default = True)
12 DBUS_PROPERTIES = 'org.freedesktop.DBus.Properties'
16 reg = telepathy.client.ManagerRegistry()
21 def get_connection_manager(reg):
22 cm = reg.GetManager('theonering')
31 def queue_action(self):
34 def append_action(self, action):
35 assert self._action is None
38 def get_next_action(self):
39 assert self._action is not None
43 if self._action is None:
45 self._action.queue_action()
47 def _on_error(self, error):
50 def _on_generic_message(self, *args):
54 class DummyAction(Action):
56 def queue_action(self):
57 gobject.idle_add(self._on_done)
60 class QuitLoop(Action):
62 def __init__(self, loop):
63 super(QuitLoop, self).__init__()
66 def queue_action(self):
70 class DisplayParams(Action):
72 def __init__(self, cm):
73 super(DisplayParams, self).__init__()
76 def queue_action(self):
77 self._cm[telepathy.interfaces.CONN_MGR_INTERFACE].GetParameters(
79 reply_handler = self._on_done,
80 error_handler = self._on_error,
83 def _on_done(self, params):
84 print "Connection Parameters:"
85 for name, flags, signature, default in params:
86 print "\t%s (%s)" % (name, signature),
88 if flags & telepathy.constants.CONN_MGR_PARAM_FLAG_REQUIRED:
90 if flags & telepathy.constants.CONN_MGR_PARAM_FLAG_REGISTER:
92 if flags & telepathy.constants.CONN_MGR_PARAM_FLAG_SECRET:
94 if flags & telepathy.constants.CONN_MGR_PARAM_FLAG_DBUS_PROPERTY:
95 print "dbus-property",
96 if flags & telepathy.constants.CONN_MGR_PARAM_FLAG_HAS_DEFAULT:
97 print "has-default(%s)" % default,
100 super(DisplayParams, self)._on_done()
103 class RequestConnection(Action):
105 def __init__(self, cm, username, password, forward):
106 super(RequestConnection, self).__init__()
110 self._serviceName = None
112 self._username = username
113 self._password = password
114 self._forward = forward
121 def serviceName(self):
122 return self._serviceName
124 def queue_action(self):
125 self._cm[telepathy.server.CONNECTION_MANAGER].RequestConnection(
128 'account': self._username,
129 'password': self._password,
130 'forward': self._forward,
132 reply_handler = self._on_done,
133 error_handler = self._on_error,
136 def _on_done(self, busName, objectPath):
137 self._serviceName = busName
138 self._conn = telepathy.client.Connection(busName, objectPath)
139 super(RequestConnection, self)._on_done()
142 class Connect(Action):
144 def __init__(self, connAction):
145 super(Connect, self).__init__()
146 self._connAction = connAction
148 def queue_action(self):
149 self._connAction.conn[telepathy.server.CONNECTION].connect_to_signal(
153 self._connAction.conn[telepathy.server.CONNECTION].Connect(
154 reply_handler = self._on_generic_message,
155 error_handler = self._on_error,
159 super(Connect, self)._on_done()
161 def _on_change(self, status, reason):
162 if status == telepathy.constants.CONNECTION_STATUS_DISCONNECTED:
163 print "Disconnected!"
165 elif status == telepathy.constants.CONNECTION_STATUS_CONNECTED:
168 elif status == telepathy.constants.CONNECTION_STATUS_CONNECTING:
171 print "Status: %r" % status
174 class SimplePresenceOptions(Action):
176 def __init__(self, connAction):
177 super(SimplePresenceOptions, self).__init__()
178 self._connAction = connAction
180 def queue_action(self):
181 self._connAction.conn[DBUS_PROPERTIES].Get(
182 telepathy.server.CONNECTION_INTERFACE_SIMPLE_PRESENCE,
184 reply_handler = self._on_done,
185 error_handler = self._on_error,
188 def _on_done(self, statuses):
189 print "\tAvailable Statuses"
190 for (key, value) in statuses.iteritems():
191 print "\t\t - %s" % key
192 super(SimplePresenceOptions, self)._on_done()
195 class NullHandle(object):
206 class UserHandle(Action):
208 def __init__(self, connAction):
209 super(UserHandle, self).__init__()
210 self._connAction = connAction
219 return [self._handle]
221 def queue_action(self):
222 self._connAction.conn[telepathy.server.CONNECTION].GetSelfHandle(
223 reply_handler = self._on_done,
224 error_handler = self._on_error,
227 def _on_done(self, handle):
228 self._handle = handle
229 super(UserHandle, self)._on_done()
232 class RequestHandle(Action):
234 def __init__(self, connAction, handleType, handleNames):
235 super(RequestHandle, self).__init__()
236 self._connAction = connAction
238 self._handleType = handleType
239 self._handleNames = handleNames
247 return [self._handle]
249 def queue_action(self):
250 self._connAction.conn[telepathy.server.CONNECTION].RequestHandles(
253 reply_handler = self._on_done,
254 error_handler = self._on_error,
257 def _on_done(self, handles):
258 self._handle = handles[0]
259 super(RequestHandle, self)._on_done()
262 class RequestChannel(Action):
264 def __init__(self, connAction, handleAction, channelType, handleType):
265 super(RequestChannel, self).__init__()
266 self._connAction = connAction
267 self._handleAction = handleAction
269 self._channelType = channelType
270 self._handleType = handleType
276 def queue_action(self):
277 self._connAction.conn[telepathy.server.CONNECTION].RequestChannel(
280 self._handleAction.handle,
282 reply_handler = self._on_done,
283 error_handler = self._on_error,
286 def _on_done(self, channelObjectPath):
287 self._channel = telepathy.client.Channel(self._connAction.serviceName, channelObjectPath)
288 super(RequestChannel, self)._on_done()
291 class EnsureChannel(Action):
293 def __init__(self, connAction, channelType, handleType, handleId):
294 super(EnsureChannel, self).__init__()
295 self._connAction = connAction
297 self._channelType = channelType
298 self._handleType = handleType
299 self._handleId = handleId
312 return [self._handle]
314 def queue_action(self):
316 telepathy.server.CHANNEL_INTERFACE+".ChannelType": self._channelType,
317 telepathy.server.CHANNEL_INTERFACE+".TargetHandleType": self._handleType,
318 telepathy.server.CHANNEL_INTERFACE+".TargetID": self._handleId,
320 self._connAction.conn[telepathy.server.CONNECTION_INTERFACE_REQUESTS].EnsureChannel(
322 reply_handler = self._on_done,
323 error_handler = self._on_error,
326 def _on_done(self, yours, channelObjectPath, properties):
327 print "Create?", not not yours
328 print "Path:", channelObjectPath
329 print "Properties:", properties
330 self._channel = telepathy.client.Channel(self._connAction.serviceName, channelObjectPath)
331 self._handle = properties[telepathy.server.CHANNEL_INTERFACE+".TargetHandle"]
332 super(EnsureChannel, self)._on_done()
335 class CloseChannel(Action):
337 def __init__(self, connAction, chanAction):
338 super(CloseChannel, self).__init__()
339 self._connAction = connAction
340 self._chanAction = chanAction
343 def queue_action(self):
344 self._chanAction.channel[telepathy.server.CHANNEL].Close(
345 reply_handler = self._on_done,
346 error_handler = self._on_error,
350 super(CloseChannel, self)._on_done()
353 class ContactHandles(Action):
355 def __init__(self, connAction, chanAction):
356 super(ContactHandles, self).__init__()
357 self._connAction = connAction
358 self._chanAction = chanAction
365 def queue_action(self):
366 self._chanAction.channel[DBUS_PROPERTIES].Get(
367 telepathy.server.CHANNEL_INTERFACE_GROUP,
369 reply_handler = self._on_done,
370 error_handler = self._on_error,
373 def _on_done(self, handles):
374 self._handles = list(handles)
375 super(ContactHandles, self)._on_done()
378 class SimplePresenceStatus(Action):
380 def __init__(self, connAction, handleAction):
381 super(SimplePresenceStatus, self).__init__()
382 self._connAction = connAction
383 self._handleAction = handleAction
385 def queue_action(self):
386 self._connAction.conn[telepathy.server.CONNECTION_INTERFACE_SIMPLE_PRESENCE].GetPresences(
387 self._handleAction.handles,
388 reply_handler = self._on_done,
389 error_handler = self._on_error,
392 def _on_done(self, aliases):
394 for hid, (presenceType, presence, presenceMessage) in aliases.iteritems():
395 print "\t\t%s:" % hid, presenceType, presence, presenceMessage
396 super(SimplePresenceStatus, self)._on_done()
399 class SetSimplePresence(Action):
401 def __init__(self, connAction, status, message):
402 super(SetSimplePresence, self).__init__()
403 self._connAction = connAction
404 self._status = status
405 self._message = message
407 def queue_action(self):
408 self._connAction.conn[telepathy.server.CONNECTION_INTERFACE_SIMPLE_PRESENCE].SetPresence(
411 reply_handler = self._on_done,
412 error_handler = self._on_error,
416 super(SetSimplePresence, self)._on_done()
419 class Aliases(Action):
421 def __init__(self, connAction, handleAction):
422 super(Aliases, self).__init__()
423 self._connAction = connAction
424 self._handleAction = handleAction
426 def queue_action(self):
427 self._connAction.conn[telepathy.server.CONNECTION_INTERFACE_ALIASING].RequestAliases(
428 self._handleAction.handles,
429 reply_handler = self._on_done,
430 error_handler = self._on_error,
433 def _on_done(self, aliases):
435 for h, alias in zip(self._handleAction.handles, aliases):
436 print "\t\t", h, alias
437 super(Aliases, self)._on_done()
442 def __init__(self, connAction, chanAction, handleAction):
443 super(Call, self).__init__()
444 self._connAction = connAction
445 self._chanAction = chanAction
446 self._handleAction = handleAction
448 def queue_action(self):
449 self._chanAction.channel[telepathy.server.CHANNEL_TYPE_STREAMED_MEDIA].RequestStreams(
450 self._handleAction.handle,
451 [telepathy.constants.MEDIA_STREAM_TYPE_AUDIO],
452 reply_handler = self._on_done,
453 error_handler = self._on_error,
456 def _on_done(self, handle):
458 super(Call, self)._on_done()
461 class SendText(Action):
463 def __init__(self, connAction, chanAction, handleAction, messageType, message):
464 super(SendText, self).__init__()
465 self._connAction = connAction
466 self._chanAction = chanAction
467 self._handleAction = handleAction
468 self._messageType = messageType
469 self._message = message
471 def queue_action(self):
472 self._chanAction.channel[telepathy.server.CHANNEL_TYPE_TEXT].Send(
475 reply_handler = self._on_done,
476 error_handler = self._on_error,
481 super(SendText, self)._on_done()
486 def __init__(self, length):
487 super(Sleep, self).__init__()
488 self._length = length
490 def queue_action(self):
491 gobject.timeout_add(self._length, self._on_done)
497 super(Block, self).__init__()
499 def queue_action(self):
503 #super(SendText, self)._on_done()
507 class Disconnect(Action):
509 def __init__(self, connAction):
510 super(Disconnect, self).__init__()
511 self._connAction = connAction
513 def queue_action(self):
514 self._connAction.conn[telepathy.server.CONNECTION].Disconnect(
515 reply_handler = self._on_done,
516 error_handler = self._on_error,
520 if __name__ == '__main__':
521 loop = gobject.MainLoop()
524 cm = get_connection_manager(reg)
526 nullHandle = NullHandle()
528 dummy = DummyAction()
533 dp = DisplayParams(cm)
534 lastAction.append_action(dp)
535 lastAction = lastAction.get_next_action()
538 username = sys.argv[1]
539 password = sys.argv[2]
540 forward = sys.argv[3]
541 reqcon = RequestConnection(cm, username, password, forward)
542 lastAction.append_action(reqcon)
543 lastAction = lastAction.get_next_action()
546 reqcon = RequestConnection(cm, username, password, forward)
547 lastAction.append_action(reqcon)
548 lastAction = lastAction.get_next_action()
550 con = Connect(reqcon)
551 lastAction.append_action(con)
552 lastAction = lastAction.get_next_action()
555 spo = SimplePresenceOptions(reqcon)
556 lastAction.append_action(spo)
557 lastAction = lastAction.get_next_action()
560 uh = UserHandle(reqcon)
561 lastAction.append_action(uh)
562 lastAction = lastAction.get_next_action()
564 ua = Aliases(reqcon, uh)
565 lastAction.append_action(ua)
566 lastAction = lastAction.get_next_action()
568 sps = SimplePresenceStatus(reqcon, uh)
569 lastAction.append_action(sps)
570 lastAction = lastAction.get_next_action()
573 setdnd = SetSimplePresence(reqcon, "dnd", "")
574 lastAction.append_action(setdnd)
575 lastAction = lastAction.get_next_action()
577 sps = SimplePresenceStatus(reqcon, uh)
578 lastAction.append_action(sps)
579 lastAction = lastAction.get_next_action()
581 setdnd = SetSimplePresence(reqcon, "available", "")
582 lastAction.append_action(setdnd)
583 lastAction = lastAction.get_next_action()
585 sps = SimplePresenceStatus(reqcon, uh)
586 lastAction.append_action(sps)
587 lastAction = lastAction.get_next_action()
590 sl = Sleep(10 * 1000)
591 lastAction.append_action(sl)
592 lastAction = lastAction.get_next_action()
595 rclh = RequestHandle(reqcon, telepathy.HANDLE_TYPE_LIST, ["subscribe"])
596 lastAction.append_action(rclh)
597 lastAction = lastAction.get_next_action()
599 rclc = RequestChannel(
602 telepathy.CHANNEL_TYPE_CONTACT_LIST,
603 telepathy.HANDLE_TYPE_LIST,
605 lastAction.append_action(rclc)
606 lastAction = lastAction.get_next_action()
608 ch = ContactHandles(reqcon, rclc)
609 lastAction.append_action(ch)
610 lastAction = lastAction.get_next_action()
612 ca = Aliases(reqcon, ch)
613 lastAction.append_action(ca)
614 lastAction = lastAction.get_next_action()
617 accountNumber = sys.argv[4]
618 enChan = EnsureChannel(reqcon, telepathy.CHANNEL_TYPE_TEXT, telepathy.HANDLE_TYPE_CONTACT, accountNumber)
619 lastAction.append_action(enChan)
620 lastAction = lastAction.get_next_action()
622 sendDebugtext = SendText(reqcon, enChan, enChan, telepathy.CHANNEL_TEXT_MESSAGE_TYPE_NORMAL, "Boo!")
623 lastAction.append_action(sendDebugtext)
624 lastAction = lastAction.get_next_action()
627 rch = RequestHandle(reqcon, telepathy.HANDLE_TYPE_CONTACT, ["18005558355"]) #(1-800-555-TELL)
628 lastAction.append_action(rch)
629 lastAction = lastAction.get_next_action()
631 # making a phone call
634 smHandleType = telepathy.HANDLE_TYPE_CONTACT
636 smHandle = nullHandle
637 smHandleType = telepathy.HANDLE_TYPE_NONE
638 rsmc = RequestChannel(
641 telepathy.CHANNEL_TYPE_STREAMED_MEDIA,
644 lastAction.append_action(rsmc)
645 lastAction = lastAction.get_next_action()
648 call = Call(reqcon, rsmc, rch)
649 lastAction.append_action(call)
650 lastAction = lastAction.get_next_action()
653 rtc = RequestChannel(
656 telepathy.CHANNEL_TYPE_TEXT,
659 lastAction.append_action(rtc)
660 lastAction = lastAction.get_next_action()
663 closechan = CloseChannel(reqcon, rtc)
664 lastAction.append_action(closechan)
665 lastAction = lastAction.get_next_action()
667 rtc = RequestChannel(
670 telepathy.CHANNEL_TYPE_TEXT,
673 lastAction.append_action(rtc)
674 lastAction = lastAction.get_next_action()
677 sendtext = SendText(reqcon, rtc, rch, telepathy.CHANNEL_TEXT_MESSAGE_TYPE_NORMAL, "Boo!")
678 lastAction.append_action(sendtext)
679 lastAction = lastAction.get_next_action()
683 lastAction.append_action(bl)
684 lastAction = lastAction.get_next_action()
687 sl = Sleep(30 * 1000)
688 lastAction.append_action(sl)
689 lastAction = lastAction.get_next_action()
691 dis = Disconnect(reqcon)
692 lastAction.append_action(dis)
693 lastAction = lastAction.get_next_action()
695 quitter = QuitLoop(loop)
696 lastAction.append_action(quitter)
697 lastAction = lastAction.get_next_action()
699 firstAction.queue_action()