1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
#!/usr/bin/env ruby
# Ag -- archiving all the 'golden' flamewars on -dev
# Alex Legler <a3li@gentoo.org>
$VERBOSE=nil
require 'bundler/setup'
require 'mail'
require 'maildir'
require 'elasticsearch'
require 'optparse'
require 'parallel'
require 'ruby-progressbar'
require_relative 'lib/utils'
require_relative 'lib/threading'
require_relative 'lib/rendering'
require_relative 'lib/storage'
require_relative 'lib/hotfixes'
$options = OpenStruct.new
$options.action = nil
$options.name = nil
$options.index_only = false
$options.no_threading = false
$options.debug = false
$options.readonly = false
$options.jobs = false
$options.progress = true
$options.need_argument = true
$options.argmode = nil
op = OptionParser.new do |opts|
opts.banner = "Usage: ag <<--index-full|--index-new|--delete-msg|--delete-index|--reindex|--info> <--list listname>> <[--file|--msgid|--hash] <maildir/file/hash/messageid>> [options]"
opts.on('--index-full', 'Read the full past archive from Maildir/cur. Needs --list and a Maildir') do
abort 'Can only select one action' if $options.action != nil
$options.action = :do_full
$options.argmode = :dir
end
opts.on('--index-new', 'Read new messages from Maildir/new and move them to Maildir/cur. Needs --list and a Maildir') do
abort 'Can only select one action' if $options.action != nil
$options.action = :do_incremental
$options.argmode = :dir
end
opts.on('--delete-msg', 'Delete message. Needs --list and one of --file, --msgid, or --hash') do
abort 'Can only select one action' if $options.action != nil
$options.action = :do_delete_msg
end
opts.on('--create-index', 'Create index but do not populate. Needs --list') do
abort 'Can only select one action' if $options.action != nil
$options.action = :do_create_index
$options.need_argument = false
end
opts.on('--delete-index', 'Delete index. Needs --list') do
abort 'Can only select one action' if $options.action != nil
$options.action = :do_delete_index
$options.need_argument = false
end
opts.on('--info', 'Display message details. Needs --list and one of --file, --msgid, or --hash') do
abort 'Can only select one action' if $options.action != nil
$options.action = :do_info
end
opts.on('--reindex', 'Reindex message. Needs --list and --file') do
abort 'Can only select one action' if $options.action != nil
$options.action = :do_reindex
end
opts.on('--list NAME', 'Name of the mailing list to work with') do |name|
if name =~ /^[0-9a-zA-Z-]+$/
$options.name = name
else
abort 'List name can only consist of letters, numbers and hyphens.'
end
end
opts.on('--file', 'The argument is a file') do
$options.argmode = :file
end
opts.on('--msgid', 'The argument is a Message-Id') do
$options.argmode = :msgid
end
opts.on('--hash', 'The argument is a X-Archives-Hash') do
$options.argmode = :hash
end
opts.on('--index-only', 'Only delete the message from the index, not from disk') do
$options.index_only = true
end
opts.on('--no-threading', 'Only index, don\'t update threading') do
$options.no_threading = true
end
opts.on('--debug', 'Print debug messages') do
$options.debug = true
end
opts.on('--readonly', 'Do not alter the maildir in any way') do
$options.readonly = true
end
opts.on('--jobs JOBS', 'Number of parallel jobs to run (defaults to 75% of core count)') do |jobs|
$options.jobs = jobs.to_i
end
opts.on('--progress', 'Display the progress bar') do
$options.progress = true
end
opts.on('--no-progress', 'Do not display the progress bar') do
$options.progress = false
end
end
op.parse!
abort op.help unless $options.action
abort 'List name required' unless $options.name
$options.dir = ARGV[0] or abort 'Need a Maildir/File/Hash/Message-Id to work with' if $options.need_argument
if($options.argmode == :dir)
# Open maildir and set serializer
$maildir = Maildir.new(File.join($options.dir), false)
$maildir.serializer = Maildir::Serializer::Mail.new
end
# Connect to Elasticsearch
$es = Elasticsearch::Client.new(log: false)
$es.transport.reload_connections!
Ag::Utils.proc_count = $options.jobs
###############################################################################
def do_full
abort "Wrong argument type: #{$options.argmode.to_s}" unless $options.argmode == :dir
begin
Ag::Storage.delete_index($options.name)
rescue Elasticsearch::Transport::Transport::Errors::NotFound => e
$stderr.puts "Index did not exist yet. Creating." if $options.debug
end
Ag::Storage.create_index($options.name)
messages = $maildir.list(:cur)
opts = {
:in_processes => Ag::Utils.proc_count,
}
opts[:progress] = "Importing #{$options.name}" if $options.progress
Parallel.each(messages, opts) do |maildir_message|
mail = maildir_message.data
begin
Ag::Storage.store($options.name, mail, maildir_message.unique_name)
rescue => e
$stderr.puts "Cannot save message #{mail.message_id}: (#{e.class}) #{e.message}" if $options.debug
next
end
end
Ag::Threading.calc($options.name) unless $options.no_threading
end
def do_incremental
abort "Wrong argument type: #{$options.argmode.to_s}" unless $options.argmode == :dir
messages = $maildir.list(:cur)
opts = {
:in_processes => Ag::Utils.proc_count,
}
opts[:progress] = "Importing #{$options.name}" if $options.progress
Parallel.each(messages, opts) do |maildir_message|
mail = maildir_message.data
begin
Ag::Storage.store($options.name, mail, maildir_message.unique_name)
maildir_message.process unless $options.readonly
rescue => e
$stderr.puts "Cannot save message #{mail.message_id} (file #{maildir_message.filename}): #{e.message}" if $options.debug
next
end
end
Ag::Threading.calc($options.name) unless $options.no_threading
end
def do_delete_msg
id = Ag::Utils.resolve_id
begin
Ag::Storage.delete($options.name, id)
rescue => e
$stderr.puts "Cannot delete message: #{e}"
end
end
def do_delete_index
begin
Ag::Storage.delete_index($options.name)
rescue Elasticsearch::Transport::Transport::Errors::NotFound => e
$stderr.puts "Index does not exist: #{e}"
rescue => e
$stderr.puts "Cannot delete index: #{e}"
end
end
def do_create_index
Ag::Storage.create($options.name)
end
def do_reindex
# http://babinho.net/2014/07/refresh-your-elasticsearch-index-with-zero-downtime/
abort 'Come back later.'
end
def do_info
id = Ag::Utils.resolve_id
begin
message = Ag::Storage.get($options.name, id)
raise 'No such message' unless message
require 'pp'
str = "Message #{id}"
$stderr.puts str
$stderr.puts '-' * str.length
pp message['_source']
rescue => e
$stderr.puts "Cannot display message: #{e}"
end
end
###############################################################################
if self.private_methods.include? $options.action
send $options.action
else
abort "Internal Error: Unknown action: #{$options.action}"
end
# vim: ts=2 sts=2 et ft=ruby:
|