Changeset 7803 for platform/tdiary/util

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

platform/tdiary/util/posttdiary/*: merge from Test_UTF8.

Location:
platform/tdiary/util/posttdiary
Files:
2 modified

Legend:

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

    r4633 r7803  
    11#!/usr/bin/env ruby 
    2 $KCODE= 'e' 
     2$KCODE= 'u' 
    33# 
    4 # posttdiary-ex: update tDiary via e-mail. $Revision: 1.9 $ 
     4# posttdiary-ex: update tDiary via e-mail. $Revision: 1.6.2.4 $ 
    55# 
    66# Copyright (C) 2002, All right reserved by TADA Tadashi <sho@spc.gr.jp> 
    77# You can redistribute it and/or modify it under GPL2. 
    88# 
    9 # 2007.11.22: v.1.66: Modified by K.Sakurai (http://ks.nwr.jp) 
     9# 2007.9.22: v.1.66: Modified by K.Sakurai (http://ks.nwr.jp) 
    1010#  Acknowledgements: 
    1111#   * Based on posttdiary.rb v1.2 by TADA. 
     
    327327                s.each do |t| 
    328328                        t.gsub!( /.*Value:/, '' ) 
    329                         v = NKF::nkf( '-m0 -Xed', t ).gsub!( /^\s+/, '' ).chomp! if $& 
     329                        v = NKF::nkf( '-m0 -wXd', t ).gsub!( /^\s+/, '' ).chomp! if $& 
    330330                end 
    331331        end 
     
    407407        end 
    408408        if sub_head =~ %r[^Content-Transfer-Encoding:\s*base64]i then 
    409                 addtext += NKF::nkf( '-eXd -mB', sub_body ) 
     409                addtext += NKF::nkf( '-wXd -mB', sub_body ) 
    410410        elsif 
    411411                addtext += sub_body 
     
    638638                        if /^[ \t]/ =~ headlines[n] then 
    639639                                s = headlines[n].sub( /^[ \t]/, '' ) 
    640                                 subject += NKF::nkf( '-eXd', s ) 
     640                                subject += NKF::nkf( '-wXd', s ) 
    641641                        else 
    642642                                break 
     
    645645                if /^Subject:(.*)$/ =~ headlines[n] then 
    646646                        s = $1.sub( /^\s+/, '' ) 
    647                         subject = NKF::nkf( '-eXd', s ) 
     647                        subject = NKF::nkf( '-wXd', s ) 
    648648                        nextline = true 
    649649                end 
     
    902902        Net::HTTP.version_1_2 
    903903 
    904         mail = NKF::nkf( '-m0 -Xed', ARGF.read ) 
     904        mail = NKF::nkf( '-m0 -wXd', ARGF.read ) 
    905905        raise "posttdiary-ex: no mail text." if not mail or mail.length == 0 
    906906         
  • platform/tdiary/util/posttdiary/posttdiary.rb

    r7629 r7803  
    11#!/usr/bin/env ruby 
    2 $KCODE= 'e' 
     2$KCODE= 'u' 
    33# 
    4 # posttdiary: update tDiary via e-mail. $Revision: 1.5 $ 
     4# posttdiary: update tDiary via e-mail. $Revision: 1.5.2.1 $ 
    55# 
    66# Copyright (C) 2002, All right reserved by TADA Tadashi <sho@spc.gr.jp> 
     
    99 
    1010def usage 
    11         <<-TEXT.gsub( /^\t{2}/, '' ) 
     11        text = <<-TEXT 
    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 
     29  TEXT 
     30  text.gsub( /\t/, '' ) 
    3031end 
    3132 
     
    3334        image_path = [] 
    3435        Dir.foreach( path ) do |file| 
    35                 if file =~ /(\d{8,})_(\d+)\./ and $1 == date then 
    36                         image_path[$2.to_i] = file 
     36                if file =~ /(\d{8,})_(\d+)\.(.*)/ then 
     37                        if $1 == date then 
     38                                image_path[$2.to_i] = file 
     39                        end 
    3740                end 
    3841        end 
     
    9093        end 
    9194        raise usage if (image_dir and not image_url) or (not image_dir and image_url) 
    92         image_dir.sub!( %r[/*$], '/' ) if image_dir 
    93         image_url.sub!( %r[/*$], '/' ) if 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 
    94101        url = ARGV.shift 
    95         if %r|http://([^:/]+)(?::(\d+))?(/.*)| =~ url then 
     102        if %r|http://([^:/]*):?(\d*)(/.*)| =~ url then 
    96103                host = $1 
    97                 port = ($2 || 80).to_i 
    98                 cgi  = $3 
     104                port = $2.to_i 
     105                cgi = $3 
     106                raise 'bad url.' if not host or not cgi 
     107                port = 80 if port == 0 
    99108        else 
    100109                raise 'bad url.' 
    101110        end 
    102  
     111         
    103112        user = ARGV.shift 
    104113        pass = ARGV.shift 
     
    108117        image_name = nil 
    109118 
    110         mail = NKF::nkf( '-m0 -Xed', ARGF.read ) 
     119        mail = NKF::nkf( '-m0 -wXd', ARGF.read ) 
    111120        raise "#{File::basename __FILE__}: no mail text." if not mail or mail.length == 0 
    112  
    113         head, body = mail.split( /(?:\r?\n){2}/, 2 ) 
     121         
     122        head, body = mail.split( /\r*\n\r*\n/, 2 ) 
    114123 
    115124        if head =~ /Content-Type:\s*Multipart\/Mixed.*boundary=\"(.*?)\"/im then 
     
    117126                        raise "no --image-path and --image-url options" 
    118127                end 
    119  
     128         
    120129                bound = "--" + $1 
    121130                body_sub = body.split( Regexp.compile( Regexp.escape( bound ) ) ) 
    122131                body_sub.each do |b| 
    123                         sub_head, sub_body = b.split( /(?:\r?\n){2}/, 2 ) 
    124  
    125                         next unless sub_head =~ /Content-Type:/i 
     132                        sub_head, sub_body = b.split( /\r*\n\r*\n/, 2 ) 
     133 
     134                        next unless sub_head =~ /Content-Type/ 
    126135 
    127136                        if sub_head =~ %r[^Content-Type:\s*text/plain]i then 
    128137                                @body = sub_body 
    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 
     138                        elsif sub_head =~ %r[^Content-Type:\s*(image/|application/octet-stream).*name=".*(\..*?)"]im 
     139                                image_ext = $2.downcase 
    135140                                now = Time::now 
    136141                                list = image_list( now.strftime( "%Y%m%d" ), image_dir ) 
     
    146151                                if /\.bmp$/i =~ image_name then 
    147152                                        bmp_to_png( image_dir + image_name ) 
    148                                         image_name.sub!( /\.bmp$/i, '.png' ) 
     153                                        image_name.sub!( /\.bmp$/, '.png' ) 
    149154                                end 
    150                                 @image_name ||= [] 
     155                                @image_name = [] unless @image_name 
    151156                                @image_name << image_name 
    152157                        end 
    153158                end 
    154         elsif head =~ /^Content-Type:\s*text\/plain/i 
     159        elsif head =~ /^Content-Type:\s*text\/plain/i  
    155160                @body = body 
    156161        else 
    157                 raise "cannot read this mail" 
     162                raise "can not read this mail" 
    158163        end 
    159164 
     
    161166                img_src = "" 
    162167                @image_name.each do |i| 
    163                         serial = i.sub( /^\d+_(\d+)\..*$/n, '\1' ) 
     168                        serial = i.sub( /^\d+_(\d+)\..*$/, '\1' ) 
    164169                        img_src += image_format.gsub( /\$0/, serial ).gsub( /\$1/, image_url + i ) 
    165170                end 
    166171                if use_subject then 
    167                         @body = "#{img_src}\n#{@body}".sub( /(?:\r?\n|\r)+\z/, "\n" ) 
     172                        @body = "#{img_src}\n#{@body.sub( /\n+\z/, '' )}" 
    168173                else 
    169                         @body = "#{@body}".sub( /(?:\r?\n|\r)+\z/, "\n" ) << img_src 
     174                        @body = "#{@body.sub( /\n+\z/, '' )}\n#{img_src}" 
    170175                end 
    171176        end 
     
    173178        addr = nil 
    174179        if /^To:(.*)$/ =~ head then 
    175                 addr = case to = $1.strip 
    176                 when /.*?\s*<(.+)>/, /(.+?)\s*\(.*\)/ 
    177                         $1 
     180                to = $1.strip 
     181                if /.*?\s*<(.*)>/ =~ to then 
     182                        addr = $1 
     183                elsif /(.*?)\s*\(.*\)/ =~ to 
     184                        addr = $1 
    178185                else 
    179                         to 
    180                 end 
    181         end 
    182  
     186                        addr = to 
     187                end 
     188        end 
     189         
    183190        if /([^-]+)-(.*)@/ =~ addr then 
    184                 user ||= $1 
    185                 pass ||= $2 
    186         end 
    187  
     191                user = $1 unless user 
     192                pass = $2 unless pass 
     193        end 
     194         
    188195        raise "no user." unless user 
    189196        raise "no passwd." unless pass 
    190  
     197         
    191198        subject = '' 
    192199        nextline = false 
    193         headlines = head.split( /(?:\r?\n|\r)+/ ) 
     200        headlines = head.split( /[\r\n]+/ ) 
    194201        for n in 0 .. headlines.size-1 
    195202                if nextline then 
    196203                        if /^[ \t]/ =~ headlines[n] then 
    197204                                s = headlines[n].sub( /^[ \t]/, '' ) 
    198                                 subject += NKF::nkf( '-eXd', s ) 
     205                                subject += NKF::nkf( '-wXd', s ) 
    199206                        else 
    200207                                break 
    201208                        end 
    202209                end 
    203                 if /^Subject:\s*(.+)$/i =~ headlines[n] then 
    204                         subject = NKF::nkf( '-eXd', $1 ) 
     210                if /^Subject:(.*)$/ =~ headlines[n] then 
     211                        s = $1.sub( /^\s+/, '' ) 
     212                        subject = NKF::nkf( '-wXd', s ) 
    205213                        nextline = true 
    206214                end 
     
    222230        Net::HTTP.start( host, port ) do |http| 
    223231                auth = ["#{user}:#{pass}"].pack( 'm' ).strip 
    224                 res, = http.get( cgi, { 
     232                res, = http.get( cgi, 
    225233                                'Authorization' => "Basic #{auth}", 
    226                                 'Referer' => url }) 
     234                                'Referer' => url ) 
    227235                if %r|<input type="hidden" name="csrf_protection_key" value="([^"]+)">| =~ res.body then 
    228236                        data << "&csrf_protection_key=#{CGI::escape( CGI::unescapeHTML( $1 ) )}" 
    229237                end 
    230                 res, = http.post( cgi, data, { 
     238                res, = http.post( cgi, data, 
    231239                                'Authorization' => "Basic #{auth}", 
    232                                 'Referer' => url }) 
     240                                'Referer' => url ) 
    233241        end 
    234242 
    235243rescue 
    236244        $stderr.puts $! 
    237         $stderr.puts $@.join( "\n" ) 
    238245        File::delete( image_dir + image_name ) if image_dir and image_name and FileTest::exist?( image_dir + image_name ) 
    239246        exit 1