aboutsummaryrefslogtreecommitdiff
path: root/ag
blob: 97c223f85b79f19fd16a98532b58b32703384b4c (plain)
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
#!/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

op = OptionParser.new do |opts|
  opts.banner = "Usage: ag <<--index-full|--index-new|--delete|--reindex|--info> <--list listname>> <[--file|--msgid|--hash] <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('--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('--info', 'Display message details. Needs --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 --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', 'Number of parallel jobs to run (defaults to 75% of core count)') do |jobs|
    $options.jobs = jobs
  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!

Ag::Utils.proc_count = $options.jobs

###############################################################################

def do_full
  Ag::Storage.create_index($options.name)

  messages = $maildir.list(:cur)

  Parallel.each(messages, in_processes: Ag::Utils.proc_count, progress: "Importing #{$options.name}") 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
  messages = $maildir.list(:cur)

  Parallel.each(messages, in_processes: Ag::Utils.proc_count, progress: "Importing #{$options.name}") 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
  id = Ag::Utils.resolve_id

  begin
    Ag::Storage.delete($options.name, id)
  rescue => e
    $stderr.puts "Cannot delete message: #{e}"
  end
end

def do_reindex
  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

###############################################################################

begin
  send $options.action
rescue NoMethodError
  abort 'Internal Error: Unknown action'
end