Você pode marcar ( t
) mensagens com anexos e, em seguida, enviar mensagens por meio de um programa pipe ( ;|
) para extrair anexos.
Eu sugiro que você defina a seguinte configuração no seu .muttrc
set pipe_split = yes # sends tagged messages 1by1 otherwise they're concatenated with $pipe_sep
unset pipe_decode # don't strip headers and eval mimes when piping
Eu escrevi um script ruby realmente simples (WIP nunca terminou, mas funciona ...) para extrair anexos. Você pode se inspirar com isso ...
#!/usr/bin/env ruby
#
# scriptname : ~/bin/mutt_utils.rb
#
# Messages tagged
# To work with tagged message you have to :set pipe_split
# Otherwise only first message tagged will be processed.
#
require 'optparse'
require 'fileutils'
require 'mail'
#
# monkeypatch Mail::Message class to fix Invalid byte sequence with ruby 1.9
# https://github.com/mikel/mail/issues/340
# https://github.com/mreinsch/mail/commit/c7818a95f9f9fddc675f75452ea5000c46c30d94
#
if RUBY_VERSION >= "1.9.1"
class Mail::Message
def raw_source=(value)
value.force_encoding("binary") if RUBY_VERSION >= "1.9.1"
@raw_source = value.to_crlf
end
end
end
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: #{File.basename $0} [OPTIONS]"
opts.on("-o", "--output PATH", "save in PATH folder") do |path|
options[:output] = path
end
opts.on("-O", "--overwrite", "overwrite existing files") do |owr|
options[:overwrite] = owr
end
opts.on("-n", "--name-regexp PATTERN", "save only attachment's name matching PATTERN") do |pat|
options[:name_pattern] = pat
end
opts.on("-c", "--content-regexp PATTERN", "save only attachment's content_type matching PATTERN") do |pat|
options[:content_pattern] = pat
end
opts.on("-v", "--verbose", "verbose") do |v|
options[:verbose] = v
end
opts.on("-h", "--help", "usage") do
puts opts
exit
end
end.parse!
puts "Options passed : #{options}" if options[:verbose]
if options[:output]
unless File.directory?(options[:output])
$stderr.puts "'#{options[:output]}' is not a valid folder !"
exit
end
end
def sanitize(value)
value.gsub(/\s/, '_').gsub(/:/, '-').gsub(/\//, '-').gsub(/\/, '-')
end
def check_criteria(criteria, pattern)
return true if pattern.nil?
criteria =~ /#{pattern}/i
end
data = ARGF.readlines.join
mail = Mail.read_from_string data #.force_encoding("UTF-8")
ts = mail.date.strftime("%Y-%m-%dT%H%M")
puts "Processing mail '#{mail.subject}' " if options[:verbose]
subject = mail.subject.nil? ? 'no subject' : mail.subject.downcase
mail.attachments.each do |att|
puts "Attachment : filename=#{att.filename} content_type=#{att.content_type}" if options[:verbose]
valid_name = check_criteria(att.filename, options[:name_pattern])
valid_content = check_criteria(att.content_type, options[:content_pattern])
unless valid_name
puts "Filename doesn't match #{options[:name_pattern]}" if options[:verbose]
end
unless valid_content
puts "Content doesn't match #{options[:content_pattern]}" if options[:verbose]
end
next unless ( valid_name && valid_content )
fn = sanitize "#{ts}_#{subject}_#{att.filename}"
fn = File.join(options[:output], fn) if options[:output]
puts "Saving attachment to #{fn}." if options[:verbose]
if (!options[:overwrite] && File.exist?(fn))
$stderr.puts "'#{fn}' already exists ! Skipping." if options[:verbose]
next
end
begin
File.open(fn, 'wb') {|f| f.write att.body.decoded }
rescue Exception => e
$stderr.puts "Error while saving #{fn} ! Cause: #{e.message}"
end
end
Não se esqueça de chmod +x ~/bin/mutt_utils.rb
.
Exemplo de uso:
marcar algumas mensagens com anexos, clique em ;
e |
.
Digite no prompt ~/bin/mutt_utils.rb -o ~/Desktop/
.
Agora você deve encontrar seus anexos em sua área de trabalho.