1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
# pingback.rb: $Revision: 1.5 $
#
# Pingback when updating.
#
# options:
# @options['pingback.url'] : URL of Pingback.
# @options['pingback.expire'] : TTL for cache.
#
# Copyright (c) 2004 MoonWolf <moonwolf@moonwolf.com>
# Distributed under the GPL
#
add_header_proc do
%Q!\t<link rel="pingback" href="#{h( @options['pingback.url'] )}" />\n!
end
add_update_proc do
date = @date.strftime( "%Y%m%d" )
diary = @diaries[date]
require 'pstore'
cache = {}
PStore::new( "#{@cache_path}/pingback.cache" ).transaction do |db|
begin
cache = db['cache'] if db.root?( 'cache' )
if /^append|replace$/ =~ @mode then
index = 0
diary.each_section do |section|
index += 1
id = "#{date}p%02d" % index
my_url = %Q|#{h( @conf.index )}#{h( anchor(@date.strftime('%Y%m%d') ) )}|
my_url[0, 0] = @conf.base_url if %r|^https?://|i !~ @conf.index
my_url += "\#p%02d" % index
my_url.gsub!( %r|/\./|, '/' )
if diary.visible?
html = ''
html << section.subtitle_to_html if section.subtitle_to_html
html << section.body_to_html
html.scan(/<a\s(.+?)>/i) {
begin
attr = $1
if attr=~/href\s*=\s*(?:"(http:.+?)"|'(http:.+?)')/
url = $1 || $2
raise if url=~/\.(gif|jpg|png|zip|lzh|rar|exe|tar|gz|bz2|txt)\z/i
@options['pingback.exclude'].each {|text|
text.strip!
next if text.empty?
raise if /#{text}/i =~ url
}
if t=cache[id+url]
if t<Time.now
pingback_send(my_url, url)
cache[id+url] = Time.now + @options['pingback.expire'].to_i
end
else
pingback_send(my_url, url)
cache[id+url] = Time.now + @options['pingback.expire'].to_i
end
end
rescue
end
}
end
end
end
db['cache'] = cache
rescue PStore::Error
end
end
end
def pingback_send(sourceURI,targetURI)
require 'uri'
uri = URI.parse(targetURI)
return unless uri.scheme=='http'
# detect Pingback server URL
target = nil
require 'net/http'
Net::HTTP.version_1_2
Net::HTTP.start(uri.host, uri.port) {|http|
path = uri.path
path << "?" << uri.query if uri.query
response = http.get(path)
if value=response['X-Pingback']
target = URI.parse(value)
elsif response.body=~%r!<link rel="pingback" href="([^"]+)" ?/?>! #"
target = URI.parse($1)
end
}
return unless target
# XMLRPC call
require 'xmlrpc/client'
path = target.path
path << "?" << target.query if target.query
server = XMLRPC::Client.new(target.host, path, target.port)
param = server.call("pingback.ping", sourceURI, targetURI)
rescue Exception,Timeout::Error
end
#
# for conf_proc
#
def pingback_init
@conf['pingback.url'] ||= ''
@conf['pingback.expire'] ||= '86400'
@conf['pingback.exclude'] ||= ''
end
def saveconf_pingback
if @mode == 'saveconf' then
@conf['pingback.url'] = @cgi.params['pingback.url'][0] || ''
@conf['pingback.expire'] = @cgi.params['pingback.expire'][0] || '86400'
@conf['pingback.exclude'] = @cgi.params['pingback.exclude'][0] || ''
end
end
|