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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
# -*- coding: utf-8 -*-
#
# pymsn - a python client library for Msn
#
# Copyright (C) 2007 Ali Sabil <ali.sabil@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
from pymsn.msnp2p.constants import *
from pymsn.msnp2p.SLP import *
from pymsn.msnp2p.transport import *
from pymsn.msnp2p.exceptions import *
import pymsn.util.guid as guid
import gobject
import base64
import random
__all__ = ['OutgoingP2PSession']
MAX_INT32 = 0x7fffffff
MAX_INT16 = 0x7fff
def _generate_id(max=MAX_INT32):
"""
Returns a random ID.
@return: a random integer between 1000 and sys.maxint
@rtype: integer
"""
return random.randint(1000, max)
class P2PSession(gobject.GObject):
__gsignals__ = {
"transfer-completed" : (gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE,
(object,))
}
def __init__(self, session_manager, peer, euf_guid="", application_id=0):
gobject.GObject.__init__(self)
self._session_manager = session_manager
self._peer = peer
self._id = _generate_id()
self._call_id = "{%s}" % guid.generate_guid()
self._euf_guid = euf_guid
self._application_id = application_id
self._cseq = 0
self._branch = "{%s}" % guid.generate_guid()
self._session_manager._register_session(self)
@property
def id(self):
return self._id
@property
def call_id(self):
return self._call_id
@property
def peer(self):
return self._peer
def _close(self):
body = SLPMessageBody(SLPContentType.SESSION_CLOSE)
self._cseq = 0
self._branch = "{%s}" % guid.generate_guid()
message = SLPRequestMessage('BYE',
"MSNMSGR:" + self._peer.account,
to = self._peer.account,
frm = self._session_manager._client.profile.account,
branch = self._branch,
cseq = self._cseq,
call_id = self._call_id)
message.body = body
self._send_p2p_data(message)
self._session_manager._unregister_session(self)
def _send_p2p_data(self, data_or_file):
if isinstance(data_or_file, SLPMessage):
session_id = 0
data = str(data_or_file)
total_size = len(data)
else:
session_id = self._id
data = data_or_file
total_size = None
blob = MessageBlob(self._application_id,
data, total_size, session_id)
self._session_manager._transport_manager.send(self.peer, blob)
def _on_blob_sent(self, blob):
if blob.session_id == 0:
# FIXME: handle the signaling correctly
return
if blob.total_size == 4 and \
blob.data.read() == ('\x00' * 4):
self._on_data_preparation_blob_sent(blob)
else:
self._on_data_blob_sent(blob)
def _on_blob_received(self, blob):
if blob.session_id == 0:
# FIXME: handle the signaling correctly
return
if blob.total_size == 4 and \
blob.data.read() == ('\x00' * 4):
self._on_data_preparation_blob_received(blob)
else:
self._on_data_blob_received(blob)
self._close()
def _on_data_preparation_blob_received(self, blob):
pass
def _on_data_preparation_blob_sent(self, blob):
pass
def _on_data_blob_sent(self, blob):
blob.data.seek(0, 0)
self.emit("transfer-completed", blob.data)
def _on_data_blob_received(self, blob):
blob.data.seek(0, 0)
self.emit("transfer-completed", blob.data)
gobject.type_register(P2PSession)
class IncomingP2PSession(P2PSession):
def __init__(self, session_manager, peer, id, message):
P2PSession.__init__(self, session_manager, peer,
message.body.euf_guid, message.body.application_id)
self._id = id
self._call_id = message.call_id
self._cseq = message.cseq
self._branch = message.branch
try:
self._context = message.body.context.strip('\x00')
except AttributeError:
raise SLPError("Incoming INVITE without context")
def accept(self, data_file):
gobject.idle_add(self._start_transfer, data_file)
def reject(self):
self._respond(603)
def _respond(self, status_code):
body = SLPMessageBody(SLPContentType.SESSION_REQUEST)
body.add_header('SessionID', self._id)
self._cseq += 1
response = SLPResponseMessage(status_code,
to = self._peer.account,
frm = self._session_manager._client.profile.account,
cseq = self._cseq,
branch = self._branch,
call_id = self._call_id)
response.body = body
self._send_p2p_data(response)
def _start_transfer(self, data_file):
self._respond(200)
self._send_p2p_data("\x00" * 4)
self._send_p2p_data(data_file)
return False
class OutgoingP2PSession(P2PSession):
def __init__(self, session_manager, peer, context, euf_guid, application_id):
P2PSession.__init__(self, session_manager, peer, euf_guid, application_id)
gobject.idle_add(self._invite, context)
def _invite(self, context):
self._session_manager._register_session(self)
body = SLPMessageBody(SLPContentType.SESSION_REQUEST)
body.add_header('EUF-GUID', self._euf_guid)
body.add_header('SessionID', self._id)
body.add_header('AppID', self._application_id)
body.add_header('Context', base64.b64encode(str(context) + '\x00'))
message = SLPRequestMessage('INVITE',
"MSNMSGR:" + self._peer.account,
to = self._peer.account,
frm = self._session_manager._client.profile.account,
branch = self._branch,
cseq = self._cseq,
call_id = self._call_id)
message.body = body
self._send_p2p_data(message)
return False
|