forked from kenkeiras/PyIC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dcc.py
184 lines (133 loc) · 4.84 KB
/
dcc.py
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
# -*- encoding: utf-8 -*-
"""
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 3 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, see <http://www.gnu.org/licenses/>.
"""
from threading import Thread
import socket
# For sending 4 byte's throught a socket
def int2uint4( n ):
u = ""
for i in range( 0, 4 ):
u = chr(n % 256) + u
n /= 256
return u
########################################################################
# DCC download thread
class dcc_download( Thread ):
####################################################################
# Constructor
def __init__( self,
msg,
func = None,
buffsize = 1024 ):
Thread.__init__( self )
self.buffsize = buffsize
self.turbo = msg.turbo
self.ip = msg.ip
self.port = msg.port
self.f = open( msg.file, "wb" )
self.func = func
self.size = msg.size
####################################################################
# Here starts the thread
def run(self):
sock = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
sock.connect(( self.ip, self.port ))
r_size = 0
while ( r_size < self.size ) or ( self.size == -1 ):
c = sock.recv( self.buffsize )
if ( not self.turbo ):
try:
r_size += len(c)
sock.send(int2uint4(len(c)))
except:
break
if ( len( c ) < 1 ):
break
self.f.write( c )
if ( r_size >= self.size ):
sock.send(int2uint4( 0 ))
self.f.close( )
sock.close( )
if ( self.func != None ):
self.func( )
########################################################################
# Cleans the message
def clean_msg( s ):
for c in range( 1, 10 ):
s = s.replace( ord( c ) ,'' )
return s
########################################################################
# Converts a port number to a string
def ntop( i ):
ip = ""
ip += str(( i / 16777216 ) % 256 ) + "."
ip += str(( i / 65536) %256 ) + "."
ip += str(( i / 256) % 256 ) + "."
ip += str( i % 256 )
return ip
########################################################################
# Get DCC offer
def decompose_dcc_offer( m ):
if ( "DCC SEND" in m ):
l = m.index( "DCC SEND" )
turbo = False
else:
if ( "DCC TSEND" in m ):
l = m.index("DCC TSEND")
turbo = True
else:
return False
if ( " " in m ):
aux = m[ m.index( " ", l + 4 ) : ]
if ( len( aux ) < 1 ):
return False
while ( aux[ 0 ] == " " ):
aux = aux[1:]
if ( len( aux ) < 1 ):
return False
if ( aux [ 0 ] == '"'):
if ( '"' in aux[ 1 : ] ):
fname = aux[ 1 : aux.index( '"', 1 ) ]
aux = aux[ aux.index( '"', 1 ) + 1 : ]
else:
return False
elif ( aux[ 0 ] == "'" ):
if ( "'" in aux[ 1 : ] ):
fname = aux[ 1 : aux.index( "'", 1 ) ]
aux = aux[ aux.index( "'", 1 ) + 1 : ]
elif ( ' ' in aux ) :
fname = aux[ : aux.index( " " )]
aux = aux[aux.index( " " ) + 1 : ]
else:
return False
# To avoid path's in the filename
fname = fname.split( "/" )[ -1 ]
fname = fname.split( "\\" )[ -1 ]
if ( len( aux ) < 1 ):
return False
while ( aux[ 0 ] == " " ):
aux = aux[ 1: ]
if ( len( aux ) > 1 ) and ( " " in aux ):
ip = int( aux[ : aux.index( " " ) ])
aux = aux[ aux.index( " " ) + 1 : ]
port = int( aux[ : aux.index( " " ) ])
ip = ntop( ip )
if ( " " in aux ):
aux = aux[ aux.index( " " ) + 1 : ]
size = int( aux )
return ( ip, port, fname, turbo, size )
if ( ip != "" ):
return ( ip, port, fname, turbo, -1 )
else:
return False
return False