-
-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathimport-thunderbird-folder
executable file
·92 lines (72 loc) · 2.57 KB
/
import-thunderbird-folder
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
#!/usr/bin/env ruby
# This script is an example of how to import messages from a Thunderbird
# folder into imap-backup. It is not meant to be a general-purpose
# Thunderbird importer, but rather a starting point for writing your own.
# Please adapt it to your specific needs.
require "bundler/inline"
gemfile do
source "https://rubygems.org"
gem "imap-backup"
gem "optparse"
gem "thunderbird", "~> 0.5.0"
end
require "imap/backup/logger"
require "imap/backup/configuration"
require "imap/backup/serializer"
require "thunderbird/mbox"
class Options
attr_accessor :email
attr_accessor :config_path
attr_accessor :folder
attr_accessor :mbox_path
attr_accessor :verbose
attr_accessor :quiet
def parse!
OptionParser.new do |opts|
opts.banner = <<~BANNER
Usage: #{$PROGRAM_NAME} [options]"
Import email messages from a Thunderbird folder into imap-backup.
BANNER
opts.on("--config=CONFIG", "The path to an existing (or new) imap-backup config file") do |v|
self.config_path = v
end
opts.on("--email=EMAIL", "The email address configured in imap-backup") do |v|
self.email = v
end
opts.on("--folder=FOLDER", "The folder name to import into") do |v|
self.folder = v
end
opts.on("--mbox=MBOX_PATH", "The path to a Thunderbird folder") do |v|
self.mbox_path = v
end
opts.on("-q", "--quiet", "Do not print any output") do
self.quiet = true
end
opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
self.verbose = v
end
end.parse!
raise "Please supply a --config PATH option" if !config_path
raise "Please supply a --email EMAIL option" if !email
raise "Please supply a --folder FOLDER option" if !folder
raise "Please supply a --mbox PATH option" if !mbox_path
end
def for_logging
{verbose: [verbose], quiet: quiet}
end
end
options = Options.new.tap(&:parse!)
Imap::Backup::Logger.setup_logging(options.for_logging)
config = Imap::Backup::Configuration.new(path: options.config_path)
account = config.accounts.find { |a| a.username == options.email }
raise "No account found for email address '#{options.email}'" if account.nil?
mbox = Thunderbird::Mbox.new(path: options.mbox_path)
serializer = Imap::Backup::Serializer.new(account.local_path, options.folder)
serializer.force_uid_validity(mbox.uid_validity)
mbox.each do |id, message|
uid = id.to_i
next if serializer.uids.include?(uid)
# Remove Thunderbird mbox "From" line
message.sub!(/^From[\s\r\n]*/m, "")
serializer.append(id, message, [])
end