Changeset 7079 for platform/tdiary/util
- Timestamp:
- 02/24/08 02:22:08 (9 months ago)
- Files:
-
- 1 modified
-
platform/tdiary/util/posttdiary/posttdiary.rb (modified) (13 diffs)
Legend:
- Unmodified
- Added
- Removed
-
platform/tdiary/util/posttdiary/posttdiary.rb
r7072 r7079 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 … … 71 68 use_subject = false 72 69 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], 75 72 ['--image-format', '-f', GetoptLong::REQUIRED_ARGUMENT], 76 ['--use-subject', '-s', GetoptLong::NO_ARGUMENT]73 ['--use-subject', '-s', GetoptLong::NO_ARGUMENT] 77 74 ) 78 75 begin … … 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 95 if %r|http://([^:/]*):?(\d*)(/.*)| =~ url then … … 109 102 raise 'bad url.' 110 103 end 111 104 112 105 user = ARGV.shift 113 106 pass = ARGV.shift … … 119 112 mail = NKF::nkf( '-m0 -Xed', ARGF.read ) 120 113 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 ) 123 116 124 117 if head =~ /Content-Type:\s*Multipart\/Mixed.*boundary=\"(.*?)\"/im then … … 126 119 raise "no --image-path and --image-url options" 127 120 end 128 121 129 122 bound = "--" + $1 130 123 body_sub = body.split( Regexp.compile( Regexp.escape( bound ) ) ) 131 124 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 135 128 136 129 if sub_head =~ %r[^Content-Type:\s*text/plain]i then 137 130 @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 ) 142 139 image_name = now.strftime( "%Y%m%d" ) + "_" + list.length.to_s + image_ext 143 140 File::umask( 022 ) … … 151 148 if /\.bmp$/i =~ image_name then 152 149 bmp_to_png( image_dir + image_name ) 153 image_name.sub!( /\.bmp$/ , '.png' )150 image_name.sub!( /\.bmp$/i, '.png' ) 154 151 end 155 @image_name = [] unless @image_name152 @image_name ||= [] 156 153 @image_name << image_name 157 154 end 158 155 end 159 elsif head =~ /^Content-Type:\s*text\/plain/i 156 elsif head =~ /^Content-Type:\s*text\/plain/i 160 157 @body = body 161 158 else … … 166 163 img_src = "" 167 164 @image_name.each do |i| 168 serial = i.sub( /^\d+_(\d+)\. .*$/, '\1' )165 serial = i.sub( /^\d+_(\d+)\./n, '\1' ) 169 166 img_src += image_format.gsub( /\$0/, serial ).gsub( /\$1/, image_url + i ) 170 167 end 171 168 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" ) 173 170 else 174 @body = "#{ (@body || '').sub( /\n+\z/, '' )}\n#{img_src}"171 @body = "#{@body}".sub( /(?:\r?\n|\r)+\z/, "\n" ) << img_src 175 172 end 176 173 end … … 178 175 addr = nil 179 176 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 185 181 else 186 addr =to187 end 188 end 189 182 to 183 end 184 end 185 190 186 if /([^-]+)-(.*)@/ =~ addr then 191 user = $1 unless user192 pass = $2 unless pass193 end 194 187 user ||= $1 188 pass ||= $2 189 end 190 195 191 raise "no user." unless user 196 192 raise "no passwd." unless pass 197 193 198 194 subject = '' 199 195 nextline = false 200 headlines = head.split( / [\r\n]+/ )196 headlines = head.split( /(?:\r?\n|\r)+/ ) 201 197 for n in 0 .. headlines.size-1 202 198 if nextline then … … 208 204 end 209 205 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 ) 213 208 nextline = true 214 209 end … … 230 225 Net::HTTP.start( host, port ) do |http| 231 226 auth = ["#{user}:#{pass}"].pack( 'm' ).strip 232 res, = http.get( cgi, 227 res, = http.get( cgi, { 233 228 'Authorization' => "Basic #{auth}", 234 'Referer' => url)229 'Referer' => url }) 235 230 if %r|<input type="hidden" name="csrf_protection_key" value="([^"]+)">| =~ res.body then 236 231 data << "&csrf_protection_key=#{CGI::escape( CGI::unescapeHTML( $1 ) )}" 237 232 end 238 res, = http.post( cgi, data, 233 res, = http.post( cgi, data, { 239 234 'Authorization' => "Basic #{auth}", 240 'Referer' => url)235 'Referer' => url }) 241 236 end 242 237
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)