2003-04-18 22:53:25 +00:00
|
|
|
#################################
|
|
|
|
|
#
|
2003-04-20 18:02:51 +00:00
|
|
|
# $Dwarf: uuencode.rb,v 1.3 2003/04/20 16:58:07 ward Exp $
|
2003-04-18 22:53:25 +00:00
|
|
|
# $Source$
|
|
|
|
|
#
|
|
|
|
|
# article.rb
|
|
|
|
|
#
|
|
|
|
|
# (C) 2002, Ward Wouts
|
|
|
|
|
#
|
|
|
|
|
#################################
|
|
|
|
|
|
|
|
|
|
require 'tempfile'
|
|
|
|
|
|
2003-04-20 16:34:11 +00:00
|
|
|
class UUEncode
|
|
|
|
|
class << self
|
2003-04-18 22:53:25 +00:00
|
|
|
|
|
|
|
|
Debuglevel = 0
|
|
|
|
|
|
|
|
|
|
def uudecode(data, outfile=nil)
|
|
|
|
|
case data.type.to_s
|
|
|
|
|
when "Array"
|
|
|
|
|
print "Calling _uudecode_array\n" if Debuglevel>0
|
|
|
|
|
mode, filename, body = _uudecode_array(data)
|
|
|
|
|
when "File", "Tempfile"
|
|
|
|
|
unless outfile
|
|
|
|
|
print "uudecode: need outfile\n"
|
|
|
|
|
exit
|
|
|
|
|
end
|
|
|
|
|
print "Calling _uudecode_file\n" if Debuglevel>0
|
|
|
|
|
mode, filename, body = _uudecode_file(data, outfile)
|
|
|
|
|
else
|
|
|
|
|
print "Funny stuff in uudecode. Data of type \"#{data.type.to_s}\"\n"
|
|
|
|
|
end
|
|
|
|
|
return mode, filename, body
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def _uudecode_file(file, outfile)
|
|
|
|
|
mode = 0600
|
|
|
|
|
filename = "unknown"
|
|
|
|
|
c = 0
|
2003-04-20 16:58:07 +00:00
|
|
|
lines = file.pos # horrible assumption FH is at end of file
|
2003-04-18 22:53:25 +00:00
|
|
|
percent = 0
|
|
|
|
|
mark = lines/100
|
2003-04-20 16:58:07 +00:00
|
|
|
file.pos = 0
|
2003-04-18 22:53:25 +00:00
|
|
|
|
|
|
|
|
while (! file.eof)
|
|
|
|
|
line = file.gets
|
|
|
|
|
print "line: #{line}" if Debuglevel > 0
|
|
|
|
|
if line =~ /^begin(.*)/
|
|
|
|
|
m = $1
|
|
|
|
|
print "beginning matched; rest: #{m}\n" if Debuglevel > 0
|
|
|
|
|
if m =~ /^(\s+(\d+))?(\s+(.*?\S))?\s*\Z/
|
|
|
|
|
mode = $2
|
|
|
|
|
filename = $4
|
|
|
|
|
print "found beginning\n" if Debuglevel > 0
|
|
|
|
|
else
|
|
|
|
|
print "mode, file set to defaults: #{m}\n"
|
|
|
|
|
end
|
|
|
|
|
break
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if file.eof
|
|
|
|
|
print "Not UUencoded!\n"
|
|
|
|
|
return false
|
|
|
|
|
end
|
|
|
|
|
print "c: #{c} mark: #{mark} lines: #{lines}\n" if Debuglevel > 1
|
|
|
|
|
|
|
|
|
|
while (! file.eof)
|
|
|
|
|
if Debuglevel > 1
|
|
|
|
|
c = file.pos
|
|
|
|
|
if c > mark
|
|
|
|
|
print "#{percent}%\n"
|
|
|
|
|
print "c: #{c} mark: #{mark} lines: #{lines}\n" if Debuglevel > 1
|
|
|
|
|
percent += 1
|
|
|
|
|
mark = (lines/100)*(percent+1)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
line = file.gets
|
|
|
|
|
print "line: #{line}" if Debuglevel > 1
|
|
|
|
|
return mode, filename if line =~ /^end/
|
|
|
|
|
next if line =~ /[a-z]/
|
|
|
|
|
next if line == nil
|
2003-04-20 16:58:07 +00:00
|
|
|
next unless ((((line[0] - 32) & 077) + 2) / 3).to_i == (line.length/4).to_i
|
2003-04-18 22:53:25 +00:00
|
|
|
outfile.print line.unpack("u")
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
print "No \"end\" found!!!\n"
|
|
|
|
|
#return mode, file, outfile
|
|
|
|
|
return false
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# gaat volgens mij niet verder als er meerdere uuencoded blocks zijn...
|
|
|
|
|
# zal dan meerdere keren aangeroepen moeten worden, grmbl...
|
|
|
|
|
# tis getting a mess as we speak...
|
|
|
|
|
# toch maar een keer aparte class van maken...
|
|
|
|
|
def _uudecode_array(data)
|
|
|
|
|
decode = []
|
|
|
|
|
mode = 0600
|
|
|
|
|
filename = "unknown"
|
|
|
|
|
c = 0
|
|
|
|
|
lines = data.length
|
|
|
|
|
percent = 0
|
|
|
|
|
mark = lines/100
|
|
|
|
|
|
|
|
|
|
i = 0
|
|
|
|
|
while (i < data.length)
|
|
|
|
|
if data[i] =~ /^begin(.*)/
|
|
|
|
|
m = $1
|
|
|
|
|
print "beginning matched; rest: #{m}\n" if Debuglevel > 0
|
|
|
|
|
if m =~ /^(\s+(\d+))?(\s+(.*?\S))?\s*\Z/
|
|
|
|
|
mode = $2
|
|
|
|
|
filename = $4
|
|
|
|
|
print "found beginning\n" if Debuglevel > 0
|
|
|
|
|
else
|
|
|
|
|
print "mode, filename set to defaults: #{m}\n"
|
|
|
|
|
end
|
|
|
|
|
break
|
|
|
|
|
end
|
|
|
|
|
i += 1
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
unless (i < data.length)
|
|
|
|
|
print "Not UUencoded!\n"
|
|
|
|
|
return false
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
while (i < data.length)
|
|
|
|
|
if Debuglevel > 1
|
|
|
|
|
if c > mark
|
|
|
|
|
print "#{percent}%\n"
|
|
|
|
|
print "c: #{c} mark: #{mark} lines: #{lines} i: #{i}\n" if Debuglevel > 1
|
|
|
|
|
percent += 1
|
|
|
|
|
mark = (lines/100)*(percent+1)
|
|
|
|
|
end
|
|
|
|
|
c += 1
|
|
|
|
|
end
|
|
|
|
|
line = data[i]
|
|
|
|
|
i += 1
|
|
|
|
|
return mode, filename, decode if line =~ /^end/
|
|
|
|
|
next if line =~ /[a-z]/
|
|
|
|
|
next if line == nil
|
2003-04-20 16:58:07 +00:00
|
|
|
next unless ((((line[0] - 32) & 077) + 2) / 3).to_i == (line.length/4).to_i
|
2003-04-20 18:02:51 +00:00
|
|
|
unless line.unpack("u").eql?([""])
|
|
|
|
|
decode.concat(line.unpack("u"))
|
|
|
|
|
end
|
2003-04-18 22:53:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
print "No \"end\" found!!!\n"
|
|
|
|
|
return false
|
|
|
|
|
end
|
|
|
|
|
|
2003-04-20 16:34:11 +00:00
|
|
|
def is_uuencoded(data)
|
|
|
|
|
if data.to_s =~ /begin\s+\d+?\s+.*?\S?\s*$/m
|
|
|
|
|
return true
|
2003-04-18 22:53:25 +00:00
|
|
|
else
|
|
|
|
|
return false
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2003-04-20 16:34:11 +00:00
|
|
|
def get_filename(data)
|
|
|
|
|
if data.to_s =~ /^begin(\s+(\d+))?(\s+(.*?\S))?\s*$/m
|
2003-04-20 16:58:07 +00:00
|
|
|
return $4
|
2003-04-18 22:53:25 +00:00
|
|
|
end
|
2003-04-20 16:34:11 +00:00
|
|
|
return false
|
2003-04-18 22:53:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
end # class
|
2003-04-20 16:34:11 +00:00
|
|
|
end
|