# tb-send.rb $Revision: 1.15.2.2 $ # # Copyright (c) 2003 Junichiro Kita # You can distribute this file under the GPL. # add_edit_proc do |date| url = @cgi.params['plugin_tb_url'][0] || '' excerpt = @cgi.params['plugin_tb_excerpt'][0] || '' section = @cgi.params['plugin_tb_section'][0] || '' select_sections = '' unless @conf['tb.no_section'] then section_titles = '' idx = 1 selected = false diary = @diaries[@date.strftime('%Y%m%d')] if diary then diary.each_section do |t| anc = 'p%02d' % idx if section == anc then focus = ' selected' selected = true else focus = '' end section_titles << %[\n\t\t\t] idx += 1 end end anc = 'p%02d' % idx section_titles << %[] select_sections = <<-FROM
#{@tb_send_label_section}:
FROM end <<-FORM

TrackBack

#{@tb_send_label_url}:
#{select_sections}
#{@tb_send_label_excerpt}:
FORM end add_update_proc do tb_send_trackback if /^(append|replace)$/ =~ @mode end def tb_send_trackback urls = (@cgi.params['plugin_tb_url'][0] || '').split title = @cgi.params['title'][0] excerpt = @cgi.params['plugin_tb_excerpt'][0] section = @cgi.params['plugin_tb_section'][0] blog_name = @conf.html_title date = @date.strftime( '%Y%m%d' ) if section && !section.empty? then diary = @diaries[date] ary = []; diary.each_section{|s| ary << s} section = sprintf( 'p%02d', ary.length ) if @mode == 'append' num = section[1..-1].to_i - 1 if num < ary.size title = ary[num].subtitle_to_html if ary[num].subtitle && !ary[num].subtitle.empty? excerpt = ary[num].body_to_html if excerpt.empty? end end if !excerpt || excerpt.empty? excerpt = @diaries[date].to_html({}) end old_apply_plugin = @options['apply_plugin'] @options['apply_plugin'] = true title = apply_plugin( title, true ) excerpt = apply_plugin( excerpt, true ) @options['apply_plugin'] = old_apply_plugin #if excerpt.length >= 255 then # excerpt = @conf.shorten( excerpt.gsub( /\r/, '' ).gsub( /\n/, "\001" ), 252 ).gsub( /\001/, "\n" ) #end my_url = %Q|#{@conf.index}#{anchor(@date.strftime('%Y%m%d'))}| my_url[0, 0] = @conf.base_url if %r|^https?://|i !~ @conf.index my_url += "##{section}" if section && !section.empty? my_url.gsub!( %r|/\./|, '/' ) require 'net/http' urls.each do |url| trackback = "url=#{CGI::escape(my_url)}" trackback << "&charset=#{@tb_send_ping_charset}" trackback << "&title=#{CGI::escape( @conf.to_native( title ) )}" unless title.empty? trackback << "&excerpt=#{CGI::escape( @conf.to_native( excerpt) )}" unless excerpt.empty? trackback << "&blog_name=#{CGI::escape(blog_name)}" if %r|^http://([^/]+)(/.*)$| =~ url then request = $2 host, port = $1.split( /:/, 2 ) port = '80' unless port Net::HTTP.version_1_1 begin Net::HTTP.start( host.untaint, port.to_i ) do |http| response, = http.post( request, trackback, "Content-Type" => 'application/x-www-form-urlencoded') error, = response.body.scan(%r|(\d)|)[0] if error == '1' reason, = response.body.scan(%r|(.*)|m)[0] raise TDiaryTrackBackError.new(reason) if urls.length == 1 end end rescue raise TDiaryTrackBackError.new( "when sending TrackBack Ping: #{$!.message}" ) if urls.length == 1 end else raise TDiaryTrackBackError.new( "unknown URL: #{url}" ) if urls.length == 1 end end end # vim: ts=3