code cleaning

This commit is contained in:
Ward Wouts 2003-04-18 22:27:58 +00:00
parent 7c354b7ba7
commit 256e5692b3

View file

@ -1,6 +1,6 @@
#!/usr/local/bin/ruby -w #!/usr/local/bin/ruby -w
# $Dwarf: ripnews.rb,v 1.41 2003/04/18 21:06:51 ward Exp $ # $Dwarf: ripnews.rb,v 1.42 2003/04/18 21:39:29 ward Exp $
# $Source$ # $Source$
require 'date' require 'date'
@ -17,10 +17,9 @@ Debuglevel = 0
def save_file(dir, name, data) def save_file(dir, name, data)
print "savename: #{name}\n" if Debuglevel > 1 print "savename: #{name}\n" if Debuglevel > 1
nname = name.gsub(/\//, "-") nname = name.gsub(/\//, "-")
print "nname: #{nname}\n" if Debuglevel > 1
nname.sub!(/\s*$/, "") nname.sub!(/\s*$/, "")
nname.sub!(/^\s*/, "") nname.sub!(/^[\s\.]*/, "")
nname.sub!(/^\.*/, "") print "nname: #{nname}\n" if Debuglevel > 1
newname = nname newname = nname
count = 1 count = 1
d = Date.today d = Date.today
@ -227,16 +226,14 @@ def get_single(subj, group)
body = @articles.get_group_body(subj) body = @articles.get_group_body(subj)
if @articles.is_uuencoded(body) if @articles.is_uuencoded(body)
mode, filename, body = @articles.uudecode(body) mode, filename, body = @articles.uudecode(body)
return false unless check_ext(group, filename, "s") elsif @articles.is_yencoded(body)
return mode, filename, body
end
if @articles.is_yencoded(body)
mode, filename, body = @articles.ydecode(body) mode, filename, body = @articles.ydecode(body)
return false unless check_ext(group, filename, "s") else
return mode, filename, body print " Unknown encoding (not UU, not yEnc), skipping...\n"
return false
end end
print " Unknown encoding (not UU, not yEnc), skipping...\n" return false unless check_ext(group, filename, "s")
return false return mode, filename, body
end end
def get_multi(subj, group) def get_multi(subj, group)
@ -245,23 +242,21 @@ def get_multi(subj, group)
body = @articles.get_group_body(subj) body = @articles.get_group_body(subj)
if @articles.is_uuencoded(body) if @articles.is_uuencoded(body)
mode, filename, body = @articles.uudecode(body) mode, filename, body = @articles.uudecode(body)
return false unless check_ext(group, filename, "m")
print " should have a match here else something's wrong\n"
return mode, filename, body
elsif @articles.is_yencoded(body) elsif @articles.is_yencoded(body)
mode, filename, body = @articles.ydecode(body) mode, filename, body = @articles.ydecode(body)
return false unless check_ext(group, filename, "m") else
print " should have a match here else something's wrong\n" print " Unknown encoding (not UU, not yEnc), skipping...\n"
return mode, filename, body return false
end end
print " Unknown encoding (not UU, not yEnc), skipping...\n" return false unless check_ext(group, filename, "m")
return false print " should have a match here else something's wrong\n"
return mode, filename, body
else else
body = @articles.get_group_body_first(subj) body = @articles.get_group_body_first(subj)
if @articles.is_uuencoded(body) or @articles.is_yencoded(body) if @articles.is_uuencoded(body) or @articles.is_yencoded(body)
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"}
# hire moet een extensie check!!! # 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 @articles.is_uuencoded(body) if @articles.is_uuencoded(body)
@ -292,28 +287,23 @@ def output_data(subject, mode, filename="", body="")
print " Filename: '#{filename}'\n" if Debuglevel > 0 print " Filename: '#{filename}'\n" if Debuglevel > 0
# de-crap subject... # de-crap subject...
subject.sub!(/\s*$/, "") sub = subject.sub(/\s*$/, "") # strip trailing spaces
subject.sub!(/^[\s\.!-]*/, "") sub.sub!(/^[\s\.!-]*/, "") # strip leading spaces, dots, exclamation points and dashes
# decide on a filename # decide on a filename
if @config[group].has_key?("-L") and @config[group]["-L"] if @config[group].has_key?("-L") and @config[group]["-L"]
print "longname\n" if Debuglevel > 1 print "longname\n" if Debuglevel > 1
outfile = subject outfile = sub[0...@maxfilelength]
outfile = outfile[0...@maxfilelength]
elsif @config[group].has_key?("-C") and @config[group]["-C"] elsif @config[group].has_key?("-C") and @config[group]["-C"]
print "combinedname\n" if Debuglevel > 1 print "combinedname\n" if Debuglevel > 1
outfile = outfile[0...@maxfilelength-filename.length-3] outfile = sub[0...@maxfilelength-filename.length-3]
outfile = "#{subject} [#{filename}]" outfile = "#{outfile} [#{filename}]"
if outfile.length > @maxfilelength if outfile.length > @maxfilelength
outfile = filename outfile = filename[0...@maxfilelength]
outfile = outfile[0...@maxfilelength]
end end
else else
print "shortname\n" if Debuglevel > 1 print "shortname\n" if Debuglevel > 1
outfile = filename outfile = filename[0...@maxfilelength]
while outfile.length > @maxfilelength
outfile = outfile[0...-1]
end
end end
# do the actual saving # do the actual saving
@ -353,20 +343,18 @@ def check_ext(group, filename, mode)
end end
def get_max_file_length(tempdir=".") def get_max_file_length(tempdir=".")
i = 500 name = "a"*500
name = "a"*i begin
begin file = File.new("#{tempdir}/#{name}", "w", "0644").close
file = File.new("#{tempdir}/#{name}", "w", "0644").close File.delete("#{tempdir}/#{name}")
File.delete("#{tempdir}/#{name}") rescue Errno::ENAMETOOLONG
rescue Errno::ENAMETOOLONG name = name[0...-1]
i -= 1 retry
name = "a"*i end
retry # this is how many characters are still likely to be appended
end # is the filename already exists '-<#{date}.#{count}>' in save_file
i -= 14 # this is how many characters are still likely to be appended # this could be brought back to 5 '-<#{count}>' ...
# is the filename already exists '-<#{date}.#{count}>' in save_file return name.length - 14
# this could be brought back to 5 '-<#{count}>' ...
return i
end end
############################################################################################# #############################################################################################