- type = options[:htmlinfo][:headers]['content-type'].first rescue nil
- case type
- when /^image\/.*/
- data = PHOTO
- data << CAPTION if line
- else
- if url.match(%r{^http://(\w+\.)?youtube\.com/watch.*})
- data = VIDEO
+ ready = false
+ api_url = WRITE_URL
+ tumblr = options[:htmlinfo][:headers]['x-tumblr-user'].to_s rescue nil
+ if tumblr
+ id = url.match(/\/post\/(\d+)/)
+ if id
+ id = id[1]
+
+ read_url = READ_URL % { :user => tumblr, :id => id}
+ # TODO seems to return 503 a little too frequently
+ xml = @bot.httputil.get(read_url)
+
+ if xml
+ reblog = REXML::Document.new(xml).elements["//post"].attributes["reblog-key"] rescue nil
+ if reblog and not reblog.empty?
+ api_url = REBLOG_URL
+ data = REBLOG
+ data << COMMENT
+ html_line = CGI.escapeHTML("(via <a href=%{url}>%{tumblr}</a>" % {
+ :url => url, :tumblr => tmblr
+ }) unless html_line
+ req << (data % {
+ :id => id,
+ :reblog => reblog,
+ :desc => CGI.escape(html_line)
+ })
+ ready = true
+ end
+ end
+ end
+ end
+
+ if not ready
+ type = options[:htmlinfo][:headers]['content-type'].first rescue nil
+ case type
+ when /^image\/.*/
+ data = PHOTO