Initial import to Tizen
[profile/ivi/python-twisted.git] / doc / words / examples / msn_example.py
1 #!/usr/bin/env python
2
3 # Copyright (c) Twisted Matrix Laboratories.
4 # See LICENSE for details.
5
6 # Twisted Imports
7 from twisted.internet import reactor
8 from twisted.internet.protocol import ClientFactory
9 from twisted.words.protocols import msn
10 from twisted.python import log
11
12 # System Imports
13 import sys, getpass
14
15 """
16 This example connects to the MSN chat service and
17 prints out information about all the users on your
18 contact list (both online and offline).
19
20 The main aim of this example is to demonstrate
21 the connection process.
22
23 @author Samuel Jordan
24 """
25
26
27 def _createNotificationFac():
28     fac = msn.NotificationFactory()
29     fac.userHandle = USER_HANDLE
30     fac.password = PASSWORD
31     fac.protocol = Notification
32     return fac
33
34 class Dispatch(msn.DispatchClient):
35
36     def __init__(self):
37         msn.DispatchClient.__init__(self)
38         self.userHandle = USER_HANDLE
39
40     def gotNotificationReferral(self, host, port):
41         self.transport.loseConnection()
42         reactor.connectTCP(host, port, _createNotificationFac())
43
44 class Notification(msn.NotificationClient):
45
46     def loginFailure(self, message):
47         print 'Login failure:', message
48
49     def listSynchronized(self, *args):
50         contactList = self.factory.contacts
51         print 'Contact list has been synchronized, number of contacts = %s' % len(contactList.getContacts())
52         for contact in contactList.getContacts().values():
53             print 'Contact: %s' % (contact.screenName,)
54             print '    email: %s' % (contact.userHandle,)
55             print '   groups:'
56             for group in contact.groups:
57                 print '      - %s' % contactList.groups[group]
58             print
59
60 if __name__ == '__main__':
61     USER_HANDLE = raw_input("Email (passport): ")
62     PASSWORD = getpass.getpass()
63     log.startLogging(sys.stdout)
64     _dummy_fac = ClientFactory()
65     _dummy_fac.protocol = Dispatch
66     reactor.connectTCP('messenger.hotmail.com', 1863, _dummy_fac)
67     reactor.run()