File: linkcheck.rb

package info (click to toggle)
tdiary 5.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,092 kB
  • sloc: ruby: 23,031; javascript: 1,029; xml: 325; makefile: 26; sh: 2
file content (80 lines) | stat: -rw-r--r-- 2,057 bytes parent folder | download | duplicates (6)
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
#
# linkcheck.rb: tDiary filter for checking link to my site in TrackBack source site.
#
# specification:
#    * if source site has no URI of my site of top page, it's spam!
#    * reading only top of 100KB of source site.
#    * no response over 10 sec, it's mybe spam.
#
# Copyright (C) 2007 by TADA Tadashi <sho@spc.gr.jp>
# Distributed under GPL2 or any later version.
#

require 'open-uri'
require 'timeout'

module TDiary::Filter
	class LinkcheckFilter < Filter
		def initialize( *args )
			super( *args )
			@filter_mode = @conf['spamfilter.filter_mode']
			@filter_mode = true if @filter_mode == nil
		end

		def comment_filter( diary, comment )
			if @conf['spamfilter.linkcheck'] == 0 then
				debug( "No linkcheck to TrackBacks.", DEBUG_FULL )
				return true
			end

			# check only TrackBack
			return true unless comment.name == 'TrackBack'

			dest_uri = @conf.index.dup
			dest_uri[0, 0] = base_url if %r|^https?://|i !~ @conf.index
			dest_uri.gsub!( %r|/\./|, '/' )

			# TrackBack URI is the 1st line of comment.body.
			src_uri, = comment.body.split( /\n/ )
			unless %r|^https?://|i =~ src_uri then
				debug( "TrackBack has bad source URI." )
				comment.show = false
				return @filter_mode
			end
			if src_uri.index( dest_uri ) == 0 then
				debug( "TrackBack was sent to myself.", DEBUG_FULL )
				return true
			end

			begin
				Timeout::timeout( 10 ) do
	      		open( src_uri ) do |f|
						if f.read( 100 * 1024 ).include?( dest_uri ) then
							debug( "TrackBack has links to me.", DEBUG_FULL )
							return true
						else
							debug( "TrackBack dose not have links to me." )
							comment.show = false
							return @filter_mode
						end
					end
				end
			rescue Timeout::Error
				debug( "TrackBack source was no response." )
				comment.show = false
				return @filter_mode
			rescue
				debug( "Cannot access to TrackBack source (#{$!})." )
				comment.show = false
				return @filter_mode
			end
		end
	end
end

# Local Variables:
# mode: ruby
# indent-tabs-mode: t
# tab-width: 3
# ruby-indent-level: 3
# End: