01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#!/usr/bin/env ruby
# $Id$
require 'net/imap'
require 'ftools'
require "tempfile"
require "kconv"
require "time"
require "bsfilter"
HOST = "mp.nii.ac.jp"
UID = "masao"
PASSWORD = "XXXXXXXX"
bsfilter = Bsfilter.new
bsfilter.setup( ARGV + [ "-q" ])
delete_list = []
imap = Net::IMAP.new(HOST)
imap.login(UID, PASSWORD)
imap.select("INBOX")
# 90日(約3ヶ月)以上前のものに限定
before90days = ( Time.now - 60*60*24*90 ).strftime( "%d-%b-%Y" )
p before90days
uid_list = imap.uid_search( [ "BEFORE", before90days ] )
puts "loading #{ uid_list.size } mails..."
uid_list.each do |uid|
m = imap.uid_fetch( uid, ["RFC822","ENVELOPE","INTERNALDATE"])[0]
tmpf = Tempfile.open( File.basename( $0 ) )
tmpf.puts m.attr["RFC822"].gsub(/\r$/, "")
if bsfilter.run( tmpf.path )
puts Time.parse( m.attr["INTERNALDATE"] )
puts "Subject: " << NKF.nkf("-me", m.attr["ENVELOPE"].subject.to_s ).gsub(/\s+/, " ")
delete_list << uid
end
end
puts "deleting #{ delete_list.size } mails ..."
imap.uid_store( delete_list, "+FLAGS", [:Deleted] )
imap.expunge
imap.disconnect