ripnews/trunk/ripnews/ripnews.rb

196 lines
4.1 KiB
Ruby
Raw Normal View History

2002-04-27 20:31:59 +00:00
#!/usr/local/bin/ruby
2002-04-27 20:34:15 +00:00
# $Id$
# $Source$
2002-04-27 20:31:59 +00:00
require 'date'
require 'getoptlong'
2002-04-28 16:29:56 +00:00
require 'news/article'
2002-04-27 20:31:59 +00:00
###########################################################################
Debuglevel = 1
def save_file(dir, name, data)
print "savename: #{name}\n" if Debuglevel > 1
nname = name.gsub(/\//, "-")
print "nname: #{nname}\n" if Debuglevel > 1
newname = nname
count = 1
d = Date.today
date = "#{d.year}#{d.month}#{d.mday}"
while FileTest.exists?("#{dir}/#{newname}")
newname = "#{nname}-<#{date}.#{count}>"
count += 1
end
print "name: #{newname}\n" if Debuglevel > 1
case data.type.to_s
when "String"
if File.rename(data, "#{dir}/#{newname}")
print "Saving: #{newname}\n"
else
print "couldn't rename tempfile\n"
end
when "Array"
if file = File.new("#{dir}/#{newname}", "w", "0644")
print "Saving: #{newname}\n"
for i in data
file.print "#{i}"
end
else
print "couldn't open file for writeing\n"
end
else
print "EEEEPS\n"
end
end
def tmp_file(dir)
name = "riptmp"
print "tmpname: #{name}\n" if Debuglevel > 1
nname = name.gsub(/\//, "-")
print "nname: #{nname}\n" if Debuglevel > 1
newname = nname
count = 1
while FileTest.exists?("#{dir}/#{newname}")
newname = "#{nname}-#{count}"
count += 1
end
print "name: #{newname}\n" if Debuglevel > 1
fullname ="#{dir}/#{newname}"
if file = File.new(fullname, "w+", "0644")
return file, fullname
else
print "couldn't open tempfile for writeing\n"
end
end
def parse_options
options = {}
begin
opts = GetoptLong.new(
[ "-I", "--include", GetoptLong::REQUIRED_ARGUMENT ],
[ "-c", "--configfile", GetoptLong::REQUIRED_ARGUMENT ],
[ "-L", "--longname", GetoptLong::NO_ARGUMENT ],
[ "-S", "--singlepart", GetoptLong::NO_ARGUMENT ],
[ "-g", "--greedy", GetoptLong::NO_ARGUMENT ]
)
opts.quiet=true
opts.each do |opt, arg|
options[opt] = arg
end
rescue
print "#{opts.error_message}\n"
print "\nUsage:\n"
exit
end
return options
end
def read_config(options)
unless options.has_key?("-c")
options["-c"]=".ripnewsrc"
end
begin
config = IO.readlines("#{options[\"-c\"]}")
for i in config
if i =~ /^OPT_(.)=?(.*)$/
options["-#{$1}"]=$2 unless
options.has_key?("-#{$1}")
else
i =~ /([^=]*)=(.*)/
options["#{$1}"]=$2
end
print "#{$1}=", options["#{$1}"], "\n" if Debuglevel > 1
end
rescue
print "Coudn't open config file: #{options[\"-c\"]}\n"
exit
end
return options
end
def check_options(options)
if (Debuglevel > 1)
for i in options.keys
print "Opt: #{i} Value: #{options[i]}\n"
end
end
unless options.has_key?("-I")
print "No inclusions given. Won't match anything.\n"
exit
end
unless options.has_key?("DATADIR")
options["DATADIR"] ="."
end
# exit # zeer tijdelijk voor snel testen
end
options = {}
options = parse_options
options = read_config(options)
check_options(options)
if Debuglevel > 1
for i in options.keys
print "Opt: #{i} val: #{options[i]}\n"
end
end
2002-04-28 16:29:56 +00:00
articles = Article.new(options["NNTPSERVER"])
#articles.get_articles("alt.binaries.e-book.flood")
articles.get_articles("alt.binaries.e-book.technical")
2002-04-27 20:31:59 +00:00
for i in articles.get_group_subjects
if i =~ /#{options["-I"]}/i
print "Match: #{i}\n" if Debuglevel > 0
if articles.group_complete(i)
print "Complete: #{i}\n" if Debuglevel > 0
if options.has_key?("TMPDIR")
file, fullname = tmp_file(options["TMPDIR"])
fileout, fullnameout = tmp_file(options["TMPDIR"])
else
file=nil
fullname = ""
fileout=nil
fullnameout = ""
end
mode, filename, body = articles.uudecode_group(i, file, fileout)
if file
file.close
fileout.close
body = fullnameout
end
if mode
print "mode: #{mode}\n" if Debuglevel > 0
print "filename: #{filename}\n"
if options.has_key?("-L")
print "longname\n" if Debuglevel > 1
save_file(options["DATADIR"], i, body)
else
print "shortname\n" if Debuglevel > 1
save_file(options["DATADIR"], filename, body)
end
end
# rm file & fileout
File.delete(fullname)
else
print "Not complete: #{i}\n"
end
print "\n"
end
end
articles.quit