| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
begin |
| 7 |
require 'rubygems' |
| 8 |
gem 'sqlite3-ruby' |
| 9 |
rescue LoadError |
| 10 |
end |
| 11 |
require 'sqlite3' |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
def select_by_id(hash, db, id, mydir) |
| 19 |
ret = nil |
| 20 |
db.execute("SELECT * FROM mew WHERE (mew.id = ?);", id) do |ent| |
| 21 |
if push_results(hash, ent, mydir) |
| 22 |
ret = ent |
| 23 |
end |
| 24 |
end |
| 25 |
return ret |
| 26 |
end |
| 27 |
|
| 28 |
def select_by_parid(hash, db, id, mydir) |
| 29 |
ret = [] |
| 30 |
db.execute("SELECT * FROM mew WHERE (mew.parid = ?);", id) do |ent| |
| 31 |
if push_results(hash, ent, mydir) |
| 32 |
ret.push(ent) |
| 33 |
end |
| 34 |
end |
| 35 |
return ret |
| 36 |
end |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
def push_results(hash, ent, mydir) |
| 44 |
if hash.key?(ent['id']) |
| 45 |
if File.dirname(ent['path']) == mydir |
| 46 |
hash.delete(ent['id']) |
| 47 |
hash[ent['id']] = ent |
| 48 |
end |
| 49 |
return nil |
| 50 |
else |
| 51 |
hash[ent['id']] = ent |
| 52 |
return true |
| 53 |
end |
| 54 |
end |
| 55 |
|
| 56 |
def print_results(hash) |
| 57 |
fin = hash.to_a.sort!{|a,b| a[1]['date'] <=> b[1]['date']} |
| 58 |
fin.each do |a| |
| 59 |
puts a[1]['path'] |
| 60 |
end |
| 61 |
end |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
def search_me(hash, db, myid, mydir) |
| 69 |
ent = select_by_id(hash, db, myid, mydir) |
| 70 |
exit unless ent |
| 71 |
return ent |
| 72 |
end |
| 73 |
|
| 74 |
def search_ancestors(hash, db, parid, mydir) |
| 75 |
until parid == '' |
| 76 |
ent = select_by_id(hash, db, parid, mydir) |
| 77 |
break unless ent |
| 78 |
parid = ent['parid'] |
| 79 |
end |
| 80 |
end |
| 81 |
|
| 82 |
def search_descendants(hash, db, myid, mydir) |
| 83 |
parents = select_by_parid(hash, db, myid, mydir) |
| 84 |
while parents != [] |
| 85 |
children = [] |
| 86 |
parents.each do |ent| |
| 87 |
children += select_by_parid(hash, db, ent['id'], mydir) |
| 88 |
end |
| 89 |
parents = children |
| 90 |
end |
| 91 |
end |
| 92 |
|
| 93 |
def search_child(hash, db, myid, mydir) |
| 94 |
parents = select_by_parid(hash, db, [myid], mydir) |
| 95 |
exit unless parents != [] |
| 96 |
return parents[0] |
| 97 |
end |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
require 'optparse' |
| 105 |
|
| 106 |
opts = {} |
| 107 |
OptionParser.new {|opt| |
| 108 |
begin |
| 109 |
opt.on('-p', 'search a parent message only') {|v| opts[:p] = v } |
| 110 |
opt.on('-c', 'search a child message only') {|v| opts[:c] = v } |
| 111 |
opt.parse!(ARGV) |
| 112 |
rescue OptionParser::ParseError => e |
| 113 |
puts opt |
| 114 |
exit |
| 115 |
end |
| 116 |
} |
| 117 |
|
| 118 |
myid = ARGV[0] |
| 119 |
mydb = ARGV[1] || File.expand_path('~/Mail/id.db') |
| 120 |
mydir = ARGV[2] |
| 121 |
db = SQLite3::Database.new(mydb) |
| 122 |
db.results_as_hash = true |
| 123 |
|
| 124 |
hash = {} |
| 125 |
|
| 126 |
begin |
| 127 |
if opts[:p] |
| 128 |
my = search_me(hash, db, myid, mydir) |
| 129 |
puts my['path']; |
| 130 |
elsif opts[:c] |
| 131 |
my = search_child(hash, db, myid, mydir) |
| 132 |
puts my['path']; |
| 133 |
else |
| 134 |
my = search_me(hash, db, myid, mydir) |
| 135 |
search_ancestors(hash, db, my['parid'], mydir) |
| 136 |
search_descendants(hash, db, myid, mydir) |
| 137 |
print_results(hash) |
| 138 |
end |
| 139 |
ensure |
| 140 |
db.close |
| 141 |
end |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|