-
Notifications
You must be signed in to change notification settings - Fork 4
/
offertop.rb
executable file
·249 lines (223 loc) · 5.99 KB
/
offertop.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
#!/usr/local/bin/ruby -w
#
# $Id$
# (c) 2005, Dirk Meyer, Im Grund 4, 34317 Habichtswald
# based on wirk from:;
# (C) 2002 by dpunkt.de, Armin Roehrl, Stefan Schmiedl, Clemens Wyss 2002-01-20
#
# Updates on:
# http://anime.dinoex.net/xdcc/tools/
#
class LogHash < Hash
attr_reader :re
attr_reader :title
attr_reader :art
attr_reader :klammer
# Hash mit Standardwert 0
def initialize( re, title, artm, klammer = 1 )
super(0)
@re = re
@title = title
@art = art
@klammer = klammer
end
# absteigend sortieren nach Werten
# und abschneiden
def populaerste(n)
pop = sort { |a, b| b[1] <=> a[1] }
pop[1...n]
end
def sum
total = 0
each { |b, a|
total += a
}
total
end
def printtop(n)
$output.print sum, " #{@art}\n"
$output.print size, " verschiedene #{@title}s\n"
pop = populaerste(n)
if not pop.nil?
$output.print "Die beliebtesten #{@title}s: Zahl der #{@art}, #{@title}\n"
pop.each { |b, a|
$output.printf "%7d\t%s\n", a, b
}
end
$output.print "\n"
end
end
class LogHashPack < LogHash
def printtop(n)
$output.print sum, " #{@art}\n"
$output.print size, " verschiedene #{@title}s\n"
pop = populaerste(n)
if not pop.nil?
$output.print "Die beliebtesten #{@title}s: Zahl der #{@art}, #{@title}\n"
pop.each { |b, a|
$output.printf "%7d\t#%s\t%s\n", a, b, $packs[ b.to_i ]
}
end
$output.print "\n"
end
end
# ** 2006-02-10-06:17:25: XDCC SEND #29 requested: ihs (euirc-13824fe7.bpool.celox.de)
# ** 2006-02-10-17:48:48: XDCC SEND 15 Queued (slot): Spaghetti (euirc-6ab9fef9.adsl.alicedsl.de)
# $request_pack = / XDCC SEND #*([0-9]*) (requested|Queued .slot.): [^ ]* /
# $request_nick = / XDCC SEND #*[0-9]* (requested|Queued .slot.): ([^ )]*)/
# ** 2010-07-16-22:34:21: XDCC SEND 1039: requested (katharsis [email protected] on euirc)
# ** 2010-07-16-22:34:23: XDCC SEND 1044: Queued (slot) (katharsis [email protected] on euirc)
# ** 2010-07-16-22:34:30: XDCC SEND 1057: Queued (idle slot) (katharsis [email protected] on euirc)
$request_pack = / XDCC SEND #*([0-9]*)[:] (requested|Queued .*slot.) [(][^ ]* /
$request_nick = / XDCC SEND #*[0-9]*[:] (requested|Queued .*slot.) [(]([^ )]*)/
# ** 2006-02-10-06:17:26: XDCC [515:ihs]: Connection established (84.245.180.163:1027 -> 213.239.196.229:53686)
$connected_nick = / XDCC [\[][0-9]*[:]([^\]]*)[\]]: Connection established /
# ** 2006-02-10-06:39:30: XDCC [515:ihs]: Transfer Completed (383302 KB, 22 min 3.865 sec, 289.5 KB/sec)
$completed_nick = / XDCC [\[][0-9]*[:]([^\]]*)[\]]: Transfer Completed /
#completed_speed = / XDCC [\[][0-9]*[:][^\]]*[\]]: Transfer Completed .[0-9]* KB, [^,]*, ([0-9]*[.][0-9]) /
# ** 2006-02-10-06:18:02: Stat: 1/20 Sls, 0/20 Q, 2464.8K/s Rcd, 0 SrQ (Bdw: 8100K, 67.5K/s, 2473.1K/s Rcd)
$packs = Array.new(0)
# Die Gruppierungen im regulären Ausdruck
# werden in den LogHashtabellen gezählt
def log_search( input, *hash )
# String-Objekt aus der Schleife ziehen
split = "\n"
# hash-Indizes an MatchData angleichen
hash.unshift nil
loop {
# nächster Block und der Rest der Zeile
data = input.read(4095) or break
data += (input.gets || "")
for line in data.split(split)
for i in 1...hash.length
if md = hash[i].re.match(line)
key = md[ hash[i].klammer ]
hash[i][ key ] += 1
end
end
end
}
# Karteileichen beseitigen
hash.each { |h| h.delete(nil) if h }
hash[1..-1]
end
def get_long(string)
return string.unpack('N')[ 0 ]
end
def get_text(string)
l = string.unpack('C')[ 0 ]
l -= 1
return string[1, l]
end
def parse_buffer(buffer, bsize)
fsize = bsize - 16
ipos = 8
packnr = 0
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 3072 # XDCCS
packnr += 1
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
text = chunkdata[jpos + 7, jlen - 8]
xf = get_text( text )
$packs[ packnr ] = xf.gsub( /^.*\//, '' )
when 3074 # DESC
text = chunkdata[jpos + 7, jlen - 8]
desc = get_text( text )
$packs[ packnr ] = desc
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
end
def parse_state( filename )
File.stat(filename).file? or next
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
def make_statistik( input )
return log_search( input,
LogHashPack.new( $request_pack, 'Pack', 'Anfragen' ),
LogHash.new( $request_nick, 'Nick', 'Anfragen', 2 ),
LogHash.new( $connected_nick, 'Nick', 'Verbindungen' ),
LogHash.new( $completed_nick, 'Nick', 'Downloads' )
)
end
input = 0
$output = STDOUT
if ARGV.size > 0 then
ARGV.each { |filename|
if /[.]state/.match( filename )
parse_state( filename )
next
end
if FileTest.exist?( filename )
if ( $packs.size == 0 )
state = filename.sub( /[.].*$/, '' )
parse_state( "#{state}.state" )
end
input += 1
if input == 1
$list = make_statistik( File.open(filename, 'r') )
next
end
STDERR.print "Only 1 Logfile supported!\n"
exit 1
else
$output = File.open(filename, 'w')
end
}
else
STDERR.print "Usage: offertop <statefile> <logfile> [<outputfile>]\n"
exit 64
end
if input == 0
$list = make_statistik( STDIN )
end
$list.each { |top|
top.printtop( 10 )
}
if $output != STDOUT
$output.close
end
exit 0
# eof