restructure code
This commit is contained in:
parent
6f01b486db
commit
53879858b2
1 changed files with 130 additions and 98 deletions
|
|
@ -36,6 +36,7 @@ def save_file(dir, name, data)
|
||||||
print " Saving as: '#{newname}'\n"
|
print " Saving as: '#{newname}'\n"
|
||||||
else
|
else
|
||||||
print "couldn't rename tempfile\n"
|
print "couldn't rename tempfile\n"
|
||||||
|
return false
|
||||||
end
|
end
|
||||||
when "Array"
|
when "Array"
|
||||||
if file = File.new("#{dir}/#{newname}", "w", "0644")
|
if file = File.new("#{dir}/#{newname}", "w", "0644")
|
||||||
|
|
@ -43,10 +44,13 @@ def save_file(dir, name, data)
|
||||||
data.collect{|i| file.print "#{i}"}
|
data.collect{|i| file.print "#{i}"}
|
||||||
else
|
else
|
||||||
print "couldn't open file for writeing\n"
|
print "couldn't open file for writeing\n"
|
||||||
|
return false
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
print "EEEEPS\n"
|
print "EEEEPS Can't save data of type: #{data.type.to_s}\n"
|
||||||
|
return false
|
||||||
end
|
end
|
||||||
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_options(options)
|
def parse_options(options)
|
||||||
|
|
@ -82,7 +86,8 @@ def parse_config(default = {})
|
||||||
|
|
||||||
i = 0
|
i = 0
|
||||||
group = ""
|
group = ""
|
||||||
config = {}
|
grouparr = []
|
||||||
|
@config = {}
|
||||||
|
|
||||||
lines.collect!{|x|
|
lines.collect!{|x|
|
||||||
x.sub!(/^\s*/, "")
|
x.sub!(/^\s*/, "")
|
||||||
|
|
@ -103,42 +108,44 @@ def parse_config(default = {})
|
||||||
print "#{i}: #{line}\n" if Debuglevel > 1
|
print "#{i}: #{line}\n" if Debuglevel > 1
|
||||||
if line =~ /(.*?)\s*\+=\s*(.*)/
|
if line =~ /(.*?)\s*\+=\s*(.*)/
|
||||||
if group == ""
|
if group == ""
|
||||||
|
if default.has_key?($1)
|
||||||
default[$1] += $2
|
default[$1] += $2
|
||||||
else
|
else
|
||||||
for g in group.split('|')
|
default[$1] = $2
|
||||||
if config[g].has_key?($1)
|
end
|
||||||
config[g][$1] += $2
|
|
||||||
elsif default.has_key?($1)
|
|
||||||
config[g][$1] = default[$1] + $2
|
|
||||||
else
|
else
|
||||||
config[g][$1] = $2
|
grouparr.collect{|g|
|
||||||
end
|
if @config[g].has_key?($1)
|
||||||
|
@config[g][$1] += $2
|
||||||
|
elsif default.has_key?($1)
|
||||||
|
@config[g][$1] = default[$1] + $2
|
||||||
|
else
|
||||||
|
@config[g][$1] = $2
|
||||||
end
|
end
|
||||||
|
}
|
||||||
end
|
end
|
||||||
elsif line =~ /(.*?)\s*=\s*(.*)/
|
elsif line =~ /(.*?)\s*=\s*(.*)/
|
||||||
if group == ""
|
if group == ""
|
||||||
default[$1] = $2
|
default[$1] = $2
|
||||||
else
|
else
|
||||||
for g in group.split('|')
|
grouparr.collect{|g|
|
||||||
config[g][$1] = $2
|
@config[g][$1] = $2
|
||||||
end
|
}
|
||||||
end
|
end
|
||||||
elsif line =~ /(.*?)\s*\{/
|
elsif line =~ /(.*?)\s*\{/
|
||||||
group = $1
|
group = $1
|
||||||
for g in group.split('|')
|
grouparr = group.split('|')
|
||||||
unless config.has_key?(g)
|
grouparr.collect{|g|
|
||||||
config[g] = {}
|
@config[g] = {} unless @config.has_key?(g)
|
||||||
end
|
}
|
||||||
end
|
|
||||||
elsif line =~ /^}$/
|
elsif line =~ /^}$/
|
||||||
default.each_key{|x|
|
default.each_key{|x|
|
||||||
for g in group.split('|')
|
grouparr.collect{|g|
|
||||||
unless config[g].has_key?(x)
|
@config[g][x] = default[x] unless @config[g].has_key?(x)
|
||||||
config[g][x] = default[x]
|
}
|
||||||
end
|
|
||||||
end
|
|
||||||
}
|
}
|
||||||
group = ""
|
group = ""
|
||||||
|
grouparr = []
|
||||||
elsif line =~ /^$/
|
elsif line =~ /^$/
|
||||||
next
|
next
|
||||||
else
|
else
|
||||||
|
|
@ -153,115 +160,140 @@ def parse_config(default = {})
|
||||||
end
|
end
|
||||||
|
|
||||||
if Debuglevel > 2
|
if Debuglevel > 2
|
||||||
config.each_key{|x|
|
@config.each_key{|x|
|
||||||
print "Group: #{x}\n"
|
print "Group: #{x}\n"
|
||||||
config[x].each_key{|y|
|
@config[x].each_key{|y|
|
||||||
print "Key: '#{y}' => Value: '#{config[x][y]}'\n"
|
print "Key: '#{y}' => Value: '#{@config[x][y]}'\n"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
return true
|
||||||
return config
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def check_config(config)
|
def check_config
|
||||||
config.each_key {|i|
|
@config.each_key {|i|
|
||||||
unless config[i].has_key?("-I")
|
unless @config[i].has_key?("-I")
|
||||||
print "No inclusions given for group #{i}. Won't match anything.\n"
|
print "No inclusions given for group #{i}. Won't match anything.\n"
|
||||||
exit
|
exit
|
||||||
end
|
end
|
||||||
unless config[i].has_key?("DATADIR")
|
unless @config[i].has_key?("DATADIR")
|
||||||
config[i]["DATADIR"] ="."
|
@config[i]["DATADIR"] ="."
|
||||||
end
|
end
|
||||||
unless config[i].has_key?("PERMISSION")
|
unless @config[i].has_key?("PERMISSION")
|
||||||
config[i]["PERMISSION"] = "0755"
|
@config[i]["PERMISSION"] = "0755"
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
defaults = {'-c' => '.ripnewsrc'}
|
def get_single(articles, subj)
|
||||||
defaults = parse_options(defaults)
|
|
||||||
config = parse_config(defaults)
|
|
||||||
check_config(config)
|
|
||||||
|
|
||||||
if Debuglevel > 2
|
|
||||||
config.each_key{|i|
|
|
||||||
print "Group: #{i}\n"
|
|
||||||
config[i].each_key{|j|
|
|
||||||
print "Opt: #{j} val: #{config[i][j]}\n"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
for group in config.keys.sort
|
|
||||||
#newsrc = News::Newsrc.new(config[group]["NEWSRCNAME"])
|
|
||||||
print "Getting articles for #{group}\n"
|
|
||||||
articles = Article.new(config[group]["NNTPSERVER"], group, config[group]["NEWSRCNAME"])
|
|
||||||
#articles.set_skip_ids(config[group]["NNTPSERVER"], newsrc.marked_articles(group))
|
|
||||||
articles.get_articles(config[group]["CACHEDIR"])
|
|
||||||
|
|
||||||
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"
|
|
||||||
exit
|
|
||||||
end
|
|
||||||
for i in articles.get_group_subjects
|
|
||||||
print "#{i}\n" if Debuglevel > 2
|
|
||||||
if !(config[group].has_key?("-X") and i =~ /#{config[group]["-X"]}/) and
|
|
||||||
i =~ /#{config[group]["-I"]}/
|
|
||||||
print "Match: #{i}\n" if Debuglevel > 0
|
|
||||||
if articles.group_is_complete(i)
|
|
||||||
print " Fetching: #{i}\n"
|
|
||||||
if articles.group_is_singlepart(i)
|
|
||||||
print " Singlepart!\n"
|
print " Singlepart!\n"
|
||||||
body = articles.get_group_body(i)
|
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)
|
||||||
else
|
else
|
||||||
print "Not UUencoded!\n"
|
print "Not UUencoded!\n"
|
||||||
|
return false
|
||||||
end
|
end
|
||||||
elsif articles.group_is_multipart(i)
|
return mode, filename, body
|
||||||
|
end
|
||||||
|
|
||||||
|
def get_multi(articles, subj, group)
|
||||||
print " Multipart!\n"
|
print " Multipart!\n"
|
||||||
if config[group]["TEMPDIR"] == nil or config[group]["TEMPDIR"] == ""
|
if @config[group]["TEMPDIR"] == nil or @config[group]["TEMPDIR"] == ""
|
||||||
body = articles.get_group_body(i)
|
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)
|
||||||
else
|
else
|
||||||
print "Multipart article not UUencoded!\n"
|
print "Multipart article not UUencoded!\n"
|
||||||
|
return false
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
body = articles.get_group_body_first(i)
|
body = articles.get_group_body_first(subj)
|
||||||
|
next if body == false
|
||||||
if articles.is_uuencoded(body)
|
if articles.is_uuencoded(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"}
|
||||||
articles.get_group_body_rest(i, file)
|
next unless articles.get_group_body_rest(subj, file)
|
||||||
fileout = Tempfile.new("riptmp", config[group]["TEMPDIR"])
|
fileout = Tempfile.new("riptmp", @config[group]["TEMPDIR"])
|
||||||
mode, filename, body = articles.uudecode(file, fileout)
|
mode, filename, body = articles.uudecode(file, fileout)
|
||||||
body = fileout.path
|
body = fileout.path
|
||||||
file.close
|
file.close
|
||||||
fileout.close
|
fileout.close
|
||||||
else
|
else
|
||||||
print "Multipart article not UUencoded!\n"
|
print "Multipart article not UUencoded!\n"
|
||||||
|
return false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
return -mode, filename, body
|
||||||
|
end
|
||||||
|
|
||||||
|
def output_data(mode, filename="", body="")
|
||||||
if mode
|
if mode
|
||||||
print " mode: #{mode}\n" if Debuglevel > 0
|
print " mode: #{mode}\n" if Debuglevel > 0
|
||||||
print " Filename: '#{filename}'\n" if Debuglevel > 0
|
print " Filename: '#{filename}'\n" if Debuglevel > 0
|
||||||
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
|
||||||
save_file("#{config[group]["DATADIR"]}/#{group}", i, body)
|
if save_file("#{@config[group]["DATADIR"]}/#{group}", i, body)
|
||||||
|
articles.group_update_newsrc(i)
|
||||||
|
articles.save_newsrc unless @config[group].has_key?("-T") and @config[group]["-T"]
|
||||||
|
end
|
||||||
else
|
else
|
||||||
print "shortname\n" if Debuglevel > 1
|
print "shortname\n" if Debuglevel > 1
|
||||||
save_file("#{config[group]["DATADIR"]}/#{group}", filename, body)
|
if save_file("#{@config[group]["DATADIR"]}/#{group}", filename, body)
|
||||||
end
|
|
||||||
end
|
|
||||||
#messids = articles.get_group_messids(i)
|
|
||||||
articles.group_update_newsrc(i)
|
articles.group_update_newsrc(i)
|
||||||
|
articles.save_newsrc unless @config[group].has_key?("-T") and @config[group]["-T"]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
return true
|
||||||
|
#messids = articles.get_group_messids(i)
|
||||||
|
#articles.group_update_newsrc(i)
|
||||||
#newsrc.mark_list(group, marked)
|
#newsrc.mark_list(group, marked)
|
||||||
articles.save_newsrc unless config[group].has_key?("-T") and config[group]["-T"]
|
#articles.save_newsrc unless @config[group].has_key?("-T") and @config[group]["-T"]
|
||||||
#marked.collect!{|x| print "marked: #{x}\n"}
|
#marked.collect!{|x| print "marked: #{x}\n"}
|
||||||
|
end
|
||||||
|
|
||||||
|
#############################################################################################
|
||||||
|
|
||||||
|
defaults = {'-c' => '.ripnewsrc'}
|
||||||
|
defaults = parse_options(defaults)
|
||||||
|
parse_config(defaults)
|
||||||
|
check_config
|
||||||
|
|
||||||
|
if Debuglevel > 2
|
||||||
|
@config.each_key{|i|
|
||||||
|
print "Group: #{i}\n"
|
||||||
|
@config[i].each_key{|j|
|
||||||
|
print "Opt: #{j} val: #{@config[i][j]}\n"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
for group in @config.keys.sort
|
||||||
|
print "Getting articles for #{group}\n"
|
||||||
|
articles = Article.new(@config[group]["NNTPSERVER"], group, @config[group]["NEWSRCNAME"])
|
||||||
|
articles.get_articles(@config[group]["CACHEDIR"])
|
||||||
|
|
||||||
|
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"
|
||||||
|
exit
|
||||||
|
end
|
||||||
|
for i in articles.get_group_subjects
|
||||||
|
print "#{i}\n" if Debuglevel > 2
|
||||||
|
if !(@config[group].has_key?("-X") and i =~ /#{@config[group]["-X"]}/) and
|
||||||
|
i =~ /#{@config[group]["-I"]}/
|
||||||
|
print "Match: #{i}\n" if Debuglevel > 0
|
||||||
|
if articles.group_is_complete(i)
|
||||||
|
print " Fetching: #{i}\n"
|
||||||
|
if articles.group_is_singlepart(i)
|
||||||
|
mode, filename, body = get_single(articles, i)
|
||||||
|
elsif articles.group_is_multipart(i)
|
||||||
|
mode, filename, body = get_multi(articles, i, group)
|
||||||
|
end
|
||||||
|
output_data(mode, filename, body)
|
||||||
else
|
else
|
||||||
print " Not complete: #{i}\n"
|
print " Not complete: #{i}\n"
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue