Changeset 5857

Show
Ignore:
Timestamp:
01/30/08 10:18:53 (5 years ago)
Author:
drry
Message:

lang/ruby/chokan/trunk/plugins/auto_uri_escape.rb:

  • use Net::HTTP 1.2
  • add REVISION constant and User-Agent HTTP header
  • use URI, URI.regexp instead of regexps
  • change the sorting order of the encodings
  • fix the scope of a status code to consider that URL exists
Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/ruby/chokan/trunk/plugins/auto_uri_escape.rb

    r5415 r5857  
    44require 'net/http' 
    55 
     6Net::HTTP.version_1_2 
     7 
    68class AutoUriEscape < Chokan::PluginBase 
     9        REVISION = '$Rev: $'.gsub(/\D+/, '').to_i 
     10        def initialize(config, chokan) 
     11                super 
     12                @shunirr    = @config['shunirr'] || ' x shunirr' 
     13                @user_agent = @config['user_agent'] || "#{self.class}/#{REVISION} (chokan#{@shunirr})" 
     14        end 
     15 
    716        def on_privmsg(prefix, channel, message) 
    8                 if  @config['channel'].include?(channel) && /^http/ =~ message 
    9                         uri = message 
    10                         mes = nil 
    11                         f   = false 
    12                         if uri == URI.escape(uri) || uri.include?('%') 
    13                                 return 
    14                         end 
    15                         [Iconv.conv('Shift_JIS', 'UTF-8', uri),  
    16                                 Iconv.conv('EUC-JP', 'UTF-8', uri), 
    17                                 uri].each do |u| 
    18                                 mes = URI.escape u 
    19                                 if uri_exist? mes 
    20                                         f = true 
     17                unless @config['channel'].include?(channel) && 
     18                       URI.regexp(['http']).match(message) # message[/^http/] 
     19                        return 
     20                end 
     21                res = [] 
     22                message.split.each do |m| 
     23                        next if m == URI.escape(m) || m.include?('%') 
     24                        [ 
     25                                m, 
     26                                Iconv.conv('EUC-JP',    'UTF-8', m), 
     27                                Iconv.conv('Shift_JIS', 'UTF-8', m), 
     28                        ].each do |u| 
     29                                u   = URI.escape u 
     30                                uri = URI.extract(u, ['http']).first 
     31                                if uri && uri_exist?(uri) 
     32                                        res << uri 
    2133                                        break 
    2234                                end 
    23                                 end 
    24                         unless f 
    25                                 return 
    2635                        end 
    27                         notice channel, mes 
    2836                end 
     37                res.uniq! 
     38                return if res.empty? 
     39                notice channel, res.join(' ') 
    2940        end 
    3041 
    3142        private 
    3243        def uri_exist?(uri) 
    33                 uri = /^http:\/\/([-_.a-zA-Z0-9]+)\/(.+)$/.match(uri).to_a 
     44                uri = URI.parse uri 
    3445                res = nil 
    35                 Net::HTTP.start(uri[1], 80) do |http| 
     46                Net::HTTP.start(uri.host, uri.port) do |http| 
     47                        #uri.open_timeout = 1 
    3648                        begin 
    37                                 res = http.head "/#{uri[2]}" 
     49                                res = http.head uri.request_uri, { 'User-Agent' => @user_agent } 
    3850                        rescue 
    3951                                return false 
    4052                        end 
    4153                end 
    42                 return (res.code == '200') ? true : false 
     54                return (200 .. 399).include?(res.code.to_i) 
    4355        end 
    4456end