-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathview-state.rb
executable file
·308 lines (291 loc) · 6.57 KB
/
view-state.rb
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/usr/local/bin/ruby -w
# $Id$
# (c) 2005, Dirk Meyer, Im Grund 4, 34317 Habichtswald
#
# Updates on:
# http://anime.dinoex.net/xdcc/tools/
#
$chroot = ""
def usage(msg)
print msg, "\nUsage: #{File.basename($0)} statefile [statefile ...]\n\n"
print "export iroffer statefile to text.\n"
exit 64
end
def filesize_cache(key)
if ( $size_cache.has_key?( key ) )
return $size_cache[ key ]
end
bytes = File.size( "#{$chroot}#{key}")
$size_cache[ key ] = bytes
$size_cache_dirty += 1
return bytes
end
def makesize(bytes)
nbytes = bytes
if ( nbytes < 1000 )
return sprintf( "%3db", nbytes )
end
nbytes = ( nbytes + 512 ) / 1024
if ( nbytes < 1000 )
return sprintf( "%3dk", nbytes )
end
nbytes = ( nbytes + 512 ) / 1024
if ( nbytes < 1000 )
return sprintf( "%3dM", nbytes )
end
if ( nbytes < 10000 )
return sprintf( "%3.1fG", ( nbytes.to_f / 1024) )
end
nbytes = ( nbytes + 512 ) / 1024
if ( nbytes < 1000 )
return sprintf( "%3dG", nbytes )
end
if ( nbytes < 10000 )
return sprintf( "%3.1fT", ( nbytes.to_f / 1024 ) )
end
nbytes = ( nbytes + 512 ) / 1024
if ( nbytes < 1000 )
return sprintf( "%3dT", nbytes )
end
return sprintf( "%3dE", nbytes )
end
def ausgabe()
if $xf.nil?
return
end
$pack += 1
$all_pack += 1
printf( "%3s %3dx [%4s] %s\n", "##{$pack}", $xg, $size, $xd )
$xf = nil
end
def ausgabetotal()
$sum_size = makesize( $sum_bytes )
$transfer_size = makesize( $transfer_bytes )
$partial_bytes = $transfer_bytes - $sum_xg_bytes
if ( $partial_bytes >= 0 )
$partial_size = makesize( $partial_bytes )
else
$partial_size = '-' << makesize( $partial_bytes )
end
$total = sprintf( "total in files, [%4s] total downloaded, [%4s] partial",
$transfer_size, $partial_size )
printf( "%3s %3dx [%4s] %s\n", "##{$pack}", $sum_xg, $sum_size, $total )
end
def get_long(string)
return string.unpack('N')[ 0 ]
end
def get_xlong(string)
ll = string.unpack('NN')
l = ll[ 0 ] * ( 2 ** 32 )
l += ll[ 1 ]
return l
end
def get_text(string)
l = string.unpack('C')[ 0 ]
l -= 1
return string[1, l]
end
def parse_buffer(buffer, bsize)
$xf = nil
fsize = bsize - 16;
ipos = 8
while ipos < fsize
tag = get_long( buffer[ipos, 4] )
len = get_long( buffer[ipos + 4, 4] )
if ( len <= 8 )
printf( ":tag=%d<br>\n", tag )
printf( ":len=%d<br>\n", len )
printf( "Warning: parsing statfile aborted\n" )
ipos = fsize
break
end
case tag
when 514 # TOTAL_SENT
text = buffer[ipos + 8, len - 8]
$total = get_xlong( text )
if ( $all_transfer_bytes > 0 )
print "\n"
end
$transfer_bytes += $total
$all_transfer_bytes += $total
$pack = 0
when 3072 # XDCCS
chunkdata = buffer[ipos, len]
jpos = 8
while jpos < len
jtag = get_long( chunkdata[jpos, 4] )
jlen = get_long( chunkdata[jpos + 4, 4] )
if ( len <= 8 )
printf( ":xtag=%d<br>\n", jtag )
printf( ":xlen=%d<br>\n", jlen )
printf( "Warning: parsing statfile aborted\n" )
jpos = len
break
end
case jtag
when 0
jpos = len
when 3073 # FILE
ausgabe()
text = chunkdata[jpos + 7, jlen - 8]
$xf = get_text( text )
$bytes = filesize_cache( $xf )
$sum_bytes += $bytes
$all_bytes += $bytes
$size = makesize( $bytes )
$xd = text.gsub( /^.*\//, '' )
when 3074 # DESC
text = chunkdata[jpos + 7, jlen - 8]
$xd = get_text( text )
when 3076 # GETS
text = chunkdata[jpos + 8, jlen - 8]
$xg = get_long( text )
$sum_xg += $xg
$all_xg += $xg
$xg_bytes = $xg * $bytes
$sum_xg_bytes += $xg_bytes
$all_xg_bytes += $xg_bytes
# when 3080 # GROUP NAME
# text = chunkdata[jpos + 7, jlen - 8]
# group = get_text( text )
# when 3081 # GROUP DESC
# text = chunkdata[jpos + 7, jlen - 8]
# groupdesc = get_text( text )
# printf( "groupdesc %s %s\n", group, groupdesc )
when 3082 # LOCK
text = chunkdata[jpos + 7, jlen - 8]
$lock = get_text( text )
case $lock
when 'badcrc', 'old'
$xd << " (#{$lock})"
else
$xd << ' (gesperrt)'
end
end
jpos += jlen
r = jlen % 4
if ( r > 0 )
jpos += 4 - r
end
end
end
ipos += len;
r = len % 4;
if ( r > 0 )
ipos += 4 - r;
end
end
ausgabe()
ausgabetotal()
$pack = 0
$sum_bytes = 0
$sum_xg = 0
$sum_xg_bytes = 0
$transfer_bytes = 0
end
$size_filename = "size.data"
$size_cache_dirty = 0
$size_cache = Hash.new(0)
if ( FileTest.exist?($size_filename) )
begin
File.open($size_filename, 'r').each_line { |line|
line.delete!( "\n" )
line.delete!( "\r" )
words = line.split( ':' )
i = words[ 1 ].to_i
if ( i > 0 )
$size_cache[ words[ 0 ] ] = i
end
}
rescue
$stderr.print "Failure at #{$size_filename}: #{$!} => Skipping!\n"
end
end
$all_pack = 0
$all_bytes = 0
$all_xg = 0
$all_xg_bytes = 0
$all_transfer_bytes = 0
$pack = 0
$sum_bytes = 0
$sum_xg = 0
$sum_xg_bytes = 0
$transfer_bytes = 0
if ARGV.size > 0 then
ARGV.each { |filename|
File.stat(filename).file? or next
if ( /removed/.match( filename ) )
File.open(filename, 'r').each_line { |line|
line.delete!( "\n" )
line.delete!( "\r" )
if ( /^Do Not Edit This File[:] /.match( line ) )
parts = line.split( ': ', 2 )
words = parts[ 1 ].split( ' ', 4 )
$total = words[ 2 ].to_i
if ( $all_transfer_bytes > 0 )
print "\n"
end
$transfer_bytes += $total
$all_transfer_bytes += $total
next
end
words = line.split( ' ', 2 )
case words[ 0 ]
when 'xx_file'
$xf = words[ 1 ]
$bytes = 0
when 'xx_desc'
$xd = words[ 1 ]
when 'xx_size'
$bytes = words[ 1 ].to_i
when 'xx_gets'
if $bytes == 0
$bytes = filesize_cache( $xf )
end
$sum_bytes += $bytes
$all_bytes += $bytes
$size = makesize( $bytes )
$xg = words[ 1 ].to_i
$sum_xg += $xg
$all_xg += $xg
$xg_bytes = $xg * $bytes
$sum_xg_bytes += $xg_bytes
$all_xg_bytes += $xg_bytes
ausgabe()
end
}
else
bsize = File.size(filename)
begin
buffer = File.open(filename, 'r').read
parse_buffer( buffer, bsize )
rescue
$stderr.print "Failure at #{filename}: #{$!} => Skipping!\n"
end
end
}
else
usage('State-file not given!')
end
if ( $sum_bytes != $all_bytes )
print "\n"
$pack = $all_pack
$sum_bytes = $all_bytes
$sum_xg = $all_xg
$sum_xg_bytes = $all_xg_bytes
$transfer_bytes = $all_transfer_bytes
ausgabetotal()
end
if ( $size_cache_dirty > 0 )
f = File.new($size_filename, 'w')
if ( f.nil? )
$stderr.print "Failure to save cache #{$size_filename}\n"
exit 1
end
$size_cache.each {|key, value|
f.write( "#{key}:#{value}\n" )
}
f.close
end
exit 0
#