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
|
#!/usr/bin/env ruby
# Ag -- archiving all the 'golden' flamewars on -dev
# Alex Legler <a3li@gentoo.org>
require 'bundler/setup'
require 'mail'
require 'maildir'
require 'elasticsearch'
require 'optparse'
require_relative 'lib/utils'
require_relative 'lib/threading'
require_relative 'lib/rendering'
require_relative 'lib/storage'
$options = OpenStruct.new
$options.action = nil
$options.name = nil
$options.index_only = false
$options.no_threading = false
$options.debug = false
op = OptionParser.new do |opts|
opts.banner = "Usage: ag <<--index-full|--index-new|--delete|--reindex|--info> <--list listname>|<--fix>> <maildir/file/hash/messageid> [options]"
opts.on('--index-full', 'Read the full past archive from the .cur Maildir') do
abort 'Can only select one action' if $options.action != nil
$options.action = :do_full
end
opts.on('--index-new', 'Read new messages from .new and move them to .cur') do
abort 'Can only select one action' if $options.action != nil
$options.action = :do_incremental
end
opts.on('--fix', 'Fix up weird info sections') do
abort 'Can only select one action' if $options.action != nil
$options.action = :do_fix
end
opts.on('--delete', 'Delete message. Needs --file, --msgid, or --hash') do
abort 'Can only select one action' if $options.action != nil
$options.action = :do_delete
end
opts.on('--reindex', 'Reindex message. Needs --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|
$options.name = name
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
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'
$listname = ARGV[0] or abort 'List name required'
# Open maildir and set serializer
$maildir = Maildir.new(File.join($options.dir), false)
$maildir.serializer = Maildir::Serializer::Mail.new
# Connect to Elasticsearch
$es = Elasticsearch::Client.new(log: false)
$es.transport.reload_connections!
###############################################################################
def do_fix
Dir.chdir($options.dir) do
Dir.glob('*:2,:2,S').each do |f|
puts "Fixing #{f}"
File.rename(f, f.gsub(':2,:2,S', ':2,S'))
end
end
end
def do_full
Ag::Storage.create_index($options.name)
$maildir.list(:cur).each do |maildir_message|
mail = maildir_message.data
begin
Ag::Storage.store($options.name, mail)
rescue => e
$stderr.puts "Cannot save message #{mail.message_id}: #{e.message}"
next
end
end
Ag::Threading.calc($options.name) unless $options.no_threading
end
def do_incremental
$maildir.list(:new).each do |maildir_message|
mail = maildir_message.data
begin
Ag::Storage.store($options.name, mail)
maildir_message.process
rescue => e
$stderr.puts "Cannot save message #{mail.message_id}: #{e.message}"
next
end
end
Ag::Threading.calc($options.name) unless $options.no_threading
end
def do_delete
abort 'Come back later.'
end
def do_reindex
abort 'Come back later.'
end
def do_info
abort 'Come back later.'
end
###############################################################################
begin
send $options.action
rescue NoMethodError
abort 'Internal Error: Unknown action'
end
|