diff options
author | Dmitry Kim <dmitry point kim at gmail point com> | 2007-03-30 23:44:02 +0000 |
---|---|---|
committer | Dmitry Kim <dmitry point kim at gmail point com> | 2007-03-30 23:44:02 +0000 |
commit | b11c3c4042b03e36639370002ecf86c44f7ddde4 (patch) | |
tree | 05a35024a2d56c7e3d313317376a17cb7c41a99f /data/rbot/plugins | |
parent | b73d6c7dc6554e1c6eb6abce68350ed2c13191b8 (diff) |
*** (httputil) major rework, new caching implementation, unified request
processing
+ (httputil) post support, partial request support, other features
- (httputil) removed partial_body() and get_cached() [merged into get()]
* (plugins/, utils) minimal changes to accomodate for the new http_utils
* (utils, ircbot) moved utils initialization into utils.rb
* (tube.rb) (partially) accomodate for upstream site layout changes
Diffstat (limited to 'data/rbot/plugins')
-rw-r--r-- | data/rbot/plugins/bash.rb | 7 | ||||
-rw-r--r-- | data/rbot/plugins/dict.rb | 12 | ||||
-rw-r--r-- | data/rbot/plugins/digg.rb | 2 | ||||
-rw-r--r-- | data/rbot/plugins/fish.rb | 51 | ||||
-rw-r--r-- | data/rbot/plugins/freshmeat.rb | 4 | ||||
-rw-r--r-- | data/rbot/plugins/games/azgame.rb | 10 | ||||
-rw-r--r-- | data/rbot/plugins/games/quiz.rb | 2 | ||||
-rw-r--r-- | data/rbot/plugins/grouphug.rb | 9 | ||||
-rw-r--r-- | data/rbot/plugins/imdb.rb | 27 | ||||
-rw-r--r-- | data/rbot/plugins/rss.rb | 4 | ||||
-rw-r--r-- | data/rbot/plugins/search.rb | 2 | ||||
-rw-r--r-- | data/rbot/plugins/slashdot.rb | 4 | ||||
-rw-r--r-- | data/rbot/plugins/threat.rb | 2 | ||||
-rw-r--r-- | data/rbot/plugins/tube.rb | 64 | ||||
-rw-r--r-- | data/rbot/plugins/urban.rb | 21 | ||||
-rw-r--r-- | data/rbot/plugins/url.rb | 85 | ||||
-rw-r--r-- | data/rbot/plugins/weather.rb | 4 | ||||
-rw-r--r-- | data/rbot/plugins/wserver.rb | 38 |
18 files changed, 173 insertions, 175 deletions
diff --git a/data/rbot/plugins/bash.rb b/data/rbot/plugins/bash.rb index 6f954a16..2a5bedad 100644 --- a/data/rbot/plugins/bash.rb +++ b/data/rbot/plugins/bash.rb @@ -25,11 +25,12 @@ class BashPlugin < Plugin def bash(m, id=0) if(id != 0) - xml = @bot.httputil.get URI.parse("http://bash.org/xml/?" + id + "&num=1") + xml = @bot.httputil.get("http://bash.org/xml/?" + id + "&num=1") elsif(id == "latest") - xml = @bot.httputil.get URI.parse("http://bash.org/xml/?latest&num=1") + xml = @bot.httputil.get("http://bash.org/xml/?latest&num=1") else - xml = @bot.httputil.get URI.parse("http://bash.org/xml/?random&num=1") + xml = @bot.httputil.get("http://bash.org/xml/?random&num=1", + :cache => false) end unless xml m.reply "bash.org rss parse failed" diff --git a/data/rbot/plugins/dict.rb b/data/rbot/plugins/dict.rb index 504a9d7b..d486e97b 100644 --- a/data/rbot/plugins/dict.rb +++ b/data/rbot/plugins/dict.rb @@ -57,9 +57,10 @@ class DictPlugin < Plugin word = params[:word].downcase
url = @dmwapurl % URI.escape(word)
- xml = @bot.httputil.get_cached(url)
+ xml = nil
+ info = @bot.httputil.get_response(url) rescue nil
+ xml = info.body if info
if xml.nil?
- info = @bot.httputil.last_response
info = info ? " (#{info.code} - #{info.message})" : ""
return false if justcheck
m.reply "An error occurred while looking for #{word}#{info}"
@@ -108,7 +109,7 @@ class DictPlugin < Plugin word = params[:word].join
[word, word + "_1"].each { |check|
url = @oxurl % URI.escape(check)
- h = @bot.httputil.head(url)
+ h = @bot.httputil.head(url, :max_redir => 5)
if h
m.reply("#{word} found: #{url}") unless justcheck
return true
@@ -128,10 +129,11 @@ class DictPlugin < Plugin word = params[:word].to_s.downcase
url = @chambersurl % URI.escape(word)
- xml = @bot.httputil.get_cached(url)
+ xml = nil
+ info = @bot.httputil.get_response(url) rescue nil
+ xml = info.body if info
case xml
when nil
- info = @bot.httputil.last_response
info = info ? " (#{info.code} - #{info.message})" : ""
return false if justcheck
m.reply "An error occurred while looking for #{word}#{info}"
diff --git a/data/rbot/plugins/digg.rb b/data/rbot/plugins/digg.rb index 43b7c9f5..dc2e41cf 100644 --- a/data/rbot/plugins/digg.rb +++ b/data/rbot/plugins/digg.rb @@ -13,7 +13,7 @@ class DiggPlugin < Plugin def digg(m, params) max = params[:limit].to_i debug "max is #{max}" - xml = @bot.httputil.get_cached(URI.parse("http://digg.com/rss/index.xml")) + xml = @bot.httputil.get('http://digg.com/rss/index.xml') unless xml m.reply "digg news parse failed" return diff --git a/data/rbot/plugins/fish.rb b/data/rbot/plugins/fish.rb index 8c115f90..d7dda52b 100644 --- a/data/rbot/plugins/fish.rb +++ b/data/rbot/plugins/fish.rb @@ -30,39 +30,40 @@ class BabelPlugin < Plugin return end - http = @bot.httputil.get_proxy(URI.parse("http://babelfish.altavista.com")) - headers = { - "content-type" => "application/x-www-form-urlencoded; charset=utf-8", - 'accept-charset' => 'utf-8' + "content-type" => "application/x-www-form-urlencoded; charset=utf-8" } - http.start {|http| - resp = http.post(query, data, headers) - - if (resp.code == "200") - lines = Array.new - resp.body.each_line do |l| - lines.push l + begin + resp = @bot.httputil.get_response('http://babelfish.altavista.com'+query, + :method => :post, + :body => data, + :headers => headers) + rescue Exception => e + m.reply "http error: #{e.message}" + return end - l = lines.join(" ") - debug "babelfish response: #{l}" + if (resp.code == "200") + lines = Array.new + resp.body.each_line { |l| lines.push l } + + l = lines.join(" ") + debug "babelfish response: #{l}" - if(l =~ /^\s+<td bgcolor=white class=s><div style=padding:10px;>(.*)<\/div>/) - answer = $1 - # cache the answer - if(answer.length > 0) - @registry["#{trans_pair}/#{data_text}"] = answer + if(l =~ /^\s+<td bgcolor=white class=s><div style=padding:10px;>(.*)<\/div>/) + answer = $1 + # cache the answer + if(answer.length > 0) + @registry["#{trans_pair}/#{data_text}"] = answer + end + m.reply answer + return end - m.reply answer - return + m.reply "couldn't parse babelfish response html :(" + else + m.reply "couldn't talk to babelfish :(" end - m.reply "couldn't parse babelfish response html :(" - else - m.reply "couldn't talk to babelfish :(" - end - } end end plugin = BabelPlugin.new diff --git a/data/rbot/plugins/freshmeat.rb b/data/rbot/plugins/freshmeat.rb index c8f529cb..5a045123 100644 --- a/data/rbot/plugins/freshmeat.rb +++ b/data/rbot/plugins/freshmeat.rb @@ -12,7 +12,7 @@ class FreshmeatPlugin < Plugin search = params[:search].to_s max = 8 if max > 8 begin - xml = @bot.httputil.get_cached(URI.parse("http://freshmeat.net/search-xml/?orderby=locate_projectname_full_DESC&q=#{URI.escape(search)}")) + xml = @bot.httputil.get("http://freshmeat.net/search-xml/?orderby=locate_projectname_full_DESC&q=#{URI.escape(search)}") rescue URI::InvalidURIError, URI::BadURIError => e m.reply "illegal search string #{search}" return @@ -59,7 +59,7 @@ class FreshmeatPlugin < Plugin max = params[:limit].to_i max = 8 if max > 8 begin - xml = @bot.httputil.get(URI.parse("http://images.feedstermedia.com/feedcache/ostg/freshmeat/fm-releases-global.xml")) + xml = @bot.httputil.get('http://images.feedstermedia.com/feedcache/ostg/freshmeat/fm-releases-global.xml') unless xml m.reply "freshmeat news parse failed" return diff --git a/data/rbot/plugins/games/azgame.rb b/data/rbot/plugins/games/azgame.rb index a6979830..f62232c0 100644 --- a/data/rbot/plugins/games/azgame.rb +++ b/data/rbot/plugins/games/azgame.rb @@ -351,7 +351,7 @@ class AzGamePlugin < Plugin wc = @wordcache[:italian]
return true if wc.key?(word.to_sym)
rules = @rules[:italian]
- p = @bot.httputil.get_cached(rules[:wapurl] % word)
+ p = @bot.httputil.get(rules[:wapurl] % word)
if not p
error "could not connect!"
return false
@@ -404,11 +404,11 @@ class AzGamePlugin < Plugin l = ('a'..'z').to_a[rand(26)]
debug "getting random word from dictionary, starting with letter #{l}"
first = rules[:url] % "lettera_#{l}_0_50"
- p = @bot.httputil.get_cached(first)
+ p = @bot.httputil.get(first)
max_page = p.match(/ \/ (\d+)<\/label>/)[1].to_i
pp = rand(max_page)+1
debug "getting random word from dictionary, starting with letter #{l}, page #{pp}"
- p = @bot.httputil.get_cached(first+"&pagina=#{pp}") if pp > 1
+ p = @bot.httputil.get(first+"&pagina=#{pp}") if pp > 1
lemmi = Array.new
good = rules[:good]
bad = rules[:bad]
@@ -446,7 +446,7 @@ class AzGamePlugin < Plugin wc = @wordcache[:english]
return true if wc.key?(word.to_sym)
rules = @rules[:english]
- p = @bot.httputil.get_cached(rules[:url] % URI.escape(word))
+ p = @bot.httputil.get(rules[:url] % URI.escape(word))
if not p
error "could not connect!"
return false
@@ -497,7 +497,7 @@ class AzGamePlugin < Plugin ll = ('a'..'z').to_a[rand(26)]
random = [l,ll].join('*') + '*'
debug "getting random word from dictionary, matching #{random}"
- p = @bot.httputil.get_cached(rules[:url] % URI.escape(random))
+ p = @bot.httputil.get(rules[:url] % URI.escape(random))
debug p
lemmi = Array.new
good = rules[:good]
diff --git a/data/rbot/plugins/games/quiz.rb b/data/rbot/plugins/games/quiz.rb index 63383262..56b1bca6 100644 --- a/data/rbot/plugins/games/quiz.rb +++ b/data/rbot/plugins/games/quiz.rb @@ -203,7 +203,7 @@ class QuizPlugin < Plugin if p =~ /^https?:\/\// # Wiki data begin - serverdata = @bot.httputil.get_cached( URI.parse( p ) ) # "http://amarok.kde.org/amarokwiki/index.php/Rbot_Quiz" + serverdata = @bot.httputil.get(p) # "http://amarok.kde.org/amarokwiki/index.php/Rbot_Quiz" serverdata = serverdata.split( "QUIZ DATA START\n" )[1] serverdata = serverdata.split( "\nQUIZ DATA END" )[0] serverdata = serverdata.gsub( / /, " " ).gsub( /&/, "&" ).gsub( /"/, "\"" ) diff --git a/data/rbot/plugins/grouphug.rb b/data/rbot/plugins/grouphug.rb index 75093665..15735b9f 100644 --- a/data/rbot/plugins/grouphug.rb +++ b/data/rbot/plugins/grouphug.rb @@ -13,10 +13,15 @@ class GrouphugPlugin < Plugin end def confess(m, params) + opts = { :cache => false } path = "random" - path = "confessions/#{params[:num]}" if params[:num] + if params[:num] + path = "confessions/#{params[:num]}" + opts.delete(:cache) + end + begin - data = @bot.httputil.get_cached(URI.parse("http://grouphug.us/#{path}")) + data = @bot.httputil.get("http://grouphug.us/#{path}", opts) reg = Regexp.new( '(<td class="conf-text")(.*?)(<p>)(.*?)(</p>)', Regexp::MULTILINE ) confession = reg.match( data )[4].ircify_html diff --git a/data/rbot/plugins/imdb.rb b/data/rbot/plugins/imdb.rb index 5e4cc65a..cfadd3c8 100644 --- a/data/rbot/plugins/imdb.rb +++ b/data/rbot/plugins/imdb.rb @@ -21,16 +21,16 @@ class Imdb def search(rawstr) str = URI.escape(rawstr) - @http = @bot.httputil.get_proxy(URI.parse("http://us.imdb.com/find?q=#{str}")) - @http.start + resp = nil begin - resp, data = @http.get("/find?q=#{str}", @bot.httputil.headers) - rescue Net::ProtoRetriableError => detail - head = detail.data - if head.code == "301" or head.code == "302" - return head['location'].gsub(/http:\/\/us.imdb.com/, "").gsub(/\?.*/, "") - end + resp = @bot.httputil.get_response("http://us.imdb.com/find?q=#{str}", + :max_redir => -1) + rescue Exception => e + error e.message + warning e.backtrace.join("\n") + return nil end + if resp.code == "200" m = /<a href="(\/title\/tt[0-9]+\/?)[^"]*"(?:[^>]*)>([^<]*)<\/a>/.match(resp.body) if m @@ -50,7 +50,16 @@ class Imdb debug "IMDB: search returned NIL" return nil end - resp, data = @http.get(sr, @bot.httputil.headers) + resp = nil + begin + resp = @bot.httputil.get_response('http://us.imdb.com' + sr, + :max_redir => -1) + rescue Exception => e + error e.message + warning e.backtrace.join("\n") + return nil + end + if resp.code == "200" m = /<title>([^<]*)<\/title>/.match(resp.body) return nil if !m diff --git a/data/rbot/plugins/rss.rb b/data/rbot/plugins/rss.rb index 15b121b7..68a5271b 100644 --- a/data/rbot/plugins/rss.rb +++ b/data/rbot/plugins/rss.rb @@ -654,7 +654,9 @@ class RSSFeedsPlugin < Plugin def fetchRss(feed, m=nil)
begin
# Use 60 sec timeout, cause the default is too low
- xml = @bot.httputil.get_cached(feed.url, 60, 60)
+ xml = @bot.httputil.get(feed.url,
+ :read_timeout => 60,
+ :open_timeout => 60)
rescue URI::InvalidURIError, URI::BadURIError => e
report_problem("invalid rss feed #{feed.url}", e, m)
return nil
diff --git a/data/rbot/plugins/search.rb b/data/rbot/plugins/search.rb index 221071cb..1884b38a 100644 --- a/data/rbot/plugins/search.rb +++ b/data/rbot/plugins/search.rb @@ -62,7 +62,7 @@ class SearchPlugin < Plugin hits = params[:hits] || @bot.config['google.hits'] begin - wml = @bot.httputil.get_cached(url) + wml = @bot.httputil.get(url) rescue => e m.reply "error googling for #{what}" return diff --git a/data/rbot/plugins/slashdot.rb b/data/rbot/plugins/slashdot.rb index ef96a4b0..fa1338bd 100644 --- a/data/rbot/plugins/slashdot.rb +++ b/data/rbot/plugins/slashdot.rb @@ -12,7 +12,7 @@ class SlashdotPlugin < Plugin search = params[:search].to_s begin - xml = @bot.httputil.get_cached(URI.parse("http://slashdot.org/search.pl?content_type=rss&query=#{URI.escape(search)}")) + xml = @bot.httputil.get("http://slashdot.org/search.pl?content_type=rss&query=#{URI.escape(search)}") rescue URI::InvalidURIError, URI::BadURIError => e m.reply "illegal search string #{search}" return @@ -53,7 +53,7 @@ class SlashdotPlugin < Plugin debug params.inspect max = params[:limit].to_i debug "max is #{max}" - xml = @bot.httputil.get_cached(URI.parse("http://slashdot.org/slashdot.xml")) + xml = @bot.httputil.get('http://slashdot.org/slashdot.xml') unless xml m.reply "slashdot news parse failed" return diff --git a/data/rbot/plugins/threat.rb b/data/rbot/plugins/threat.rb index bcfc8698..dacd1058 100644 --- a/data/rbot/plugins/threat.rb +++ b/data/rbot/plugins/threat.rb @@ -24,7 +24,7 @@ class ThreatPlugin < Plugin green = "\x0303" # low black = "\x0301" # default - page = @bot.httputil.get_cached("http://www.dhs.gov/index.shtm") + page = @bot.httputil.get("http://www.dhs.gov/index.shtm") if page =~ /\"Current National Threat Level is (.*?)\"/ state = $1 diff --git a/data/rbot/plugins/tube.rb b/data/rbot/plugins/tube.rb index 0a9feb2f..16562274 100644 --- a/data/rbot/plugins/tube.rb +++ b/data/rbot/plugins/tube.rb @@ -7,56 +7,48 @@ require 'uri/common' class TubePlugin < Plugin include REXML def help(plugin, topic="") - "tube [district|circle|metropolitan|central|jubilee|bakerloo|waterloo_city|hammersmith_city|victoria|eastlondon|northern|piccadilly] => display tube service status for the specified line(Docklands Light Railway is not currently supported), tube stations => list tube stations (not lines) with problems" + "tube [district|circle|metropolitan|central|jubilee|bakerloo|waterlooandcity|hammersmithandcity|victoria|eastlondon|northern|piccadilly] => display tube service status for the specified line(Docklands Light Railway is not currently supported)" # , tube stations => list tube stations (not lines) with problems" end def tube(m, params) line = params[:line] - begin - tube_page = @bot.httputil.get_cached(URI.parse("http://www.tfl.gov.uk/tfl/service_rt_tube.shtml"), 1, 1) - rescue URI::InvalidURIError, URI::BadURIError => e - m.reply "Cannot contact Tube Service Status page" - return - end - unless tube_page - m.reply "Cannot contact Tube Service Status page" - return - end - next_line = false - tube_page.each_line {|l| - next if l == "\r\n" - next if l == "\n" - if (next_line) - if (l =~ /^<tr valign=top> <td>\s*(.*)<\/td><\/tr>/i) - m.reply $1.split(/<[^>]+>| /i).join(" ") - return - else - m.reply "There are problems on the #{line} line, but I didn't understand the page format. You should check out http://www.tfl.gov.uk/tfl/service_rt_tube.shtml for more details." - return - end + tube_page = @bot.httputil.get('http://www.tfl.gov.uk/tfl/livetravelnews/realtime/tube/default.html') + unless tube_page + m.reply "Cannot contact Tube Service Status page" + return end - next_line = true if (l =~ /class="#{line}"/i) + next_line = false + tube_page.each_line {|l| + next if l == "\r\n" + next if l == "\n" + if (next_line) + if (l =~ /^<p>\s*(.*)<\/p>/i) + m.reply $1.split(/<[^>]+>| /i).join(" ") + return + elsif l =~ /ul|h3|"message"/ + next + else + m.reply "There are problems on the #{line} line, but I didn't understand the page format. You should check out http://www.tfl.gov.uk/tfl/livetravelnews/realtime/tube/default.html for more details." + return + end + end + next_line = true if (l =~ /li class="#{line}"/i) } - m.reply "No Problems on the #{line} line." + m.reply "No Problems on the #{line} line." end def check_stations(m, params) - begin - tube_page = @bot.httputil.get_cached(URI.parse("http://www.tfl.gov.uk/tfl/service_rt_tube.shtml")) - rescue URI::InvalidURIError, URI::BadURIError => e - m.reply "Cannot contact Tube Service Status page" - return - end + tube_page = @bot.httputil.get('http://www.tfl.gov.uk/tfl/service_rt_tube.shtml') unless tube_page m.reply "Cannot contact Tube Service Status page" return end stations_array = Array.new tube_page.each_line {|l| - if (l =~ /<tr valign=top> <td valign="middle" class="Station"><b>(.*)<\/b><\/td><\/tr>\s*/i) - stations_array.push $1 - end - } + if (l =~ /<tr valign=top> <td valign="middle" class="Station"><b>(.*)<\/b><\/td><\/tr>\s*/i) + stations_array.push $1 + end + } if stations_array.empty? m.reply "There are no station-specific announcements" return @@ -67,5 +59,5 @@ class TubePlugin < Plugin end end plugin = TubePlugin.new -plugin.map 'tube stations', :action => 'check_stations' +# plugin.map 'tube stations', :action => 'check_stations' plugin.map 'tube :line' diff --git a/data/rbot/plugins/urban.rb b/data/rbot/plugins/urban.rb index c77a2e45..0b75febe 100644 --- a/data/rbot/plugins/urban.rb +++ b/data/rbot/plugins/urban.rb @@ -11,14 +11,15 @@ class UrbanPlugin < Plugin n = params[:n].nil? ? 1 : params[:n].to_i rescue 1 if words.empty? - uri = URI.parse( "http://www.urbandictionary.com/random.php" ) - @bot.httputil.head(uri) { |redir| - words = URI.unescape(redir.match(/define.php\?term=(.*)$/)[1]) rescue nil - } + resp = @bot.httputil.head('http://www.urbandictionary.com/random.php', + :max_redir => -1) + if resp.code == "302" && (loc = resp['location']) + words = URI.unescape(loc.match(/define.php\?term=(.*)$/)[1]) rescue nil + end end # we give a very high 'skip' because this will allow us to get the number of definitions by retrieving the previous definition - uri = URI.parse("http://www.urbanwap.com/search.php?term=#{URI.escape words}&skip=65536") - page = @bot.httputil.get_cached(uri) + uri = "http://www.urbanwap.com/search.php?term=#{URI.escape words}&skip=65536" + page = @bot.httputil.get(uri) if page.nil? m.reply "Couldn't retrieve an urban dictionary definition of #{words}" return @@ -38,8 +39,8 @@ class UrbanPlugin < Plugin n = numdefs end if n < numdefs - uri = URI.parse("http://www.urbanwap.com/search.php?term=#{URI.escape words}&skip=#{n-1}") - page = @bot.httputil.get_cached(uri) + uri = "http://www.urbanwap.com/search.php?term=#{URI.escape words}&skip=#{n-1}" + page = @bot.httputil.get(uri) if page.nil? case n % 10 when 1 @@ -77,7 +78,7 @@ class UrbanPlugin < Plugin end def uotd(m, params) - home = @bot.httputil.get_cached("http://www.urbanwap.com/") + home = @bot.httputil.get("http://www.urbanwap.com/") if home.nil? m.reply "Couldn't get the urban dictionary word of the day" return @@ -85,7 +86,7 @@ class UrbanPlugin < Plugin home.match(/Word of the Day: <a href="(.*?)">.*?<\/a>/) wotd = $1 debug "Urban word of the day: #{wotd}" - page = @bot.httputil.get_cached(wotd) + page = @bot.httputil.get(wotd) if page.nil? m.reply "Couldn't get the urban dictionary word of the day" else diff --git a/data/rbot/plugins/url.rb b/data/rbot/plugins/url.rb index 23a32638..95ccb5d9 100644 --- a/data/rbot/plugins/url.rb +++ b/data/rbot/plugins/url.rb @@ -40,59 +40,48 @@ class UrlPlugin < Plugin title = nil begin - @bot.httputil.get_response(url) { |response| - case response - when Net::HTTPSuccess - extra = String.new - - if response['content-type'] =~ /^text\// - - title = String.new - - # since the content is 'text/*' and is small enough to - # be a webpage, retrieve the title from the page - debug "+ getting #{url.request_uri}" - - # we act differently depending on whether we want the first par or not: - # in the first case we download the initial part and the parse it; in the second - # case we only download as much as we need to find the title - if @bot.config['url.first_par'] - partial = response.partial_body(@bot.config['http.info_bytes']) - first_par = Utils.ircify_first_html_par(partial) - extra << "\n#{LINK_INFO} text: #{first_par}" unless first_par.empty? - title = get_title_from_html(partial) - if title - return "title: #{title}#{extra}" - end - else - response.partial_body(@bot.config['http.info_bytes']) { |part| - title = get_title_from_html(part) - return "title: #{title}" if title - } - end - # if nothing was found, provide more basic info - end + range = @bot.config['http.info_bytes'] + response = @bot.httputil.get_response(url, :range => "bytes=0-#{range}") + if response.code != "206" && response.code != "200" + return "Error getting link (#{response.code} - #{response.message})" + end + extra = String.new - debug response.to_hash.inspect - unless @bot.config['url.titles_only'] - # content doesn't have title, just display info. - size = response['content-length'].gsub(/(\d)(?=\d{3}+(?:\.|$))(\d{3}\..*)?/,'\1,\2') rescue nil - size = size ? ", size: #{size} bytes" : "" - return "type: #{response['content-type']}#{size}#{extra}" - end - when Net::HTTPResponse - return "Error getting link (#{response.code} - #{response.message})" + if response['content-type'] =~ /^text\// + + body = response.body.slice(0, range) + title = String.new + + # since the content is 'text/*' and is small enough to + # be a webpage, retrieve the title from the page + debug "+ getting #{url.request_uri}" + + # we act differently depending on whether we want the first par or not: + # in the first case we download the initial part and the parse it; in the second + # case we only download as much as we need to find the title + if @bot.config['url.first_par'] + first_par = Utils.ircify_first_html_par(body) + extra << "\n#{LINK_INFO} text: #{first_par}" unless first_par.empty? + title = get_title_from_html(body) + return "title: #{title}#{extra}" if title else - raise response + title = get_title_from_html(body) + return "title: #{title}" if title end - } - rescue Object => e - if e.class <= StandardError - error e.inspect - debug e.backtrace.join("\n") + + # if nothing was found, provide more basic info end - msg = e.respond_to?(:message) ? e.message : e.to_s + debug response.to_hash.inspect + unless @bot.config['url.titles_only'] + # content doesn't have title, just display info. + size = response['content-length'].gsub(/(\d)(?=\d{3}+(?:\.|$))(\d{3}\..*)?/,'\1,\2') rescue nil + size = size ? ", size: #{size} bytes" : "" + return "type: #{response['content-type']}#{size}#{extra}" + end + rescue Exception => e + error e.inspect + debug e.backtrace.join("\n") return "Error connecting to site (#{e.message})" end end diff --git a/data/rbot/plugins/weather.rb b/data/rbot/plugins/weather.rb index 88194e90..cdabf9c1 100644 --- a/data/rbot/plugins/weather.rb +++ b/data/rbot/plugins/weather.rb @@ -165,7 +165,7 @@ class WeatherPlugin < Plugin def wu_station(m, where, units) begin - xml = @bot.httputil.get_cached(@wu_station_url % [units, URI.escape(where)]) + xml = @bot.httputil.get(@wu_station_url % [units, URI.escape(where)]) case xml when nil m.reply "couldn't retrieve weather information, sorry" @@ -187,7 +187,7 @@ class WeatherPlugin < Plugin def wu_weather(m, where, units) begin - xml = @bot.httputil.get_cached(@wu_url % [units, URI.escape(where)]) + xml = @bot.httputil.get(@wu_url % [units, URI.escape(where)]) case xml when nil m.reply "couldn't retrieve weather information, sorry" diff --git a/data/rbot/plugins/wserver.rb b/data/rbot/plugins/wserver.rb index 1baa0d90..fdb4207d 100644 --- a/data/rbot/plugins/wserver.rb +++ b/data/rbot/plugins/wserver.rb @@ -29,30 +29,26 @@ class WserverPlugin < Plugin return end - http = @bot.httputil.get_proxy(uri) - http.open_timeout = 5 - http.start {|http| - resp = http.head('/') - server = resp['Server'] - if(server && server.length > 0) - m.reply "#{uri.host} is running #{server}" - else - m.reply "couldn't tell what #{uri.host} is running" - end - - if(resp.code == "302" || resp.code == "301") - newloc = resp['location'] - newuri = URI.parse(newloc) - # detect and ignore incorrect redirects (to relative paths etc) - if (newuri.host != nil) - if(uri.host != newuri.host) - m.reply "#{uri.host} redirects to #{newuri.scheme}://#{newuri.host}" - raise resp['location'] - end + resp = @bot.httputil.head(uri) + server = resp['Server'] + if(server && server.length > 0) + m.reply "#{uri.host} is running #{server}" + else + m.reply "couldn't tell what #{uri.host} is running" + end + + if(resp.code == "302" || resp.code == "301") + newloc = resp['location'] + newuri = URI.parse(newloc) + # detect and ignore incorrect redirects (to relative paths etc) + if (newuri.host != nil) + if(uri.host != newuri.host) + m.reply "#{uri.host} redirects to #{newuri.scheme}://#{newuri.host}" + raise resp['location'] end end - } + end rescue TimeoutError => err m.reply "timed out connecting to #{uri.host}:#{uri.port} :(" return |