~ubuntu-branches/ubuntu/utopic/tdiary/utopic

« back to all changes in this revision

Viewing changes to contrib2/util/clean-spam/tdiary-comment-clean2

  • Committer: Bazaar Package Importer
  • Author(s): Daigo Moriwaki
  • Date: 2011-04-11 21:53:16 UTC
  • mfrom: (1.2.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20110411215316-ih4gt4q8p29d2wf8
Tags: 3.0.1-1
* New upstream release (Closes: #542801, #594947)
* debian/control:
 - Bumped up Standards-Version to 3.9.1.
 - Updated version dependency.
* debian/tdiary-setup.rb: Followed the upstream changes, incorporating js and
  index.fcgi

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env ruby
 
2
#
 
3
# Copyright (C) 2004 Satoru Takabayashi <satoru@namazu.org>
 
4
# You can redistribute it and/or modify it under GPL2.
 
5
#
 
6
# Modified by machu ( http://www.machu.jp/diary/ )
 
7
# 2008-03-07: adding last-modified filter with -a and -b option
 
8
#
 
9
require 'optparse'
 
10
after_date = Time.at(0)
 
11
before_date = Time.now
 
12
test = false
 
13
 
 
14
opt = OptionParser.new
 
15
opt.on('-a AFTER_HOUR') {|v|
 
16
  after_date = Time.at(Time.now - 60 * 60 * v.to_i)
 
17
}
 
18
opt.on('-b BEFORE_HOUR') {|v|
 
19
  before_date = Time.at(Time.now - 60 * 60 * v.to_i)
 
20
}
 
21
opt.on('-t') {|v|
 
22
  test = true
 
23
}
 
24
opt.parse!(ARGV)
 
25
 
 
26
puts "Usage: tdiary-comment-clean [-a AFTER_HOUR] [-b BEFORE_HOUR] PATTERN FILE..." if ARGV.length == 0
 
27
pattern = Regexp.new(ARGV.shift)
 
28
file_names = ARGV
 
29
 
 
30
class Comment
 
31
  attr_accessor :body, :date
 
32
 
 
33
  def initialize
 
34
    @body = ""
 
35
  end
 
36
 
 
37
  def <<(body)
 
38
    if body.match(/^Last-Modified: (\d+)$/)
 
39
      @date = Time.at($1.to_i)
 
40
    end
 
41
    @body << body
 
42
  end
 
43
end
 
44
 
 
45
deleted_comments = []
 
46
file_names.each {|file_name|
 
47
  i = File.open(file_name)
 
48
  first_line = i.gets
 
49
 
 
50
  comments = []
 
51
  comment = Comment.new
 
52
  while line = i.gets
 
53
    if line == ".\n"
 
54
      comments.push(comment)
 
55
      comment = Comment.new
 
56
    else
 
57
      comment << line
 
58
    end
 
59
  end
 
60
  i.close
 
61
 
 
62
  tmp_name = "tmp.#{Process.pid}"
 
63
  File.open(tmp_name, "w") {|o|
 
64
    o.print first_line
 
65
    comments.each {|comment|
 
66
      if pattern.match(comment.body) and (before_date > comment.date) and (after_date < comment.date)
 
67
        deleted_comments.push(comment)
 
68
      else
 
69
        o.print comment.body
 
70
        o.puts "."
 
71
      end
 
72
    }
 
73
  }
 
74
  File.rename(file_name, file_name + ".bak") unless test
 
75
  File.rename(tmp_name, file_name) unless test
 
76
}
 
77
 
 
78
deleted_comments.each {|comment|
 
79
  print comment.body
 
80
  puts "."
 
81
}