Changeset 7079 for platform/tdiary/util

Show
Ignore:
Timestamp:
02/24/08 02:22:08 (9 months ago)
Author:
drry
Message:

platform/tdiary/util/posttdiary/posttdiary.rb:

  • Net::HTTP#get/post の HTTP ヘッダがハッシュになっていなかったのを直しました。
  • 正規表現を修正しました。
  • いくつかの冗長な部分を簡略化しました。
  • インデントの修整をしました。
  • ほか。
Files:
1 modified

Legend:

Unmodified
Added
Removed
  • platform/tdiary/util/posttdiary/posttdiary.rb

    r7072 r7079  
    99 
    1010def usage 
    11         text = <<-TEXT 
     11        <<-TEXT.gsub( /^\t{2}/, '' ) 
    1212                #{File::basename __FILE__}: update tDiary via e-mail. 
    1313                usage: ruby #{File::basename __FILE__} [options] <url> [user] [passwd] 
     
    2727                  --use-subject,  -s: use mail subject to subtitle. 
    2828                          and insert image between subtitle and body. 
    29   TEXT 
    30   text.gsub( /\t/, '' ) 
     29        TEXT 
    3130end 
    3231 
     
    3433        image_path = [] 
    3534        Dir.foreach( path ) do |file| 
    36                 if file =~ /(\d{8,})_(\d+)\.(.*)/ then 
    37                         if $1 == date then 
    38                                 image_path[$2.to_i] = file 
    39                         end 
     35                if file =~ /(\d{8,})_(\d+)\./ and $1 == date then 
     36                        image_path[$2.to_i] = file 
    4037                end 
    4138        end 
     
    7168        use_subject = false 
    7269        parser.set_options( 
    73                 ['--image-path', '-i', GetoptLong::REQUIRED_ARGUMENT], 
    74                 ['--image-url', '-u', GetoptLong::REQUIRED_ARGUMENT], 
     70                ['--image-path',   '-i', GetoptLong::REQUIRED_ARGUMENT], 
     71                ['--image-url',    '-u', GetoptLong::REQUIRED_ARGUMENT], 
    7572                ['--image-format', '-f', GetoptLong::REQUIRED_ARGUMENT], 
    76                 ['--use-subject', '-s', GetoptLong::NO_ARGUMENT] 
     73                ['--use-subject',  '-s', GetoptLong::NO_ARGUMENT] 
    7774        ) 
    7875        begin 
     
    9390        end 
    9491        raise usage if (image_dir and not image_url) or (not image_dir and image_url) 
    95         if image_dir then 
    96                 image_dir += '/' unless %r[/$] =~ image_dir 
    97         end 
    98         if image_url then 
    99                 image_url += '/' unless %r[/$] =~ image_url 
    100         end 
     92        image_dir.sub!( %r[/*$], '/' ) if image_dir 
     93        image_url.sub!( %r[/*$], '/' ) if image_url 
    10194        url = ARGV.shift 
    10295        if %r|http://([^:/]*):?(\d*)(/.*)| =~ url then 
     
    109102                raise 'bad url.' 
    110103        end 
    111          
     104 
    112105        user = ARGV.shift 
    113106        pass = ARGV.shift 
     
    119112        mail = NKF::nkf( '-m0 -Xed', ARGF.read ) 
    120113        raise "#{File::basename __FILE__}: no mail text." if not mail or mail.length == 0 
    121          
    122         head, body = mail.split( /\r*\n\r*\n/, 2 ) 
     114 
     115        head, body = mail.split( /(?:\r?\n){2}, 2 ) 
    123116 
    124117        if head =~ /Content-Type:\s*Multipart\/Mixed.*boundary=\"(.*?)\"/im then 
     
    126119                        raise "no --image-path and --image-url options" 
    127120                end 
    128          
     121 
    129122                bound = "--" + $1 
    130123                body_sub = body.split( Regexp.compile( Regexp.escape( bound ) ) ) 
    131124                body_sub.each do |b| 
    132                         sub_head, sub_body = b.split( /\r*\n\r*\n/, 2 ) 
    133  
    134                         next unless sub_head =~ /Content-Type/ 
     125                        sub_head, sub_body = b.split( /(?:\r?\n){2}/, 2 ) 
     126 
     127                        next unless sub_head =~ /Content-Type:/i 
    135128 
    136129                        if sub_head =~ %r[^Content-Type:\s*text/plain]i then 
    137130                                @body = sub_body 
    138                         elsif sub_head =~ %r[^Content-Type:\s*(image/|application/octet-stream).*name=".*(\..*?)"]im 
    139                                 image_ext = $2.downcase 
    140                                 now = Time::now 
    141                                 list = image_list( now.strftime( "%Y%m%d" ), image_dir ) 
     131                        elsif sub_head =~ %r[ 
     132                                ^Content-Type:\s* 
     133                                (?:image/ | application/octet-stream).+ 
     134                                name=".+(\.[^.]+?)" (?# 1: extension) 
     135                        ]imx 
     136                                image_ext  = $1.downcase 
     137                                now        = Time::now 
     138                                list       = image_list( now.strftime( "%Y%m%d" ), image_dir ) 
    142139                                image_name = now.strftime( "%Y%m%d" ) + "_" + list.length.to_s + image_ext 
    143140                                File::umask( 022 ) 
     
    151148                                if /\.bmp$/i =~ image_name then 
    152149                                        bmp_to_png( image_dir + image_name ) 
    153                                         image_name.sub!( /\.bmp$/, '.png' ) 
     150                                        image_name.sub!( /\.bmp$/i, '.png' ) 
    154151                                end 
    155                                 @image_name = [] unless @image_name 
     152                                @image_name ||= [] 
    156153                                @image_name << image_name 
    157154                        end 
    158155                end 
    159         elsif head =~ /^Content-Type:\s*text\/plain/i  
     156        elsif head =~ /^Content-Type:\s*text\/plain/i 
    160157                @body = body 
    161158        else 
     
    166163                img_src = "" 
    167164                @image_name.each do |i| 
    168                         serial = i.sub( /^\d+_(\d+)\..*$/, '\1' ) 
     165                        serial = i.sub( /^\d+_(\d+)\./n, '\1' ) 
    169166                        img_src += image_format.gsub( /\$0/, serial ).gsub( /\$1/, image_url + i ) 
    170167                end 
    171168                if use_subject then 
    172                         @body = "#{img_src}\n#{(@body || '').sub( /\n+\z/, '' )}" 
     169                        @body = "#{img_src}\n#{@body}".sub( /(?:\r?\n|\r)+\z/, "\n" ) 
    173170                else 
    174                         @body = "#{(@body || '').sub( /\n+\z/, '' )}\n#{img_src}" 
     171                        @body = "#{@body}".sub( /(?:\r?\n|\r)+\z/, "\n" ) << img_src 
    175172                end 
    176173        end 
     
    178175        addr = nil 
    179176        if /^To:(.*)$/ =~ head then 
    180                 to = $1.strip 
    181                 if /.*?\s*<(.*)>/ =~ to then 
    182                         addr = $1 
    183                 elsif /(.*?)\s*\(.*\)/ =~ to 
    184                         addr = $1 
     177                addr = case to = $1.strip 
     178                when /.*?\s*<(.+)>/ 
     179                when /(.+?)\s*\(.*\)/ 
     180                        $1 
    185181                else 
    186                         addr = to 
    187                 end 
    188         end 
    189          
     182                        to 
     183                end 
     184        end 
     185 
    190186        if /([^-]+)-(.*)@/ =~ addr then 
    191                 user = $1 unless user 
    192                 pass = $2 unless pass 
    193         end 
    194          
     187                user ||= $1 
     188                pass ||= $2 
     189        end 
     190 
    195191        raise "no user." unless user 
    196192        raise "no passwd." unless pass 
    197          
     193 
    198194        subject = '' 
    199195        nextline = false 
    200         headlines = head.split( /[\r\n]+/ ) 
     196        headlines = head.split( /(?:\r?\n|\r)+/ ) 
    201197        for n in 0 .. headlines.size-1 
    202198                if nextline then 
     
    208204                        end 
    209205                end 
    210                 if /^Subject:(.*)$/ =~ headlines[n] then 
    211                         s = $1.sub( /^\s+/, '' ) 
    212                         subject = NKF::nkf( '-eXd', s ) 
     206                if /^Subject:\s*(.+)$/i =~ headlines[n] then 
     207                        subject = NKF::nkf( '-eXd', $1 ) 
    213208                        nextline = true 
    214209                end 
     
    230225        Net::HTTP.start( host, port ) do |http| 
    231226                auth = ["#{user}:#{pass}"].pack( 'm' ).strip 
    232                 res, = http.get( cgi, 
     227                res, = http.get( cgi, { 
    233228                                'Authorization' => "Basic #{auth}", 
    234                                 'Referer' => url ) 
     229                                'Referer'       => url }) 
    235230                if %r|<input type="hidden" name="csrf_protection_key" value="([^"]+)">| =~ res.body then 
    236231                        data << "&csrf_protection_key=#{CGI::escape( CGI::unescapeHTML( $1 ) )}" 
    237232                end 
    238                 res, = http.post( cgi, data, 
     233                res, = http.post( cgi, data, { 
    239234                                'Authorization' => "Basic #{auth}", 
    240                                 'Referer' => url ) 
     235                                'Referer'       => url }) 
    241236        end 
    242237