4
# Copyright (c) 2004-2005 T.Shimomura <redbug@netlife.gr.jp>
5
# You can redistribute it and/or modify it under GPL2.
6
# Please use version 1.0.0 (not 1.0.0G) if GPL doesn't want to be forced on me.
15
class AntirefspamFilter < Filter
16
# ͭ���ˤ���Ȼ��ꤷ���ե�����˥ǥХå�����ʸ������ɵ�����
17
def debug_out(filename, str)
19
filename = File.join(@conf.data_path,"AntiRefSpamFilter",filename)
20
File::open(filename, "a+") {|f|
26
# str �˻��ꤵ�줿ʸ����Ŭ�ڤʥ�����ޤ�Ǥ��뤫������å�
27
def isIncludeMyUrl(str)
28
# str ��������URL���ޤޤ�Ƥ��뤫�ɤ���
29
base_url = @conf.base_url
30
unless base_url.empty?
31
if str.include? base_url
36
# str �˥ȥåץڡ���URL���ޤޤ�Ƥ��뤫�ɤ���
37
unless @conf.index_page.empty?
38
if /\Ahttps?:\/\// =~ @conf.index_page
39
if str.include? @conf.index_page
45
# str �˵��Ƥ������褬�ޤޤ�Ƥ��뤫�ɤ���
46
if (myurl = @conf['antirefspam.myurl']) && !myurl.empty?
51
url = myurl.gsub("/", "\\/").gsub(":", "\\:")
61
def referer_filter(referer)
62
conf_disable = @conf['antirefspam.disable'] != nil ? @conf['antirefspam.disable'].to_s : ''
63
conf_checkreftable = @conf['antirefspam.checkreftable'] != nil ? @conf['antirefspam.checkreftable'].to_s : ''
64
conf_trustedurl = @conf['antirefspam.trustedurl'] != nil ? @conf['antirefspam.trustedurl'].to_s : ''
65
conf_proxy_server = @conf['antirefspam.proxy_server'] != nil && @conf['antirefspam.proxy_server'].size > 0 ? @conf['antirefspam.proxy_server'].to_s : nil
66
conf_proxy_port = @conf['antirefspam.proxy_port'] != nil && @conf['antirefspam.proxy_port'].size > 0 ? @conf['antirefspam.proxy_port'].to_s : nil
68
if conf_disable == 'true' or # ��������å���ͭ���ǤϤʤ����ϥ��롼����
69
referer == nil or # �����̵��
70
referer.size <= 1 or # �����Υ���ƥʤǹ������郎���ʤ��ʤ�������б����뤿�ᡢ�������ʸ������ξ��ϵ���
71
isIncludeMyUrl(referer) # ��ʬ�������⤫��Υ�Ͽ��ꤹ��
76
# "����Ǥ���URL" �Ĥ��ļ��Ф���referer�ȹ��פ��뤫�����å�����
77
conf_trustedurl.each_line do |trusted|
78
trusted.sub!(/\r?\n/,'')
79
next if trusted =~ /\A(\#|\s*)\z/ # #�ޤ��϶���ǻϤޤ�Ԥ��ɤ����Ф�
81
# �ޤ��� "����Ǥ��� URL" �� referer �˴ޤޤ�뤫�ɤ���
82
if referer.include? trusted
83
debug_out("trusted", trusted+" (include?) "+referer)
87
# �ޤޤ�ʤ��ä����� "����Ǥ��� URL" ������ɽ���Ȥߤʤ��ƺƥ����å�
89
if referer =~ Regexp.new( trusted.gsub("/", "\\/").gsub(":", "\\:") )
90
debug_out("trusted", trusted+" (=~) "+referer)
94
debug_out("error_config", "trustedurl: "+trusted)
99
if conf_checkreftable == 'true'
100
# "URL�ִ��ꥹ��" �Ĥ��ļ��Ф���referer�ȹ��פ��뤫�����å�����
101
@conf.referer_table.each do |url, name|
103
if /#{url}/i =~ referer && url != '^(.{50}).*$'
104
debug_out("trusted", url+" (=~referer_table) "+referer)
108
debug_out("error_config", "referer_table: "+url)
113
@work_path = File.join(@conf.data_path,"AntiRefSpamFilter")
114
@spamurl_list = File.join(@work_path,"spamurls") # referer spam �������
115
@spamip_list = File.join(@work_path,"spamips") # referer spam ��IP����
116
@safeurl_list = File.join(@work_path,"safeurls") # �����餯������Τʤ��������
118
# �ǥ��쥯�ȥ�/�ե����뤬¸�ߤ��ʤ���к��
119
unless File.exist? @work_path
120
Dir::mkdir(@work_path)
122
unless File.exist? @spamurl_list
123
File::open(@spamurl_list, "a").close
125
unless File.exist? @safeurl_list
126
File::open(@safeurl_list, "a").close
129
uri = URI.parse(referer)
130
temp_filename = File.join(@work_path,uri.host)
131
# �����å����ˤ��оݤΥɥᥤ��̾����ä�����ե��������
133
File::open(temp_filename, File::RDONLY | File::CREAT | File::EXCL).close
135
# ���� SPAM URL �Ȥߤʤ�����ʸ�ϰʸ�ϵ���
136
spamurls = IO::readlines(@spamurl_list).map {|url| url.chomp }
137
if spamurls.include? referer
141
# ���� SPAM URL �Ǥʤ���Ƚ�Ǥ�����ʸ�ϵ���
142
safeurls = IO::readlines(@safeurl_list).map {|url| url.chomp }
143
if safeurls.include? referer
147
# ��� URL �� HTML �����ĥ�äƤ���
148
Net::HTTP.version_1_2 # ���ޤ��ʤ��餷��
151
Net::HTTP::Proxy(conf_proxy_server, conf_proxy_port).start(uri.host, uri.port) do |http|
153
response, = http.get("/")
155
response, = http.get(uri.request_uri)
160
# body �������� URL ���ޤޤ�Ƥ��ʤ���� SPAM �Ȥߤʤ�
161
unless isIncludeMyUrl(body)
162
File::open(@spamurl_list, "a+") {|f|
165
File::open(@spamip_list, "a+") {|f|
166
f.puts [@cgi.remote_addr, Time.now.utc.strftime("%Y/%m/%d %H:%M:%S UTC")].join("\t")
170
File::open(@safeurl_list, "a+") {|f|
175
# ���顼���Ф����� @spamurl_list ������ʤ�������ˤ�����ʤ�
179
rescue StandardError, TimeoutError
180
# ���ߥ����å���ʤ顢����ϥ���˴��ꤷ�ʤ�
184
File::delete(temp_filename)
194
def log_spamcomment( diary, comment )
195
@work_path = File.join(@conf.data_path,"AntiRefSpamFilter")
196
@spamcomment_list = File.join(@work_path,"spamcomments") # comment spam ���
198
# �ǥ��쥯�ȥ�/�ե����뤬¸�ߤ��ʤ���к��
199
unless File.exist? @work_path
200
Dir::mkdir(@work_path)
202
unless File.exist? @spamcomment_list
203
File::open(@spamcomment_list, "a").close
206
File::open(@spamcomment_list, "a+") {|f|
207
f.puts "From: "+comment.name+" <"+comment.mail+">"
208
f.puts "To: "+diary.date.to_s
209
f.puts "Date: "+comment.date.to_s
215
def comment_filter( diary, comment )
216
# �ĥå��ߤ����ܸ�(�Ҥ餬��/��������)���ޤޤ�Ƥ��ʤ�����Ե���
217
if @conf['antirefspam.comment_kanaonly'] != nil
218
if @conf['antirefspam.comment_kanaonly'].to_s == 'true'
219
unless comment.body =~ /[��-��-����]/
220
log_spamcomment( diary, comment )
226
# �ĥå��ߤ�ʸ���������ꤷ����°���Ǥʤ��ʤ��Ե���
227
maxsize = @conf['antirefspam.comment_maxsize'].to_i
229
unless comment.body.size <= maxsize
230
log_spamcomment( comment )
235
# NG��ɤ����ĤǤ�ޤޤ�Ƥ������Ե���
236
if @conf['antirefspam.comment_ngwords'] != nil
237
ngwords = @conf['antirefspam.comment_ngwords']
238
ngwords.to_s.each_line do |ngword|
239
ngword.sub!(/\r?\n/,'')
240
if comment.body.downcase.include? ngword.downcase
241
log_spamcomment( comment )
245
# �ޤޤ�ʤ��ä����� "NG���" ������ɽ���Ȥߤʤ��ƺƥ����å�
247
if comment.body =~ Regexp.new( ngword, Regexp::MULTILINE )
248
log_spamcomment( comment )
252
debug_out("error_config", "comment_ngwords: "+ngword)