split off ydecoding stuff

This commit is contained in:
Ward Wouts 2003-04-20 18:39:13 +00:00
parent cf34fe16c8
commit 7a09a4634e
2 changed files with 20 additions and 318 deletions

View file

@ -1,6 +1,6 @@
################################# #################################
# #
# $Dwarf: article.rb,v 1.59 2002/11/05 10:29:51 ward Exp $ # $Dwarf: article.rb,v 1.60 2003/04/20 16:34:40 ward Exp $
# $Source$ # $Source$
# #
# article.rb # article.rb
@ -49,7 +49,6 @@ def initialize(nntpservers, groupname, newsrc="~/.newsrc")
del_server(server) del_server(server)
end end
} }
@ymap = {}
end end
def reconnect(server) def reconnect(server)
@ -527,311 +526,6 @@ def save_cache(cachedir)
end end
end end
#######################################################################
def ydecode(data, outfile=nil)
if @ymap.empty?
(-106..255).each do |b|
@ymap[b]=((b-42)%256)
end
end
case data.type.to_s
when "Array"
print "Calling _ydecode_array\n" if Debuglevel>0
mode, filename, body = _ydecode_array(data)
when "File", "Tempfile"
unless outfile
print "ydecode: need outfile\n"
exit
end
print "Calling _ydecode_file\n" if Debuglevel>0
mode, filename, body = _ydecode_file(data, outfile)
else
print "Funny stuff in ydecode. Data of type \"#{data.type.to_s}\"\n"
end
return mode, filename, body
end
def _ydecode_line(line)
i = 0
ll = line.length
ostr = ''
while i < ll
if line[i] == 0x3d
i += 1
line[i] -= 64
end
ostr << @ymap[line[i]]
i += 1
end
return ostr, ostr.length
end
def _ydecode_file(file, outfile)
mode = 0600
filename = "unknown"
lines = file.pos
file.pos = 0
bytes = 0
total = 0
oldpartend = 0
search_begin = false
while (! file.eof)
line = file.gets
print "line: #{line}" if Debuglevel > 0
if line =~ /^\=ybegin\s+(.*line\=.*)/
m = $1
print "ybegin match; rest: #{m}\n" if Debuglevel > 0
if m =~ /^\s*(part\=(\d+)\s+)?(total\=(\d+)\s+)?(line\=(\d+))(\s*size\=(\d+))(\s*name=(.*))\Z/
part = $2.to_i
total = $4.to_i
linesize = $6.to_i
totalsize = $8.to_i
filename = $10
print "found beginning"
if part != nil
print " of part #{part}"
end
if total != nil
print " of #{total}"
end
print ", linesize = #{linesize}, size = #{totalsize}, filename = #{filename}\n"
break
else
print "not a valid yenc begin line\n"
end
end
end
if file.eof
print "Not yencoded!\n"
return false
end
print " ydecoding...\n"
while (! file.eof)
print "at #{file.pos} need to go to #{lines}\n" if Debuglevel > 1
line = file.gets
line.chop!
if line =~ /^=yend\s+(.*)\Z/
m = $1
m =~ /(\s*size=(\d+)\s+)(\s*part=(\d+))?(\s+crc32=(\S+))?/
size = $2.to_i
part = $4.to_i
crc = $6
if size != bytes
print "part size mismatch, is #{bytes}, should be #{size}\n"
end
if part == nil
return mode, filename
end
total += bytes
if total >= totalsize
if total != totalsize
print "total size mismatch, is #{total}, should be #{totalsize}\n"
end
return mode, filename
end
search_begin = true
bytes = 0
next
end
if search_begin && line =~ /^\=ybegin\s+(.*)\Z/
m = $1
search_begin = false
if m =~ /^\s*(part\=(\d+)\s+)?(total\=(\d+)\s+)?(line\=(\d+))(\s*size\=(\d+))(\s*name=(.*))\Z/
part = $2.to_i
total = $4.to_i
linesize = $6.to_i
totalsize = $8.to_i
filename = $10
print "found beginning of part #{part}, linesize = #{linesize}, size = #{totalsize}, filename = #{filename}\n" if Debuglevel > 0
end
next
end
if search_begin == true
next
end
if line =~ /^=ypart\s+(\s*begin=(\d+))(\s+end=(\d+))/
b = $2
e = $4
print " next part begin #{b}, end #{e}\n"
if b.to_i == oldpartend + 1
oldpartend = e.to_i
else
raise PermError, "Parts not continuous! last end #{oldpartend}, begin #{b}"
end
next
end
# This seems to be a common 'error' - maybe I misunderstand the spec or
# something
# if line.length != linesize
# print "linesize mismatch, was #{line.length}, should be #{linesize}...\n"
# end
# i = 0
# ll = line.length
# ostr = ''
# while i < ll
# if line[i] == 0x3d
# i += 1
# line[i] -= 64
# end
# ostr << ymap[line[i]]
# i += 1
# end
ostr, ostrl = _ydecode_line(line)
outfile << ostr
bytes += ostrl
end
print "No \"=yend\" found!!!\n"
return mode, filename, outfile
end
# toch maar een keer aparte class van maken... geld ook voor dit geneuzel
def _ydecode_array(data)
decode = ""
mode = 0600
filename = "unknown"
c = 0
lines = data.length
bytes = 0
percent = 0
mark = lines/100
# ymap = {}
# (-106..255).each do |b|
# ymap[b]=((b-42)%256)
# end
i = 0
while (i < data.length)
if data[i] =~ /^\=ybegin\s+(.*line\=.*)/
m = $1
print "ybegin match; rest: #{m}\n" if Debuglevel > 0
if m =~ /^\s*(part\=(\d+)\s+)?(total\=(\d+)\s+)?(line\=(\d+))(\s*size\=(\d+))(\s*name=(.*))\Z/
part = $2.to_i
total = $4.to_i
linesize = $6.to_i
size = $8.to_i
filename = $10
print "found beginning, linesize = #{linesize}, size = #{size}, filename = #{filename}\n" if Debuglevel > 0
i += 1
break
else
print "not a valid yenc begin line\n"
end
end
i += 1
end
unless (i < data.length)
print "Not yencoded!\n"
return false
end
print "ydecoding...\n"
while (i < data.length)
print "at #{i} need to go to #{data.length}\r" if Debuglevel > 1
line = data[i]
i += 1
if line =~ /^\=yend(\s+size=(\d+))(\s+crc32=(\S+))?/
size = $2.to_i
crc = $4
if size != decode.length
print "size mismatch, was #{decode.length}, should be #{size}\n"
end
dec = [ decode ]
return mode, filename, dec
end
if line =~ /^\=ypart.*\Z/
# ignore for now
next
end
# This seems to be a common 'error' - maybe I misunderstand the spec or
# something
# if line.length != linesize
# print "#{i}: linesize mismatch, was #{line.length}, should be #{linesize}...\n"
# end
# j = 0
# ll = line.length
# ostr = ''
# while j < ll
# if line[j] == 0x3d
# j += 1
# line[j] -= 64
# end
# ostr << ymap[line[j]]
# j += 1
# end
# decode << ostr
# bytes += ostr.length
ostr, ostrl = _ydecode_line(line)
decode << ostr
bytes += ostrl
# special = 0
# str = ""
# line.each_byte { |b|
# if special == 0
# if b == 0x3d
# special = 1
# next
# end
# else
# special = 0
# b = (b - 64) % 256
# end
# str << ((b - 42) % 256).chr
# }
# decode << str
end
print "${i}: no \"=yend\" found!!!\n"
dec = [ decode ]
return mode, filename, dec
end
def ydecode_group(subj, tempdir=nil)
group_subjects unless @grouped
body = get_group_body_first(subj)
if body.to_s =~ /=ybegin/
print "yencoded!\n" if Debuglevel > 0
#if (file and outfile)
if (tempdir != nil)
file = Tempfile.new("#{tempdir}/riptmp")
body.collect{|i| file.print "#{i}\n"}
get_group_body_rest(subj, file)
mode, filename, result = ydecode(file, outfile)
else
body.concat(get_group_body_rest(subj))
mode, filename, result = ydecode(body)
end
return mode, filename, result
else
print "Not yencoded!\n" if Debuglevel > 0
return false
end
end
def is_yencoded(data)
if data.to_s =~ /=ybegin/
return true
else
return false
end
end
############################################################### ###############################################################
# a bas64 decoder... # a bas64 decoder...

View file

@ -1,6 +1,6 @@
#!/usr/local/bin/ruby -w #!/usr/local/bin/ruby -w
# $Dwarf: ripnews.rb,v 1.45 2003/04/19 12:42:12 ward Exp $ # $Dwarf: ripnews.rb,v 1.46 2003/04/20 16:34:40 ward Exp $
# $Source$ # $Source$
require 'date' require 'date'
@ -228,10 +228,13 @@ def get_single(subj, group)
if UUEncode.is_uuencoded(body) if UUEncode.is_uuencoded(body)
filename = UUEncode.get_filename(body) filename = UUEncode.get_filename(body)
return false unless check_ext(group, filename, "s") return false unless check_ext(group, filename, "s")
print " UUDecoding...\n"
mode, filename, body = UUEncode.uudecode(body) mode, filename, body = UUEncode.uudecode(body)
elsif @articles.is_yencoded(body) elsif YEnc.is_yencoded(body)
mode, filename, body = @articles.ydecode(body) filename = YEnc.get_filename(body)
return false unless check_ext(group, filename, "s") return false unless check_ext(group, filename, "s")
print " YDecoding...\n"
mode, filename, body = YEnc.ydecode(body)
else else
print " Unknown encoding (not UU, not yEnc), skipping...\n" print " Unknown encoding (not UU, not yEnc), skipping...\n"
return false return false
@ -246,10 +249,13 @@ def get_multi(subj, group)
if UUEncode.is_uuencoded(body) if UUEncode.is_uuencoded(body)
filename = UUEncode.get_filename(body) filename = UUEncode.get_filename(body)
return false unless check_ext(group, filename, "m") return false unless check_ext(group, filename, "m")
print " UUDecoding...\n"
mode, filename, body = UUEncode.uudecode(body) mode, filename, body = UUEncode.uudecode(body)
elsif @articles.is_yencoded(body) elsif YEnc.is_yencoded(body)
mode, filename, body = @articles.ydecode(body) filename = YEnc.get_filename(body)
return false unless check_ext(group, filename, "m") return false unless check_ext(group, filename, "m")
print " YDecoding...\n"
mode, filename, body = YEnc.ydecode(body)
else else
print " Unknown encoding (not UU, not yEnc), skipping...\n" print " Unknown encoding (not UU, not yEnc), skipping...\n"
return false return false
@ -257,23 +263,25 @@ def get_multi(subj, group)
return mode, filename, body return mode, filename, body
else else
body = @articles.get_group_body_first(subj) body = @articles.get_group_body_first(subj)
if UUEncode.is_uuencoded(body) or @articles.is_yencoded(body) if UUEncode.is_uuencoded(body) or YEnc.is_yencoded(body)
if UUEncode.is_uuencoded(body) if UUEncode.is_uuencoded(body)
filename = UUEncode.get_filename(body) filename = UUEncode.get_filename(body)
return false unless check_ext(group, filename, "m") return false unless check_ext(group, filename, "m")
elsif YEnc.is_yencoded(body)
filename = YEnc.get_filename(body)
return false unless check_ext(group, filename, "m")
end end
file = Tempfile.new("riptmp", @config[group]["TEMPDIR"]) file = Tempfile.new("riptmp", @config[group]["TEMPDIR"])
body.collect{|x| file.print "#{x}\n"} body.collect{|x| file.print "#{x}\n"}
# hier moet een extensie check!!!
return false unless @articles.get_group_body_rest(subj, file) return false unless @articles.get_group_body_rest(subj, file)
fileout = Tempfile.new("riptmp", @config[group]["TEMPDIR"]) fileout = Tempfile.new("riptmp", @config[group]["TEMPDIR"])
if UUEncode.is_uuencoded(body) if UUEncode.is_uuencoded(body)
print " UUDecoding...\n"
mode, filename, body = UUEncode.uudecode(file, fileout) mode, filename, body = UUEncode.uudecode(file, fileout)
elsif @articles.is_yencoded(body) elsif YEnc.is_yencoded(body)
mode, filename, body = @articles.ydecode(file, fileout) print " YDecoding...\n"
mode, filename, body = YEnc.ydecode(file, fileout)
end end
# in plaats van hier
return false unless check_ext(group, filename, "m")
body = fileout.path body = fileout.path
bodybase = body.sub(/\/[^\/]*$/, "") bodybase = body.sub(/\/[^\/]*$/, "")
File.rename(body, "#{bodybase}/ripnewsdecode") File.rename(body, "#{bodybase}/ripnewsdecode")