summaryrefslogtreecommitdiff
path: root/test.py
blob: f75eb127ee0f512737b31e15c3e50ccda56783e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env python

import papyon
import papyon.event


import logging
import gobject

logging.basicConfig(level=logging.DEBUG)

finished = False

def get_proxies():
    import urllib
    proxies = urllib.getproxies()
    result = {}
    if 'https' not in proxies and \
            'http' in proxies:
        url = proxies['http'].replace("http://", "https://")
        result['https'] = papyon.Proxy(url)
    for type, url in proxies.items():
        if type == 'no': continue
        if type == 'https' and url.startswith('http://'):
            url = url.replace('http://', 'https://', 1)
        result[type] = papyon.Proxy(url)
    return result


class ClientEvents(papyon.event.ClientEventInterface):
    def on_client_state_changed(self, state):
        if state == papyon.event.ClientState.CLOSED:
            self._client.quit()
        elif state == papyon.event.ClientState.OPEN:
            self._client.profile.display_name = "Kimbix"
            self._client.profile.presence = papyon.Presence.ONLINE
            self._client.profile.current_media = ("I listen to", "Nothing")
            for contact in self._client.address_book.contacts:
                print contact
            #self._client.profile.personal_message = "Testing papyon, and freeing the pandas!"
            gobject.timeout_add(5000, self._client.start_conversation)

    def on_client_error(self, error_type, error):
        print "ERROR :", error_type, " ->", error

class AnnoyingConversation(papyon.event.ConversationEventInterface):
    def on_conversation_user_joined(self, contact):
        gobject.timeout_add(5000, self.annoy_user)

    def annoy_user(self):
        msg = "Let's free the pandas ! (testing papyon)"
        formatting = papyon.TextFormat("Comic Sans MS",
                         papyon.TextFormat.UNDERLINE | papyon.TextFormat.BOLD,
                         'FF0000')
        self._client.send_text_message(papyon.ConversationMessage(msg, formatting))
#         self._client.send_nudge()
#         self._client.send_typing_notification()
        return True

    def on_conversation_user_typing(self, contact):
        pass

    def on_conversation_message_received(self, sender, message):
        pass

    def on_conversation_error(self, error_type, error):
        print "ERROR :", error_type, " ->", error

class Client(papyon.Client):
    def __init__(self, account, quit, http_mode=False):
        server = ('messenger.hotmail.com', 1863)
        self.quit = quit
        self.account = account
        if http_mode:
            from papyon.transport import HTTPPollConnection
            papyon.Client.__init__(self, server, get_proxies(), HTTPPollConnection)
        else:
            papyon.Client.__init__(self, server, proxies = get_proxies())
        self._event_handler = ClientEvents(self)
        gobject.idle_add(self._connect)

    def _connect(self):
        self.login(*self.account)
        return False

    def start_conversation(self):
        global peer

        for state in [papyon.Presence.ONLINE, \
                          papyon.Presence.BUSY, \
                          papyon.Presence.IDLE, \
                          papyon.Presence.AWAY, \
                          papyon.Presence.BE_RIGHT_BACK, \
                          papyon.Presence.ON_THE_PHONE, \
                          papyon.Presence.OUT_TO_LUNCH]:
            print "Trying %s" % state
            contacts = self.address_book.contacts.\
                search_by_presence(state)

            if len(contacts) == 0:
                print "No %s contacts" % state
            else:
                for contact in contacts:
                    print "%s is %s" % (contact.display_name, state)
                    if contact.account == peer:
                        print "Inviting %s for a webcam" % contact.display_name
                        self._webcam_handler.invite(contact)
                        
                        return False

        return True

def main():
    import sys
    import getpass
    import signal

    global peer

    if "--http" in sys.argv:
        http_mode = True
        sys.argv.remove('--http')
    else:
        http_mode = False

    if len(sys.argv) < 2:
        account = raw_input('Account: ')
    else:
        account = sys.argv[1]

    if len(sys.argv) < 3:
        passwd = getpass.getpass('Password: ')
    else:
        passwd = sys.argv[2]

    if len(sys.argv) < 4:
        peer = raw_input('Send webcam to : ')
    else:
        peer = sys.argv[3]

    mainloop = gobject.MainLoop(is_running=True)

    def quit():
        mainloop.quit()

    def sigterm_cb():
        gobject.idle_add(quit)

    signal.signal(signal.SIGTERM, sigterm_cb)

    n = Client((account, passwd), quit, http_mode)

    while mainloop.is_running():
        try:
            mainloop.run()
        except KeyboardInterrupt:
            quit()

if __name__ == '__main__':
    main()