Changeset 7804 for platform/tdiary/util

Show
Ignore:
Timestamp:
03/11/08 21:01:14 (9 months ago)
Author:
hsbt
Message:

platform/tdiary/util/posttdiary/posttdiary: rollback to r7629,and support utf-8

Files:
1 modified

Legend:

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

    r7803 r7804  
    22$KCODE= 'u' 
    33# 
    4 # posttdiary: update tDiary via e-mail. $Revision: 1.5.2.1 $ 
     4# posttdiary: update tDiary via e-mail. $Revision: 1.5 $ 
    55# 
    66# Copyright (C) 2002, All right reserved by TADA Tadashi <sho@spc.gr.jp> 
     
    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 
     
    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 
    102         if %r|http://([^:/]*):?(\d*)(/.*)| =~ url then 
     95        if %r|http://([^:/]+)(?::(\d+))?(/.*)| =~ url then 
    10396                host = $1 
    104                 port = $2.to_i 
    105                 cgi = $3 
    106                 raise 'bad url.' if not host or not cgi 
    107                 port = 80 if port == 0 
     97                port = ($2 || 80).to_i 
     98                cgi  = $3 
    10899        else 
    109100                raise 'bad url.' 
    110101        end 
    111          
     102 
    112103        user = ARGV.shift 
    113104        pass = ARGV.shift 
     
    117108        image_name = nil 
    118109 
    119         mail = NKF::nkf( '-m0 -wXd', ARGF.read ) 
     110        mail = NKF::nkf( '-m0 -Xwd', ARGF.read ) 
    120111        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 ) 
     112 
     113        head, body = mail.split( /(?:\r?\n){2}/, 2 ) 
    123114 
    124115        if head =~ /Content-Type:\s*Multipart\/Mixed.*boundary=\"(.*?)\"/im then 
     
    126117                        raise "no --image-path and --image-url options" 
    127118                end 
    128          
     119 
    129120                bound = "--" + $1 
    130121                body_sub = body.split( Regexp.compile( Regexp.escape( bound ) ) ) 
    131122                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/ 
     123                        sub_head, sub_body = b.split( /(?:\r?\n){2}/, 2 ) 
     124 
     125                        next unless sub_head =~ /Content-Type:/i 
    135126 
    136127                        if sub_head =~ %r[^Content-Type:\s*text/plain]i then 
    137128                                @body = sub_body 
    138                         elsif sub_head =~ %r[^Content-Type:\s*(image/|application/octet-stream).*name=".*(\..*?)"]im 
    139                                 image_ext = $2.downcase 
     129                        elsif sub_head =~ %r[ 
     130                                ^Content-Type:\s* 
     131                                (?:image/ | application/octet-stream).+ 
     132                                name=".+(\.[^.]+?)" (?# 1: extension) 
     133                        ]imx 
     134                                image_ext = $1.downcase 
    140135                                now = Time::now 
    141136                                list = image_list( now.strftime( "%Y%m%d" ), image_dir ) 
     
    151146                                if /\.bmp$/i =~ image_name then 
    152147                                        bmp_to_png( image_dir + image_name ) 
    153                                         image_name.sub!( /\.bmp$/, '.png' ) 
     148                                        image_name.sub!( /\.bmp$/i, '.png' ) 
    154149                                end 
    155                                 @image_name = [] unless @image_name 
     150                                @image_name ||= [] 
    156151                                @image_name << image_name 
    157152                        end 
    158153                end 
    159         elsif head =~ /^Content-Type:\s*text\/plain/i  
     154        elsif head =~ /^Content-Type:\s*text\/plain/i 
    160155                @body = body 
    161156        else 
    162                 raise "can not read this mail" 
     157                raise "cannot read this mail" 
    163158        end 
    164159 
     
    166161                img_src = "" 
    167162                @image_name.each do |i| 
    168                         serial = i.sub( /^\d+_(\d+)\..*$/, '\1' ) 
     163                        serial = i.sub( /^\d+_(\d+)\..*$/n, '\1' ) 
    169164                        img_src += image_format.gsub( /\$0/, serial ).gsub( /\$1/, image_url + i ) 
    170165                end 
    171166                if use_subject then 
    172                         @body = "#{img_src}\n#{@body.sub( /\n+\z/, '' )}" 
     167                        @body = "#{img_src}\n#{@body}".sub( /(?:\r?\n|\r)+\z/, "\n" ) 
    173168                else 
    174                         @body = "#{@body.sub( /\n+\z/, '' )}\n#{img_src}" 
     169                        @body = "#{@body}".sub( /(?:\r?\n|\r)+\z/, "\n" ) << img_src 
    175170                end 
    176171        end 
     
    178173        addr = nil 
    179174        if /^To:(.*)$/ =~ head then 
    180                 to = $1.strip 
    181                 if /.*?\s*<(.*)>/ =~ to then 
    182                         addr = $1 
    183                 elsif /(.*?)\s*\(.*\)/ =~ to 
    184                         addr = $1 
     175                addr = case to = $1.strip 
     176                when /.*?\s*<(.+)>/, /(.+?)\s*\(.*\)/ 
     177                        $1 
    185178                else 
    186                         addr = to 
    187                 end 
    188         end 
    189          
     179                        to 
     180                end 
     181        end 
     182 
    190183        if /([^-]+)-(.*)@/ =~ addr then 
    191                 user = $1 unless user 
    192                 pass = $2 unless pass 
    193         end 
    194          
     184                user ||= $1 
     185                pass ||= $2 
     186        end 
     187 
    195188        raise "no user." unless user 
    196189        raise "no passwd." unless pass 
    197          
     190 
    198191        subject = '' 
    199192        nextline = false 
    200         headlines = head.split( /[\r\n]+/ ) 
     193        headlines = head.split( /(?:\r?\n|\r)+/ ) 
    201194        for n in 0 .. headlines.size-1 
    202195                if nextline then 
     
    208201                        end 
    209202                end 
    210                 if /^Subject:(.*)$/ =~ headlines[n] then 
    211                         s = $1.sub( /^\s+/, '' ) 
    212                         subject = NKF::nkf( '-wXd', s ) 
     203                if /^Subject:\s*(.+)$/i =~ headlines[n] then 
     204                        subject = NKF::nkf( '-wXd', $1 ) 
    213205                        nextline = true 
    214206                end 
     
    230222        Net::HTTP.start( host, port ) do |http| 
    231223                auth = ["#{user}:#{pass}"].pack( 'm' ).strip 
    232                 res, = http.get( cgi, 
     224                res, = http.get( cgi, { 
    233225                                'Authorization' => "Basic #{auth}", 
    234                                 'Referer' => url ) 
     226                                'Referer' => url }) 
    235227                if %r|<input type="hidden" name="csrf_protection_key" value="([^"]+)">| =~ res.body then 
    236228                        data << "&csrf_protection_key=#{CGI::escape( CGI::unescapeHTML( $1 ) )}" 
    237229                end 
    238                 res, = http.post( cgi, data, 
     230                res, = http.post( cgi, data, { 
    239231                                'Authorization' => "Basic #{auth}", 
    240                                 'Referer' => url ) 
     232                                'Referer' => url }) 
    241233        end 
    242234 
    243235rescue 
    244236        $stderr.puts $! 
     237        $stderr.puts $@.join( "\n" ) 
    245238        File::delete( image_dir + image_name ) if image_dir and image_name and FileTest::exist?( image_dir + image_name ) 
    246239        exit 1