Changeset 7804 for platform/tdiary/util
- Timestamp:
- 03/11/08 21:01:14 (9 months ago)
- Files:
-
- 1 modified
-
platform/tdiary/util/posttdiary/posttdiary.rb (modified) (12 diffs)
Legend:
- Unmodified
- Added
- Removed
-
platform/tdiary/util/posttdiary/posttdiary.rb
r7803 r7804 2 2 $KCODE= 'u' 3 3 # 4 # posttdiary: update tDiary via e-mail. $Revision: 1.5 .2.1$4 # posttdiary: update tDiary via e-mail. $Revision: 1.5 $ 5 5 # 6 6 # Copyright (C) 2002, All right reserved by TADA Tadashi <sho@spc.gr.jp> … … 9 9 10 10 def usage 11 text = <<-TEXT11 <<-TEXT.gsub( /^\t{2}/, '' ) 12 12 #{File::basename __FILE__}: update tDiary via e-mail. 13 13 usage: ruby #{File::basename __FILE__} [options] <url> [user] [passwd] … … 27 27 --use-subject, -s: use mail subject to subtitle. 28 28 and insert image between subtitle and body. 29 TEXT 30 text.gsub( /\t/, '' ) 29 TEXT 31 30 end 32 31 … … 34 33 image_path = [] 35 34 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 40 37 end 41 38 end … … 93 90 end 94 91 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 101 94 url = ARGV.shift 102 if %r|http://([^:/] *):?(\d*)(/.*)| =~ url then95 if %r|http://([^:/]+)(?::(\d+))?(/.*)| =~ url then 103 96 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 108 99 else 109 100 raise 'bad url.' 110 101 end 111 102 112 103 user = ARGV.shift 113 104 pass = ARGV.shift … … 117 108 image_name = nil 118 109 119 mail = NKF::nkf( '-m0 - wXd', ARGF.read )110 mail = NKF::nkf( '-m0 -Xwd', ARGF.read ) 120 111 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 ) 123 114 124 115 if head =~ /Content-Type:\s*Multipart\/Mixed.*boundary=\"(.*?)\"/im then … … 126 117 raise "no --image-path and --image-url options" 127 118 end 128 119 129 120 bound = "--" + $1 130 121 body_sub = body.split( Regexp.compile( Regexp.escape( bound ) ) ) 131 122 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 135 126 136 127 if sub_head =~ %r[^Content-Type:\s*text/plain]i then 137 128 @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 140 135 now = Time::now 141 136 list = image_list( now.strftime( "%Y%m%d" ), image_dir ) … … 151 146 if /\.bmp$/i =~ image_name then 152 147 bmp_to_png( image_dir + image_name ) 153 image_name.sub!( /\.bmp$/ , '.png' )148 image_name.sub!( /\.bmp$/i, '.png' ) 154 149 end 155 @image_name = [] unless @image_name150 @image_name ||= [] 156 151 @image_name << image_name 157 152 end 158 153 end 159 elsif head =~ /^Content-Type:\s*text\/plain/i 154 elsif head =~ /^Content-Type:\s*text\/plain/i 160 155 @body = body 161 156 else 162 raise "can not read this mail"157 raise "cannot read this mail" 163 158 end 164 159 … … 166 161 img_src = "" 167 162 @image_name.each do |i| 168 serial = i.sub( /^\d+_(\d+)\..*$/ , '\1' )163 serial = i.sub( /^\d+_(\d+)\..*$/n, '\1' ) 169 164 img_src += image_format.gsub( /\$0/, serial ).gsub( /\$1/, image_url + i ) 170 165 end 171 166 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" ) 173 168 else 174 @body = "#{@body .sub( /\n+\z/, '' )}\n#{img_src}"169 @body = "#{@body}".sub( /(?:\r?\n|\r)+\z/, "\n" ) << img_src 175 170 end 176 171 end … … 178 173 addr = nil 179 174 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 185 178 else 186 addr =to187 end 188 end 189 179 to 180 end 181 end 182 190 183 if /([^-]+)-(.*)@/ =~ addr then 191 user = $1 unless user192 pass = $2 unless pass193 end 194 184 user ||= $1 185 pass ||= $2 186 end 187 195 188 raise "no user." unless user 196 189 raise "no passwd." unless pass 197 190 198 191 subject = '' 199 192 nextline = false 200 headlines = head.split( / [\r\n]+/ )193 headlines = head.split( /(?:\r?\n|\r)+/ ) 201 194 for n in 0 .. headlines.size-1 202 195 if nextline then … … 208 201 end 209 202 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 ) 213 205 nextline = true 214 206 end … … 230 222 Net::HTTP.start( host, port ) do |http| 231 223 auth = ["#{user}:#{pass}"].pack( 'm' ).strip 232 res, = http.get( cgi, 224 res, = http.get( cgi, { 233 225 'Authorization' => "Basic #{auth}", 234 'Referer' => url )226 'Referer' => url }) 235 227 if %r|<input type="hidden" name="csrf_protection_key" value="([^"]+)">| =~ res.body then 236 228 data << "&csrf_protection_key=#{CGI::escape( CGI::unescapeHTML( $1 ) )}" 237 229 end 238 res, = http.post( cgi, data, 230 res, = http.post( cgi, data, { 239 231 'Authorization' => "Basic #{auth}", 240 'Referer' => url )232 'Referer' => url }) 241 233 end 242 234 243 235 rescue 244 236 $stderr.puts $! 237 $stderr.puts $@.join( "\n" ) 245 238 File::delete( image_dir + image_name ) if image_dir and image_name and FileTest::exist?( image_dir + image_name ) 246 239 exit 1
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)