~dhillon-v10/qa-regression-testing/mago-packages-checking

« back to all changes in this revision

Viewing changes to mago/application/pidgin/msn_utils.py

  • Committer: Vikram Dhillon
  • Date: 2010-01-12 02:42:18 UTC
  • Revision ID: dhillonv10@gmail.com-20100112024218-7ntl2wrpbxqjb3kx
Initial commit: getting in the data from mago

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python -u
 
2
 
 
3
import gobject, gtk
 
4
import sys
 
5
from time import sleep
 
6
import traceback
 
7
import gobject
 
8
import pymsn
 
9
import pymsn.event
 
10
 
 
11
def get_proxies():
 
12
    import urllib
 
13
    proxies = urllib.getproxies()
 
14
    result = {}
 
15
    if 'https' not in proxies and \
 
16
            'http' in proxies:
 
17
        url = proxies['http'].replace("http://", "https://")
 
18
        result['https'] = pymsn.Proxy(url)
 
19
    for type, url in proxies.items():
 
20
        if type == 'no': continue
 
21
        if type == 'https' and url.startswith('http://'):
 
22
            url = url.replace('http://', 'https://', 1)
 
23
        result[type] = pymsn.Proxy(url)
 
24
    return result
 
25
 
 
26
class ClientEvents(pymsn.event.ClientEventInterface):
 
27
    def on_client_state_changed(self, state):
 
28
        if state == pymsn.event.ClientState.CLOSED:
 
29
            self._client.quit()
 
30
        elif state == pymsn.event.ClientState.OPEN:
 
31
            self._client.profile.presence = pymsn.Presence.ONLINE
 
32
            print "Connected"
 
33
 
 
34
    def on_client_error(self, error_type, error):
 
35
        print "ERROR :", error_type, " ->", error
 
36
 
 
37
class InviteEvents(pymsn.event.InviteEventInterface):
 
38
    def __init__(self, client):
 
39
        self.conv = ''
 
40
    def on_invite_conversation(self, conversation):
 
41
        self.conv = conversation
 
42
 
 
43
class AnnoyingConversation(pymsn.event.ConversationEventInterface):
 
44
    
 
45
    def __init__(self, conv, body):
 
46
        pymsn.event.ConversationEventInterface.__init__(self, conv)
 
47
        self.talking = True
 
48
        self.body    = body
 
49
        self.last_message = ""
 
50
        self.last_sender = ""
 
51
 
 
52
    def on_conversation_user_joined(self, contact):
 
53
        gobject.timeout_add(5000, self.annoy_user)
 
54
 
 
55
    def annoy_user(self):
 
56
        if self.body != '':
 
57
            self._client.send_text_message(pymsn.ConversationMessage(self.body))
 
58
        self.talking = False
 
59
        return False 
 
60
 
 
61
    def on_conversation_user_typing(self, contact):
 
62
        print "typing"
 
63
        pass
 
64
 
 
65
    def on_conversation_message_received(self, sender, message):
 
66
        self.last_message = message.content
 
67
        self.last_sender  = sender.account
 
68
        pass
 
69
 
 
70
    def on_conversation_error(self, error_type, error):
 
71
        print "ERROR :", error_type, " ->", error       
 
72
 
 
73
class ClientMSN(pymsn.Client):
 
74
    def __init__(self, account, debug=False):
 
75
        server = ('messenger.hotmail.com', 1863)
 
76
        
 
77
        self.main_loop = gobject.MainLoop()
 
78
        self.is_connected = False
 
79
        self.messages = []
 
80
        self._exception = None
 
81
        self.debug = debug
 
82
        self.quit = quit
 
83
        self.account = account
 
84
        self.is_talking = False
 
85
        self.current = ''
 
86
        self.conv = ''
 
87
        self._convo_events = None
 
88
 
 
89
        pymsn.Client.__init__(self, server, proxies = get_proxies())
 
90
        self._event_handler = ClientEvents(self)
 
91
        self._invite_events = InviteEvents(self)
 
92
 
 
93
    def connect(self, register):
 
94
        self.is_connected = True
 
95
        self.login(*self.account)
 
96
        return False
 
97
 
 
98
    def disconnect(self):
 
99
        self.is_connected = False
 
100
        self.logout()
 
101
 
 
102
    def start_conversation(self, userid, body):
 
103
        self.is_talking = True
 
104
        contacts = self.address_book.contacts.\
 
105
                search_by_presence(pymsn.Presence.ONLINE)
 
106
 
 
107
        if len(contacts) == 0:
 
108
            print "No online contacts"
 
109
            return True
 
110
        else:
 
111
            for contact in contacts:
 
112
                if contact.account == userid:
 
113
                    print "Inviting %s for a conversation" % contact.display_name
 
114
                    self.conv = pymsn.Conversation(self, [contact])
 
115
                    self.current = contact.account
 
116
                    self._convo_events = AnnoyingConversation(self.conv, body)
 
117
                    self._convo_events.talking = True
 
118
            return False
 
119
 
 
120
    def send_text_message_to_current_conversation(self, body):
 
121
        self.conv.send_text_message(pymsn.ConversationMessage(body))
 
122
 
 
123
    def loop_iter(self):
 
124
        pass
 
125
 
 
126
    def loop(self):
 
127
        self.main_loop.run()
 
128
 
 
129
    def connection_stablished(self):
 
130
        if self.state == pymsn.event.ClientState.OPEN:
 
131
            return True
 
132
        else:
 
133
            return False
 
134
 
 
135