Debian Codegraph @

bin/smew

raw symbols SCIP
1 #!/usr/bin/env ruby
2 #
3 # smew.rb: Lookuping Message-ID: DB with Sqlite3
4 #
5
6 begin
7 require 'rubygems'
8 gem 'sqlite3-ruby'
9 rescue LoadError
10 end
11 require 'sqlite3'
12
13 ################################################################
14 ##
15 ## DB search
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 ## Results
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 ## Search
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 ## Main
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 # Copyright (C) 2008 Mew developing team.
144 # All rights reserved.
145 #
146 # Redistribution and use in source and binary forms, with or without
147 # modification, are permitted provided that the following conditions
148 # are met:
149 #
150 # 1. Redistributions of source code must retain the above copyright
151 # notice, this list of conditions and the following disclaimer.
152 # 2. Redistributions in binary form must reproduce the above copyright
153 # notice, this list of conditions and the following disclaimer in the
154 # documentation and/or other materials provided with the distribution.
155 # 3. Neither the name of the team nor the names of its contributors
156 # may be used to endorse or promote products derived from this software
157 # without specific prior written permission.
158 #
159 # THIS SOFTWARE IS PROVIDED BY THE TEAM AND CONTRIBUTORS ``AS IS'' AND
160 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
161 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
162 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TEAM OR CONTRIBUTORS BE
163 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
164 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
165 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
166 # BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
167 # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
168 # OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
169 # IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.