Skip to content

Commit

Permalink
UrlFetchTitle: メッセージを変更した
Browse files Browse the repository at this point in the history
refs #22, #25
fixes #34

* 暫定的にその他のエラーのメッセージをそのまま流すようにした
* charset を表示しないようにした
* Content-Length が存在しない場合に表示しないようにした
* HTTP エラーの表示を明確にした
* 自己署名証明書許可判定でのエラーを解消した
  • Loading branch information
ochaochaocha3 committed Mar 16, 2015
1 parent 449056a commit 061241b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
20 changes: 15 additions & 5 deletions lib/rgrb/plugin/url_fetch_title/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,12 @@ def fetch_title(url)
end
rescue SocketError
'(サーバーに接続できませんでした)'
rescue Timeout::Error, Net::HTTP::Persistent::Error
rescue Timeout::Error
'(タイムアウト)'
rescue Mechanize::ResponseCodeError => response_code_error
response_code_to_body(response_code_error.response_code.to_i)
rescue => e
"(#{e.class}: #{e})"
end

"#{@reply_prefix}#{body}#{@reply_suffix}"
Expand All @@ -119,10 +121,13 @@ def fetch_title(url)
# @return [String]
def http_header_to_body(header)
contents = [
header['Content-Type'],
header['Content-Length'].to_i.to_s(:human_size)
header['Content-Type'].split(';').first
]

if content_length = header['Content-Length']
contents << content_length.to_i.to_s(:human_size)
end

"(#{contents.join('; ')})"
end
private :http_header_to_body
Expand All @@ -138,7 +143,7 @@ def response_code_to_body(response_code)
'不明なエラー'
end

"(#{response_code} #{reason_phrase})"
"(HTTP #{response_code} #{reason_phrase})"
end
private :response_code_to_body

Expand All @@ -164,7 +169,12 @@ def new_agent(url)
# @param [String] url 調べる URL
# @return [Boolean]
def no_ssl_verify?(url)
hostname = URI.parse(url).hostname
begin
hostname = URI.parse(url).hostname
rescue
return false
end

hostname.end_with?(*@no_ssl_verify)
end
private :no_ssl_verify?
Expand Down
11 changes: 9 additions & 2 deletions lib/rgrb/plugin/url_fetch_title/irc_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,15 @@ def initialize(*args)
# NOTICE でページのタイトルを返す
# @return [void]
def fetch_title(m)
URI.extract(m.message, %w(http https)) do |url|
m.target.send(@generator.fetch_title(url), true)
urls = URI.extract(m.message, %w(http https))
unless urls.empty?
log(m.raw, :incoming, :info)

urls.each do |url|
message = @generator.fetch_title(url)
m.target.send(message, true)
log("<NOTICE to #{m.target}> #{message}", :outgoing, :info)
end
end
end
end
Expand Down

0 comments on commit 061241b

Please sign in to comment.