print -> puts

plus nog iets
This commit is contained in:
Ward Wouts 2008-02-04 21:37:09 +00:00
parent 8dcb4890ce
commit 102205e9eb

View file

@ -81,7 +81,7 @@ Debuglevel = 0
@tstart = Time.now
def save_file(dir, name, data)
print "savename: #{name}\n" if Debuglevel > 1
puts "savename: #{name}" if Debuglevel > 1
nname = name.gsub(/\//, "-")
nname.gsub!(/>/, "")
@ -138,6 +138,7 @@ def save_file(dir, name, data)
nname.gsub!(/Û/, "U"); nname.gsub!(/û/, "u")
nname.gsub!(/§/, "S"); nname.gsub!(/£/, "L")
nname.gsub!(/ß/, "ss")
### nname.gsub!('#{160.chr}', "")
### if nname.match(/([#{128.chr}-#{255.chr}])/)
@ -147,7 +148,7 @@ def save_file(dir, name, data)
nname.sub!(/\s*$/, "")
nname.sub!(/^[\s\.-]*/, "")
print "nname: #{nname}\n" if Debuglevel > 1
puts "nname: #{nname}" if Debuglevel > 1
newname = nname[0...@maxfilelength] # all the recodings could have made
# the filename too long
count = 1
@ -157,41 +158,44 @@ def save_file(dir, name, data)
newname = "#{nname}-<#{date}.#{count}>"
count += 1
end
print "name: #{newname}\n" if Debuglevel > 1
puts "name: #{newname}" if Debuglevel > 1
begin
case data.class.to_s
when "String"
if File.move(data, "#{dir}/#{newname}")
print " Saving as: '#{newname}'\n"
puts " Saving as: '#{newname}'"
else
print "couldn't rename tempfile\n"
puts "couldn't rename tempfile"
return false
end
when "Array"
if file = File.new("#{dir}/#{newname}", "w", 0644)
print " Saving as: '#{newname}'\n"
puts " Saving as: '#{newname}'"
data.collect{|i| file.print "#{i}"}
else
print "couldn't open file for writeing\n"
puts "couldn't open file for writeing"
return false
end
when "Tempfile"
if File.move(data.path, "#{dir}/#{newname}")
print " Saving as: '#{newname}'\n"
puts " Saving as: '#{newname}'"
else
print "couldn't rename tempfile\n"
puts "couldn't rename tempfile #{data.path}"
return false
end
else
print "EEEEPS Can't save data of class: #{data.class.to_s}\n"
puts "EEEEPS Can't save data of class: #{data.class.to_s}"
return false
end
rescue Errno::ENOENT, Errno::EINVAL
print "Caught #{$!.class} (save_file)\n"
print "Error: #{$!}\n"
print "What the *beep* happened?\n"
print "(if Errno::EINVAL probably evil chars in filename)\n"
puts "Caught #{$!.class} (save_file)"
puts "Error: #{$!}"
puts "What the *beep* happened?"
puts "(if Errno::EINVAL probably evil chars in filename)"
if data.class.to_s == "Tempfile"
puts "keeping #{data.path}"
end
return false
end
return true
@ -216,31 +220,36 @@ def parse_options(options)
options[opt] = arg
end
rescue GetoptLong::InvalidOption
print "#{$!}\n"
puts "#{$!}"
usage
end
return options
end
def usage
print "\nUsage:\n\n"
print "ripnews.rb [-I <pattern>] [-c <file>] [-L] [-C] [-M] [-S] [-T] [-X <pattern>]\n\n"
print "-I <pattern> specify an include pattern\n"
print "-c <file> specify an alternate configfile\n"
print "-L use subject as filename\n"
print "-C use combined filenames\n"
print "-M get multipart articles\n"
print "-s exit silently if already running\n"
print "-S get singlepart articles\n"
print "-T test mode, don't update newsrc file\n"
print "-X <pattern> specify an exclude pattern\n"
puts <<EOT
Usage:
ripnews.rb [-I <pattern>] [-c <file>] [-L] [-C] [-M] [-S] [-T] [-X <pattern>]
-I <pattern> specify an include pattern
-c <file> specify an alternate configfile
-L use subject as filename
-C use combined filenames
-M get multipart articles
-s exit silently if already running
-S get singlepart articles
-T test mode, don't update newsrc file
-X <pattern> specify an exclude pattern
EOT
exit
end
def parse_config(default = {})
if ! default.has_key?('-s')
print "Parsing config\n"
print "#{default['-c']}\n"
puts "Parsing config"
puts "#{default['-c']}"
end
if FileTest.readable?("#{default['-c']}")
file = File.new("#{default['-c']}")
@ -282,7 +291,7 @@ def parse_config(default = {})
if line =~ /^OPT_(.*?)=(.*)/
line = "-#{$1}=#{$2}"
end
print "#{i}: #{line}\n" if Debuglevel > 1
puts "#{i}: #{line}" if Debuglevel > 1
if line =~ /(.*?)\s*\+=\s*(.*)/
if group == ""
if default.has_key?($1)
@ -326,21 +335,21 @@ def parse_config(default = {})
elsif line =~ /^$/
next
else
print "Error parsing config on line: #{i}\n"
puts "Error parsing config on line: #{i}"
return false
end
end
if group != ""
print "Error parsing config: group not terminated on line #{i}\n"
puts "Error parsing config: group not terminated on line #{i}"
return false
end
if Debuglevel > 2
config.each_key{|x|
print "Group: #{x}\n"
puts "Group: #{x}"
config[x].each_key{|y|
print "Key: '#{y}' => Value: '#{config[x][y]}'\n"
puts "Key: '#{y}' => Value: '#{config[x][y]}'"
}
}
end
@ -349,12 +358,12 @@ end
def check_config
if @config.length == 0
print "No configuration, nothing to do\n"
puts "No configuration, nothing to do"
exit
end
@config.each_key {|i|
unless @config[i].has_key?("-I")
print "No inclusions given for group #{i}. Won't match anything.\n"
puts "No inclusions given for group #{i}. Won't match anything."
end
@config[i]["DATADIR"] ="." unless @config[i].has_key?("DATADIR")
@config[i]["PERMISSION"] = "0755" unless @config[i].has_key?("PERMISSION")
@ -383,15 +392,15 @@ def lock
begin
Process.kill(0, pid.to_i)
if ! @config[group].has_key?('-s')
print "Already running, exiting...\n"
puts "Already running, exiting..."
end
exit
rescue Errno::ESRCH
print "Stale lock found... removing...\n"
puts "Stale lock found... removing..."
File.unlink(@config[group]["LOCKFILE"])
end
else
print "Empty lockfile found... removing...\n"
puts "Empty lockfile found... removing..."
File.unlink(@config[group]["LOCKFILE"])
end
end
@ -414,31 +423,31 @@ def renice
end
def get_single(subj, group)
print "Fetching singlepart article: #{subj}\n"
puts "Fetching singlepart article: #{subj}"
body = @articles.get_group_body(subj)
if UUEncode.is_uuencoded(body)
filename = UUEncode.get_filename(body)
print " filename #{filename}\n"
puts " filename #{filename}"
unless check_ext(group, filename, "s", subj)
print " Skipping article...\n"
puts " Skipping article..."
return false
end
print " UUDecoding...\n"
puts " UUDecoding..."
mode, filename, body = UUEncode.uudecode(body)
elsif YEnc.is_yencoded(body)
filename = YEnc.get_filename(body)
unless check_ext(group, filename, "s", subj)
print " Skipping article...\n"
puts " Skipping article..."
return false
end
print " YDecoding...\n"
puts " YDecoding..."
mode, filename, body = YEnc.ydecode(body)
else
print " Unknown encoding (not UU, not yEnc), skipping...\n"
puts " Unknown encoding (not UU, not yEnc), skipping..."
return false
end
if mode == false
print " Decoding failed skipping article...\n"
puts " Decoding failed skipping article..."
return false
end
output_data(subj, mode, filename, body)
@ -446,22 +455,22 @@ def get_single(subj, group)
end
def get_multi(subj, group)
print "Fetching multipart article: #{subj}\n"
puts "Fetching multipart article: #{subj}"
body = @articles.get_group_body_first(subj)
if UUEncode.is_uuencoded(body) or YEnc.is_yencoded(body)
if UUEncode.is_uuencoded(body)
filename = UUEncode.get_filename(body)
print " filename #{filename}\n"
puts " filename #{filename}"
unless check_ext(group, filename, "m", subj)
print " Skipping article...\n"
puts " Skipping article..."
return false
end
elsif YEnc.is_yencoded(body)
print "yencc\n"
puts "yenc"
filename = YEnc.get_filename(body)
print "filename #{filename}\n"
puts "filename #{filename}"
unless check_ext(group, filename, "m", subj)
print " Skipping article...\n"
puts " Skipping article..."
return false
end
end
@ -469,7 +478,7 @@ def get_multi(subj, group)
if @config[group]["TEMPDIR"] == nil or @config[group]["TEMPDIR"] == ""
bodyrest = @articles.get_group_body_rest(subj)
unless bodyrest
print " Skipping article...\n"
puts " Skipping article..."
return false
end
body.concat(bodyrest)
@ -478,7 +487,7 @@ def get_multi(subj, group)
body.collect{|x| file.print "#{x}\n"}
unless @articles.get_group_body_rest(subj, file)
print " Skipping article...\n"
puts " Skipping article..."
return false
end
fileout = Tempfile.new("riptmp", @config[group]["TEMPDIR"])
@ -489,14 +498,14 @@ def get_multi(subj, group)
Thread.pass
# puts "inside thread post pass\n"
if UUEncode.is_uuencoded(tbody)
print " UUDecoding...\n"
puts " UUDecoding..."
if tfile
tmode, tfilename, tbody = UUEncode.uudecode(tfile, tfileout)
else
tmode, tfilename, tbody = UUEncode.uudecode(tbody)
end
elsif YEnc.is_yencoded(tbody)
print " YDecoding...\n"
puts " YDecoding..."
begin
if tfile
tmode, tfilename, tbody = YEnc.ydecode(tfile, tfileout)
@ -509,13 +518,13 @@ def get_multi(subj, group)
# XXX return succes even though it's not true
Thread.current.exit
rescue PermError
print "#{$!}\n"
print " Skipping article...\n"
puts "#{$!}"
puts " Skipping article..."
Thread.current.exit
end
end
if tmode == false
print " Decoding failed skipping article...\n"
puts " Decoding failed skipping article..."
Thread.current.exit
end
@ -539,7 +548,7 @@ def get_multi(subj, group)
return true
else
print " Unknown encoding (not UU, not yEnc), skipping...\n"
puts " Unknown encoding (not UU, not yEnc), skipping..."
return false
end
end
@ -552,8 +561,8 @@ end
def output_data(subject, mode, filename="", body="")
group = @articles.get_groupname
print " mode: #{mode}\n" if Debuglevel > 0
print " Filename: '#{filename}'\n" if Debuglevel > 0
puts " mode: #{mode}" if Debuglevel > 0
puts " Filename: '#{filename}'" if Debuglevel > 0
# de-crap subject...
sub = subject.sub(/\s*$/, "") # strip trailing spaces
@ -561,17 +570,17 @@ def output_data(subject, mode, filename="", body="")
# decide on a filename
if @config[group].has_key?("-L") and @config[group]["-L"]
print "longname\n" if Debuglevel > 1
puts "longname" if Debuglevel > 1
outfile = sub[0...@maxfilelength]
elsif @config[group].has_key?("-C") and @config[group]["-C"]
print "combinedname\n" if Debuglevel > 1
puts "combinedname" if Debuglevel > 1
outfile = sub[0...@maxfilelength-filename.length-3]
outfile = "#{outfile} [#{filename}]"
if outfile.length > @maxfilelength
outfile = filename[0...@maxfilelength]
end
elsif @config[group].has_key?("-CP") and @config[group]["-CP"]
print "combinedname\n" if Debuglevel > 1
puts "combinedname" if Debuglevel > 1
poster = @articles.get_group_poster(subject)
outfile = sub[0...@maxfilelength-poster.length-filename.length-6]
outfile = "#{outfile} [#{poster}] [#{filename}]"
@ -579,7 +588,7 @@ def output_data(subject, mode, filename="", body="")
outfile = filename[0...@maxfilelength]
end
else
print "shortname\n" if Debuglevel > 1
puts "shortname" if Debuglevel > 1
outfile = filename[0...@maxfilelength]
end
@ -596,27 +605,27 @@ def check_ext(group, filename, mode, subject)
case mode
when "s"
if @config[group].has_key?("-SD") && ( filename =~ /\.(#{@config[group]["-SD"]})$/ )
print "Marking '#{subject}' as read\n"
puts "Marking '#{subject}' as read"
@articles.group_update_newsrc(subject)
return false
end
return @config[group].has_key?("-S") ? ( filename =~ /\.(#{@config[group]["-S"]})$/ ) : true
when "m"
if @config[group].has_key?("-MD") && ( filename =~ /\.(#{@config[group]["-MD"]})$/ )
print "Marking '#{subject}' as read\n"
puts "Marking '#{subject}' as read"
@articles.group_update_newsrc(subject)
return false
end
return @config[group].has_key?("-M") ? ( filename =~ /\.(#{@config[group]["-M"]})$/ ) : true
else
print "Illegal mode \"#{mode}\" in check_ext\n"
puts "Illegal mode \"#{mode}\" in check_ext"
exit
end
end
def get_max_file_length(tempdir=".")
if ! FileTest.directory?("#{tempdir}") || ! FileTest.writable?("#{tempdir}")
print "Tempdir '#{tempdir}' is not a writable directory\n"
puts "Tempdir '#{tempdir}' is not a writable directory"
exit
end
# this is quite stupid, there is no guarantee at all the generated file names
@ -630,8 +639,8 @@ def get_max_file_length(tempdir=".")
name = name[0...-1]
retry
rescue Errno::ENOENT
print "#{$!}\n"
print "strange...\n"
puts "#{$!}"
puts "strange..."
retry
end
# this is how many characters are still likely to be appended
@ -668,27 +677,27 @@ def startup
renice
trap("HUP") {
print "Rereading config...\n"
puts "Rereading config..."
config = parse_config(@defaults)
if config != false
@config = config
check_config
print "Done reading config\n"
puts "Done reading config"
else
print "Keeping old config due to errors\n"
puts "Keeping old config due to errors"
end
}
@maxfilelength = get_max_file_length(@config[@config.keys[0]]["TEMPDIR"])
print "\n$Id$\n"
print "Starting: #{@tstart}\n"
puts "\n$Id$"
puts "Starting: #{@tstart}"
if Debuglevel > 2
@config.each_key{|i|
print "Group: #{i}\n"
puts "Group: #{i}"
@config[i].each_key{|j|
print "Opt: #{j} val: #{@config[i][j]}\n"
puts "Opt: #{j} val: #{@config[i][j]}"
}
}
end
@ -700,10 +709,10 @@ def main
@decode_threads = []
@newsrc_lock = Mutex.new
profile_mem("#{group} start")
print "\nGetting articles for #{group}\n"
puts "\nGetting articles for #{group}"
@articles = Article.new(@config[group]["NNTPSERVER"], group, @config[group]["NEWSRCNAME"])
fill_preselector(group)
print "initialized\n"
puts "initialized"
@articles.get_articles(@config[group]["CACHEDIR"])
profile_mem("#{group} articles read")
@ -711,23 +720,23 @@ def main
_create_group_dir(group)
for subj in @articles.get_group_subjects.sort{|a, b| ward_sort(a, b)}
print "#{subj}\n" if Debuglevel > 2
puts "#{subj}" if Debuglevel > 2
poster = @articles.get_group_poster(subj)
# explicitly mark as read
if @config[group].has_key?("-MR") and subj =~ /#{@config[group]["-MR"]}/
print "Marking '#{subj}' as read\n"
puts "Marking '#{subj}' as read"
_mark_read(subj)
# get the juicy bits
elsif @config[group].has_key?("-MRF") and poster =~ /#{@config[group]["-MRF"]}/
print "Marking poster '#{poster}' as read\n"
puts "Marking poster '#{poster}' as read (subj: '#{subj}')"
_mark_read(subj)
elsif !(@config[group].has_key?("-X") and subj =~ /#{@config[group]["-X"]}/) and
subj =~ /#{@config[group]["-I"]}/
print "Match: #{subj}\n" if Debuglevel > 0
puts "Match: #{subj}" if Debuglevel > 0
_get_article(subj, group)
elsif !(@config[group].has_key?("-XF") and poster =~ /#{@config[group]["-XF"]}/) and
@config[group].has_key?("-IF") and poster =~ /#{@config[group]["-IF"]}/
print "Poster match: #{poster}\n" if Debuglevel > 0
puts "Poster match: #{poster}" if Debuglevel > 0
_get_article(subj, group)
else
_mark_remaining(subj, group)
@ -747,7 +756,7 @@ end
def ending
tend = Time.now
print "\nFinished: #{tend}\n"
puts "\nFinished: #{tend}"
runtime = (tend - @tstart).to_i
h=runtime/3600
m=runtime%3600
@ -760,7 +769,7 @@ end
def _create_group_dir(group)
unless FileTest.directory?("#{@config[group]["DATADIR"]}/#{group}") or
Dir.mkdir("#{@config[group]["DATADIR"]}/#{group}", @config[group]["PERMISSION"].oct)
print "eeeps, couldn't create dir\n"
puts "eeeps, couldn't create dir"
exit
end
end
@ -768,7 +777,7 @@ end
def _primary_thres_skip(subj, group)
if @config[group].has_key?("PRIMARYTHRES")
if ( @articles.group_percentage_primary(subj) < @config[group]["PRIMARYTHRES"].to_i )
print "Only #{@articles.group_percentage_primary(subj)}% on primary, skipping: #{subj}\n"
puts "Only #{@articles.group_percentage_primary(subj)}% on primary, skipping: #{subj}"
return true
end
end
@ -778,7 +787,7 @@ end
def _fallback_thres_skip(subj, group)
if @config[group].has_key?("FALLBACKTHRES")
if ( @articles.group_percentage_fallback(subj) > @config[group]["FALLBACKTHRES"].to_i )
print "#{@articles.group_percentage_fallback(subj)}% only on fallback, skipping: #{subj}\n"
puts "#{@articles.group_percentage_fallback(subj)}% only on fallback, skipping: #{subj}"
return true
end
end
@ -802,14 +811,14 @@ def _get_article(subj, group)
get_multi(subj, group)
end
rescue TempError, PermError, YencError
print "#{$!}\n"
print " Skipping article...\n"
#print "Caught #{$!.class}\n"
#print "Error: #{$!}\n"
puts "#{$!}"
puts " Skipping article..."
#puts "Caught #{$!.class}"
#puts "Error: #{$!}"
end
end
else
print "Not complete: #{subj}\n"
puts "Not complete: #{subj}"
end
end
@ -821,7 +830,7 @@ def _mark_remaining(subj, group)
if subj =~ /#{@config[group]["-I"]}/
puts "fucking up here"
end
print "Marking remaining '#{subj}' as read\n"
puts "Marking remaining '#{subj}' as read"
@articles.group_update_newsrc(subj)
end
end