3
import asyncore, getpass
12
10
logging.basicConfig(level=logging.DEBUG)
16
16
proxies = urllib.getproxies()
18
if 'https' not in proxies and \
20
url = proxies['http'].replace("http://", "https://")
21
result['https'] = pymsn.Proxy(url)
18
22
for type, url in proxies.items():
21
result[type] = pymsn.network.ProxyInfos.build_from_string(url)
23
if type == 'no': continue
24
if type == 'https' and url.startswith('http://'):
25
url = url.replace('http://', 'https://', 1)
26
result[type] = pymsn.Proxy(url)
24
class NS(pymsn.Client):
25
def __init__(self, server, account, mainloop, http_method=False):
27
transport = pymsn.transport.HTTPPollConnection
29
transport = pymsn.transport.DirectConnection
30
pymsn.Client.__init__(self, server, account, proxies = getproxies(),
31
transport_class = transport)
32
gobject.idle_add(self.connect_cb)
33
self.mainloop = mainloop
39
def on_connect_failure(self, proto):
40
print "Connect failed"
43
def on_login_failure(self, proto):
47
def on_login_success(self, proto):
48
self.dp_fetched = False
49
#gobject.timeout_add(5000, self.__find_contact)
50
gobject.timeout_add(5000, self.__publish_avatar)
52
def on_switchboard_invitation(self, proto, server, key,
53
session, passport, friendly_name):
54
inv = [self.connection.get_contact_by_passport(passport),]
55
self.switchboard = pymsn.Conversation(self, invitee=inv,
56
server=server, key=key,
59
def __publish_avatar(self):
60
data = file("pymsn.png").read()
61
self.profile.display_picture = data
63
def __find_contact(self):
64
for contact in self._protocol._contacts.values():
65
passport = contact.get_property("passport")
66
presence = contact.get_property("presence")
67
if presence != pymsn.PresenceStatus.OFFLINE:
68
print "Contact %s is online" % contact.get_property("passport")
70
gobject.idle_add(self.__fetch_dp, contact)
30
class ClientEvents(pymsn.event.ClientEventInterface):
31
def on_client_state_changed(self, state):
32
if state == pymsn.event.ClientState.CLOSED:
34
elif state == pymsn.event.ClientState.OPEN:
35
self._client.profile.display_name = "Kimbix"
36
self._client.profile.presence = pymsn.Presence.ONLINE
37
self._client.profile.current_media = ("I listen to", "Nothing")
38
for contact in self._client.address_book.contacts:
40
#self._client.profile.personal_message = "Testing pymsn, and freeing the pandas!"
41
gobject.timeout_add(5000, self._client.start_conversation)
43
def on_client_error(self, error_type, error):
44
print "ERROR :", error_type, " ->", error
46
class AnnoyingConversation(pymsn.event.ConversationEventInterface):
47
def on_conversation_user_joined(self, contact):
48
gobject.timeout_add(5000, self.annoy_user)
51
msg = "Let's free the pandas ! (testing pymsn)"
52
formatting = pymsn.TextFormat("Comic Sans MS",
53
pymsn.TextFormat.UNDERLINE | pymsn.TextFormat.BOLD,
55
self._client.send_text_message(pymsn.ConversationMessage(msg, formatting))
56
# self._client.send_nudge()
57
# self._client.send_typing_notification()
75
def __fetch_dp(self, contact):
60
def on_conversation_user_typing(self, contact):
63
def on_conversation_message_received(self, sender, message):
66
def on_conversation_error(self, error_type, error):
67
print "ERROR :", error_type, " ->", error
69
class Client(pymsn.Client):
70
def __init__(self, account, quit, http_mode=False):
71
server = ('messenger.hotmail.com', 1863)
73
self.account = account
75
from pymsn.transport import HTTPPollConnection
76
pymsn.Client.__init__(self, server, get_proxies(), HTTPPollConnection)
78
pymsn.Client.__init__(self, server, proxies = get_proxies())
79
self._event_handler = ClientEvents(self)
80
gobject.idle_add(self._connect)
83
self.login(*self.account)
86
def start_conversation(self):
87
contacts = self.address_book.contacts.\
88
search_by_presence(pymsn.Presence.ONLINE)
89
for c in self.address_book.contacts:
90
if c.account == "@hotmail.com":
91
print "Fetching space of : %s with cid %s\n" % (c.display_name, c.cid)
92
self.spaces_service.get_contact_card(c)
94
if len(contacts) == 0:
95
print "No online contacts"
98
for contact in contacts:
99
if contact.account == "johann.prieur@gmail.com":
100
print "Inviting %s for a conversation" % contact.display_name
101
self.conv = pymsn.Conversation(self, [contact])
102
self._convo_events = AnnoyingConversation(self.conv)
79
self.dp_fetched = True
81
dpc = pymsn.msnp2p.DisplayPictureCall(self)
82
dpc.request(contact, self.__on_dp_request_done)
86
def __on_dp_request_done(self, result):
88
print "_on_dp_request_done: Failed to fetch DP"
91
print "_on_dp_request_done: Got DP! %d bytes of data" % len(result)
110
if "--http" in sys.argv:
112
sys.argv.remove('--http')
95
116
if len(sys.argv) < 2:
96
117
account = raw_input('Account: ')
98
119
account = sys.argv[1]
100
121
if len(sys.argv) < 3:
101
122
passwd = getpass.getpass('Password: ')
103
124
passwd = sys.argv[2]
105
126
mainloop = gobject.MainLoop(is_running=True)
106
http_method = '--http' in sys.argv
107
n = NS(('messenger.hotmail.com', 1863), (account, passwd), mainloop, http_method)
113
131
def sigterm_cb():
114
gobject.idle_add(quit_cb)
132
gobject.idle_add(quit)
116
134
signal.signal(signal.SIGTERM, sigterm_cb)
136
n = Client((account, passwd), quit, http_mode)
118
138
while mainloop.is_running():
121
141
except KeyboardInterrupt:
124
144
if __name__ == '__main__':