#! /usr/local/bin/ruby # $Id: feeder.rb 213 2008-12-26 16:00:57Z fred $ # # Copyright (c) 2006-2007 Frédéric Senault. All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # 3. Neither the name of Frédéric Senault or any contributors may be # used to endorse or promote products derived from this software # without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF # SUCH DAMAGE. # feeder module, takes articles from RSS feeds end posts them $:.push(File.dirname($0)) begin require 'rubygems' rescue Exception end require 'thread' require 'open-uri' require 'netnntp' require 'rss2nntp' require 'feed_tools' require 'zlib' require 'timeout' Thread.abort_on_exception = true n = [] conf = RSS2NNTPConf.instance.parse work = Queue.new conf.feeds.each { |c| work.enq(c) } arts = Queue.new #t = (1..5).collect do |tn| # puts "Creating thread #{tn}" if conf[:debug] # Thread.new do tn = 1 while true do begin cfgfeed = nil if work.empty? # puts "Thread #{tn} is done" # Thread.current.exit break end cfgfeed = work.shift puts "Thread #{tn} took #{cfgfeed.url}" if conf[:debug] data = '' ct = '' begin Timeout.timeout(60) do open(cfgfeed.url, "User-Agent" => "#{conf[:program]} v#{conf[:version]}; #{conf[:abuse]}") do |f| if cfgfeed.gzip? || f.content_encoding.include?('gzip') data = Zlib::GzipReader.new(f).read else data = f.read end ct = f.content_type end end rescue Timeout::Error, Exception => e raise e end nm = 0 begin unless ct == 'text/html' && data[0, 100] =~ /]*>/ && data[0, 100] =~ /]*>/i && data[0, 100] =~ /]*>/i rss = FeedTools::Feed.new() rss.feed_data = data rss.items.each do |i| nm += 1 puts "Thread #{tn} - analyzing message #{nm} -- #{cfgfeed.url}" if conf[:debug] cm = RSSArticle.new(i, rss, cfgfeed) arts.enq(cm) puts "Thread #{tn} - contents of message #{nm} -- #{cfgfeed.url}" if conf[:debug] end else raise "Probable abnormal HTML content." end rescue Exception => e puts "Thread #{tn} - message content error #{e.message}" if conf[:debug] end rescue Exception => e puts "Failed to read feed #{cfgfeed.url} : #{e.message}" end end # end #end #lt.each { |t| t.join } if conf[:post] && !arts.empty? puts "Posting..." if conf[:debug] Net::NNTP.debug = true if conf[:debug] Net::NNTP.start(conf[:server]) do |nntp| until arts.empty? do a = arts.deq r = nntp.check(a['Message-ID']) if(r.ok) then r = nntp.takethis(a) puts "Takethis : #{r.to_s}" unless r.ok else puts "Check : #{r.to_s}" if conf[:debug] end end end end n = []